Move InitXLogInsert() call from InitXLOGAccess() to BaseInit().
authorRobert Haas <rhaas@postgresql.org>
Tue, 16 Nov 2021 14:43:17 +0000 (09:43 -0500)
committerRobert Haas <rhaas@postgresql.org>
Tue, 16 Nov 2021 14:43:17 +0000 (09:43 -0500)
At present, there is an undocumented coding rule that you must call
RecoveryInProgress(), or do something else that results in a call
to InitXLogInsert(), before trying to write WAL. Otherwise, the
WAL construction buffers won't be initialized, resulting in
failures.

Since it's not good to rely on a status inquiry function like
RecoveryInProgress() having the side effect of initializing
critical data structures, instead do the initialization eariler,
when the backend first starts up.

Patch by me. Reviewed by Nathan Bossart and Michael Paquier.

Discussion: http://postgr.es/m/CA+TgmoY7b65qRjzHN_tWUk8B4sJqk1vj1d31uepVzmgPnZKeLg@mail.gmail.com

src/backend/access/transam/xlog.c
src/backend/utils/init/postinit.c

index e073121a7e7eb352747c172e281006b363b1bcf2..355d1737c39bd9e328a46d652b1684a583715c2f 100644 (file)
@@ -8677,9 +8677,6 @@ InitXLOGAccess(void)
    (void) GetRedoRecPtr();
    /* Also update our copy of doPageWrites. */
    doPageWrites = (Insert->fullPageWrites || Insert->forcePageWrites);
-
-   /* Also initialize the working areas for constructing WAL records */
-   InitXLogInsert();
 }
 
 /*
@@ -9129,16 +9126,6 @@ CreateCheckPoint(int flags)
    if (RecoveryInProgress() && (flags & CHECKPOINT_END_OF_RECOVERY) == 0)
        elog(ERROR, "can't create a checkpoint during recovery");
 
-   /*
-    * Initialize InitXLogInsert working areas before entering the critical
-    * section.  Normally, this is done by the first call to
-    * RecoveryInProgress() or LocalSetXLogInsertAllowed(), but when creating
-    * an end-of-recovery checkpoint, the LocalSetXLogInsertAllowed call is
-    * done below in a critical section, and InitXLogInsert cannot be called
-    * in a critical section.
-    */
-   InitXLogInsert();
-
    /*
     * Prepare to accumulate statistics.
     *
index 78bc64671ec5a277e741e2d4675e3088a62eb3b4..0c56c38a141de9a6d780d26ca4b47efe9d114956 100644 (file)
@@ -541,6 +541,12 @@ BaseInit(void)
     * file shutdown hook can report temporary file statistics.
     */
    InitTemporaryFileAccess();
+
+   /*
+    * Initialize local buffers for WAL record construction, in case we
+    * ever try to insert XLOG.
+    */
+   InitXLogInsert();
 }