I am selecting a dataset of records from table 'tablename' and then iterating through the dataset, updating one record at time (UPDATE tablename SET... WHERE id=x).
Each update is done within a transaction block, and each one sets relation RowExclusiveLocks on the
tablename
tablename_pkey
and indexes in tablename
However I was a little surprised to see a page SIReadLock to be set on
tablename_pkey
Under what circumstances would this happen ?
The documentation says:
In
src/backend/storage/lmgr/README-SSIyou learn more:So this is how PostgreSQL checks if a concurrent transaction adds another row that would influence the result of the query and thus has a read-write dependency with the current transaction. Theoretically, it would be enough to lock the "spaces" between the index entry and the adjacent entries, but for practical reasons, the whole page is locked.
As a consequence, you can get false positive serialization failures in PostgreSQL, but that is no less that the documentation promises: