Thread: pgsql: Assert that a snapshot is active or registered before it's used

pgsql: Assert that a snapshot is active or registered before it's used

From
Heikki Linnakangas
Date:
Assert that a snapshot is active or registered before it's used

The comment in GetTransactionSnapshot() said that you "should call
RegisterSnapshot or PushActiveSnapshot on the returned snap if it is
to be used very long". That felt too unclear to me. Make the comment
more strongly worded.

To enforce that rule and to catch potential bugs where a snapshot
might get invalidated while it's still in use, add an assertion to
HeapTupleSatisfiesMVCC() to check that the snapshot is registered or
pushed to active stack. No new bugs were found by this, but it seems
like good future-proofing. It's not a great place for the check;
HeapTupleSatisfiesMVCC() is in fact safe to call with an unregistered
snapshot, and the assertion won't catch other unsafe uses. But it goes
a long way in practice.

Fix a few cases that were playing fast and loose with that and just
assumed that the snapshot cannot be invalidated during a scan. Those
assumptions were not wrong, but they're not performance critical, so
let's drop the excuses and just register the snapshot. These were
false positives found by the new assertion.

Discussion: https://www.postgresql.org/message-id/7c56f180-b9e1-481e-8c1d-efa63de3ecbb@iki.fi

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/8076c00592e40e8dbd1fce7a98b20d4bf075e4ba

Modified Files
--------------
src/backend/access/heap/heapam_visibility.c |  9 +++++++++
src/backend/access/index/genam.c            |  8 ++------
src/backend/commands/dbcommands.c           |  3 ++-
src/backend/utils/cache/relcache.c          | 15 +++++++++------
src/backend/utils/time/snapmgr.c            |  8 ++++----
5 files changed, 26 insertions(+), 17 deletions(-)