summaryrefslogtreecommitdiff
path: root/src/include
diff options
context:
space:
mode:
authorTom Lane2021-10-01 15:10:12 +0000
committerTom Lane2021-10-01 15:10:12 +0000
commit04ef2021e3ca7207b87ffce437b0b4db73a7f6b2 (patch)
treeb78ca53d74decf30106660f9bab9424459ea6b37 /src/include
parent649e561f65c579630e5c31ee65308eefdd21ec93 (diff)
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.h4
-rw-r--r--src/include/utils/snapmgr.h1
2 files changed, 5 insertions, 0 deletions
diff --git a/src/include/utils/portal.h b/src/include/utils/portal.h
index cb40bfa761a..af0afcf9856 100644
--- a/src/include/utils/portal.h
+++ b/src/include/utils/portal.h
@@ -195,6 +195,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.
@@ -202,6 +204,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;
/*
@@ -219,6 +222,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 ad7c15dc1a2..651ff609982 100644
--- a/src/include/utils/snapmgr.h
+++ b/src/include/utils/snapmgr.h
@@ -110,6 +110,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);