Re: PoC: Simplify recovery after dropping a table by LOGGING the restore LSN
Re: PoC: Simplify recovery after dropping a table by LOGGING the restore LSN
От:
Kirill Reshke <reshkekirill@gmail.com>
Дата:
On Fri, 8 Nov 2024 at 08:47, Kirk Wolak wrote: > > Hackers, > The concept was driven by an all too common support request. A user accidentally dropped the wrong table. (this could also be applied to dropping a database, etc). > > If we had the LSN before the drop, this would be easier. So we actually log the LSN when the lock is required so that we have an accurate LSN and the recovery is much simpler. > > All we are doing is inserting a simple LOG message: > Acquired drop table lock on table . Restore at This is indeed useful for the number of accidental data loss recovery. > > Comments are appreciated! > > Should we ALSO consider this for: > - DROP DATABASE > - TRUNCATE TABLE > - DELETE (only when it is without a WHERE clause?) > - UPDATE (only when it is without a WHERE clause?) options 1 & 2 looks sane, but logging lsn for DELETE/UPDATE looks extra. I am not convinced this change is necessary to be done inside PostgreSQL. What stops us from logging all the same inside object access hook defined by extension? This way we can define any rule on when to log this. There are a number of cases to consider, pointed out by Jim, such as the TEMP table and the UNLOGGED table. [0] I want to highlight that we are logging the current WAL insert pointer, which can be arbitrarily less than the actual LSN of the commit record that deletes the table in case of high load. We first acquire a deletion lock, then we assemble the xlog record and only then we insert this record into the WAL file. So, the log message should be something like 'Restore at lsn xxx or later'. [0] https://www.postgresql.org/message-id/3bda6b56-16bd-48fe-8e23-5ef58a6a4e34%40uni-muenster.de -- Best regards, Kirill Reshke
Re: PoC: Simplify recovery after dropping a table by LOGGING the restore LSN
Subject: Re: PoC: Simplify recovery after dropping a table by LOGGING the restore LSN
Hi all,
I've implemented an improved version of this feature that addresses
Kirill's concern about logging the wrong LSN [1].
The key difference: instead of logging WAL insert pointer at lock time,
this patch logs the actual commit LSN by extending XactCallback to pass
the commit LSN from the commit record.
Main improvements:
- Logs real commit LSN, not an earlier position
- Handles ROLLBACK correctly (no log for aborted drops)
- Supports SAVEPOINT, ROLLBACK TO, COMMIT AND CHAIN
- New GUC: log_object_drops (default: off)
- Includes DROP DATABASE support
- Comprehensive TAP tests
This makes the LSN reliable for PITR recovery regardless of system load.
Patch attached. Feedback welcome!
[1] https://www.postgresql.org/message-id/CALdSSPiRDvbuPgZKAB1%2BBDb3Nfe-i3PYkiy%3DScMZ7tU5f0toKQ%40mail.gmail.com
--
Hi all,
I've implemented an improved version of this feature that addresses
Kirill's concern about logging the wrong LSN [1].
The key difference: instead of logging WAL insert pointer at lock time,
this patch logs the actual commit LSN by extending XactCallback to pass
the commit LSN from the commit record.
Main improvements:
- Logs real commit LSN, not an earlier position
- Handles ROLLBACK correctly (no log for aborted drops)
- Supports SAVEPOINT, ROLLBACK TO, COMMIT AND CHAIN
- New GUC: log_object_drops (default: off)
- Includes DROP DATABASE support
- Comprehensive TAP tests
This makes the LSN reliable for PITR recovery regardless of system load.
Patch attached. Feedback welcome!
[1] https://www.postgresql.org/message-id/CALdSSPiRDvbuPgZKAB1%2BBDb3Nfe-i3PYkiy%3DScMZ7tU5f0toKQ%40mail.gmail.com
--
Best regards,
Dmitry Lebedev
пт, 28 нояб. 2025 г. в 16:39, Kirill Reshke <reshkekirill@gmail.com>:
On Fri, 8 Nov 2024 at 08:47, Kirk Wolak <wolakk@gmail.com> wrote:
>
> Hackers,
> The concept was driven by an all too common support request. A user accidentally dropped the wrong table. (this could also be applied to dropping a database, etc).
>
> If we had the LSN before the drop, this would be easier. So we actually log the LSN when the lock is required so that we have an accurate LSN and the recovery is much simpler.
>
> All we are doing is inserting a simple LOG message:
> Acquired drop table lock on table <relname>. Restore at <LSN>
This is indeed useful for the number of accidental data loss recovery.
>
> Comments are appreciated!
>
> Should we ALSO consider this for:
> - DROP DATABASE
> - TRUNCATE TABLE
> - DELETE (only when it is without a WHERE clause?)
> - UPDATE (only when it is without a WHERE clause?)
options 1 & 2 looks sane, but logging lsn for DELETE/UPDATE looks extra.
I am not convinced this change is necessary to be done inside
PostgreSQL. What stops us from logging all the same inside object
access hook defined by extension? This way we can define any rule on
when to log this.
There are a number of cases to consider, pointed out by Jim, such as
the TEMP table and the UNLOGGED table. [0]
I want to highlight that we are logging the current WAL insert
pointer, which can be arbitrarily less than the actual LSN of the
commit record that deletes the table in case of high load. We first
acquire a deletion lock, then we assemble the xlog record and only
then we insert this record into the WAL file. So, the log message
should be something like 'Restore at lsn xxx or later'.
[0] https://www.postgresql.org/message-id/3bda6b56-16bd-48fe-8e23-5ef58a6a4e34%40uni-muenster.de
--
Best regards,
Kirill Reshke
Re: PoC: Simplify recovery after dropping a table by LOGGING the restore LSN
> On 1 Dec 2025, at 12:11, Дмитрий Лебедев wrote: > > I've implemented an improved version of this feature that addresses > Kirill's concern about logging the wrong LSN [1]. Cool! On a first glance patch looks good. I'll test it more later, for now I've fixed indentation and rebased on master to fix conflicts with recent "wait for LSN feature". Please also avoid top posting :) Post answers below cited text. And do not post unrelevant citations at all. Mail archives a read more frequently than written. Also, it's a good idea to number patch versions so reviewers can post feedback for some specific version. Here I used version number 5. Thanks! Best regards, Andrey Borodin.
Re: PoC: Simplify recovery after dropping a table by LOGGING the restore LSN
Hello hackers,
Attached is v6 of the patch to log the target LSN on DROP TABLE,
TRUNCATE TABLE, and DROP DATABASE when the GUC log_object_drops is
enabled (default off). This is based on Dmitry Lebedev's earlier work,
which I have revised and extended.
When a permanent table is dropped or truncated and the transaction
commits, the server logs a message containing the commit LSN. The
patch adds a new variable, XactLastCommitStart, which records
ProcLastRecPtr (the start of the commit WAL record) immediately after
it is written in RecordTransactionCommit(). The logging is handled by
a transaction callback, ensuring it only runs when the transaction
actually commits. Rolled-back operations, including ROLLBACK TO
SAVEPOINT, are discarded and never logged.
Every permanent table that is physically dropped is logged, including
tables dropped by DROP SCHEMA ... CASCADE and child partitions, so
dropping a schema or a partitioned table produces one line per table.
TRUNCATE is handled the same way. DROP DATABASE logs an intermediate
LSN (the WAL insert position at the
time of the drop) instead of the commit LSN.
This LSN provides an exact recovery target. Setting
recovery_target_lsn to this value with recovery_target_inclusive =
false stops recovery just before the commit record, preserving the
table. For example:
LOG: table "public.t" (OID 16384) dropped, lsn=0/01776698
HINT: To recover dropped or truncated tables, use
recovery_target_lsn = '0/01776698' with recovery_target_inclusive =
false.
On Mon, Sep 28, 2026 at 11:37 AM Kirill Reshke wrote:
>
> I am not convinced this change is necessary to be done inside
> PostgreSQL. What stops us from logging all the same inside object
> access hook defined by extension? This way we can define any rule on
> when to log this.
>
The object access hook runs when the DROP executes, before the
transaction commits, so the commit LSN doesn't exist yet at that
point. To log it, an extension would also need a transaction callback
that runs at commit and would have to discard entries from rolled-back
savepoints. That is what this patch does, with a new
XactLastCommitStart variable for the LSN.
> There are a number of cases to consider, pointed out by Jim, such as
> the TEMP table and the UNLOGGED table. [0]
>
Both are filtered out. We check relation persistence and only register
permanent relations (RELPERSISTENCE_PERMANENT). Neither TEMP nor
UNLOGGED tables produce log entries.
Best,
Salma Elsayed
Re: PoC: Simplify recovery after dropping a table by LOGGING the restore LSN
Hi Salma On 28/09/2026 15:54, Salma El-Sayed wrote: > Attached is v6 of the patch to log the target LSN on DROP TABLE, > TRUNCATE TABLE, and DROP DATABASE when the GUC log_object_drops is > enabled (default off). This is based on Dmitry Lebedev's earlier work, > which I have revised and extended. Thanks for the patch! v6 no longer applies to master, since two new tests were added. 0001 is v6 rebased, with the test file renamed to 060_drop_table_logging.pl and meson.build updated accordingly -- no other changes. == drop database == IIUC simply adding a lsn recorded after a drop database expectedly restores an invalid database, which I can confirm: psql: error: connection to server on socket "/tmp/.s.PGSQL.5433" failed: FATAL: cannot connect to invalid database "db" HINT: Use DROP DATABASE to drop invalid databases. But I was wondering if capturing the lsn right after pgstat_drop_database(db_id) woul be a better option here.I tested that locally, and recovering to that lsn gives back a valid database, with its original CONNECTION LIMIT, settings and data, and no manual step needed -- That's in 0002. It still needs more testing.. WDYT? Thanks for working on this. Best, Jim
Re: PoC: Simplify recovery after dropping a table by LOGGING the restore LSN
Hi Jim, On Sun, Oct 11, 2026 at 1:27 AM Jim Jones wrote: > v6 no longer applies to master, since two new tests were added. 0001 is > v6 rebased, with the test file renamed to > 060_drop_table_logging.pl and meson.build updated accordingly -- no > other changes. Thanks for your response and for fixing the rebase conflict. > > == drop database == > > But I was wondering if capturing the lsn right after > pgstat_drop_database(db_id) woul be a better option here.I tested that > locally, and recovering to that lsn gives back a valid database, with > its original CONNECTION LIMIT, settings and data, and no manual step > needed -- That's in 0002. It still needs more testing.. > I agree that avoiding the manual step is much better. I tested your patch and it works as expected. I included your fix in the main patch and updated the documentation accordingly. I also had an LLM help write more tests (for drop db recovery and truncate). Attached is v8 rebased on master. Best regards, Salma Elsayed
Re: PoC: Simplify recovery after dropping a table by LOGGING the restore LSN
Hi Salma
On 12/10/2026 14:14, Salma El-Sayed wrote:
> Attached is v8 rebased on master.
Thanks for the update.
I played a bit with v8 and it seems that the lsn is logged a bit too
early. If a drop database command fails, it still gets logged:
psql (20devel)
Type "help" for help.
postgres=# create table t (g int, pad text);
CREATE TABLE
postgres=# insert into t select g, repeat('x', 100) from
generate_series(1, 600000) g;
INSERT 0 600000
postgres=# create database db;
CREATE DATABASE
postgres=# update t set g = g + 1;
UPDATE 600000
postgres=# set log_object_drops to on;
SET
postgres=# set statement_timeout = '20ms';
SET
postgres=# drop database db;
ERROR: canceling statement due to statement timeout
postgres=# select oid,datname from pg_database where datname = 'db';
oid | datname
-------+---------
16414 | db
(1 row)
.. but the logs show:
LOG: database "db" (OID 16414) dropped, lsn=0/47AAC7E0
Just for testing, I placed the ereport after remove_dbtablespaces() and
the lsn is no longer logged in the scenario above -- see 0002, but it
needs more testing. WDYT?
I will take a closer look at the tests in the next few days.
Best, Jim
Re: PoC: Simplify recovery after dropping a table by LOGGING the restore LSN
Hi Jim,
Thanks for your response.
>
> postgres=# create table t (g int, pad text);
> CREATE TABLE
> postgres=# insert into t select g, repeat('x', 100) from
> generate_series(1, 600000) g;
> INSERT 0 600000
> postgres=# create database db;
> CREATE DATABASE
> postgres=# update t set g = g + 1;
> UPDATE 600000
> postgres=# set log_object_drops to on;
> SET
> postgres=# set statement_timeout = '20ms';
> SET
> postgres=# drop database db;
> ERROR: canceling statement due to statement timeout
>
> postgres=# select oid,datname from pg_database where datname = 'db';
> oid | datname
> -------+---------
> 16414 | db
> (1 row)
>
>
> .. but the logs show:
>
> LOG: database "db" (OID 16414) dropped, lsn=0/47AAC7E0
>
But in this exact scenario, didn't the database become invalid?
postgres=# \c db
connection to server on socket "/tmp/.s.PGSQL.5435" failed: FATAL:
cannot connect to invalid database "db"
HINT: Use DROP DATABASE to drop invalid databases.
The LSN value itself is fine to recover to. The problem is the word
"dropped," since the drop did not actually complete.
I tested with injection points. If an error occurs after v9's logging
point, we get the same situation again: the log assumes the database
was dropped when it was not. I moved the LSN registration earlier in
dropdb(), so that a failure after that point still has something to
log.
I am now using a callback, similar to how we handle DROP TABLE. On
commit, we log the LSN as before. On abort, we still log a message,
but state the drop did not complete instead of saying it dropped. If
"drop database db;" is retried on an already invalid database, we
still log a message, but without a recovery_target_lsn, and we state
that the original recovery point is not known to this session.
Attached is v10 with 0002 for the callback implementation. Let me know
what you think.
It still needs more testing.
Best regards,
Salma Elsayed
Re: PoC: Simplify recovery after dropping a table by LOGGING the restore LSN
От:
Zsolt Parragi <zsolt.parragi@percona.com>
Дата:
Hello
drop_lsn = GetXLogInsertRecPtr();
-
+ RegisterDropDatabaseLog(db_id, dbname, drop_lsn, already_invalid);
+ }
...
+ if (!dropdb_callback_registered)
+ {
+ RegisterXactCallback(DropDbXactCallback, NULL);
If a backend crashes between them the callback doesn't run and we are
left with an invalid database without logging it.
You can reproduce this by stopping the drop at the
'dropdb-after-invalid-marker' injection point and killing the backend.
Re: PoC: Simplify recovery after dropping a table by LOGGING the restore LSN
От:
Jim Jones <jim.jones@uni-muenster.de>
Дата:
On 15/10/2026 16:26, Salma El-Sayed wrote: > But in this exact scenario, didn't the database become invalid? > postgres=# \c db > connection to server on socket "/tmp/.s.PGSQL.5435" failed: FATAL: > cannot connect to invalid database "db" > HINT: Use DROP DATABASE to drop invalid databases. > > The LSN value itself is fine to recover to. The problem is the word > "dropped," since the drop did not actually complete. Right, this is how I see it too. Stating that the database was dropped while it is still there (valid or not) isn't ideal. On top of what Zsolt pointed out: == new warning == postgres=# drop database db; ERROR: canceling statement due to statement timeout WARNING: DROP DATABASE "db" (OID 16393) did not complete; the database may be left invalid and require DROP DATABASE to be run again HINT: Files belonging to this database may already have been removed. To recover data from before this attempt, use recovery_target_lsn = '0/10276790' with recovery_target_inclusive = false. This warning is now sent to the client, and I have mixed feelings about it. If a cancelled drop database should tell the user that the database was left invalid, that's useful whether or not this GUC is set, and it belongs in a separate patch, not behind log_object_drops. While I understand the motivation behind it, I'd argue that it should be logged only in the server -- the user most likely cannot act on it anyway :) But it is just my opinion. Also, the primary message is quite long (two clauses). I recommend taking a look at the error style guide[1] Thanks for working on this! Best, Jim 1 - https://www.postgresql.org/docs/18/error-style-guide.html
Re: PoC: Simplify recovery after dropping a table by LOGGING the restore LSN
От:
Salma Elsayed <salmasayed182003@gmail.com>
Дата:
Hi Jim and Zsolt, On Fri, Oct 16, 2026 at 12:25 AM Zsolt Parragi wrote: > > If a backend crashes between them the callback doesn't run and we are > left with an invalid database without logging it. > > You can reproduce this by stopping the drop at the > 'dropdb-after-invalid-marker' injection point and killing the backend. Thanks, I was able to reproduce it. I don't think the callback can cover this case at all, since nothing runs in the backend after it dies. The only option I could think of was logging the lsn with a neutral message right after we capture it, before the database is marked invalid. I tried it and I don't think it's a good fit. At that point we don't know if the drop will succeed, so the message can only say the drop is starting. We would still need the callback afterwards to log the real outcome, so every DROP DATABASE would log two messages, and after a crash the user would only have the neutral one and would need to check pg_database to find out what actually happened. What makes DROP DATABASE harder than DROP TABLE is that it marks the database invalid before doing work it can't roll back, so a failed or crashed drop can leave the database broken. So I'd like to remove DROP DATABASE from this patch and keep it to DROP TABLE and TRUNCATE TABLE, which was the original scope. DROP DATABASE could be handled separately in another patch. What do you think? On Fri, Oct 16, 2026 at 3:18 PM Jim Jones wrote: > > This warning is now sent to the client, and I have mixed feelings about > it. If a cancelled drop database should tell the user that the database > was left invalid, that's useful whether or not this GUC is set, and it > belongs in a separate patch, not behind log_object_drops. > I agree. Warning that a cancelled DROP DATABASE left the database invalid is a separate issue from log_object_drops and should be its own patch. > > Also, the primary message is quite long (two clauses). I recommend > taking a look at the error style guide[1] > Thanks. That message goes away with DROP DATABASE, but I'll go through the style guide and apply it to the DROP TABLE and TRUNCATE messages in the next version. Best regards, Salma Elsayed
Re: PoC: Simplify recovery after dropping a table by LOGGING the restore LSN
От:
Kirk Wolak <wolakk@gmail.com>
Дата:
On Wed, Oct 21, 2026 at 7:18 AM Salma Elsayed <salmasayed182003@gmail.com> wrote:
Hi Jim and Zsolt,
On Fri, Oct 16, 2026 at 12:25 AM Zsolt Parragi
<zsolt.parragi@percona.com> wrote:
>
> If a backend crashes between them the callback doesn't run and we are
> left with an invalid database without logging it.
>
...
So I'd like to remove DROP DATABASE from this patch and keep it to
DROP TABLE and TRUNCATE TABLE, which was the original scope. DROP
DATABASE could be handled separately in another patch. What do you
think?
+1
Having worked on the original patch, our goal was protecting the table data.
The attempt to add DROP DATABASE "seemed" like it could come along for the ride,
but it is clearly a different beast, for the reasons you describe.
Having worked on the original patch, our goal was protecting the table data.
The attempt to add DROP DATABASE "seemed" like it could come along for the ride,
but it is clearly a different beast, for the reasons you describe.
Kirk Out!
Re: PoC: Simplify recovery after dropping a table by LOGGING the restore LSN
От:
Jim Jones <jim.jones@uni-muenster.de>
Дата:
On 10/21/26 2:49 PM, Kirk Wolak wrote: > Having worked on the original patch, our goal was protecting the > table data. > The attempt to add DROP DATABASE "seemed" like it could come along for > the ride, > but it is clearly a different beast, for the reasons you describe. +1 I agree that leaving drop database for another patch is a good idea. Having it would be quite nice, bit it's indeed much harder than we anticipated, so I guess it's ok to postpone it. Best, Jim
Re: PoC: Simplify recovery after dropping a table by LOGGING the restore LSN
Hi, On Wed, Oct 21, 2026 at 4:50 PM Jim Jones wrote: > > I agree that leaving drop database for another patch is a good idea. > Having it would be quite nice, bit it's indeed much harder than we > anticipated, so I guess it's ok to postpone it. > Attached is v11, which removes DROP DATABASE as discussed, applies the error style guide to the log messages, and simplifies the TAP test. Best, Salma Elsayed
Re: PoC: Simplify recovery after dropping a table by LOGGING the restore LSN
От:
Jim Jones <jim.jones@uni-muenster.de>
Дата:
Hi Salma
On 28/10/2026 21:17, Salma Elsayed wrote:
> Attached is v11, which removes DROP DATABASE as discussed, applies the
> error style guide to the log messages, and simplifies the TAP test.
Thanks for updating the patch.
A few questions:
== callback functions ==
The comments of RegisterXactCallback and RegisterSubXactCallback suggest
they're meant for extensions: "These functions are intended for use by
dynamically loaded modules.". Are they really the best fit here?
== RegisterDropOrTruncateTable ==
The function already expects reloid, so it could look for rel_name and
rel_namespace itself if needed -- currently the caller needs to do it.
Have you considered simply (relid, is_truncate)?
== doDeletion ==
Is the relname != NULL guard and the "unknown" schemaname really
necessary? I imagined that at this point the relation is locked and must
exist. Also, IMHO silently skipping a log entry or just logging a
placeholder is arguably not a good idea for this feature.
== prepared transactions ==
In DropTableXactCallback there is a whole else if block just to log that
logging the lsn is not possible. Also, I'd argue that the log message in
the following scenario is not very helpful:
psql (20devel)
Type "help" for help.
postgres=# create table t as select 1;
SELECT 1
postgres=# set client_min_messages to log;
SET
postgres=# set log_object_drops to on;
SET
postgres=# begin;
BEGIN
postgres=*# drop table t;
DROP TABLE
postgres=*# prepare transaction 'p';
LOG: table "public.t" (OID 27558) is being dropped inside a prepared
transaction
DETAIL: Automatic recovery LSN capture is not supported for two-phase
commit.
HINT: If this table needs to be recovered, the WAL around the eventual
COMMIT PREPARED will need to be inspected manually.
PREPARE TRANSACTION
postgres=# rollback prepared 'p';
ROLLBACK PREPARED
postgres=# select * from t;
?column?
----------
1
(1 row)
A simplistic approach would be just to make it explicit in the docs and
not log anything.
Thanks for working on this!
Best, Jim
Re: PoC: Simplify recovery after dropping a table by LOGGING the restore LSN
Hi Jim, Thanks for the review. On Thu, Oct 29, 2026 at 6:24 PM Jim Jones wrote: > == callback functions == > > The comments of RegisterXactCallback and RegisterSubXactCallback suggest > they're meant for extensions: "These functions are intended for use by > dynamically loaded modules.". Are they really the best fit here? > I removed the RegisterXactCallback and RegisterSubXactCallback calls and replaced them with AtEOSubXact_DropOrTruncateTable and AtEOXact_DropOrTruncateTable. They are now called directly from xact.c, next to AtEOXact_on_commit_actions and AtEOSubXact_on_commit_actions, which already handle ON COMMIT actions this way. > == RegisterDropOrTruncateTable == > > The function already expects reloid, so it could look for rel_name and > rel_namespace itself if needed -- currently the caller needs to do it. > Have you considered simply (relid, is_truncate)? > Done. It now takes (relid, is_truncate) and looks up the names itself. > == doDeletion == > > Is the relname != NULL guard and the "unknown" schemaname really > necessary? I imagined that at this point the relation is locked and must > exist. Also, IMHO silently skipping a log entry or just logging a > placeholder is arguably not a good idea for this feature. > Agreed. I removed the relname check and the "unknown" schema name. > == prepared transactions == > > A simplistic approach would be just to make it explicit in the docs and > not log anything. > Agreed. Nothing is logged for prepared transactions now, and the docs say so. While testing I also found that TRUNCATE of a partitioned table with no partitions hit an Assert when wal_level is minimal, because no commit record is written. Fixed in this version. v12 attached. Best, Salma Elsayed
Re: PoC: Simplify recovery after dropping a table by LOGGING the restore LSN
От:
Jim Jones <jim.jones@uni-muenster.de>
Дата:
Hi Salma
On 30/10/2026 16:25, Salma Elsayed wrote:
> v12 attached.
Thanks for the updated patch.
Before I jump into the test coverage, here a few more comments on the code:
== GUC name (bikeshedding) ==
The name log_object_drops is perhaps not the best fit anymore. The
feature only covers permanent tables, which is much narrower than
"object", and it also covers truncate, which is broader than "_drops". I
don't think we should spend too much time on this right now, but I
thought I should mention it for later.
== wal_level minimal ==
The lsn is being logged also when the cluster is set to wal_level =
minimal. The docs say "The minimal level generates the least WAL volume.
It logs no row information for permanent relations in transactions that
create or rewrite them." [1]
my $minimal = PostgreSQL::Test::Cluster->new('minimal');
$minimal->init;
$minimal->append_conf('postgresql.conf', 'log_object_drops = on');
$minimal->start;
$minimal->safe_psql('postgres',
'CREATE TABLE t_minimal (id int); DROP TABLE t_minimal;');
unlike(slurp_file($minimal->logfile),
$any_logged_re, 'nothing is logged with wal_level = minimal');
$minimal->stop;
== errhint in AtEOXact_DropOrTruncateTable (nit) ==
The two branches in "if (info->is_truncate) can be simplified into a
single ereport call, e.g.
ereport(LOG,
info->is_truncate ?
errmsg("table \"%s.%s\" (OID %u) truncated at LSN %X/%08X",
info->schemaname, info->relname, info->reloid,
LSN_FORMAT_ARGS(XactLastCommitStart)) :
errmsg("table \"%s.%s\" (OID %u) dropped at LSN %X/%08X",
info->schemaname, info->relname, info->reloid,
LSN_FORMAT_ARGS(XactLastCommitStart)),
info == last ?
errhint("To recover dropped or truncated tables, use "
"recovery_target_lsn = '%X/%08X' with "
"recovery_target_inclusive = false.",
LSN_FORMAT_ARGS(XactLastCommitStart)) : 0);
Best, Jim
1 -
https://www.postgresql.org/docs/current/runtime-config-wal.html#GUC-WAL-LEVEL
Re: PoC: Simplify recovery after dropping a table by LOGGING the restore LSN
От:
Zsolt Parragi <zsolt.parragi@percona.com>
Дата:
Hello DROP TABLE also has a similar loss window as DROP DATABASE: if the backend dies with kill -9 / immediate shutdown with a committed DROP TABLE waiting for synchronous replication. (create table; alter system set synchronous_standby_names='nosuchstandby'; drop table; kill) Logging in RecordTransactionCommit right after END_CRIT_SECTION could be a better choice that would fix this, and could work without XactLastCommitStart?
Re: PoC: Simplify recovery after dropping a table by LOGGING the restore LSN
Hi, Thanks for the reviews. On Sun, Nov 1, 2026 at 4:27 PM Jim Jones wrote: > == GUC name (bikeshedding) == > I don't think we should spend too much time on this right now, but I > thought I should mention it for later. > I agree, we can change it later. > == wal_level minimal == > > The lsn is being logged also when the cluster is set to wal_level = > minimal. The docs say "The minimal level generates the least WAL volume. > It logs no row information for permanent relations in transactions that > create or rewrite them." [1] > I added an XLogIsNeeded() check to RegisterDropOrTruncateTable, so nothing is logged when wal_level is minimal. I also added your test and a note in the docs. > == errhint in AtEOXact_DropOrTruncateTable (nit) == > > The two branches in "if (info->is_truncate) can be simplified into a > single ereport call, e.g. > Done. On Thu, Nov 5, 2026 at 12:17 AM Zsolt Parragi wrote: > DROP TABLE also has a similar loss window as DROP DATABASE: if the > backend dies with kill -9 / immediate shutdown with a committed DROP > TABLE waiting for synchronous replication. > (create table; alter system set > synchronous_standby_names='nosuchstandby'; drop table; kill) > Yes, I was able to reproduce this. > Logging in RecordTransactionCommit right after END_CRIT_SECTION could > be a better choice that would fix this, and could work without > XactLastCommitStart? > I moved the logging into RecordTransactionCommit, right after END_CRIT_SECTION. It uses a local commit_lsn, so XactLastCommitStart is removed, and AtEOXact_DropOrTruncateTable now only frees the list. With this change, the LSN is logged before the kill in your scenario, and recovering to that LSN brings the table back. v13 attached. Thanks, Salma Elsayed
Re: PoC: Simplify recovery after dropping a table by LOGGING the restore LSN
От:
"Andrey M. Borodin" <x4mmm@yandex-team.ru>
Дата:
Hi Salma, The default text-log prefix has no database name. IDK, users might want it: if you do a lot of CREATE DATABASE ... TEMPLATE ... table names and oids are same all the way. I tried Zsolt's SyncRep scenario with v13. Both messages were already logged as expected. What do you think about adding a TAP check for the message while the backend still waits in SyncRep? I'm not convinced we need it, I don't foresee what kind of bugs could hide there in future. Just raising the question about this tests. Thank you! Best regards, Andrey Borodin.
Re: PoC: Simplify recovery after dropping a table by LOGGING the restore LSN
Hello, I renamed the GUC to log_table_recovery_lsn, since it only covers tables and it also covers TRUNCATE. I am open to a better name. On Sun, Nov 8, 2026 at 8:58 AM Andrey M. Borodin wrote: > > Hi Salma, > > The default text-log prefix has no database name. IDK, users might want > it: if you do a lot of CREATE DATABASE ... TEMPLATE ... table names and > oids are same all the way. > Thanks for the review. I added the database name to the log message: LOG: table "public.orders" (OID 16389) in database "shop_a" dropped at LSN 0/02813958 LOG: table "public.orders" (OID 16389) in database "shop_b" dropped at LSN 0/02817300 Let me know if this looks like the right way to add it. > I tried Zsolt's SyncRep scenario with v13. Both messages were already > logged as expected. What do you think about adding a TAP check for the > message while the backend still waits in SyncRep? > I'm not convinced we need it, I don't foresee what kind of bugs could > hide there in future. Just raising the question about this tests. > I added a small test that checks the message is already in the log while the backend waits in SyncRep. It fails if the logging is moved after the wait. If you think it is not worth keeping, I am okay with that. v14 is attached. Thanks, Salma Elsayed