pg.ddx.io pgsql-bugs@postgresql.org mailing list archivehelp / color / mirror / Atom feed
ReplicationSlotRelease may set the statusFlags of other processes in PG14 3+ messages / 2 participants [nested] [flat]
* ReplicationSlotRelease may set the statusFlags of other processes in PG14 @ 2024-03-16 14:29 feichanghong <feichanghong@qq.com> 0 siblings, 1 reply; 3+ messages in thread From: feichanghong @ 2024-03-16 14:29 UTC (permalink / raw) To: pgsql-bugs <pgsql-bugs@lists.postgresql.org>; +Cc: andres <andres@anarazel.de>; sawada.mshk <sawada.mshk@gmail.com>; horikyota.ntt <horikyota.ntt@gmail.com> Hello hackers, I encountered an issue where an assertion failure occurred for ProcGlobal->statusFlags. The detailed analysis is as follows: # Issue background PostgreSQL version: REL_14_STABLE v14.11 3621ffd9f2 The assert failure occurs when the autovacuum worker process commits a transaction: proc->statusFlags is 3, and ProcGlobal->statusFlags[proc->pgxactoff] is 0. The specific stack trace is as follows: ``` #0 0x00007f174c401277 in raise () from /lib64/libc.so.6 #1 0x00007f174c402968 in abort () from /lib64/libc.so.6 #2 0x00000000010f1403 in ExceptionalCondition (conditionName=0x1728760 "proc->statusFlags == ProcGlobal->statusFlags[proc->pgxactoff]", errorType=0x1728420 "FailedAssertion", fileName=0x17284b8 "procarray.c", lineNumber=776) at assert.c:44 #3 0x0000000000e997d0 in ProcArrayEndTransaction (proc=0x7f1731630f00, latestXid=0) at procarray.c:776 #4 0x00000000008cfb18 in CommitTransaction () at xact.c:2447 #5 0x00000000008d090e in CommitTransactionCommand () at xact.c:3293 #6 0x0000000000bb5652 in vacuum_rel (relid=2608, relation=0x56f76f8, params=0x56e399c) at vacuum.c:2174 #7 0x0000000000bb300e in vacuum (relations=0x5535b40, params=0x56e399c, bstrategy=0x56e3a30, isTopLevel=true) at vacuum.c:486 #8 0x0000000000dc01a9 in autovacuum_do_vac_analyze (tab=0x56e3998, bstrategy=0x56e3a30) at autovacuum.c:3467 #9 0x0000000000dbeb94 in do_autovacuum () at autovacuum.c:2632 #10 0x0000000000dbd530 in AutoVacWorkerMain (argc=0, argv=0x0) at autovacuum.c:1769 #11 0x0000000000dbd12a in StartAutoVacWorker () at autovacuum.c:1545 #12 0x0000000000dd894f in StartAutovacuumWorker () at postmaster.c:6441 #13 0x0000000000dd8189 in sigusr1_handler (postgres_signal_arg=10) at postmaster.c:6108 #14 <signal handler called> #15 0x00007f174c4bffc3 in __select_nocancel () from /lib64/libc.so.6 #16 0x0000000000dd1d67 in ServerLoop () at postmaster.c:1881 #17 0x0000000000dd16d9 in PostmasterMain (argc=3, argv=0x533e060) at postmaster.c:1582 #18 0x0000000000ca4af5 in main (argc=3, argv=0x533e060) at main.c:216 ``` # Analysis and Reproduction ## Analysis A process utilizing replication slots (usually walsender) calls callback functions in the order of RemoveProcFromArray->ProcKill upon abnormal exit. Within RemoveProcFromArray, MyProc is already removed from the ProcArray. ProcKill then attempts to set ProcGlobal->statusFlags[MyProc->pgxactoff] again via ReplicationSlotRelease. By this time, the flag may already be assigned to another process. ## Reproduction This issue only occurs in PG14. In versions prior to PG14, ProcGlobal->statusFlags had not been refactored. In versions following PG14, it has been fixed by commit 2f6501f (discussed in [1]). Therefore, I've cc'd Masahiko, Kyotaro, and Andres in hopes that they can provide some insights. To replicate the issue, execute the following steps: 1. Apply the attached v1-0000-v14-invalidate-pgxactoff-after-remove-pgproc.patch, where pgxactoff is set to an invalid value in ProcArrayRemove, and some checks are added. 2. Use the SQL below to terminate the walsender process. ``` select pg_terminate_backend(pid) from pg_stat_activity where backend_type = 'walsender'; ``` # Fix To fix the issue, I have provided some patches in the attachment: 1. Backpatching 2f6501f into the PG14 version will fix the problem. 2. In PG14-head, ProcArrayRemove needs to reset pgxactoff, and some assert checks should be done when setting ProcGlobal->statusFlags. [1] https://www.postgresql.org/message-id/29273AF3-9684-4069-9257-D05090B03B99@amazon.com Best Regards, Fei Changhong Alibaba Cloud Computing Ltd. Attachments: download all (4 files, .tar.gz)v1-0000-head-invalidate-pgxactoff-after-remove-pgproc.patch (application/octet-stream, 5.4K) download
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c index de89be8d75..b0eb267971 100644 --- a/src/backend/commands/indexcmds.c +++ b/src/backend/commands/indexcmds.c @@ -4563,6 +4563,7 @@ set_indexsafe_procflags(void) LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE); MyProc->statusFlags |= PROC_IN_SAFE_IC; + Assert(PGXactOffIsValid(MyProc->pgxactoff)); ProcGlobal->statusFlags[MyProc->pgxactoff] = MyProc->statusFlags; LWLockRelease(ProcArrayLock); } diff --git a/src/backend/commands/vacuum.c b/src/backend/commands/vacuum.c index e63c86cae4..8b79c6b6fd 100644 --- a/src/backend/commands/vacuum.c +++ b/src/backend/commands/vacuum.c @@ -1991,6 +1991,7 @@ vacuum_rel(Oid relid, RangeVar *relation, VacuumParams *params, MyProc->statusFlags |= PROC_IN_VACUUM; if (params->is_wraparound) MyProc->statusFlags |= PROC_VACUUM_FOR_WRAPAROUND; + Assert(PGXactOffIsValid(MyProc->pgxactoff)); ProcGlobal->statusFlags[MyProc->pgxactoff] = MyProc->statusFlags; LWLockRelease(ProcArrayLock); } diff --git a/src/backend/replication/logical/logical.c b/src/backend/replication/logical/logical.c index 51ffb623c0..34b3bdd61c 100644 --- a/src/backend/replication/logical/logical.c +++ b/src/backend/replication/logical/logical.c @@ -195,6 +195,7 @@ StartupDecodingContext(List *output_plugin_options, { LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE); MyProc->statusFlags |= PROC_IN_LOGICAL_DECODING; + Assert(PGXactOffIsValid(MyProc->pgxactoff)); ProcGlobal->statusFlags[MyProc->pgxactoff] = MyProc->statusFlags; LWLockRelease(ProcArrayLock); } diff --git a/src/backend/replication/slot.c b/src/backend/replication/slot.c index 91ca397857..73c8ec7f62 100644 --- a/src/backend/replication/slot.c +++ b/src/backend/replication/slot.c @@ -696,6 +696,7 @@ ReplicationSlotRelease(void) /* might not have been set when we've been a plain slot */ LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE); MyProc->statusFlags &= ~PROC_IN_LOGICAL_DECODING; + Assert(PGXactOffIsValid(MyProc->pgxactoff)); ProcGlobal->statusFlags[MyProc->pgxactoff] = MyProc->statusFlags; LWLockRelease(ProcArrayLock); diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c index bc40c454de..cfb2fd6cd4 100644 --- a/src/backend/replication/walsender.c +++ b/src/backend/replication/walsender.c @@ -308,6 +308,7 @@ InitWalSender(void) Assert(MyProc->xmin == InvalidTransactionId); LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE); MyProc->statusFlags |= PROC_AFFECTS_ALL_HORIZONS; + Assert(PGXactOffIsValid(MyProc->pgxactoff)); ProcGlobal->statusFlags[MyProc->pgxactoff] = MyProc->statusFlags; LWLockRelease(ProcArrayLock); } diff --git a/src/backend/storage/ipc/procarray.c b/src/backend/storage/ipc/procarray.c index b3cd248fb6..b8201418ed 100644 --- a/src/backend/storage/ipc/procarray.c +++ b/src/backend/storage/ipc/procarray.c @@ -608,6 +608,7 @@ ProcArrayRemove(PGPROC *proc, TransactionId latestXid) Assert(ProcGlobal->subxidStates[myoff].overflowed == false); ProcGlobal->statusFlags[myoff] = 0; + proc->pgxactoff = InvalidPGXactOff; /* Keep the PGPROC array sorted. See notes above */ movecount = arrayP->numProcs - myoff - 1; @@ -715,6 +716,7 @@ ProcArrayEndTransaction(PGPROC *proc, TransactionId latestXid) Assert(!LWLockHeldByMe(ProcArrayLock)); LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE); Assert(proc->statusFlags == ProcGlobal->statusFlags[proc->pgxactoff]); + Assert(PGXactOffIsValid(MyProc->pgxactoff)); proc->statusFlags &= ~PROC_VACUUM_STATE_MASK; ProcGlobal->statusFlags[proc->pgxactoff] = proc->statusFlags; LWLockRelease(ProcArrayLock); @@ -755,6 +757,7 @@ ProcArrayEndTransactionInternal(PGPROC *proc, TransactionId latestXid) if (proc->statusFlags & PROC_VACUUM_STATE_MASK) { proc->statusFlags &= ~PROC_VACUUM_STATE_MASK; + Assert(PGXactOffIsValid(MyProc->pgxactoff)); ProcGlobal->statusFlags[proc->pgxactoff] = proc->statusFlags; } @@ -2636,6 +2639,7 @@ ProcArrayInstallRestoredXmin(TransactionId xmin, PGPROC *proc) MyProc->xmin = TransactionXmin = xmin; MyProc->statusFlags = (MyProc->statusFlags & ~PROC_XMIN_FLAGS) | (proc->statusFlags & PROC_XMIN_FLAGS); + Assert(PGXactOffIsValid(MyProc->pgxactoff)); ProcGlobal->statusFlags[MyProc->pgxactoff] = MyProc->statusFlags; result = true; diff --git a/src/backend/storage/lmgr/proc.c b/src/backend/storage/lmgr/proc.c index 162b1f919d..1941618aa3 100644 --- a/src/backend/storage/lmgr/proc.c +++ b/src/backend/storage/lmgr/proc.c @@ -192,6 +192,10 @@ InitProcGlobal(void) procs = (PGPROC *) ShmemAlloc(TotalProcs * sizeof(PGPROC)); MemSet(procs, 0, TotalProcs * sizeof(PGPROC)); ProcGlobal->allProcs = procs; + for (int index = 0; index < TotalProcs; index++) + { + procs[index].pgxactoff = InvalidPGXactOff; + } /* XXX allProcCount isn't really all of them; it excludes prepared xacts */ ProcGlobal->allProcCount = MaxBackends + NUM_AUXILIARY_PROCS; diff --git a/src/include/storage/proc.h b/src/include/storage/proc.h index 18891a86fb..72ca279f78 100644 --- a/src/include/storage/proc.h +++ b/src/include/storage/proc.h @@ -127,6 +127,12 @@ typedef enum PROC_WAIT_STATUS_ERROR, } ProcWaitStatus; +/* + * Invalid value for PGPROC->pgxactoff. + */ +#define InvalidPGXactOff -1 +#define PGXactOffIsValid(pgxactoff) ((pgxactoff) != InvalidPGXactOff) + /* * Each backend has a PGPROC struct in shared memory. There is also a list of * currently-unused PGPROC structs that will be reallocated to new backends.v1-0000-v14-backpatch-2f6501f.patch (application/octet-stream, 5.2K) download
commit 526bb78facd8791921adfe37e307f8cb8e249c37 Author: Andres Freund <andres@anarazel.de> Date: Mon Feb 14 16:44:28 2022 -0800 Move replication slot release to before_shmem_exit(). Previously, replication slots were released in ProcKill() on error, resulting in reporting replication slot drop of ephemeral slots after the stats subsystem was already shut down. To fix this problem, move replication slot release to a before_shmem_exit() hook that is called before the stats collector shuts down. There wasn't really a good reason for the slot handling to be in ProcKill() anyway. Patch by Masahiko Sawada, with very minor polishing by me. I, Andres, wrote a test for dropping slots during process exit, but there may be some OS dependent issues around the number of times FATAL error messages are displayed due to a still debated libpq issue. So that test will be committed separately / later. Reviewed-By: Kyotaro Horiguchi <horikyota.ntt@gmail.com> Reviewed-By: Andres Freund <andres@anarazel.de> Author: Masahiko Sawada <sawada.mshk@gmail.com> Discussion: https://postgr.es/m/CAD21AoDAeEpAbZEyYJsPZJUmSPaRicVSBObaL7sPaofnKz+9zg@mail.gmail.com diff --git a/src/backend/replication/slot.c b/src/backend/replication/slot.c index 037a347cba..27f802427a 100644 --- a/src/backend/replication/slot.c +++ b/src/backend/replication/slot.c @@ -46,6 +46,7 @@ #include "pgstat.h" #include "replication/slot.h" #include "storage/fd.h" +#include "storage/ipc.h" #include "storage/proc.h" #include "storage/procarray.h" #include "utils/builtins.h" @@ -99,6 +100,7 @@ ReplicationSlot *MyReplicationSlot = NULL; int max_replication_slots = 0; /* the maximum number of replication * slots */ +static void ReplicationSlotShmemExit(int code, Datum arg); static void ReplicationSlotDropAcquired(void); static void ReplicationSlotDropPtr(ReplicationSlot *slot); @@ -160,6 +162,29 @@ ReplicationSlotsShmemInit(void) } } +/* + * Register the callback for replication slot cleanup and releasing. + */ +void +ReplicationSlotInitialize(void) +{ + before_shmem_exit(ReplicationSlotShmemExit, 0); +} + +/* + * Release and cleanup replication slots. + */ +static void +ReplicationSlotShmemExit(int code, Datum arg) +{ + /* Make sure active replication slots are released */ + if (MyReplicationSlot != NULL) + ReplicationSlotRelease(); + + /* Also cleanup all the temporary slots. */ + ReplicationSlotCleanup(); +} + /* * Check whether the passed slot name is valid and report errors at elevel. * diff --git a/src/backend/storage/lmgr/proc.c b/src/backend/storage/lmgr/proc.c index f62e622a7b..27d4966c70 100644 --- a/src/backend/storage/lmgr/proc.c +++ b/src/backend/storage/lmgr/proc.c @@ -853,13 +853,6 @@ ProcKill(int code, Datum arg) /* Cancel any pending condition variable sleep, too */ ConditionVariableCancelSleep(); - /* Make sure active replication slots are released */ - if (MyReplicationSlot != NULL) - ReplicationSlotRelease(); - - /* Also cleanup all the temporary slots. */ - ReplicationSlotCleanup(); - /* * Detach from any lock group of which we are a member. If the leader * exist before all other group members, its PGPROC will remain allocated diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c index c613495ee2..8d034844d6 100644 --- a/src/backend/tcop/postgres.c +++ b/src/backend/tcop/postgres.c @@ -4273,8 +4273,8 @@ PostgresMain(int argc, char *argv[], * We can't release replication slots inside AbortTransaction() as we * need to be able to start and abort transactions while having a slot * acquired. But we never need to hold them across top level errors, - * so releasing here is fine. There's another cleanup in ProcKill() - * ensuring we'll correctly cleanup on FATAL errors as well. + * so releasing here is fine. There also is a before_shmem_exit() + * callback ensuring correct cleanup on FATAL errors. */ if (MyReplicationSlot != NULL) ReplicationSlotRelease(); diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c index 0b5a3050f1..d756de19f3 100644 --- a/src/backend/utils/init/postinit.c +++ b/src/backend/utils/init/postinit.c @@ -40,6 +40,7 @@ #include "pgstat.h" #include "postmaster/autovacuum.h" #include "postmaster/postmaster.h" +#include "replication/slot.h" #include "replication/walsender.h" #include "storage/bufmgr.h" #include "storage/fd.h" @@ -548,6 +549,12 @@ BaseInit(void) InitSync(); smgrinit(); InitBufferPoolAccess(); + + /* + * Initialize replication slots after pgstat. The exit hook might need to + * drop ephemeral slots, which in turn triggers stats reporting. + */ + ReplicationSlotInitialize(); } diff --git a/src/include/replication/slot.h b/src/include/replication/slot.h index c0fca56bee..bad07e4ffc 100644 --- a/src/include/replication/slot.h +++ b/src/include/replication/slot.h @@ -207,6 +207,7 @@ extern void ReplicationSlotSave(void); extern void ReplicationSlotMarkDirty(void); /* misc stuff */ +extern void ReplicationSlotInitialize(void); extern bool ReplicationSlotValidateName(const char *name, int elevel); extern void ReplicationSlotReserveWal(void); extern void ReplicationSlotsComputeRequiredXmin(bool already_locked);v1-0000-v14-invalidate-pgxactoff-after-remove-pgproc.patch (application/octet-stream, 4.9K) download
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c index e4c792228f..85d10ba338 100644 --- a/src/backend/commands/indexcmds.c +++ b/src/backend/commands/indexcmds.c @@ -4397,6 +4397,7 @@ set_indexsafe_procflags(void) LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE); MyProc->statusFlags |= PROC_IN_SAFE_IC; + Assert(PGXactOffIsValid(MyProc->pgxactoff)); ProcGlobal->statusFlags[MyProc->pgxactoff] = MyProc->statusFlags; LWLockRelease(ProcArrayLock); } diff --git a/src/backend/commands/vacuum.c b/src/backend/commands/vacuum.c index 69ac276687..d4b095ed0e 100644 --- a/src/backend/commands/vacuum.c +++ b/src/backend/commands/vacuum.c @@ -2011,6 +2011,7 @@ vacuum_rel(Oid relid, RangeVar *relation, VacuumParams *params, MyProc->statusFlags |= PROC_IN_VACUUM; if (params->is_wraparound) MyProc->statusFlags |= PROC_VACUUM_FOR_WRAPAROUND; + Assert(PGXactOffIsValid(MyProc->pgxactoff)); ProcGlobal->statusFlags[MyProc->pgxactoff] = MyProc->statusFlags; LWLockRelease(ProcArrayLock); } diff --git a/src/backend/replication/logical/logical.c b/src/backend/replication/logical/logical.c index 41243d0187..1dd47e158a 100644 --- a/src/backend/replication/logical/logical.c +++ b/src/backend/replication/logical/logical.c @@ -195,6 +195,7 @@ StartupDecodingContext(List *output_plugin_options, { LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE); MyProc->statusFlags |= PROC_IN_LOGICAL_DECODING; + Assert(PGXactOffIsValid(MyProc->pgxactoff)); ProcGlobal->statusFlags[MyProc->pgxactoff] = MyProc->statusFlags; LWLockRelease(ProcArrayLock); } diff --git a/src/backend/replication/slot.c b/src/backend/replication/slot.c index e70f066eb8..c1a46df56d 100644 --- a/src/backend/replication/slot.c +++ b/src/backend/replication/slot.c @@ -593,6 +593,7 @@ ReplicationSlotRelease(void) /* might not have been set when we've been a plain slot */ LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE); MyProc->statusFlags &= ~PROC_IN_LOGICAL_DECODING; + Assert(PGXactOffIsValid(MyProc->pgxactoff)); ProcGlobal->statusFlags[MyProc->pgxactoff] = MyProc->statusFlags; LWLockRelease(ProcArrayLock); } diff --git a/src/backend/storage/ipc/procarray.c b/src/backend/storage/ipc/procarray.c index e630302351..3ec2d19cd3 100644 --- a/src/backend/storage/ipc/procarray.c +++ b/src/backend/storage/ipc/procarray.c @@ -611,6 +611,7 @@ ProcArrayRemove(PGPROC *proc, TransactionId latestXid) Assert(ProcGlobal->subxidStates[myoff].overflowed == false); ProcGlobal->statusFlags[myoff] = 0; + proc->pgxactoff = InvalidPGXactOff; /* Keep the PGPROC array sorted. See notes above */ movecount = arrayP->numProcs - myoff - 1; @@ -718,6 +719,7 @@ ProcArrayEndTransaction(PGPROC *proc, TransactionId latestXid) Assert(!LWLockHeldByMe(ProcArrayLock)); LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE); Assert(proc->statusFlags == ProcGlobal->statusFlags[proc->pgxactoff]); + Assert(PGXactOffIsValid(MyProc->pgxactoff)); proc->statusFlags &= ~PROC_VACUUM_STATE_MASK; ProcGlobal->statusFlags[proc->pgxactoff] = proc->statusFlags; LWLockRelease(ProcArrayLock); @@ -758,6 +760,7 @@ ProcArrayEndTransactionInternal(PGPROC *proc, TransactionId latestXid) if (proc->statusFlags & PROC_VACUUM_STATE_MASK) { proc->statusFlags &= ~PROC_VACUUM_STATE_MASK; + Assert(PGXactOffIsValid(MyProc->pgxactoff)); ProcGlobal->statusFlags[proc->pgxactoff] = proc->statusFlags; } @@ -2655,6 +2658,7 @@ ProcArrayInstallRestoredXmin(TransactionId xmin, PGPROC *proc) MyProc->xmin = TransactionXmin = xmin; MyProc->statusFlags = (MyProc->statusFlags & ~PROC_XMIN_FLAGS) | (proc->statusFlags & PROC_XMIN_FLAGS); + Assert(PGXactOffIsValid(MyProc->pgxactoff)); ProcGlobal->statusFlags[MyProc->pgxactoff] = MyProc->statusFlags; result = true; diff --git a/src/backend/storage/lmgr/proc.c b/src/backend/storage/lmgr/proc.c index e9e445bb21..2d33f43d50 100644 --- a/src/backend/storage/lmgr/proc.c +++ b/src/backend/storage/lmgr/proc.c @@ -192,6 +192,10 @@ InitProcGlobal(void) procs = (PGPROC *) ShmemAlloc(TotalProcs * sizeof(PGPROC)); MemSet(procs, 0, TotalProcs * sizeof(PGPROC)); ProcGlobal->allProcs = procs; + for (int index = 0; index < TotalProcs; index++) + { + procs[index].pgxactoff = InvalidPGXactOff; + } /* XXX allProcCount isn't really all of them; it excludes prepared xacts */ ProcGlobal->allProcCount = MaxBackends + NUM_AUXILIARY_PROCS; diff --git a/src/include/storage/proc.h b/src/include/storage/proc.h index ef74f32693..c137ce228d 100644 --- a/src/include/storage/proc.h +++ b/src/include/storage/proc.h @@ -132,6 +132,12 @@ typedef enum PROC_WAIT_STATUS_ERROR, } ProcWaitStatus; +/* + * Invalid value for PGPROC->pgxactoff. + */ +#define InvalidPGXactOff -1 +#define PGXactOffIsValid(pgxactoff) ((pgxactoff) != InvalidPGXactOff) + /* * Each backend has a PGPROC struct in shared memory. There is also a list of * currently-unused PGPROC structs that will be reallocated to new backends.^ permalink raw reply [nested|flat] 3+ messages in threadv1-0000-v15-v16-invalidate-pgxactoff-after-remove-pgproc.patch (application/octet-stream, 5.4K) download
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c index e4c792228f..85d10ba338 100644 --- a/src/backend/commands/indexcmds.c +++ b/src/backend/commands/indexcmds.c @@ -4397,6 +4397,7 @@ set_indexsafe_procflags(void) LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE); MyProc->statusFlags |= PROC_IN_SAFE_IC; + Assert(PGXactOffIsValid(MyProc->pgxactoff)); ProcGlobal->statusFlags[MyProc->pgxactoff] = MyProc->statusFlags; LWLockRelease(ProcArrayLock); } diff --git a/src/backend/commands/vacuum.c b/src/backend/commands/vacuum.c index 69ac276687..d4b095ed0e 100644 --- a/src/backend/commands/vacuum.c +++ b/src/backend/commands/vacuum.c @@ -2011,6 +2011,7 @@ vacuum_rel(Oid relid, RangeVar *relation, VacuumParams *params, MyProc->statusFlags |= PROC_IN_VACUUM; if (params->is_wraparound) MyProc->statusFlags |= PROC_VACUUM_FOR_WRAPAROUND; + Assert(PGXactOffIsValid(MyProc->pgxactoff)); ProcGlobal->statusFlags[MyProc->pgxactoff] = MyProc->statusFlags; LWLockRelease(ProcArrayLock); } diff --git a/src/backend/replication/logical/logical.c b/src/backend/replication/logical/logical.c index 41243d0187..1dd47e158a 100644 --- a/src/backend/replication/logical/logical.c +++ b/src/backend/replication/logical/logical.c @@ -195,6 +195,7 @@ StartupDecodingContext(List *output_plugin_options, { LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE); MyProc->statusFlags |= PROC_IN_LOGICAL_DECODING; + Assert(PGXactOffIsValid(MyProc->pgxactoff)); ProcGlobal->statusFlags[MyProc->pgxactoff] = MyProc->statusFlags; LWLockRelease(ProcArrayLock); } diff --git a/src/backend/replication/slot.c b/src/backend/replication/slot.c index e70f066eb8..c1a46df56d 100644 --- a/src/backend/replication/slot.c +++ b/src/backend/replication/slot.c @@ -593,6 +593,7 @@ ReplicationSlotRelease(void) /* might not have been set when we've been a plain slot */ LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE); MyProc->statusFlags &= ~PROC_IN_LOGICAL_DECODING; + Assert(PGXactOffIsValid(MyProc->pgxactoff)); ProcGlobal->statusFlags[MyProc->pgxactoff] = MyProc->statusFlags; LWLockRelease(ProcArrayLock); } diff --git a/src/backend/replication/walsender.c b/src/backend/replication/walsender.c index 4c53de08b9..2e670f6d0f 100644 --- a/src/backend/replication/walsender.c +++ b/src/backend/replication/walsender.c @@ -296,6 +296,7 @@ InitWalSender(void) Assert(MyProc->xmin == InvalidTransactionId); LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE); MyProc->statusFlags |= PROC_AFFECTS_ALL_HORIZONS; + Assert(PGXactOffIsValid(MyProc->pgxactoff)); ProcGlobal->statusFlags[MyProc->pgxactoff] = MyProc->statusFlags; LWLockRelease(ProcArrayLock); } diff --git a/src/backend/storage/ipc/procarray.c b/src/backend/storage/ipc/procarray.c index e630302351..3ec2d19cd3 100644 --- a/src/backend/storage/ipc/procarray.c +++ b/src/backend/storage/ipc/procarray.c @@ -611,6 +611,7 @@ ProcArrayRemove(PGPROC *proc, TransactionId latestXid) Assert(ProcGlobal->subxidStates[myoff].overflowed == false); ProcGlobal->statusFlags[myoff] = 0; + proc->pgxactoff = InvalidPGXactOff; /* Keep the PGPROC array sorted. See notes above */ movecount = arrayP->numProcs - myoff - 1; @@ -718,6 +719,7 @@ ProcArrayEndTransaction(PGPROC *proc, TransactionId latestXid) Assert(!LWLockHeldByMe(ProcArrayLock)); LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE); Assert(proc->statusFlags == ProcGlobal->statusFlags[proc->pgxactoff]); + Assert(PGXactOffIsValid(MyProc->pgxactoff)); proc->statusFlags &= ~PROC_VACUUM_STATE_MASK; ProcGlobal->statusFlags[proc->pgxactoff] = proc->statusFlags; LWLockRelease(ProcArrayLock); @@ -758,6 +760,7 @@ ProcArrayEndTransactionInternal(PGPROC *proc, TransactionId latestXid) if (proc->statusFlags & PROC_VACUUM_STATE_MASK) { proc->statusFlags &= ~PROC_VACUUM_STATE_MASK; + Assert(PGXactOffIsValid(MyProc->pgxactoff)); ProcGlobal->statusFlags[proc->pgxactoff] = proc->statusFlags; } @@ -2655,6 +2658,7 @@ ProcArrayInstallRestoredXmin(TransactionId xmin, PGPROC *proc) MyProc->xmin = TransactionXmin = xmin; MyProc->statusFlags = (MyProc->statusFlags & ~PROC_XMIN_FLAGS) | (proc->statusFlags & PROC_XMIN_FLAGS); + Assert(PGXactOffIsValid(MyProc->pgxactoff)); ProcGlobal->statusFlags[MyProc->pgxactoff] = MyProc->statusFlags; result = true; diff --git a/src/backend/storage/lmgr/proc.c b/src/backend/storage/lmgr/proc.c index e9e445bb21..2d33f43d50 100644 --- a/src/backend/storage/lmgr/proc.c +++ b/src/backend/storage/lmgr/proc.c @@ -192,6 +192,10 @@ InitProcGlobal(void) procs = (PGPROC *) ShmemAlloc(TotalProcs * sizeof(PGPROC)); MemSet(procs, 0, TotalProcs * sizeof(PGPROC)); ProcGlobal->allProcs = procs; + for (int index = 0; index < TotalProcs; index++) + { + procs[index].pgxactoff = InvalidPGXactOff; + } /* XXX allProcCount isn't really all of them; it excludes prepared xacts */ ProcGlobal->allProcCount = MaxBackends + NUM_AUXILIARY_PROCS; diff --git a/src/include/storage/proc.h b/src/include/storage/proc.h index ef74f32693..c137ce228d 100644 --- a/src/include/storage/proc.h +++ b/src/include/storage/proc.h @@ -132,6 +132,12 @@ typedef enum PROC_WAIT_STATUS_ERROR, } ProcWaitStatus; +/* + * Invalid value for PGPROC->pgxactoff. + */ +#define InvalidPGXactOff -1 +#define PGXactOffIsValid(pgxactoff) ((pgxactoff) != InvalidPGXactOff) + /* * Each backend has a PGPROC struct in shared memory. There is also a list of * currently-unused PGPROC structs that will be reallocated to new backends.
* Re: ReplicationSlotRelease may set the statusFlags of other processes in PG14 @ 2024-03-19 03:57 Michael Paquier <michael@paquier.xyz> parent: feichanghong <feichanghong@qq.com> 0 siblings, 1 reply; 3+ messages in thread From: Michael Paquier @ 2024-03-19 03:57 UTC (permalink / raw) To: feichanghong <feichanghong@qq.com>; +Cc: pgsql-bugs <pgsql-bugs@lists.postgresql.org>; andres <andres@anarazel.de>; sawada.mshk <sawada.mshk@gmail.com>; horikyota.ntt <horikyota.ntt@gmail.com> On Sat, Mar 16, 2024 at 10:29:03PM +0800, feichanghong wrote: > A process utilizing replication slots (usually walsender) calls callback > functions in the order of RemoveProcFromArray->ProcKill upon abnormal exit. > Within RemoveProcFromArray, MyProc is already removed from the ProcArray. > ProcKill then attempts to set ProcGlobal->statusFlags[MyProc->pgxactoff] again > via ReplicationSlotRelease. By this time, the flag may already be assigned to > another process. Oops. > To replicate the issue, execute the following steps: > 1. Apply the attached v1-0000-v14-invalidate-pgxactoff-after-remove-pgproc.patch, > where pgxactoff is set to an invalid value in ProcArrayRemove, and some > checks are added. > 2. Use the SQL below to terminate the walsender process. > ``` > select pg_terminate_backend(pid) from pg_stat_activity where backend_type = 'walsender'; > ``` > # Fix > > To fix the issue, I have provided some patches in the attachment: > 1. Backpatching 2f6501f into the PG14 version will fix the problem. > 2. In PG14-head, ProcArrayRemove needs to reset pgxactoff, and some assert > checks should be done when setting ProcGlobal->statusFlags. Yeah, that's something that we had better fix in all stable branches. The asserts would offer some protection moving on, but I would take the safer move of only adding a protection like what you are suggestion on HEAD and not in stable branches, just in case we're missing something around them. -- Michael Attachments: signature.asc (application/pgp-signature, 832B) download ^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: ReplicationSlotRelease may set the statusFlags of other processes in PG14 @ 2024-03-27 06:53 feichanghong <feichanghong@qq.com> parent: Michael Paquier <michael@paquier.xyz> 0 siblings, 0 replies; 3+ messages in thread From: feichanghong @ 2024-03-27 06:53 UTC (permalink / raw) To: Michael Paquier <michael@paquier.xyz>; +Cc: pgsql-bugs <pgsql-bugs@lists.postgresql.org>; andres <andres@anarazel.de>; sawada.mshk <sawada.mshk@gmail.com>; horikyota.ntt <horikyota.ntt@gmail.com> Dear Michael, Thank you for your attention. > On Mar 19, 2024, at 11:57, Michael Paquier <michael@paquier.xyz> wrote: > > Yeah, that's something that we had better fix in all stable branches. Yes, as I mentioned in my last reply, it is only necessary to fix the issue in V14. > The asserts would offer some protection moving on, but I would take > the safer move of only adding a protection like what you are > suggestion on HEAD and not in stable branches, just in case we're > missing something around them. I agree it, we may only need to add assert checks on HEAD. Best Regards, Fei Changhong Alibaba Cloud Computing Ltd. ^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2024-03-27 06:53 UTC | newest] Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed) -- links below jump to the message on this page -- 2024-03-16 14:29 ReplicationSlotRelease may set the statusFlags of other processes in PG14 feichanghong <feichanghong@qq.com> 2024-03-19 03:57 ` Michael Paquier <michael@paquier.xyz> 2024-03-27 06:53 ` feichanghong <feichanghong@qq.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions for how to clone and mirror all data and code used for this inbox