On Thu, Jul 24, 2025 at 4:24 AM Dimitrios Apostolou <[email protected]> wrote:
> Hello Stepan! > > I can see you tested my patch by itself, and with the following command: > > On Monday 2025-07-21 07:58, Stepan Neretin wrote: > > > pg_restore -d "$DB_NAME" -j "$JOBS" --clean --if-exists --freeze > "$DUMP_FILE" > /dev/null > > On the other hand, I have tested combining --freeze with --data-only > --clean, as implemented by another patch of mine at: > > > https://www.postgresql.org/message-id/flat/413c1cd8-1d6d-90ba-ac7b-b226a4dad5ed%40gmx.net#44f06ce85a14a6238125a0ad8c9767db > > In fact I was considering that patch as a requirement to this one, but I > see that you are using my patch independently to issue --clean --freeze > without --data-only. As far as I understand, this will also issue a > TRUNCATE before each COPY, and the benefits of FREEZE will be the same. > Thank you for revealing this to me. > > > >The main area of concern is the implementation method in > `pg_backup_archiver.c`. The patch introduces a `do_copy` function that > modifies the `COPY` statement string to inject the `WITH (FREEZE)` option. > > > >```c > >if (cp_end - 10 > cp && > > strncmp(cp_end - 10, "FROM stdin", 10) == 0 > > ) > > cp_is_well_formed = true; > >``` > > > >This approach of string parsing is quite fragile. It makes a strong > assumption that the `COPY` statement generated by `pg_dump` will always end > with `... FROM stdin;` (preceded by optional whitespace). While this is > true now, it creates a tight and brittle coupling between `pg_dump`'s > output format and > >`pg_restore`'s parsing logic. Any future changes to `pg_dump` that might > add options or slightly alter the `COPY` syntax could break this feature > silently or with a non-obvious warning. > > It troubled me too. At least I have a pg_log_warning() message in this > case, and nothing detrimental will happen, it's just that the FREEZE > option will not be used and the message will be logged. > > > > >A more robust solution would be to avoid string manipulation entirely. > `pg_restore` should assemble the `COPY` command from its constituent parts > (table name, column list, etc.) and then conditionally append the `WITH > (FREEZE)` clause before the final `FROM stdin;`. This would decouple the > feature from > >the exact string representation in the dump archive. > > Totally agree, but this sounds impossible to implement. Unfortunately > pg_dump generates SQL commands, and pg_restore edits them and executes > them. I wouldn't know where to start to change this whole architecture. > > > > >An alternative—and arguably cleaner—approach might be to shift this logic > to pg_dump. > > Thinking it more thoroughly, pg_dump is not the place to compose the > restoration SQL commands. The proper place is pg_restore. > I guess it's being done in pg_dump for historical reasons, but I don't > think pg_dump should complicate its commands further (e.g. by adding > WITH FREEZE), as this is a choice to make during restore. > I think I've seen more places in pg_restore that edit the > commands before executing them. > > > >One important consideration that needs to be highlighted in the > documentation for this feature is the impact on WAL generation. `COPY ... > WITH (FREEZE)` is known to generate significantly more WAL traffic because > it must log the freezing of tuples, which can be a surprise for users. > Maybe we can insert > >already frozen pages? > > There should be no WAL traffic at all. If the transaction starts with a > TRUNCATE TABLE then COPY skips the wal completely. This gives a big > performance and stability gain when bulk-loading data, and this is what > my 2 patches try to leverage in more cases. > > And as far as I can tell, if there is no TRUNCATE in the same > transaction, then pg_restore will output error like the following: > > ERROR: cannot perform COPY FREEZE because the table was not created or > truncated in the current subtransaction > > I hope such an erroris acceptable, since the sysadmin can just remove > --freeze then and re-run the command. > > >Additionally, it should be noted that the freeze option only works > correctly when performing a fresh load of data into empty tables. > > Do you think this patch has chances of going in by itself? If so then > yes, I should definitely update the docs and submit it again > individually. > > > Thank you for the rebase and review. > > Dimitris > Hi, Just a rebase of the latest pg_restore --freeze patch onto the current master. No other changes. Thanks, Stepan Neretin
From a5f2a3eb0be88bc433ed28beed56fd7577f5f82c Mon Sep 17 00:00:00 2001 From: Stepan Neretin <[email protected]> Date: Mon, 21 Jul 2025 12:22:02 +0700 Subject: [PATCH] pg_restore --freeze pg_restore now invokes COPY FROM ... WITH (FREEZE) Needs also options --section=data --clean -j N so that each pg_restore worker process wraps each COPY in a transaction and precedes it with TRUNCATE. That way the data insertion is optimized and the visibility table is written without needing VACUUMing. Rebased-by: Stepan Neretin <[email protected]> --- src/bin/pg_dump/pg_backup.h | 1 + src/bin/pg_dump/pg_backup_archiver.c | 43 +++++++++++++++++++++++++++- src/bin/pg_dump/pg_dump.c | 9 +++++- src/bin/pg_dump/pg_restore.c | 4 +++ 4 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/bin/pg_dump/pg_backup.h b/src/bin/pg_dump/pg_backup.h index c7bdda1deed..6e1b8c45f67 100644 --- a/src/bin/pg_dump/pg_backup.h +++ b/src/bin/pg_dump/pg_backup.h @@ -117,6 +117,7 @@ typedef struct _restoreOptions int no_security_labels; /* Skip security label entries */ int no_subscriptions; /* Skip subscription entries */ int strict_names; + int freeze; /* COPY FREEZE */ const char *filename; int dumpSections; diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c index d3e0006fa07..f336dc49f9a 100644 --- a/src/bin/pg_dump/pg_backup_archiver.c +++ b/src/bin/pg_dump/pg_backup_archiver.c @@ -99,6 +99,9 @@ static void restore_toc_entries_parallel(ArchiveHandle *AH, TocEntry *pending_list); static void restore_toc_entries_postfork(ArchiveHandle *AH, TocEntry *pending_list); +static void do_copy(ArchiveHandle *AH, + const char *cp, + bool freeze); static void pending_list_header_init(TocEntry *l); static void pending_list_append(TocEntry *l, TocEntry *te); static void pending_list_remove(TocEntry *te); @@ -1036,7 +1039,7 @@ restore_toc_entry(ArchiveHandle *AH, TocEntry *te, bool is_parallel) */ if (te->copyStmt && strlen(te->copyStmt) > 0) { - ahprintf(AH, "%s", te->copyStmt); + do_copy(AH, te->copyStmt, ropt->freeze); AH->outputKind = OUTPUT_COPYDATA; } else @@ -1099,6 +1102,44 @@ restore_toc_entry(ArchiveHandle *AH, TocEntry *te, bool is_parallel) return status; } +static void +do_copy(ArchiveHandle *AH, const char *cp, bool freeze) +{ + const size_t cp_len = strlen(cp); + bool cp_is_well_formed = false; + const char *cp_end; + + Assert(cp_len > 0); /* Have checked it in caller */ + + /* + * Check if the COPY statement is well written so that we can inject the + * FREEZE option. + */ + if (freeze) + { + int i = cp_len - 1; + + /* Cut off the trailing semicolon and whitespace. */ + while (i > 0 && + (cp[i] == ' ' || cp[i] == '\n' || cp[i] == ';')) + i--; + cp_end = &cp[i + 1]; + + if (cp_end - 10 > cp && + strncmp(cp_end - 10, "FROM stdin", 10) == 0 + ) + cp_is_well_formed = true; + else + pg_log_warning("COPY statement from dump file is not in the right form; Will not inject the FREEZE option"); + } + + if (freeze && cp_is_well_formed) + ahprintf(AH, "%.*s WITH (FREEZE);\n", + (int) (cp_end - cp), cp); + else + ahprintf(AH, "%s", cp); +} + /* * Allocate a new RestoreOptions block. * This is mainly so we can initialize it, but also for future expansion, diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index a3f38f3b886..48c54fedd8c 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -2875,7 +2875,14 @@ dumpTableData(Archive *fout, const TableDataInfo *tdinfo) if (dopt->dump_inserts == 0) { - /* Dump/restore using COPY */ + /* + * Dump/restore using COPY. + * + * NOTE: do not use options in the COPY statement, as the restore + * process appends its own options. In fact pg_restore always expects + * + * COPY ... FROM stdin; + */ dumpFn = dumpTableData_copy; /* must use 2 steps here 'cause fmtId is nonreentrant */ printfPQExpBuffer(copyBuf, "COPY %s ", diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c index 84b8d410c9e..e694601b35d 100644 --- a/src/bin/pg_dump/pg_restore.c +++ b/src/bin/pg_dump/pg_restore.c @@ -84,6 +84,7 @@ main(int argc, char **argv) static int strict_names = 0; static int statistics_only = 0; static int with_statistics = 0; + static int freeze = 0; struct option cmdopts[] = { {"clean", 0, NULL, 'c'}, @@ -142,6 +143,7 @@ main(int argc, char **argv) {"statistics-only", no_argument, &statistics_only, 1}, {"filter", required_argument, NULL, 4}, {"restrict-key", required_argument, NULL, 6}, + {"freeze", no_argument, &freeze, 1}, {NULL, 0, NULL, 0} }; @@ -455,6 +457,7 @@ main(int argc, char **argv) opts->no_publications = no_publications; opts->no_security_labels = no_security_labels; opts->no_subscriptions = no_subscriptions; + opts->freeze = freeze; if (if_exists && !opts->dropSchema) pg_fatal("option %s requires option %s", @@ -568,6 +571,7 @@ usage(const char *progname) printf(_(" --enable-row-security enable row security\n")); printf(_(" --filter=FILENAME restore or skip objects based on expressions\n" " in FILENAME\n")); + printf(_(" --freeze COPY FREEZE (read the manual for caveats)\n")); printf(_(" --if-exists use IF EXISTS when dropping objects\n")); printf(_(" --no-comments do not restore comment commands\n")); printf(_(" --no-data do not restore data\n")); -- 2.56.1
