Skip to content

Commit 5d2a47a

Browse files
committed
Rework shutdown callback of archiver modules
As currently designed, with a callback registered in a ERROR_CLEANUP block, the shutdown callback would get called twice when updating archive_library on SIGHUP, which is something that we want to avoid to ease the life of extension writers. Anyway, an ERROR in the archiver process is treated as a FATAL, stopping it immediately, hence there is no need for a ERROR_CLEANUP block. Instead of that, the shutdown callback is not called upon before_shmem_exit(), giving to the modules the opportunity to do any cleanup actions before the server shuts down its subsystems. While on it, this commit adds some testing coverage for the shutdown callback. Neither shell_archive nor basic_archive have been using it, and one is added to shell_archive, whose trigger is checked in a TAP test through a shutdown sequence. Author: Nathan Bossart, Bharath Rupireddy Reviewed-by: Kyotaro Horiguchi, Michael Paquier Discussion: https://postgr.es/m/20221015221328.GB1821022@nathanxps13 Backpatch-through: 15
1 parent 25fb957 commit 5d2a47a

File tree

4 files changed

+33
-21
lines changed

4 files changed

+33
-21
lines changed

doc/src/sgml/config.sgml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3618,8 +3618,9 @@ include_dir 'conf.d'
36183618
The library to use for archiving completed WAL file segments. If set to
36193619
an empty string (the default), archiving via shell is enabled, and
36203620
<xref linkend="guc-archive-command"/> is used. Otherwise, the specified
3621-
shared library is used for archiving. For more information, see
3622-
<xref linkend="backup-archiving-wal"/> and
3621+
shared library is used for archiving. The WAL archiver process is
3622+
restarted by the postmaster when this parameter changes. For more
3623+
information, see <xref linkend="backup-archiving-wal"/> and
36233624
<xref linkend="archive-modules"/>.
36243625
</para>
36253626
<para>

src/backend/postmaster/pgarch.c

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ static void pgarch_die(int code, Datum arg);
144144
static void HandlePgArchInterrupts(void);
145145
static int ready_file_comparator(Datum a, Datum b, void *arg);
146146
static void LoadArchiveLibrary(void);
147-
static void call_archive_module_shutdown_callback(int code, Datum arg);
147+
static void pgarch_call_module_shutdown_cb(int code, Datum arg);
148148

149149
/* Report shared memory space needed by PgArchShmemInit */
150150
Size
@@ -252,13 +252,7 @@ PgArchiverMain(void)
252252
/* Load the archive_library. */
253253
LoadArchiveLibrary();
254254

255-
PG_ENSURE_ERROR_CLEANUP(call_archive_module_shutdown_callback, 0);
256-
{
257-
pgarch_MainLoop();
258-
}
259-
PG_END_ENSURE_ERROR_CLEANUP(call_archive_module_shutdown_callback, 0);
260-
261-
call_archive_module_shutdown_callback(0, 0);
255+
pgarch_MainLoop();
262256

263257
proc_exit(0);
264258
}
@@ -791,19 +785,14 @@ HandlePgArchInterrupts(void)
791785

792786
if (archiveLibChanged)
793787
{
794-
/*
795-
* Call the currently loaded archive module's shutdown callback,
796-
* if one is defined.
797-
*/
798-
call_archive_module_shutdown_callback(0, 0);
799-
800788
/*
801789
* Ideally, we would simply unload the previous archive module and
802790
* load the new one, but there is presently no mechanism for
803791
* unloading a library (see the comment above
804792
* internal_load_library()). To deal with this, we simply restart
805793
* the archiver. The new archive module will be loaded when the
806-
* new archiver process starts up.
794+
* new archiver process starts up. Note that this triggers the
795+
* module's shutdown callback, if defined.
807796
*/
808797
ereport(LOG,
809798
(errmsg("restarting archiver process because value of "
@@ -846,15 +835,15 @@ LoadArchiveLibrary(void)
846835
if (ArchiveContext.archive_file_cb == NULL)
847836
ereport(ERROR,
848837
(errmsg("archive modules must register an archive callback")));
838+
839+
before_shmem_exit(pgarch_call_module_shutdown_cb, 0);
849840
}
850841

851842
/*
852-
* call_archive_module_shutdown_callback
853-
*
854-
* Calls the loaded archive module's shutdown callback, if one is defined.
843+
* Call the shutdown callback of the loaded archive module, if defined.
855844
*/
856845
static void
857-
call_archive_module_shutdown_callback(int code, Datum arg)
846+
pgarch_call_module_shutdown_cb(int code, Datum arg)
858847
{
859848
if (ArchiveContext.shutdown_cb != NULL)
860849
ArchiveContext.shutdown_cb();

src/backend/postmaster/shell_archive.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
static bool shell_archive_configured(void);
2525
static bool shell_archive_file(const char *file, const char *path);
26+
static void shell_archive_shutdown(void);
2627

2728
void
2829
shell_archive_init(ArchiveModuleCallbacks *cb)
@@ -31,6 +32,7 @@ shell_archive_init(ArchiveModuleCallbacks *cb)
3132

3233
cb->check_configured_cb = shell_archive_configured;
3334
cb->archive_file_cb = shell_archive_file;
35+
cb->shutdown_cb = shell_archive_shutdown;
3436
}
3537

3638
static bool
@@ -155,3 +157,9 @@ shell_archive_file(const char *file, const char *path)
155157
elog(DEBUG1, "archived write-ahead log file \"%s\"", file);
156158
return true;
157159
}
160+
161+
static void
162+
shell_archive_shutdown(void)
163+
{
164+
elog(DEBUG1, "archiver process shutting down");
165+
}

src/test/recovery/t/020_archive_status.pl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,18 @@
234234
".done files created after archive success with archive_mode=always on standby"
235235
);
236236

237+
# Check that the archiver process calls the shell archive module's shutdown
238+
# callback.
239+
$standby2->append_conf('postgresql.conf', "log_min_messages = debug1");
240+
$standby2->reload;
241+
242+
# Run a query to make sure that the reload has taken effect.
243+
$standby2->safe_psql('postgres', q{SELECT 1});
244+
my $log_location = -s $standby2->logfile;
245+
246+
$standby2->stop;
247+
my $logfile = slurp_file($standby2->logfile, $log_location);
248+
ok( $logfile =~ qr/archiver process shutting down/,
249+
'check shutdown callback of shell archive module');
250+
237251
done_testing();

0 commit comments

Comments
 (0)