Initialize atomic variable waitStart in PGPROC, at postmaster startup.
authorFujii Masao <[email protected]>
Mon, 22 Feb 2021 09:25:00 +0000 (18:25 +0900)
committerFujii Masao <[email protected]>
Mon, 22 Feb 2021 09:25:00 +0000 (18:25 +0900)
Commit 46d6e5f567 added the atomic variable "waitStart" into PGPROC struct,
to store the time at which wait for lock acquisition started. Previously
this variable was initialized every time each backend started. Instead,
this commit makes postmaster initialize it at the startup, to ensure that
the variable should be initialized before any use of it.

This commit also moves the code to initialize "waitStart" variable for
prepare transaction, from TwoPhaseGetDummyProc() to MarkAsPreparingGuts().
Because MarkAsPreparingGuts() is more proper place to do that since
it initializes other PGPROC variables.

Author: Fujii Masao
Reviewed-by: Atsushi Torikoshi
Discussion: https://postgr.es/m/1df88660-6f08-cc6e-b7e2-f85296a2bdab@oss.nttdata.com

src/backend/access/transam/twophase.c
src/backend/storage/lmgr/proc.c

index 70d22577ceee755b7fcf9a4ed5c60e02f85970d3..c9b13062b0d5dbc453034f25cdb1a3a6d2c0b440 100644 (file)
@@ -475,6 +475,7 @@ MarkAsPreparingGuts(GlobalTransaction gxact, TransactionId xid, const char *gid,
    proc->lwWaitMode = 0;
    proc->waitLock = NULL;
    proc->waitProcLock = NULL;
+   pg_atomic_init_u64(&proc->waitStart, 0);
    for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
        SHMQueueInit(&(proc->myProcLocks[i]));
    /* subxid data must be filled later by GXactLoadSubxactData */
@@ -873,15 +874,8 @@ PGPROC *
 TwoPhaseGetDummyProc(TransactionId xid, bool lock_held)
 {
    GlobalTransaction gxact = TwoPhaseGetGXact(xid, lock_held);
-   PGPROC     *dummy = &ProcGlobal->allProcs[gxact->pgprocno];
 
-   /*
-    * Initialize atomic variable in dummy proc so that GetLockStatusData()
-    * can read it later.
-    */
-   pg_atomic_init_u64(&dummy->waitStart, 0);
-
-   return dummy;
+   return &ProcGlobal->allProcs[gxact->pgprocno];
 }
 
 /************************************************************************/
index db9529a534a35370e45e15aeaa3a4cd0ee18bd7d..897045ee27262918714686f09d986f6b11b8463c 100644 (file)
@@ -281,6 +281,7 @@ InitProcGlobal(void)
         */
        pg_atomic_init_u32(&(procs[i].procArrayGroupNext), INVALID_PGPROCNO);
        pg_atomic_init_u32(&(procs[i].clogGroupNext), INVALID_PGPROCNO);
+       pg_atomic_init_u64(&(procs[i].waitStart), 0);
    }
 
    /*
@@ -402,7 +403,7 @@ InitProcess(void)
    MyProc->lwWaitMode = 0;
    MyProc->waitLock = NULL;
    MyProc->waitProcLock = NULL;
-   pg_atomic_init_u64(&MyProc->waitStart, 0);
+   pg_atomic_write_u64(&MyProc->waitStart, 0);
 #ifdef USE_ASSERT_CHECKING
    {
        int         i;
@@ -581,7 +582,7 @@ InitAuxiliaryProcess(void)
    MyProc->lwWaitMode = 0;
    MyProc->waitLock = NULL;
    MyProc->waitProcLock = NULL;
-   pg_atomic_init_u64(&MyProc->waitStart, 0);
+   pg_atomic_write_u64(&MyProc->waitStart, 0);
 #ifdef USE_ASSERT_CHECKING
    {
        int         i;