Quantcast
Channel: Question and Answer » transaction
Viewing all articles
Browse latest Browse all 35

PostgreSQL visibility of records changes from other transaction inside the current one

$
0
0

I have a table “tst” which has a “status” column using which we process records in that table.
Once the records are processed we set the status to ‘Y’;

    #d tst
                 Table "core.tst"
     Column |  Type   | Modifiers
    --------+---------+-----------
     id     | integer | not null
     status | text    |
    Indexes:
            "amn2_pkey" PRIMARY KEY, btree (id)

Issue: At the start of the long(approx 20 mins) transaction I have 3 records in tst table as shown below

    #select * from tst;
     id | status
    ----+--------
        1 | N
        2 | N
        3 | N
    (3 rows)

But during the transaction I have couple of more records being inserted into the tst table, but I was assuming that these new records will not be visible to the current transaction, but proved wrong on testing.

Session 1:

    DO $$ 
    BEGIN 
        perform pg_sleep(20) ; 
        UPDATE amn2 SET des = 'Y' ; 
    END $$;

Session 2:

    INSERT INTO tst VALUES ( 4, 'N');

Post the Session 1, all the records were updated to status ‘Y’ including id 4.

Is this expected? Or some DB/table serializable set up missing?


Viewing all articles
Browse latest Browse all 35

Trending Articles