I just found myself carefully counting the zeros in a call to pg_usleep(). Besides getting my eyes checked, perhaps there should be a wrapper called pg_ssleep() than can be used for multisecond sleeps. Or maybe the USECS_PER_SEC macro should be used more widely. I attached a patch for the former approach. I don't have a strong opinion, but I do think it's worth improving readability a bit here.
-- Nathan Bossart Amazon Web Services: https://aws.amazon.com
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c index 3feee28d19..63de896cae 100644 --- a/src/backend/access/nbtree/nbtpage.c +++ b/src/backend/access/nbtree/nbtpage.c @@ -2976,7 +2976,7 @@ _bt_pendingfsm_finalize(Relation rel, BTVacState *vstate) * never be effective without some other backend concurrently consuming an * XID. */ - pg_usleep(5000000L); + pg_ssleep(5); #endif /* diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index f9f0f6db8d..87664045d0 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -5103,7 +5103,7 @@ StartupXLOG(void) /* This is just to allow attaching to startup process with a debugger */ #ifdef XLOG_REPLAY_DELAY if (ControlFile->state != DB_SHUTDOWNED) - pg_usleep(60000000L); + pg_ssleep(60); #endif /* diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c index ff6149a179..744adff984 100644 --- a/src/backend/postmaster/autovacuum.c +++ b/src/backend/postmaster/autovacuum.c @@ -441,7 +441,7 @@ AutoVacLauncherMain(int argc, char *argv[]) (errmsg_internal("autovacuum launcher started"))); if (PostAuthDelay) - pg_usleep(PostAuthDelay * 1000000L); + pg_ssleep(PostAuthDelay); SetProcessingMode(InitProcessing); @@ -561,7 +561,7 @@ AutoVacLauncherMain(int argc, char *argv[]) * Sleep at least 1 second after any error. We don't want to be * filling the error logs as fast as we can. */ - pg_usleep(1000000L); + pg_ssleep(1); } /* We can now handle ereport(ERROR) */ @@ -687,7 +687,7 @@ AutoVacLauncherMain(int argc, char *argv[]) * of a worker will continue to fail in the same way. */ AutoVacuumShmem->av_signal[AutoVacForkFailed] = false; - pg_usleep(1000000L); /* 1s */ + pg_ssleep(1); SendPostmasterSignal(PMSIGNAL_START_AUTOVAC_WORKER); continue; } @@ -1708,7 +1708,7 @@ AutoVacWorkerMain(int argc, char *argv[]) (errmsg_internal("autovacuum: processing database \"%s\"", dbname))); if (PostAuthDelay) - pg_usleep(PostAuthDelay * 1000000L); + pg_ssleep(PostAuthDelay); /* And do an appropriate amount of work */ recentXid = ReadNextTransactionId(); diff --git a/src/backend/postmaster/bgworker.c b/src/backend/postmaster/bgworker.c index 0dd22b2351..6d38dfeeba 100644 --- a/src/backend/postmaster/bgworker.c +++ b/src/backend/postmaster/bgworker.c @@ -759,7 +759,7 @@ StartBackgroundWorker(void) /* Apply PostAuthDelay */ if (PostAuthDelay > 0) - pg_usleep(PostAuthDelay * 1000000L); + pg_ssleep(PostAuthDelay); /* * Set up signal handlers. diff --git a/src/backend/postmaster/bgwriter.c b/src/backend/postmaster/bgwriter.c index 9bb47da404..147f9b1e38 100644 --- a/src/backend/postmaster/bgwriter.c +++ b/src/backend/postmaster/bgwriter.c @@ -196,7 +196,7 @@ BackgroundWriterMain(void) * to be repeated, and we don't want to be filling the error logs as * fast as we can. */ - pg_usleep(1000000L); + pg_ssleep(1); /* * Close all open files after any error. This is helpful on Windows, diff --git a/src/backend/postmaster/checkpointer.c b/src/backend/postmaster/checkpointer.c index aaad5c5228..12786229dd 100644 --- a/src/backend/postmaster/checkpointer.c +++ b/src/backend/postmaster/checkpointer.c @@ -310,7 +310,7 @@ CheckpointerMain(void) * to be repeated, and we don't want to be filling the error logs as * fast as we can. */ - pg_usleep(1000000L); + pg_ssleep(1); /* * Close all open files after any error. This is helpful on Windows, diff --git a/src/backend/postmaster/pgarch.c b/src/backend/postmaster/pgarch.c index e551af2905..6460c69726 100644 --- a/src/backend/postmaster/pgarch.c +++ b/src/backend/postmaster/pgarch.c @@ -450,7 +450,7 @@ pgarch_ArchiverCopyLoop(void) } /* wait a bit before retrying */ - pg_usleep(1000000L); + pg_ssleep(1); continue; } @@ -482,7 +482,7 @@ pgarch_ArchiverCopyLoop(void) xlog))); return; /* give up archiving for now */ } - pg_usleep(1000000L); /* wait a bit before retrying */ + pg_ssleep(1); /* wait a bit before retrying */ } } } diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c index 2552327d90..6b80f423a8 100644 --- a/src/backend/postmaster/postmaster.c +++ b/src/backend/postmaster/postmaster.c @@ -4292,7 +4292,7 @@ BackendInitialize(Port *port) * is not honored until after authentication.) */ if (PreAuthDelay > 0) - pg_usleep(PreAuthDelay * 1000000L); + pg_ssleep(PreAuthDelay); /* This flag will remain set until InitPostgres finishes authentication */ ClientAuthInProgress = true; /* limit visibility of log messages */ diff --git a/src/backend/postmaster/walwriter.c b/src/backend/postmaster/walwriter.c index 513e580c51..a003717efd 100644 --- a/src/backend/postmaster/walwriter.c +++ b/src/backend/postmaster/walwriter.c @@ -189,7 +189,7 @@ WalWriterMain(void) * to be repeated, and we don't want to be filling the error logs as * fast as we can. */ - pg_usleep(1000000L); + pg_ssleep(1); /* * Close all open files after any error. This is helpful on Windows, diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c index 2f07ca7a0e..5647795f4a 100644 --- a/src/backend/utils/init/postinit.c +++ b/src/backend/utils/init/postinit.c @@ -973,7 +973,7 @@ InitPostgres(const char *in_dbname, Oid dboid, /* Apply PostAuthDelay as soon as we've read all options */ if (PostAuthDelay > 0) - pg_usleep(PostAuthDelay * 1000000L); + pg_ssleep(PostAuthDelay); /* initialize client encoding */ InitializeClientEncoding(); @@ -1180,7 +1180,7 @@ InitPostgres(const char *in_dbname, Oid dboid, /* Apply PostAuthDelay as soon as we've read all options */ if (PostAuthDelay > 0) - pg_usleep(PostAuthDelay * 1000000L); + pg_ssleep(PostAuthDelay); /* * Initialize various default states that can't be set up until we've diff --git a/src/bin/pg_basebackup/pg_recvlogical.c b/src/bin/pg_basebackup/pg_recvlogical.c index f3c7937a1d..ca8bfac936 100644 --- a/src/bin/pg_basebackup/pg_recvlogical.c +++ b/src/bin/pg_basebackup/pg_recvlogical.c @@ -991,7 +991,7 @@ main(int argc, char **argv) /* translator: check source for value for %d */ pg_log_info("disconnected; waiting %d seconds to try again", RECONNECT_SLEEP_TIME); - pg_usleep(RECONNECT_SLEEP_TIME * 1000000); + pg_ssleep(RECONNECT_SLEEP_TIME); } } } diff --git a/src/bin/pg_upgrade/exec.c b/src/bin/pg_upgrade/exec.c index 5b2edebe41..a1fc352eb8 100644 --- a/src/bin/pg_upgrade/exec.c +++ b/src/bin/pg_upgrade/exec.c @@ -151,7 +151,7 @@ exec_prog(const char *log_filename, const char *opt_log_file, for (iter = 0; iter < 4 && log == NULL; iter++) { - pg_usleep(1000000); /* 1 sec */ + pg_ssleep(1); log = fopen(log_file, "a"); } } diff --git a/src/bin/pg_waldump/pg_waldump.c b/src/bin/pg_waldump/pg_waldump.c index 44b5c8726e..610ebc777c 100644 --- a/src/bin/pg_waldump/pg_waldump.c +++ b/src/bin/pg_waldump/pg_waldump.c @@ -1217,7 +1217,7 @@ main(int argc, char **argv) break; else { - pg_usleep(1000000L); /* 1 second */ + pg_ssleep(1); continue; } } diff --git a/src/include/port.h b/src/include/port.h index e66193bed9..43ce931766 100644 --- a/src/include/port.h +++ b/src/include/port.h @@ -163,6 +163,11 @@ extern int pg_disable_aslr(void); /* Portable delay handling */ extern void pg_usleep(long microsec); +static inline void pg_ssleep(long sec) +{ + pg_usleep(sec * 1000000L); +} + /* Portable SQL-like case-independent comparisons and conversions */ extern int pg_strcasecmp(const char *s1, const char *s2); extern int pg_strncasecmp(const char *s1, const char *s2, size_t n); diff --git a/src/test/regress/pg_regress.c b/src/test/regress/pg_regress.c index 6cd5998b9d..018237d750 100644 --- a/src/test/regress/pg_regress.c +++ b/src/test/regress/pg_regress.c @@ -2386,7 +2386,7 @@ regression_main(int argc, char *argv[], exit(2); } - pg_usleep(1000000L); + pg_ssleep(1); } if (i >= wait_seconds) {