Skip to content

Commit 499c9b1

Browse files
committed
Improve some check logic in pg_receivewal
The following things are improved: - Fetch the system identifier from the source server before any WAL streaming loop. This triggers extra checks to make sure that pg_receivewal is still connected to a server with the same system ID with a correct timeline. - Switch umask() (for file creation mode mask) and RetrieveWalSegSize() (to fetch the size of WAL segments) a bit later before the initial stream attempt. If the connection was done with a database, pg_receivewal would fail but those commands were still executed, which was a waste. The slot creation and drop are now done before retrieving the segment size. Author: Bharath Rupireddy Reviewed-by: Ronan Dunklau, Michael Paquier Discussion: https://postgr.es/m/CALj2ACX00YYeyBfoi55Cy=NrP-FcfMgiYYx1qRUEib3yjCVoaA@mail.gmail.com
1 parent d7897ab commit 499c9b1

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

src/bin/pg_basebackup/pg_receivewal.c

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ StreamLog(void)
372372
XLogRecPtr serverpos;
373373
TimeLineID servertli;
374374
StreamCtl stream;
375+
char *sysidentifier;
375376

376377
MemSet(&stream, 0, sizeof(stream));
377378

@@ -399,7 +400,7 @@ StreamLog(void)
399400
* at the same time, necessary if not valid data can be found in the
400401
* existing output directory.
401402
*/
402-
if (!RunIdentifySystem(conn, NULL, &servertli, &serverpos, NULL))
403+
if (!RunIdentifySystem(conn, &sysidentifier, &servertli, &serverpos, NULL))
403404
exit(1);
404405

405406
/*
@@ -435,6 +436,7 @@ StreamLog(void)
435436
stream.do_sync);
436437
stream.partial_suffix = ".partial";
437438
stream.replication_slot = replication_slot;
439+
stream.sysidentifier = sysidentifier;
438440

439441
ReceiveXlogStream(conn, &stream);
440442

@@ -449,6 +451,7 @@ StreamLog(void)
449451

450452
FreeWalDirectoryMethod();
451453
pg_free(stream.walmethod);
454+
pg_free(stream.sysidentifier);
452455
}
453456

454457
/*
@@ -687,20 +690,6 @@ main(int argc, char **argv)
687690
if (!RunIdentifySystem(conn, NULL, NULL, NULL, &db_name))
688691
exit(1);
689692

690-
/*
691-
* Set umask so that directories/files are created with the same
692-
* permissions as directories/files in the source data directory.
693-
*
694-
* pg_mode_mask is set to owner-only by default and then updated in
695-
* GetConnection() where we get the mode from the server-side with
696-
* RetrieveDataDirCreatePerm() and then call SetDataDirectoryCreatePerm().
697-
*/
698-
umask(pg_mode_mask);
699-
700-
/* determine remote server's xlog segment size */
701-
if (!RetrieveWalSegSize(conn))
702-
exit(1);
703-
704693
/*
705694
* Check that there is a database associated with connection, none should
706695
* be defined in this context.
@@ -712,6 +701,16 @@ main(int argc, char **argv)
712701
exit(1);
713702
}
714703

704+
/*
705+
* Set umask so that directories/files are created with the same
706+
* permissions as directories/files in the source data directory.
707+
*
708+
* pg_mode_mask is set to owner-only by default and then updated in
709+
* GetConnection() where we get the mode from the server-side with
710+
* RetrieveDataDirCreatePerm() and then call SetDataDirectoryCreatePerm().
711+
*/
712+
umask(pg_mode_mask);
713+
715714
/*
716715
* Drop a replication slot.
717716
*/
@@ -737,6 +736,10 @@ main(int argc, char **argv)
737736
exit(0);
738737
}
739738

739+
/* determine remote server's xlog segment size */
740+
if (!RetrieveWalSegSize(conn))
741+
exit(1);
742+
740743
/*
741744
* Don't close the connection here so that subsequent StreamLog() can
742745
* reuse it.

0 commit comments

Comments
 (0)