diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2021-10-01 11:10:12 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2021-10-01 11:10:12 -0400 |
commit | cded2c46099eecc9fc8572bc09675407fb178c44 (patch) | |
tree | 447c158a05444ee6570e306976465e38a28b1656 /src/include | |
parent | f2cf745a038c984092b74fe5b82b892721f9310e (diff) | |
download | postgresql-cded2c46099eecc9fc8572bc09675407fb178c44.tar.gz postgresql-cded2c46099eecc9fc8572bc09675407fb178c44.zip |
Fix Portal snapshot tracking to handle subtransactions properly.
Commit 84f5c2908 forgot to consider the possibility that
EnsurePortalSnapshotExists could run inside a subtransaction with
lifespan shorter than the Portal's. In that case, the new active
snapshot would be popped at the end of the subtransaction, leaving
a dangling pointer in the Portal, with mayhem ensuing.
To fix, make sure the ActiveSnapshot stack entry is marked with
the same subtransaction nesting level as the associated Portal.
It's certainly safe to do so since we won't be here at all unless
the stack is empty; hence we can't create an out-of-order stack.
Let's also apply this logic in the case where PortalRunUtility
sets portalSnapshot, just to be sure that path can't cause similar
problems. It's slightly less clear that that path can't create
an out-of-order stack, so add an assertion guarding it.
Report and patch by Bertrand Drouvot (with kibitzing by me).
Back-patch to v11, like the previous commit.
Discussion: https://postgr.es/m/ff82b8c5-77f4-3fe7-6028-fcf3303e82dd@amazon.com
Diffstat (limited to 'src/include')
-rw-r--r-- | src/include/utils/portal.h | 4 | ||||
-rw-r--r-- | src/include/utils/snapmgr.h | 1 |
2 files changed, 5 insertions, 0 deletions
diff --git a/src/include/utils/portal.h b/src/include/utils/portal.h index 364fae4b651..3c30ccbc6f8 100644 --- a/src/include/utils/portal.h +++ b/src/include/utils/portal.h @@ -193,6 +193,8 @@ typedef struct PortalData TimestampTz creation_time; /* time at which this portal was defined */ bool visible; /* include this portal in pg_cursors? */ + /* Stuff added at the end to avoid ABI break in stable branches: */ + /* * Outermost ActiveSnapshot for execution of the portal's queries. For * all but a few utility commands, we require such a snapshot to exist. @@ -200,6 +202,7 @@ typedef struct PortalData * and helps to reduce thrashing of the process's exposed xmin. */ Snapshot portalSnapshot; /* active snapshot, or NULL if none */ + int createLevel; /* creating subxact's nesting level */ } PortalData; /* @@ -217,6 +220,7 @@ extern void AtCleanup_Portals(void); extern void PortalErrorCleanup(void); extern void AtSubCommit_Portals(SubTransactionId mySubid, SubTransactionId parentSubid, + int parentLevel, ResourceOwner parentXactOwner); extern void AtSubAbort_Portals(SubTransactionId mySubid, SubTransactionId parentSubid, diff --git a/src/include/utils/snapmgr.h b/src/include/utils/snapmgr.h index 62351a06bdf..adb47dcd22c 100644 --- a/src/include/utils/snapmgr.h +++ b/src/include/utils/snapmgr.h @@ -111,6 +111,7 @@ extern void InvalidateCatalogSnapshot(void); extern void InvalidateCatalogSnapshotConditionally(void); extern void PushActiveSnapshot(Snapshot snapshot); +extern void PushActiveSnapshotWithLevel(Snapshot snapshot, int snap_level); extern void PushCopiedSnapshot(Snapshot snapshot); extern void UpdateActiveSnapshotCommandId(void); extern void PopActiveSnapshot(void); |