When dumping INSERT statements, optionally add ON CONFLICT DO NOTHING.
Author: Surafel Temesgen
Reviewed-by: Takeshi Ideriha, Nico Williams, Dilip Kumar
Discussion: https://postgr.es/m/CALAY4q-PQ9cOEzs2%2BQHK5ObfF_4QbmBaYXbZx6BGGN66Q-n8FA%40mail.gmail.com
index f402d46b0cfbd74818f0dc7a1ecaf39d04bd77a7..8286b2ddddaf6a4ad128f33427f6ff730b9aa277 100644 (file)
</listitem>
</varlistentry>
+ <varlistentry>
+ <term><option>--on-conflict-do-nothing</option></term>
+ <listitem>
+ <para>
+ Add <literal>ON CONFLICT DO NOTHING</literal> to
+ <command>INSERT</command> commands.
+ This option is not valid unless <option>--inserts</option> or
+ <option>--column-inserts</option> is also specified.
+ </para>
+ </listitem>
+ </varlistentry>
+
<varlistentry>
<term><option>--quote-all-identifiers</option></term>
<listitem>
index 22cb7907035ad1dbd1d96f34e54912e9521f01f7..94d76c30db3e305b652ea4cb7a25ca7b80b021bd 100644 (file)
</listitem>
</varlistentry>
+ <varlistentry>
+ <term><option>--on-conflict-do-nothing</option></term>
+ <listitem>
+ <para>
+ Add <literal>ON CONFLICT DO NOTHING</literal> to
+ <command>INSERT</command> commands.
+ This option is not valid unless <option>--inserts</option> or
+ <option>--column-inserts</option> is also specified.
+ </para>
+ </listitem>
+ </varlistentry>
+
<varlistentry>
<term><option>--quote-all-identifiers</option></term>
<listitem>
index ceedd481fb4740b5674413728ef4875086d60f80..42cf441aaf67344f167d458fcd0a45fe75e77f81 100644 (file)
char *outputSuperuser;
int sequence_data; /* dump sequence data even in schema-only mode */
+ int do_nothing;
} DumpOptions;
/*
index 463639208d6723bbe22cc5f32818bfe119fabe0d..8458e59b242d82c96b4ddf9034c18ff420174321 100644 (file)
{"no-unlogged-table-data", no_argument, &dopt.no_unlogged_table_data, 1},
{"no-subscriptions", no_argument, &dopt.no_subscriptions, 1},
{"no-sync", no_argument, NULL, 7},
+ {"on-conflict-do-nothing", no_argument, &dopt.do_nothing, 1},
{NULL, 0, NULL, 0}
};
if (dopt.if_exists && !dopt.outputClean)
exit_horribly(NULL, "option --if-exists requires option -c/--clean\n");
+ if (dopt.do_nothing && !(dopt.dump_inserts || dopt.column_inserts))
+ exit_horribly(NULL, "option --on-conflict-do-nothing requires option --inserts or --column-inserts\n");
+
/* Identify archive format to emit */
archiveFormat = parseArchiveFormat(format, &archiveMode);
printf(_(" --no-synchronized-snapshots do not use synchronized snapshots in parallel jobs\n"));
printf(_(" --no-tablespaces do not dump tablespace assignments\n"));
printf(_(" --no-unlogged-table-data do not dump unlogged table data\n"));
+ printf(_(" --on-conflict-do-nothing add ON CONFLICT DO NOTHING to INSERT commands\n"));
printf(_(" --quote-all-identifiers quote all identifiers, even if not key words\n"));
printf(_(" --section=SECTION dump named section (pre-data, data, or post-data)\n"));
printf(_(" --serializable-deferrable wait until the dump can run without anomalies\n"));
break;
}
}
- archputs(");\n", fout);
+
+ if (!dopt->do_nothing)
+ archputs(");\n", fout);
+ else
+ archputs(") ON CONFLICT DO NOTHING;\n", fout);
}
if (PQntuples(res) <= 0)
index 54db0cd1745916847bbdb29107bfa21acb899d1d..eb29d318a48f82184478b634544b85b1add1d72f 100644 (file)
@@ -78,6 +78,7 @@ static int no_unlogged_table_data = 0;
static int no_role_passwords = 0;
static int server_version;
static int load_via_partition_root = 0;
+static int on_conflict_do_nothing = 0;
static char role_catalog[10];
#define PG_AUTHID "pg_authid"
{"no-subscriptions", no_argument, &no_subscriptions, 1},
{"no-sync", no_argument, NULL, 4},
{"no-unlogged-table-data", no_argument, &no_unlogged_table_data, 1},
+ {"on-conflict-do-nothing", no_argument, &on_conflict_do_nothing, 1},
{NULL, 0, NULL, 0}
};
appendPQExpBufferStr(pgdumpopts, " --no-subscriptions");
if (no_unlogged_table_data)
appendPQExpBufferStr(pgdumpopts, " --no-unlogged-table-data");
+ if (on_conflict_do_nothing)
+ appendPQExpBufferStr(pgdumpopts, " --on-conflict-do-nothing");
/*
* If there was a database specified on the command line, use that,
printf(_(" --no-sync do not wait for changes to be written safely to disk\n"));
printf(_(" --no-tablespaces do not dump tablespace assignments\n"));
printf(_(" --no-unlogged-table-data do not dump unlogged table data\n"));
+ printf(_(" --on-conflict-do-nothing add ON CONFLICT DO NOTHING to INSERT commands\n"));
printf(_(" --quote-all-identifiers quote all identifiers, even if not key words\n"));
printf(_(" --use-set-session-authorization\n"
" use SET SESSION AUTHORIZATION commands instead of\n"
index 8be5770ba473952098a118ff1e22834934bba875..17edf444b2599e48a76970beea610bb4ee473248 100644 (file)
use Config;
use PostgresNode;
use TestLib;
-use Test::More tests => 68;
+use Test::More tests => 70;
my $tempdir = TestLib::tempdir;
my $tempdir_short = TestLib::tempdir_short;
qr/\Qpg_restore: unrecognized archive format "garbage";\E/,
'pg_dump: unrecognized archive format');
+command_fails_like(
+ [ 'pg_dump', '--on-conflict-do-nothing' ],
+ qr/\Qpg_dump: option --on-conflict-do-nothing requires option --inserts or --column-inserts\E/,
+ 'pg_dump: option --on-conflict-do-nothing requires option --inserts or --column-inserts');
+
# pg_dumpall command-line argument checks
command_fails_like(
[ 'pg_dumpall', '-g', '-r' ],