Re: [PATCH] Add recovery boundary WAL record for database and tablespace commands
Re: [PATCH] Add recovery boundary WAL record for database and tablespace commands
От:
shihao zhong <zhong950419@gmail.com>
Дата:
Hi JoongHyuk,
Thanks for picking this up. Both reports have been open for years and
Thanks for picking this up. Both reports have been open for years and
took me some time to understand why we need that.
One question. The 2019 report also mentions recovery_target_xid set
to the xid of DROP DATABASE, with recovery_target_inclusive = off.
With the patch I still get
FATAL: cannot connect to invalid database "db_dropped"
Do you plan to handle this case, or should the docs mention it?
Thanks,
Shihao
to the xid of DROP DATABASE, with recovery_target_inclusive = off.
With the patch I still get
FATAL: cannot connect to invalid database "db_dropped"
Do you plan to handle this case, or should the docs mention it?
Thanks,
Shihao
On Mon, Sep 7, 2026 at 1:21 AM JoongHyuk Shin <sjh910805@gmail.com> wrote:
CREATE/DROP DATABASE, ALTER DATABASE SET TABLESPACE
and CREATE/DROP TABLESPACE change directories as their WAL is replayed,
while recovery_target_time is only checked on commit and abort records.
So a target set before such a command does not stop recovery before it,
leaving a dropped database gone or an orphan directory.
This was reported in [1] and again in [2].
0001 emits an XLOG2 record carrying a timestamp
before the first irreversible record of each command
and stops recovery before it when the timestamp is past the target.
Stopping there loses nothing, as replaying the record has no effect.
0002 makes recovery_min_apply_delay wait on the record too.
A delayed standby currently removes a dropped database's directory
as soon as it reads the drop, long before the delay expires.
[1] https://www.postgresql.org/message-id/flat/aac5c3e6-2805-ba06-143b-070478aa9434@loxodata.com
[2] https://www.postgresql.org/message-id/flat/43f0c3df-a23c-4159-9610-934d3dceb80c@pgbackrest.org
--
JH Shin
Re: [PATCH] Add recovery boundary WAL record for database and tablespace commands
Hi Shihao,
Thanks for the review. I should have described the problem in more
detail in my first message.
For now, I don't plan to handle the xid case in this patch, in code or
in the docs. With the xid of DROP DATABASE and
recovery_target_inclusive = off, recovery stops right before its commit
record, as documented, but the changes DROP DATABASE makes before
committing have been replayed by then. Recovery knows it has reached an
xid target when it reads that transaction's commit record, so the
operator can instead give the xid of a transaction known to have
committed before DROP DATABASE started, with
recovery_target_inclusive = on. Whether the docs should say this is a
separate question. It is not specific to this patch or to xid targets,
as an LSN target between those changes and the commit, with
recovery_target_inclusive = off, leaves the same result on master.
But a time target is different. Recovery knows it has passed a time
target only when it reads a later timestamp, which on master comes only
from commit and abort records, so the first one can be the commit of
DROP DATABASE itself, after its changes have been replayed. The
boundary record this patch adds gives recovery a timestamp ahead of
those changes.
v2 is only a rebase onto current master.
--
JoongHyuk Shin
Thanks for the review. I should have described the problem in more
detail in my first message.
For now, I don't plan to handle the xid case in this patch, in code or
in the docs. With the xid of DROP DATABASE and
recovery_target_inclusive = off, recovery stops right before its commit
record, as documented, but the changes DROP DATABASE makes before
committing have been replayed by then. Recovery knows it has reached an
xid target when it reads that transaction's commit record, so the
operator can instead give the xid of a transaction known to have
committed before DROP DATABASE started, with
recovery_target_inclusive = on. Whether the docs should say this is a
separate question. It is not specific to this patch or to xid targets,
as an LSN target between those changes and the commit, with
recovery_target_inclusive = off, leaves the same result on master.
But a time target is different. Recovery knows it has passed a time
target only when it reads a later timestamp, which on master comes only
from commit and abort records, so the first one can be the commit of
DROP DATABASE itself, after its changes have been replayed. The
boundary record this patch adds gives recovery a timestamp ahead of
those changes.
v2 is only a rebase onto current master.
--
JoongHyuk Shin
Re: [PATCH] Add recovery boundary WAL record for database and tablespace commands
От:
João Rito <joaovictor.rito@gmail.com>
Дата:
Hi JoongHyuk,
I tested v2 (both patches) on top of master 9f191dce61e, built with
--enable-cassert --enable-debug --enable-tap-tests. Both patches apply
in order with git apply and build without warnings. These TAP tests pass:
0001: src/test/recovery t/003_recovery_targets.pl (33 tests)
0001+0002: t/003_recovery_targets.pl and t/005_replay_delay.pl
(37 tests)
I also ran PITR by hand: a primary with archive_mode, a base backup,
then DROP DATABASE, CREATE DATABASE (STRATEGY file_copy), CREATE
TABLESPACE, DROP TABLESPACE and ALTER DATABASE SET TABLESPACE, each
preceded by a recorded time, LSN and restore point. I recovered every
case with recovery_target_time (inclusive on and off), LSN (on and
off), name and xid, on master and with 0001, and looked at the
catalog and at base/ and pg_tblspc/ after promotion.
What works well:
- On master, a time target just before the command ends in a broken
state, exactly as you describe. DROP DATABASE: dbx is in the catalog
but its directory is gone ("cannot connect to invalid database").
CREATE DATABASE leaves an orphan base/. CREATE TABLESPACE leaves
an orphan pg_tblspc/. DROP TABLESPACE leaves a tablespace in
pg_tablespace with no directory.
- With 0001, the same targets stop with "recovery stopping before
recovery boundary" and the cluster is clean in all four cases, for
both inclusive on and off.
- LSN and name targets behave the same as on master.
- pg_waldump shows the new record as "XLOG2 ... RECOVERY_BOUNDARY time
...", and the record sits just before the first non-transactional
record of each command.
- xid target: your explanation matches what I see. With the xid of
DROP DATABASE and inclusive = off I get the same "cannot connect to
invalid database", and with the xid of the previous transaction and
inclusive = on the database is intact.
Points I found:
1. A time target between the boundary record and the commit record
still breaks the database. The boundary time is taken when the
command starts, but the commit comes later, and CREATE DATABASE and
DROP DATABASE wait for checkpoints in between. In my runs:
- DROP DATABASE, target = boundary time + 1 microsecond, inclusive
on: "recovery stopping before commit of transaction 672", dbx is
in the catalog, its directory is missing, and connecting gives
"cannot connect to invalid database".
- CREATE DATABASE (file_copy), target halfway between the boundary
(13:43:49.99) and the commit (13:43:51.01), inclusive on or off:
recovery stops before the commit and base/16397 is left as an
orphan directory.
I do not see an easy code fix, because recovery only learns about
the commit later. Maybe the docs for recovery_target_time could
say that a target taken while such a command is running can still
leave this result.
2. With recovery_target_inclusive = on and a target exactly equal to
the boundary time, recovery does not stop at the boundary (the test
is boundary_time > target), replays the file operation, and then
stops before the commit because the commit time is later. Result
for DROP DATABASE: dbx in the catalog, directory missing. With
inclusive = off the same target stops before the boundary and the
database is fine. Since the boundary record is not a transaction
that can be "included", maybe it should use >= for both settings.
This is only an edge case (microsecond equality), but the two
settings give different results for the same WAL.
3. 0001 does not touch the documentation. The recovery_target_time
and recovery_target_inclusive text in config.sgml (or the PITR
chapter) does not mention that recovery now also stops before these
commands, nor that xid and LSN targets are not covered (the xid
case Shihao reported). Even one sentence with the workaround you
gave (use the xid of the previous transaction with inclusive = on)
would help operators. 0002 does update the recovery_min_apply_delay
paragraph.
4. Small thing about messages. "recovery stopping before recovery
boundary, time ..." and the pg_waldump line do not say which
command the boundary belongs to, so an operator reading the log
cannot tell if it was a DROP DATABASE or a CREATE TABLESPACE
without looking at the next WAL record. Not important, only a
suggestion (for example a one-byte command type in the record).
For 0002 I only ran the TAP test (005_replay_delay.pl passes with the
new case); I did not test it by hand.
Thanks for working on this. Losing a database or getting orphan
directories after PITR is a real problem.
Regards,
João Rito
Em qua., 11 de nov. de 2026 às 11:18, JoongHyuk Shin
escreveu:
>
> Hi Shihao,
>
> Thanks for the review. I should have described the problem in more
> detail in my first message.
>
> For now, I don't plan to handle the xid case in this patch, in code or
> in the docs. With the xid of DROP DATABASE and
> recovery_target_inclusive = off, recovery stops right before its commit
> record, as documented, but the changes DROP DATABASE makes before
> committing have been replayed by then. Recovery knows it has reached an
> xid target when it reads that transaction's commit record, so the
> operator can instead give the xid of a transaction known to have
> committed before DROP DATABASE started, with
> recovery_target_inclusive = on. Whether the docs should say this is a
> separate question. It is not specific to this patch or to xid targets,
> as an LSN target between those changes and the commit, with
> recovery_target_inclusive = off, leaves the same result on master.
>
> But a time target is different. Recovery knows it has passed a time
> target only when it reads a later timestamp, which on master comes only
> from commit and abort records, so the first one can be the commit of
> DROP DATABASE itself, after its changes have been replayed. The
> boundary record this patch adds gives recovery a timestamp ahead of
> those changes.
>
> v2 is only a rebase onto current master.
>
> --
> JoongHyuk Shin
Re: [PATCH] Add recovery boundary WAL record for database and tablespace commands
Hi João,
Thanks for testing v2 and for the detailed report. v3 is attached.
On Wed, Nov 11, 2026 at 11:27 PM João Rito <joaovictor.rito@gmail.com> wrote:
> 1. A time target between the boundary record and the commit record
> still breaks the database.
I don't plan to document this case. I'd expect an operator to take the
target from the statement's log_statement line, or from the last commit
before the command with recovery_target_inclusive = on, and with v3 both
stop before the command. A target that falls between the boundary record
and the commit behaves as it does on master, and covering it is not a
goal of this patch.
> 2. With recovery_target_inclusive = on and a target exactly equal to
> the boundary time, recovery does not stop at the boundary (the test
> is boundary_time > target), replays the file operation, and then
> stops before the commit because the commit time is later.
Agreed. The boundary record is not a transaction that
recovery_target_inclusive could include.
> 3. 0001 does not touch the documentation. The recovery_target_time
> and recovery_target_inclusive text in config.sgml (or the PITR
> chapter) does not mention that recovery now also stops before these
> commands, nor that xid and LSN targets are not covered (the xid
> case Shihao reported).
The recovery_target_inclusive entry is unchanged, for the reason given
in point 2. For xid and LSN targets I kept the docs as they are. As I
wrote to Shihao, the xid and LSN cases are not specific to this patch,
since master gives the same result. They are also not limited to
database commands. With recovery_target_xid set to a CREATE TABLE
transaction and recovery_target_inclusive = off, the table's file is
left behind.
> 4. Small thing about messages. "recovery stopping before recovery
> boundary, time ..." and the pg_waldump line do not say which
> command the boundary belongs to
Thanks for the suggestion. I kept the record to the time stamp, which
is all the stop check and the apply delay use. If others find the
command type useful, I can add it.
Thanks for testing v2 and for the detailed report. v3 is attached.
On Wed, Nov 11, 2026 at 11:27 PM João Rito <joaovictor.rito@gmail.com> wrote:
> 1. A time target between the boundary record and the commit record
> still breaks the database.
I don't plan to document this case. I'd expect an operator to take the
target from the statement's log_statement line, or from the last commit
before the command with recovery_target_inclusive = on, and with v3 both
stop before the command. A target that falls between the boundary record
and the commit behaves as it does on master, and covering it is not a
goal of this patch.
> 2. With recovery_target_inclusive = on and a target exactly equal to
> the boundary time, recovery does not stop at the boundary (the test
> is boundary_time > target), replays the file operation, and then
> stops before the commit because the commit time is later.
Agreed. The boundary record is not a transaction that
recovery_target_inclusive could include.
> 3. 0001 does not touch the documentation. The recovery_target_time
> and recovery_target_inclusive text in config.sgml (or the PITR
> chapter) does not mention that recovery now also stops before these
> commands, nor that xid and LSN targets are not covered (the xid
> case Shihao reported).
The recovery_target_inclusive entry is unchanged, for the reason given
in point 2. For xid and LSN targets I kept the docs as they are. As I
wrote to Shihao, the xid and LSN cases are not specific to this patch,
since master gives the same result. They are also not limited to
database commands. With recovery_target_xid set to a CREATE TABLE
transaction and recovery_target_inclusive = off, the table's file is
left behind.
> 4. Small thing about messages. "recovery stopping before recovery
> boundary, time ..." and the pg_waldump line do not say which
> command the boundary belongs to
Thanks for the suggestion. I kept the record to the time stamp, which
is all the stop check and the apply delay use. If others find the
command type useful, I can add it.
Re: [PATCH] Add recovery boundary WAL record for database and tablespace commands
От:
João Rito <joaovictor.rito@gmail.com>
Дата:
Hi JoongHyuk,
Thank you for v3. I tested it on master (87524d0), with both patches applied.
1. TAP tests: t/https://www.google.com/url?q=http://003_recovery_targets.pl&source=gmail&ust=1794638038862000&sa=E and t/https://www.google.com/url?q=http://005_replay_delay.pl&source=gmail&ust=1794638038862000&sa=E pass (Files=2, Tests=36, PASS).
2. Manual PITR. Before each command I inserted a row, then ran recovery_target_time restores for five commands: DROP DATABASE, CREATE DATABASE (FILE_COPY), ALTER DATABASE ... SET TABLESPACE, CREATE TABLESPACE and DROP TABLESPACE.
For each one I tried these targets:
- the exact boundary time, with recovery_target_inclusive = on;
- the same, with inclusive = off;
- the statement time from log_statement;
- the time of the previous commit.
In all of them recovery logged "recovery stopping before recovery boundary", and the cluster came up clean, with the command not applied. So the >= check now works the same way for both inclusive values. Thanks for fixing that.
3. The docs under recovery_target_time read clearly to me.
4. As expected from your point 1, a target between the boundary and the commit (I used boundary + 1 microsecond) still stops before the commit. In that case I saw:
- DROP DATABASE: the database stays in the catalog with datconnlimit -2 (INVALID), and connecting to it fails.
- CREATE DATABASE (FILE_COPY): an orphan directory under base/, with no catalog row.
- CREATE TABLESPACE: an orphan directory, with no catalog row.
- ALTER DATABASE SET TABLESPACE and DROP TABLESPACE: no visible problem.
The window was about 20 microseconds for the tablespace commands, but about 93 ms for DROP DATABASE and about 970 ms for CREATE DATABASE on my small test. I understand keeping this out of scope. Maybe a short sentence in the docs could say that a target falling inside that window can still leave this state, so users pick a time before the command.
Thanks again for working on this.
Regards,
João Rito
Thank you for v3. I tested it on master (87524d0), with both patches applied.
1. TAP tests: t/https://www.google.com/url?q=http://003_recovery_targets.pl&source=gmail&ust=1794638038862000&sa=E and t/https://www.google.com/url?q=http://005_replay_delay.pl&source=gmail&ust=1794638038862000&sa=E pass (Files=2, Tests=36, PASS).
2. Manual PITR. Before each command I inserted a row, then ran recovery_target_time restores for five commands: DROP DATABASE, CREATE DATABASE (FILE_COPY), ALTER DATABASE ... SET TABLESPACE, CREATE TABLESPACE and DROP TABLESPACE.
For each one I tried these targets:
- the exact boundary time, with recovery_target_inclusive = on;
- the same, with inclusive = off;
- the statement time from log_statement;
- the time of the previous commit.
In all of them recovery logged "recovery stopping before recovery boundary", and the cluster came up clean, with the command not applied. So the >= check now works the same way for both inclusive values. Thanks for fixing that.
3. The docs under recovery_target_time read clearly to me.
4. As expected from your point 1, a target between the boundary and the commit (I used boundary + 1 microsecond) still stops before the commit. In that case I saw:
- DROP DATABASE: the database stays in the catalog with datconnlimit -2 (INVALID), and connecting to it fails.
- CREATE DATABASE (FILE_COPY): an orphan directory under base/, with no catalog row.
- CREATE TABLESPACE: an orphan directory, with no catalog row.
- ALTER DATABASE SET TABLESPACE and DROP TABLESPACE: no visible problem.
The window was about 20 microseconds for the tablespace commands, but about 93 ms for DROP DATABASE and about 970 ms for CREATE DATABASE on my small test. I understand keeping this out of scope. Maybe a short sentence in the docs could say that a target falling inside that window can still leave this state, so users pick a time before the command.
Thanks again for working on this.
Regards,
João Rito
On Thu, Nov 12, 2026 08:58 AM, JoongHyuk Shin <sjh910805@gmail.com> wrote:
Hi João,
Thanks for testing v2 and for the detailed report. v3 is attached.
On Wed, Nov 11, 2026 at 11:27 PM João Rito <joaovictor.rito@gmail.com> wrote:
> 1. A time target between the boundary record and the commit record
> still breaks the database.
I don't plan to document this case. I'd expect an operator to take the
target from the statement's log_statement line, or from the last commit
before the command with recovery_target_inclusive = on, and with v3 both
stop before the command. A target that falls between the boundary record
and the commit behaves as it does on master, and covering it is not a
goal of this patch.
> 2. With recovery_target_inclusive = on and a target exactly equal to
> the boundary time, recovery does not stop at the boundary (the test
> is boundary_time > target), replays the file operation, and then
> stops before the commit because the commit time is later.
Agreed. The boundary record is not a transaction that
recovery_target_inclusive could include.
> 3. 0001 does not touch the documentation. The recovery_target_time
> and recovery_target_inclusive text in config.sgml (or the PITR
> chapter) does not mention that recovery now also stops before these
> commands, nor that xid and LSN targets are not covered (the xid
> case Shihao reported).
The recovery_target_inclusive entry is unchanged, for the reason given
in point 2. For xid and LSN targets I kept the docs as they are. As I
wrote to Shihao, the xid and LSN cases are not specific to this patch,
since master gives the same result. They are also not limited to
database commands. With recovery_target_xid set to a CREATE TABLE
transaction and recovery_target_inclusive = off, the table's file is
left behind.
> 4. Small thing about messages. "recovery stopping before recovery
> boundary, time ..." and the pg_waldump line do not say which
> command the boundary belongs to
Thanks for the suggestion. I kept the record to the time stamp, which
is all the stop check and the apply delay use. If others find the
command type useful, I can add it.
Re: [PATCH] Add recovery boundary WAL record for database and tablespace commands
От:
JoongHyuk Shin <sjh910805@gmail.com>
Дата:
Hi João,
Thanks for testing v3.
On Fri, Nov 13, 2026 at 9:17 PM João Rito <joaovictor.rito@gmail.com> wrote:
> Maybe a short sentence in the
> docs could say that a target falling inside that window can still leave
> this state, so users pick a time before the command.
I'd rather not add it. The state you saw is what recovery leaves when
it stops before the commit of one of these commands, with the changes
the command makes before committing already replayed. A time target is
not the only way to end up there. With recovery_target_xid set to the
command's transaction and recovery_target_inclusive = off, recovery
stops at the same place. I kept the docs as they are for that case,
and I don't think the state should be described under
recovery_target_time alone. The text v3 adds there says where recovery
now stops, and I'd prefer to keep it to that.
--
JoongHyuk Shin