Skip to content

Commit f4a3e67

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 1dbd02d commit f4a3e67

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
@@ -82,7 +82,7 @@ InitRecoveryTransactionEnvironment(void)
8282
* hold AccessShareLocks so never block while we write or lock new rows.
8383
*/
8484
vxid.backendId = MyBackendId;
85-
vxid.localTransactionId = GetNextLocalTransactionId();
85+
vxid.localTransactionId = MyProc->lxid = GetNextLocalTransactionId();
8686
VirtualXactLockTableInsert(vxid);
8787

8888
standbyState = STANDBY_INITIALIZED;
@@ -98,11 +98,18 @@ InitRecoveryTransactionEnvironment(void)
9898
void
9999
ShutdownRecoveryTransactionEnvironment(void)
100100
{
101+
VirtualTransactionId vxid;
102+
101103
/* Mark all tracked in-progress transactions as finished. */
102104
ExpireAllKnownAssignedTransactionIds();
103105

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

108115

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)