Skip to content

Commit eff9ff5

Browse files
vigneshwaran-cCommitfest Bot
authored and
Commitfest Bot
committed
pg_dump, pg_dumpall, pg_restore: Add --no-policies option
Add --no-policies option to control row level security policy handling in dump and restore operations. When this option is used, both CREATE POLICY commands and ALTER TABLE ... ENABLE ROW LEVEL SECURITY commands are excluded from dumps and skipped during restores. This is useful in scenarios where policies need to be redefined in the target system or when moving data between environments with different security requirements.
1 parent 5eabd91 commit eff9ff5

File tree

9 files changed

+67
-0
lines changed

9 files changed

+67
-0
lines changed

doc/src/sgml/ref/pg_dump.sgml

+9
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,15 @@ PostgreSQL documentation
11051105
</listitem>
11061106
</varlistentry>
11071107

1108+
<varlistentry>
1109+
<term><option>--no-policies</option></term>
1110+
<listitem>
1111+
<para>
1112+
Do not dump row security policies.
1113+
</para>
1114+
</listitem>
1115+
</varlistentry>
1116+
11081117
<varlistentry>
11091118
<term><option>--no-publications</option></term>
11101119
<listitem>

doc/src/sgml/ref/pg_dumpall.sgml

+9
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,15 @@ exclude database <replaceable class="parameter">PATTERN</replaceable>
441441
</listitem>
442442
</varlistentry>
443443

444+
<varlistentry>
445+
<term><option>--no-policies</option></term>
446+
<listitem>
447+
<para>
448+
Do not dump row security policies.
449+
</para>
450+
</listitem>
451+
</varlistentry>
452+
444453
<varlistentry>
445454
<term><option>--no-publications</option></term>
446455
<listitem>

doc/src/sgml/ref/pg_restore.sgml

+10
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,16 @@ PostgreSQL documentation
723723
</listitem>
724724
</varlistentry>
725725

726+
<varlistentry>
727+
<term><option>--no-policies</option></term>
728+
<listitem>
729+
<para>
730+
Do not output commands to restore row security policies, even if
731+
the archive contains them.
732+
</para>
733+
</listitem>
734+
</varlistentry>
735+
726736
<varlistentry>
727737
<term><option>--no-publications</option></term>
728738
<listitem>

src/bin/pg_dump/pg_backup.h

+2
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ typedef struct _restoreOptions
111111
int column_inserts;
112112
int if_exists;
113113
int no_comments; /* Skip comments */
114+
int no_policies; /* Skip row security policies */
114115
int no_publications; /* Skip publication entries */
115116
int no_security_labels; /* Skip security label entries */
116117
int no_subscriptions; /* Skip subscription entries */
@@ -183,6 +184,7 @@ typedef struct _dumpOptions
183184
int no_comments;
184185
int no_security_labels;
185186
int no_publications;
187+
int no_policies; /* Skip row security policies */
186188
int no_subscriptions;
187189
int no_toast_compression;
188190
int no_unlogged_table_data;

src/bin/pg_dump/pg_backup_archiver.c

+7
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ dumpOptionsFromRestoreOptions(RestoreOptions *ropt)
188188
dopt->disable_dollar_quoting = ropt->disable_dollar_quoting;
189189
dopt->dump_inserts = ropt->dump_inserts;
190190
dopt->no_comments = ropt->no_comments;
191+
dopt->no_policies = ropt->no_policies;
191192
dopt->no_publications = ropt->no_publications;
192193
dopt->no_security_labels = ropt->no_security_labels;
193194
dopt->no_subscriptions = ropt->no_subscriptions;
@@ -2966,6 +2967,12 @@ _tocEntryRequired(TocEntry *te, teSection curSection, ArchiveHandle *AH)
29662967
if (ropt->no_comments && strcmp(te->desc, "COMMENT") == 0)
29672968
return 0;
29682969

2970+
/* If it's a policy, maybe ignore it */
2971+
if (ropt->no_policies &&
2972+
(strcmp(te->desc, "POLICY") == 0 ||
2973+
strcmp(te->desc, "ROW SECURITY") == 0))
2974+
return 0;
2975+
29692976
/*
29702977
* If it's a publication or a table part of a publication, maybe ignore
29712978
* it.

src/bin/pg_dump/pg_dump.c

+6
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@ main(int argc, char **argv)
508508
{"no-toast-compression", no_argument, &dopt.no_toast_compression, 1},
509509
{"no-unlogged-table-data", no_argument, &dopt.no_unlogged_table_data, 1},
510510
{"no-sync", no_argument, NULL, 7},
511+
{"no-policies", no_argument, &dopt.no_policies, 1},
511512
{"on-conflict-do-nothing", no_argument, &dopt.do_nothing, 1},
512513
{"rows-per-insert", required_argument, NULL, 10},
513514
{"include-foreign-data", required_argument, NULL, 11},
@@ -1259,6 +1260,7 @@ help(const char *progname)
12591260
printf(_(" --load-via-partition-root load partitions via the root table\n"));
12601261
printf(_(" --no-comments do not dump comment commands\n"));
12611262
printf(_(" --no-data do not dump data\n"));
1263+
printf(_(" --no-policies do not dump row security policies\n"));
12621264
printf(_(" --no-publications do not dump publications\n"));
12631265
printf(_(" --no-schema do not dump schema\n"));
12641266
printf(_(" --no-security-labels do not dump security label assignments\n"));
@@ -4215,6 +4217,10 @@ dumpPolicy(Archive *fout, const PolicyInfo *polinfo)
42154217
if (!dopt->dumpSchema)
42164218
return;
42174219

4220+
/* Skip if --no-policies was specified */
4221+
if (dopt->no_policies)
4222+
return;
4223+
42184224
/*
42194225
* If polname is NULL, then this record is just indicating that ROW LEVEL
42204226
* SECURITY is enabled for the table. Dump as ALTER TABLE <table> ENABLE

src/bin/pg_dump/pg_dumpall.c

+5
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ static int no_table_access_method = 0;
101101
static int no_tablespaces = 0;
102102
static int use_setsessauth = 0;
103103
static int no_comments = 0;
104+
static int no_policies = 0;
104105
static int no_publications = 0;
105106
static int no_security_labels = 0;
106107
static int no_data = 0;
@@ -173,6 +174,7 @@ main(int argc, char *argv[])
173174
{"use-set-session-authorization", no_argument, &use_setsessauth, 1},
174175
{"no-comments", no_argument, &no_comments, 1},
175176
{"no-data", no_argument, &no_data, 1},
177+
{"no-policies", no_argument, &no_policies, 1},
176178
{"no-publications", no_argument, &no_publications, 1},
177179
{"no-role-passwords", no_argument, &no_role_passwords, 1},
178180
{"no-schema", no_argument, &no_schema, 1},
@@ -457,6 +459,8 @@ main(int argc, char *argv[])
457459
appendPQExpBufferStr(pgdumpopts, " --no-comments");
458460
if (no_data)
459461
appendPQExpBufferStr(pgdumpopts, " --no-data");
462+
if (no_policies)
463+
appendPQExpBufferStr(pgdumpopts, " --no-policies");
460464
if (no_publications)
461465
appendPQExpBufferStr(pgdumpopts, " --no-publications");
462466
if (no_security_labels)
@@ -681,6 +685,7 @@ help(void)
681685
printf(_(" --load-via-partition-root load partitions via the root table\n"));
682686
printf(_(" --no-comments do not dump comment commands\n"));
683687
printf(_(" --no-data do not dump data\n"));
688+
printf(_(" --no-policies do not dump row security policies\n"));
684689
printf(_(" --no-publications do not dump publications\n"));
685690
printf(_(" --no-role-passwords do not dump passwords for roles\n"));
686691
printf(_(" --no-schema do not dump schema\n"));

src/bin/pg_dump/pg_restore.c

+4
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ main(int argc, char **argv)
7474
static int use_setsessauth = 0;
7575
static int no_comments = 0;
7676
static int no_data = 0;
77+
static int no_policies = 0;
7778
static int no_publications = 0;
7879
static int no_schema = 0;
7980
static int no_security_labels = 0;
@@ -129,6 +130,7 @@ main(int argc, char **argv)
129130
{"use-set-session-authorization", no_argument, &use_setsessauth, 1},
130131
{"no-comments", no_argument, &no_comments, 1},
131132
{"no-data", no_argument, &no_data, 1},
133+
{"no-policies", no_argument, &no_policies, 1},
132134
{"no-publications", no_argument, &no_publications, 1},
133135
{"no-schema", no_argument, &no_schema, 1},
134136
{"no-security-labels", no_argument, &no_security_labels, 1},
@@ -385,6 +387,7 @@ main(int argc, char **argv)
385387
opts->noTablespace = outputNoTablespaces;
386388
opts->use_setsessauth = use_setsessauth;
387389
opts->no_comments = no_comments;
390+
opts->no_policies = no_policies;
388391
opts->no_publications = no_publications;
389392
opts->no_security_labels = no_security_labels;
390393
opts->no_subscriptions = no_subscriptions;
@@ -505,6 +508,7 @@ usage(const char *progname)
505508
printf(_(" --no-data do not restore data\n"));
506509
printf(_(" --no-data-for-failed-tables do not restore data of tables that could not be\n"
507510
" created\n"));
511+
printf(_(" --no-policies do not restore row level security policies\n"));
508512
printf(_(" --no-publications do not restore publications\n"));
509513
printf(_(" --no-schema do not restore schema\n"));
510514
printf(_(" --no-security-labels do not restore security labels\n"));

src/bin/pg_dump/t/002_pg_dump.pl

+15
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,13 @@
579579
'postgres',
580580
],
581581
},
582+
no_policies => {
583+
dump_cmd => [
584+
'pg_dump', '--no-sync',
585+
"--file=$tempdir/no_policies.sql",
586+
'--no-policies', 'postgres',
587+
],
588+
},
582589
no_privs => {
583590
dump_cmd => [
584591
'pg_dump', '--no-sync',
@@ -803,6 +810,7 @@
803810
no_toast_compression => 1,
804811
no_large_objects => 1,
805812
no_owner => 1,
813+
no_policies => 1,
806814
no_privs => 1,
807815
no_statistics => 1,
808816
no_table_access_method => 1,
@@ -1328,6 +1336,7 @@
13281336
unlike => {
13291337
exclude_dump_test_schema => 1,
13301338
exclude_test_table => 1,
1339+
no_policies => 1,
13311340
only_dump_measurement => 1,
13321341
},
13331342
},
@@ -2948,6 +2957,7 @@
29482957
unlike => {
29492958
exclude_dump_test_schema => 1,
29502959
exclude_test_table => 1,
2960+
no_policies => 1,
29512961
only_dump_measurement => 1,
29522962
},
29532963
},
@@ -2969,6 +2979,7 @@
29692979
unlike => {
29702980
exclude_dump_test_schema => 1,
29712981
exclude_test_table => 1,
2982+
no_policies => 1,
29722983
only_dump_measurement => 1,
29732984
},
29742985
},
@@ -2990,6 +3001,7 @@
29903001
unlike => {
29913002
exclude_dump_test_schema => 1,
29923003
exclude_test_table => 1,
3004+
no_policies => 1,
29933005
only_dump_measurement => 1,
29943006
},
29953007
},
@@ -3011,6 +3023,7 @@
30113023
unlike => {
30123024
exclude_dump_test_schema => 1,
30133025
exclude_test_table => 1,
3026+
no_policies => 1,
30143027
only_dump_measurement => 1,
30153028
},
30163029
},
@@ -3032,6 +3045,7 @@
30323045
unlike => {
30333046
exclude_dump_test_schema => 1,
30343047
exclude_test_table => 1,
3048+
no_policies => 1,
30353049
only_dump_measurement => 1,
30363050
},
30373051
},
@@ -3053,6 +3067,7 @@
30533067
unlike => {
30543068
exclude_dump_test_schema => 1,
30553069
exclude_test_table => 1,
3070+
no_policies => 1,
30563071
only_dump_measurement => 1,
30573072
},
30583073
},

0 commit comments

Comments
 (0)