Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9668c4a

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 d1e2a38 commit 9668c4a

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
@@ -3625,8 +3625,9 @@ include_dir 'conf.d'
36253625
The library to use for archiving completed WAL file segments. If set to
36263626
an empty string (the default), archiving via shell is enabled, and
36273627
<xref linkend="guc-archive-command"/> is used. Otherwise, the specified
3628-
shared library is used for archiving. For more information, see
3629-
<xref linkend="backup-archiving-wal"/> and
3628+
shared library is used for archiving. The WAL archiver process is
3629+
restarted by the postmaster when this parameter changes. For more
3630+
information, see <xref linkend="backup-archiving-wal"/> and
36303631
<xref linkend="archive-modules"/>.
36313632
</para>
36323633
<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
}
@@ -803,19 +797,14 @@ HandlePgArchInterrupts(void)
803797

804798
if (archiveLibChanged)
805799
{
806-
/*
807-
* Call the currently loaded archive module's shutdown callback,
808-
* if one is defined.
809-
*/
810-
call_archive_module_shutdown_callback(0, 0);
811-
812800
/*
813801
* Ideally, we would simply unload the previous archive module and
814802
* load the new one, but there is presently no mechanism for
815803
* unloading a library (see the comment above
816804
* internal_load_library()). To deal with this, we simply restart
817805
* the archiver. The new archive module will be loaded when the
818-
* new archiver process starts up.
806+
* new archiver process starts up. Note that this triggers the
807+
* module's shutdown callback, if defined.
819808
*/
820809
ereport(LOG,
821810
(errmsg("restarting archiver process because value of "
@@ -858,15 +847,15 @@ LoadArchiveLibrary(void)
858847
if (ArchiveContext.archive_file_cb == NULL)
859848
ereport(ERROR,
860849
(errmsg("archive modules must register an archive callback")));
850+
851+
before_shmem_exit(pgarch_call_module_shutdown_cb, 0);
861852
}
862853

863854
/*
864-
* call_archive_module_shutdown_callback
865-
*
866-
* Calls the loaded archive module's shutdown callback, if one is defined.
855+
* Call the shutdown callback of the loaded archive module, if defined.
867856
*/
868857
static void
869-
call_archive_module_shutdown_callback(int code, Datum arg)
858+
pgarch_call_module_shutdown_cb(int code, Datum arg)
870859
{
871860
if (ArchiveContext.shutdown_cb != NULL)
872861
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
@@ -156,3 +158,9 @@ shell_archive_file(const char *file, const char *path)
156158
elog(DEBUG1, "archived write-ahead log file \"%s\"", file);
157159
return true;
158160
}
161+
162+
static void
163+
shell_archive_shutdown(void)
164+
{
165+
elog(DEBUG1, "archiver process shutting down");
166+
}

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)