pg_restore_truncate_v4.patch
text/x-patch
Filename: pg_restore_truncate_v4.patch
Type: text/x-patch
Part: 0
Patch
Same data as JSON:
GET /api/v1/attachments/:id/patch
the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes.
API reference →
Format: unified
Series: patch v4
| File | + | − |
|---|---|---|
| doc/src/sgml/ref/pg_restore.sgml | 20 | 0 |
| src/bin/pg_dump/pg_backup_archiver.c | 19 | 2 |
| src/bin/pg_dump/pg_backup.h | 2 | 0 |
| src/bin/pg_dump/pg_restore.c | 4 | 0 |
diff --git a/doc/src/sgml/ref/pg_restore.sgml b/doc/src/sgml/ref/pg_restore.sgml
index 1b9db6b..23b0d16 100644
--- a/doc/src/sgml/ref/pg_restore.sgml
+++ b/doc/src/sgml/ref/pg_restore.sgml
@@ -546,6 +546,26 @@
</varlistentry>
<varlistentry>
+ <term><option>--truncate-tables</></term>
+ <listitem>
+ <para>
+ This option is only relevant when performing a data-only
+ restore. It instructs <application>pg_restore</application>
+ to execute commands to truncate the target tables while the
+ data is reloaded. Use this when restoring tables or schemas
+ and <option>--clean</option> cannot be used because dependent
+ objects would be destroyed.
+ </para>
+
+ <para>
+ The <option>--disable-triggers</option> will almost always
+ always need to be used in conjunction with this option to
+ disable check constraints on foreign keys.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
<term><option>--use-set-session-authorization</option></term>
<listitem>
<para>
diff --git a/src/bin/pg_dump/pg_backup.h b/src/bin/pg_dump/pg_backup.h
index 3b49395..0aaf1d3 100644
--- a/src/bin/pg_dump/pg_backup.h
+++ b/src/bin/pg_dump/pg_backup.h
@@ -101,6 +101,8 @@ typedef struct _restoreOptions
int noTablespace; /* Don't issue tablespace-related commands */
int disable_triggers; /* disable triggers during data-only
* restore */
+ int truncate_tables; /* truncate tables during data-only
+ * restore */
int use_setsessauth;/* Use SET SESSION AUTHORIZATION commands
* instead of OWNER TO */
int no_security_labels; /* Skip security label entries */
diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c
index 1fead28..c7fdc79 100644
--- a/src/bin/pg_dump/pg_backup_archiver.c
+++ b/src/bin/pg_dump/pg_backup_archiver.c
@@ -309,6 +309,11 @@ RestoreArchive(Archive *AHX)
if (ropt->createDB && ropt->single_txn)
exit_horribly(modulename, "-C and -1 are incompatible options\n");
+ /* When the schema is dropped and re-created then no point
+ * truncating tables. */
+ if (ropt->dropSchema && ropt->truncate_tables)
+ exit_horribly(modulename, "-c and --truncate-tables are incompatible options\n");
+
/*
* If we're going to do parallel restore, there are some restrictions.
*/
@@ -403,6 +408,10 @@ RestoreArchive(Archive *AHX)
}
}
+ /* Truncate tables only when restoring data. */
+ if (!ropt->dataOnly && ropt->truncate_tables)
+ exit_horribly(modulename, "--truncate-tables requires the --data-only option\n");
+
/*
* Setup the output file if necessary.
*/
@@ -562,6 +571,7 @@ restore_toc_entry(ArchiveHandle *AH, TocEntry *te,
int retval = 0;
teReqs reqs;
bool defnDumped;
+ bool truncate;
AH->currentTE = te;
@@ -696,14 +706,21 @@ restore_toc_entry(ArchiveHandle *AH, TocEntry *te,
* server, so no need to see if we should issue BEGIN.
*/
StartTransaction(AH);
+ truncate = 1;
+ } else
+ /* Truncate the table when asked to. */
+ truncate = ropt->truncate_tables;
+ if (truncate) {
/*
* If the server version is >= 8.4, make sure we issue
* TRUNCATE with ONLY so that child tables are not
- * wiped.
+ * wiped. If we don't know the server version
+ * then err on the side of safety.
*/
ahprintf(AH, "TRUNCATE TABLE %s%s;\n\n",
- (PQserverVersion(AH->connection) >= 80400 ?
+ ((!AH->connection ||
+ PQserverVersion(AH->connection) >= 80400) ?
"ONLY " : ""),
fmtId(te->tag));
}
diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c
index 49d799b..316fe63 100644
--- a/src/bin/pg_dump/pg_restore.c
+++ b/src/bin/pg_dump/pg_restore.c
@@ -77,6 +77,7 @@ main(int argc, char **argv)
static int disable_triggers = 0;
static int no_data_for_failed_tables = 0;
static int outputNoTablespaces = 0;
+ static int truncate_tables = 0;
static int use_setsessauth = 0;
static int no_security_labels = 0;
@@ -119,6 +120,7 @@ main(int argc, char **argv)
{"no-tablespaces", no_argument, &outputNoTablespaces, 1},
{"role", required_argument, NULL, 2},
{"section", required_argument, NULL, 3},
+ {"truncate-tables", no_argument, &truncate_tables, 1},
{"use-set-session-authorization", no_argument, &use_setsessauth, 1},
{"no-security-labels", no_argument, &no_security_labels, 1},
@@ -324,6 +326,7 @@ main(int argc, char **argv)
opts->disable_triggers = disable_triggers;
opts->noDataForFailedTables = no_data_for_failed_tables;
opts->noTablespace = outputNoTablespaces;
+ opts->truncate_tables = truncate_tables;
opts->use_setsessauth = use_setsessauth;
opts->no_security_labels = no_security_labels;
@@ -434,6 +437,7 @@ usage(const char *progname)
printf(_(" --no-security-labels do not restore security labels\n"));
printf(_(" --no-tablespaces do not restore tablespace assignments\n"));
printf(_(" --section=SECTION restore named section (pre-data, data, or post-data)\n"));
+ printf(_(" --truncate-tables truncate tables before restore\n"));
printf(_(" --use-set-session-authorization\n"
" use SET SESSION AUTHORIZATION commands instead of\n"
" ALTER OWNER commands to set ownership\n"));