pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #18258: The redo of the drop db wal needs to forward the Min Recovery Point
4+ messages / 3 participants
[nested] [flat]

* BUG #18258: The redo of the drop db wal needs to forward the Min Recovery Point
@ 2023-12-24 15:02  PG Bug reporting form <noreply@postgresql.org>
  0 siblings, 1 reply; 4+ messages in thread

From: PG Bug reporting form @ 2023-12-24 15:02 UTC (permalink / raw)
  To: pgsql-bugs@lists.postgresql.org; +Cc: azurezhao8369@gmail.com

The following bug has been logged on the website:

Bug reference:      18258
Logged by:          azure young
Email address:      azurezhao8369@gmail.com
PostgreSQL version: 16.1
Operating system:   linux
Description:        

The redo of the drop db log needs to promptly advance the Min Recovery Point
(MRP).
because if the drop db wal is redone but the MRP is not advanced,
it means that the MRP is before the drop db, but the DB directory is
deleted.
If the standby restarts at this time and redoes,
for the directory does not exist,
there will be an invalid page related to this directory.
When it reaches the MRP, it triggers a consistency check, which will fail
and panic.



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: BUG #18258: The redo of the drop db wal needs to forward the Min Recovery Point
@ 2026-11-10 03:29  shihao zhong <zhong950419@gmail.com>
  parent: PG Bug reporting form <noreply@postgresql.org>
  0 siblings, 1 reply; 4+ messages in thread

From: shihao zhong @ 2026-11-10 03:29 UTC (permalink / raw)
  To: azurezhao8369@gmail.com; pgsql-bugs@lists.postgresql.org; +Cc: Noah Misch <noah@leadboat.com>

Hi,

I found this bug on Noah's wanted list [1], so here is a patch. I can
reproduce it on master. If the standby restarts after replaying the
drop record, it reaches consistency too early and then fails with:

  PANIC:  WAL contains references to invalid pages

DROP TABLESPACE and ALTER DATABASE SET TABLESPACE have the same gap.
The patch calls XLogFlush() before the files are removed, like
xact_redo_commit() does. The new TAP test fails without the fix and
passes with it.

Thanks,
Shihao

[1] https://wiki.postgresql.org/wiki/User:Nmisch/Wanted

Attachments: download all (2 files, .tar.gz)

v1-0002-Add-test-for-minRecoveryPoint-after-drop-redo.patch (application/octet-stream, 3.7K) download From 5c1df80ba5a581ead702e2036100cedff3926d83 Mon Sep 17 00:00:00 2001 From: shihao zhong <zhong950419@gmail.com> Date: Mon, 19 Oct 2026 14:25:57 -0400 Subject: [PATCH 2/2] Add test for minRecoveryPoint after drop redo Test that a standby stopped between a drop record and its commit record can restart. Discussion: https://postgr.es/m/18258-1e2fd3058dda84b0@postgresql.org --- src/test/recovery/t/033_replay_tsp_drops.pl | 71 +++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/src/test/recovery/t/033_replay_tsp_drops.pl b/src/test/recovery/t/033_replay_tsp_drops.pl index 93c89550511..92454e859ce 100644 --- a/src/test/recovery/t/033_replay_tsp_drops.pl +++ b/src/test/recovery/t/033_replay_tsp_drops.pl @@ -142,4 +142,75 @@ while ($max_attempts-- >= 0) } ok($max_attempts > 0, "invalid directory creation is detected"); +# Replay of DROP DATABASE and DROP TABLESPACE must advance minRecoveryPoint +# before removing files, or a standby stopped before the commit record would +# reach consistency too early on restart and fail on the removed files. +$node_primary = PostgreSQL::Test::Cluster->new('primary4'); +$node_primary->init(allows_streaming => 1); +# Without full-page images, replay of the INSERT below reads the dropped page. +$node_primary->append_conf('postgresql.conf', 'full_page_writes = off'); +$node_primary->start; +my $tsdir = PostgreSQL::Test::Utils::tempdir_short(); +$node_primary->safe_psql( + 'postgres', qq[ + CREATE TABLESPACE ts2 LOCATION '$tsdir'; + CREATE DATABASE db3; + ]); +$node_primary->safe_psql('db3', + 'CREATE TABLE t (a int); INSERT INTO t VALUES (1)'); +my $tsoid2 = $node_primary->safe_psql('postgres', + "SELECT oid FROM pg_tablespace WHERE spcname = 'ts2'"); +my $dboid = $node_primary->safe_psql('postgres', + "SELECT oid FROM pg_database WHERE datname = 'db3'"); +my $backup_tsdir = PostgreSQL::Test::Utils::tempdir_short(); +$node_primary->backup('backup4', + backup_options => ["--tablespace-mapping=$tsdir=$backup_tsdir"]); +$node_standby = PostgreSQL::Test::Cluster->new('standby4'); +$node_standby->init_from_backup( + $node_primary, 'backup4', + has_streaming => 1, + tablespace_map => + { $tsoid2 => PostgreSQL::Test::Utils::tempdir_short() . '/ts' }); +$node_standby->start; + +# Stop the standby after it replays $drop_sql but before its commit record, +# then check that $path is gone and that the standby restarts. +sub test_drop +{ + my ($drop_sql, $db, $setup_sql, $path) = @_; + + # Make the restart replay $setup_sql again. + $node_primary->safe_psql('postgres', 'CHECKPOINT'); + $node_primary->wait_for_replay_catchup($node_standby); + $node_standby->stop; + $node_primary->safe_psql($db, $setup_sql); + my $xid = $node_primary->safe_psql('postgres', + 'SELECT pg_snapshot_xmax(pg_current_snapshot())'); + $node_primary->safe_psql('postgres', $drop_sql); + + $node_standby->append_conf( + 'postgresql.conf', qq( +recovery_target_xid = '$xid' +recovery_target_inclusive = off +recovery_target_action = 'pause' +)); + $node_standby->start; + $node_standby->poll_query_until('postgres', + "SELECT pg_get_wal_replay_pause_state() = 'paused'") + or die "timed out waiting for recovery to pause"; + ok(!-e ($node_standby->data_dir . "/$path"), "$drop_sql: files removed"); + + $node_standby->stop('immediate'); + $node_standby->append_conf('postgresql.conf', "recovery_target_xid = ''"); + ok($node_standby->start(fail_ok => 1), "$drop_sql: standby restarts") + or die "standby did not restart"; +} + +test_drop('DROP DATABASE db3', 'db3', 'INSERT INTO t VALUES (2)', + "base/$dboid"); +test_drop( + 'DROP TABLESPACE ts2', 'postgres', + 'CREATE TABLE t2 (a int) TABLESPACE ts2; DROP TABLE t2', + "pg_tblspc/$tsoid2"); + done_testing(); -- 2.37.1 (Apple Git-137.1)
v1-0001-Update-minRecoveryPoint-before-removing-files-in-dro.patch (application/octet-stream, 2.1K) download From 1695dce1825ea8687f6c32ccc2a243a5de37be9f Mon Sep 17 00:00:00 2001 From: shihao zhong <zhong950419@gmail.com> Date: Mon, 19 Oct 2026 14:25:57 -0400 Subject: [PATCH 1/2] Update minRecoveryPoint before removing files in drop redo Replay of DROP DATABASE and DROP TABLESPACE removed files before updating minRecoveryPoint. A standby restarted after that could reach consistency too early and then fail replaying older WAL that touches the removed files. Call XLogFlush() first, as xact_redo_commit() does. Reported-by: azure young <azurezhao8369@gmail.com> Discussion: https://postgr.es/m/18258-1e2fd3058dda84b0@postgresql.org --- src/backend/commands/dbcommands.c | 6 ++++++ src/backend/commands/tablespace.c | 3 +++ 2 files changed, 9 insertions(+) diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c index 7e3fc59eafd..969baee98df 100644 --- a/src/backend/commands/dbcommands.c +++ b/src/backend/commands/dbcommands.c @@ -3471,6 +3471,12 @@ dbase_redo(XLogReaderState *record) /* Close all smgr fds in all backends. */ WaitForProcSignalBarrier(EmitProcSignalBarrier(PROCSIGNAL_BARRIER_SMGRRELEASE)); + /* + * Update minimum recovery point to cover this record before removing + * the files, like xact_redo_commit() does for dropped relations. + */ + XLogFlush(record->EndRecPtr); + for (i = 0; i < xlrec->ntablespaces; i++) { dst_path = GetDatabasePath(xlrec->db_id, xlrec->tablespace_ids[i]); diff --git a/src/backend/commands/tablespace.c b/src/backend/commands/tablespace.c index 7c40176b7b9..ac47b37d9aa 100644 --- a/src/backend/commands/tablespace.c +++ b/src/backend/commands/tablespace.c @@ -1555,6 +1555,9 @@ tblspc_redo(XLogReaderState *record) /* Close all smgr fds in all backends. */ WaitForProcSignalBarrier(EmitProcSignalBarrier(PROCSIGNAL_BARRIER_SMGRRELEASE)); + /* Update minimum recovery point first, as in dbase_redo() */ + XLogFlush(record->EndRecPtr); + /* * If we issued a WAL record for a drop tablespace it implies that * there were no files in it at all when the DROP was done. That means -- 2.37.1 (Apple Git-137.1)
^ permalink raw reply [nested|flat] 4+ messages in thread

* Re: BUG #18258: The redo of the drop db wal needs to forward the Min Recovery Point
@ 2026-11-13 06:58  Michael Paquier <michael@paquier.xyz>
  parent: shihao zhong <zhong950419@gmail.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Michael Paquier @ 2026-11-13 06:58 UTC (permalink / raw)
  To: shihao zhong <zhong950419@gmail.com>; +Cc: azurezhao8369@gmail.com, pgsql-bugs@lists.postgresql.org, Noah Misch <noah@leadboat.com>

On Mon, Nov 09, 2026 at 10:29:45PM -0500, shihao zhong wrote:
> DROP TABLESPACE and ALTER DATABASE SET TABLESPACE have the same gap.
> The patch calls XLogFlush() before the files are removed, like
> xact_redo_commit() does. The new TAP test fails without the fix and
> passes with it.

@@ -1555,6 +1555,9 @@ tblspc_redo(XLogReaderState *record)
         /* Close all smgr fds in all backends. */
         WaitForProcSignalBarrier(EmitProcSignalBarrier(PROCSIGNAL_BARRIER_SMGRRELEASE));
 
+        /* Update minimum recovery point first, as in dbase_redo() */
+        XLogFlush(record->EndRecPtr);
+
         /*
          * If we issued a WAL record for a drop tablespace it implies that
          * there were no files in it at all when the DROP was done. That means

Hmm.  You may be right here.  I would bet that this area of the code
has already been discussed regarding this problem, but perhaps my
fuzzy memories are failing me.  Did you do a search of the mailing
lists regarding that?

I was wondering if this is worth the cycles spent on to begin with,
but we already have a 033_replay_tsp_drops.pl that serves this exact
purpose, so perhaps we do.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: BUG #18258: The redo of the drop db wal needs to forward the Min Recovery Point
@ 2026-11-15 03:41  shihao zhong <zhong950419@gmail.com>
  parent: Michael Paquier <michael@paquier.xyz>
  0 siblings, 0 replies; 4+ messages in thread

From: shihao zhong @ 2026-11-15 03:41 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: azurezhao8369@gmail.com, pgsql-bugs@lists.postgresql.org, Noah Misch <noah@leadboat.com>

Hi Michael,

>  Did you do a search of the mailing
> lists regarding that?

Thanks for looking. Yes, I searched. The closest is a 2009 review [1]
where Heikki said an XLogFlush() in dbase_redo() did not help, because
his concern was the window after the commit record.

That became the ForceSyncCommit() flush in xact_redo_commit(), so
the window between the drop record and its commit was never covered.

Thanks,
Shihao

[1] https://postgr.es/m/4B1B8824.2070808@enterprisedb.com

^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2026-11-15 03:41 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2023-12-24 15:02 BUG #18258: The redo of the drop db wal needs to forward the Min Recovery Point PG Bug reporting form <noreply@postgresql.org>
2026-11-10 03:29 ` shihao zhong <zhong950419@gmail.com>
2026-11-13 06:58   ` Michael Paquier <michael@paquier.xyz>
2026-11-15 03:41     ` shihao zhong <zhong950419@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox