Skip to content

Commit 2783898

Browse files
committed
Relax lock level for setting PGPROC->statusFlags
We don't actually need a lock to set PGPROC->statusFlags itself; what we do need is a shared lock on either XidGenLock or ProcArrayLock in order to ensure MyProc->pgxactoff keeps still while we modify the mirror array in ProcGlobal->statusFlags. Some places were using an exclusive lock for that, which is excessive. Relax those to use shared lock only. procarray.c has a couple of places with somewhat brittle assumptions about PGPROC changes: ProcArrayEndTransaction uses only shared lock, so it's permissible to change MyProc only. On the other hand, ProcArrayEndTransactionInternal also changes other procs, so it must hold exclusive lock. Add asserts to ensure those assumptions continue to hold. Author: Álvaro Herrera <alvherre@alvh.no-ip.org> Reviewed-by: Michael Paquier <michael@paquier.xyz> Discussion: https://postgr.es/m/20201117155501.GA13805@alvherre.pgsql
1 parent 2cccb62 commit 2783898

File tree

6 files changed

+16
-5
lines changed

6 files changed

+16
-5
lines changed

src/backend/commands/vacuum.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1741,7 +1741,7 @@ vacuum_rel(Oid relid, RangeVar *relation, VacuumParams *params)
17411741
* MyProc->xid/xmin, otherwise GetOldestNonRemovableTransactionId()
17421742
* might appear to go backwards, which is probably Not Good.
17431743
*/
1744-
LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
1744+
LWLockAcquire(ProcArrayLock, LW_SHARED);
17451745
MyProc->statusFlags |= PROC_IN_VACUUM;
17461746
if (params->is_wraparound)
17471747
MyProc->statusFlags |= PROC_VACUUM_FOR_WRAPAROUND;

src/backend/replication/logical/logical.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ StartupDecodingContext(List *output_plugin_options,
181181
*/
182182
if (!IsTransactionOrTransactionBlock())
183183
{
184-
LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
184+
LWLockAcquire(ProcArrayLock, LW_SHARED);
185185
MyProc->statusFlags |= PROC_IN_LOGICAL_DECODING;
186186
ProcGlobal->statusFlags[MyProc->pgxactoff] = MyProc->statusFlags;
187187
LWLockRelease(ProcArrayLock);

src/backend/replication/slot.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ ReplicationSlotRelease(void)
527527
MyReplicationSlot = NULL;
528528

529529
/* might not have been set when we've been a plain slot */
530-
LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
530+
LWLockAcquire(ProcArrayLock, LW_SHARED);
531531
MyProc->statusFlags &= ~PROC_IN_LOGICAL_DECODING;
532532
ProcGlobal->statusFlags[MyProc->pgxactoff] = MyProc->statusFlags;
533533
LWLockRelease(ProcArrayLock);

src/backend/storage/ipc/procarray.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,8 @@ ProcArrayEndTransaction(PGPROC *proc, TransactionId latestXid)
662662
/* avoid unnecessarily dirtying shared cachelines */
663663
if (proc->statusFlags & PROC_VACUUM_STATE_MASK)
664664
{
665+
/* Only safe to change my own flags with just share lock */
666+
Assert(proc == MyProc);
665667
Assert(!LWLockHeldByMe(ProcArrayLock));
666668
LWLockAcquire(ProcArrayLock, LW_SHARED);
667669
Assert(proc->statusFlags == ProcGlobal->statusFlags[proc->pgxactoff]);
@@ -682,7 +684,11 @@ ProcArrayEndTransactionInternal(PGPROC *proc, TransactionId latestXid)
682684
{
683685
size_t pgxactoff = proc->pgxactoff;
684686

685-
Assert(LWLockHeldByMe(ProcArrayLock));
687+
/*
688+
* Note: we need exclusive lock here because we're going to
689+
* change other processes' PGPROC entries.
690+
*/
691+
Assert(LWLockHeldByMeInMode(ProcArrayLock, LW_EXCLUSIVE));
686692
Assert(TransactionIdIsValid(ProcGlobal->xids[pgxactoff]));
687693
Assert(ProcGlobal->xids[pgxactoff] == proc->xid);
688694

src/backend/storage/lmgr/deadlock.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ FindLockCycleRecurseMember(PGPROC *checkProc,
623623
* because that flag is set at process start and never
624624
* reset. There is logic elsewhere to avoid canceling an
625625
* autovacuum that is working to prevent XID wraparound
626-
* problems (which needs to read a different vacuumFlag
626+
* problems (which needs to read a different statusFlags
627627
* bit), but we don't do that here to avoid grabbing
628628
* ProcArrayLock.
629629
*/

src/include/storage/proc.h

+5
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ typedef enum
9898
* The semaphore and lock-activity fields in a prepared-xact PGPROC are unused,
9999
* but its myProcLocks[] lists are valid.
100100
*
101+
* We allow many fields of this struct to be accessed without locks, such as
102+
* statusFlags or delayChkpt. However, keep in mind that writing mirrored ones
103+
* (see below) requires holding ProcArrayLock or XidGenLock in at least shared
104+
* mode, so that pgxactoff does not change concurrently.
105+
*
101106
* Mirrored fields:
102107
*
103108
* Some fields in PGPROC (see "mirrored in ..." comment) are mirrored into an

0 commit comments

Comments
 (0)