Skip to content

Commit 6f9a9da

Browse files
Correctly init/deinit recovery xact environment.
Previously we performed VirtualXactLockTableInsert but didn't set MyProc->lxid for Startup process. pg_locks now correctly shows "1/1" for vxid of Startup process during Hot Standby. At end of Hot Standby the Virtual Transaction was not deleted, leading to problems after promoting to normal running for some commands, such as CREATE INDEX CONCURRENTLY.
1 parent 1da5bef commit 6f9a9da

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

src/backend/storage/ipc/standby.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ InitRecoveryTransactionEnvironment(void)
8181
* hold AccessShareLocks so never block while we write or lock new rows.
8282
*/
8383
vxid.backendId = MyBackendId;
84-
vxid.localTransactionId = GetNextLocalTransactionId();
84+
vxid.localTransactionId = MyProc->lxid = GetNextLocalTransactionId();
8585
VirtualXactLockTableInsert(vxid);
8686

8787
standbyState = STANDBY_INITIALIZED;
@@ -97,11 +97,18 @@ InitRecoveryTransactionEnvironment(void)
9797
void
9898
ShutdownRecoveryTransactionEnvironment(void)
9999
{
100+
VirtualTransactionId vxid;
101+
100102
/* Mark all tracked in-progress transactions as finished. */
101103
ExpireAllKnownAssignedTransactionIds();
102104

103105
/* Release all locks the tracked transactions were holding */
104106
StandbyReleaseAllLocks();
107+
108+
/* Cleanup our VirtualTransaction */
109+
vxid.backendId = MyBackendId;
110+
vxid.localTransactionId = MyProc->lxid;
111+
VirtualXactLockTableDelete(vxid);
105112
}
106113

107114

src/backend/storage/lmgr/lmgr.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,24 @@ VirtualXactLockTableInsert(VirtualTransactionId vxid)
534534
(void) LockAcquire(&tag, ExclusiveLock, false, false);
535535
}
536536

537+
/*
538+
* VirtualXactLockTableDelete
539+
*
540+
* Release a Virtual Transaction lock. Only called by Startup process
541+
* at end of Hot Standby.
542+
*/
543+
void
544+
VirtualXactLockTableDelete(VirtualTransactionId vxid)
545+
{
546+
LOCKTAG tag;
547+
548+
Assert(VirtualTransactionIdIsValid(vxid));
549+
550+
SET_LOCKTAG_VIRTUALTRANSACTION(tag, vxid);
551+
552+
(void) LockRelease(&tag, ExclusiveLock, false);
553+
}
554+
537555
/*
538556
* VirtualXactLockTableWait
539557
*

src/include/storage/lmgr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ extern bool ConditionalXactLockTableWait(TransactionId xid);
5858

5959
/* Lock a VXID (used to wait for a transaction to finish) */
6060
extern void VirtualXactLockTableInsert(VirtualTransactionId vxid);
61+
extern void VirtualXactLockTableDelete(VirtualTransactionId vxid);
6162
extern void VirtualXactLockTableWait(VirtualTransactionId vxid);
6263
extern bool ConditionalVirtualXactLockTableWait(VirtualTransactionId vxid);
6364

0 commit comments

Comments
 (0)