@@ -97,6 +97,9 @@ static const char *username_subquery;
97
97
/* obsolete as of 7.3: */
98
98
static Oid g_last_builtin_oid ; /* value of the last builtin oid */
99
99
100
+ /* The specified names/patterns should to match at least one entity */
101
+ static int strict_names = 0 ;
102
+
100
103
/*
101
104
* Object inclusion/exclusion lists
102
105
*
@@ -131,10 +134,12 @@ static void setup_connection(Archive *AH, DumpOptions *dopt,
131
134
static ArchiveFormat parseArchiveFormat (const char * format , ArchiveMode * mode );
132
135
static void expand_schema_name_patterns (Archive * fout ,
133
136
SimpleStringList * patterns ,
134
- SimpleOidList * oids );
137
+ SimpleOidList * oids ,
138
+ bool strict_names );
135
139
static void expand_table_name_patterns (Archive * fout ,
136
140
SimpleStringList * patterns ,
137
- SimpleOidList * oids );
141
+ SimpleOidList * oids ,
142
+ bool strict_names );
138
143
static NamespaceInfo * findNamespace (Archive * fout , Oid nsoid , Oid objoid );
139
144
static void dumpTableData (Archive * fout , DumpOptions * dopt , TableDataInfo * tdinfo );
140
145
static void refreshMatViewData (Archive * fout , TableDataInfo * tdinfo );
@@ -333,6 +338,7 @@ main(int argc, char **argv)
333
338
{"section" , required_argument , NULL , 5 },
334
339
{"serializable-deferrable" , no_argument , & dopt .serializable_deferrable , 1 },
335
340
{"snapshot" , required_argument , NULL , 6 },
341
+ {"strict-names" , no_argument , & strict_names , 1 },
336
342
{"use-set-session-authorization" , no_argument , & dopt .use_setsessauth , 1 },
337
343
{"no-security-labels" , no_argument , & dopt .no_security_labels , 1 },
338
344
{"no-synchronized-snapshots" , no_argument , & dopt .no_synchronized_snapshots , 1 },
@@ -690,27 +696,32 @@ main(int argc, char **argv)
690
696
if (schema_include_patterns .head != NULL )
691
697
{
692
698
expand_schema_name_patterns (fout , & schema_include_patterns ,
693
- & schema_include_oids );
699
+ & schema_include_oids ,
700
+ strict_names );
694
701
if (schema_include_oids .head == NULL )
695
702
exit_horribly (NULL , "No matching schemas were found\n" );
696
703
}
697
704
expand_schema_name_patterns (fout , & schema_exclude_patterns ,
698
- & schema_exclude_oids );
705
+ & schema_exclude_oids ,
706
+ false);
699
707
/* non-matching exclusion patterns aren't an error */
700
708
701
709
/* Expand table selection patterns into OID lists */
702
710
if (table_include_patterns .head != NULL )
703
711
{
704
712
expand_table_name_patterns (fout , & table_include_patterns ,
705
- & table_include_oids );
713
+ & table_include_oids ,
714
+ strict_names );
706
715
if (table_include_oids .head == NULL )
707
716
exit_horribly (NULL , "No matching tables were found\n" );
708
717
}
709
718
expand_table_name_patterns (fout , & table_exclude_patterns ,
710
- & table_exclude_oids );
719
+ & table_exclude_oids ,
720
+ false);
711
721
712
722
expand_table_name_patterns (fout , & tabledata_exclude_patterns ,
713
- & tabledata_exclude_oids );
723
+ & tabledata_exclude_oids ,
724
+ false);
714
725
715
726
/* non-matching exclusion patterns aren't an error */
716
727
@@ -905,6 +916,8 @@ help(const char *progname)
905
916
printf (_ (" --section=SECTION dump named section (pre-data, data, or post-data)\n" ));
906
917
printf (_ (" --serializable-deferrable wait until the dump can run without anomalies\n" ));
907
918
printf (_ (" --snapshot=SNAPSHOT use given synchronous snapshot for the dump\n" ));
919
+ printf (_ (" --strict-names require table and/or schema include patterns to\n"
920
+ " match at least one entity each\n" ));
908
921
printf (_ (" --use-set-session-authorization\n"
909
922
" use SET SESSION AUTHORIZATION commands instead of\n"
910
923
" ALTER OWNER commands to set ownership\n" ));
@@ -1135,7 +1148,8 @@ parseArchiveFormat(const char *format, ArchiveMode *mode)
1135
1148
static void
1136
1149
expand_schema_name_patterns (Archive * fout ,
1137
1150
SimpleStringList * patterns ,
1138
- SimpleOidList * oids )
1151
+ SimpleOidList * oids ,
1152
+ bool strict_names )
1139
1153
{
1140
1154
PQExpBuffer query ;
1141
1155
PGresult * res ;
@@ -1151,38 +1165,41 @@ expand_schema_name_patterns(Archive *fout,
1151
1165
query = createPQExpBuffer ();
1152
1166
1153
1167
/*
1154
- * We use UNION ALL rather than UNION; this might sometimes result in
1155
- * duplicate entries in the OID list, but we don't care.
1168
+ * This might sometimes result in duplicate entries in the OID list,
1169
+ * but we don't care.
1156
1170
*/
1157
1171
1158
1172
for (cell = patterns -> head ; cell ; cell = cell -> next )
1159
1173
{
1160
- if (cell != patterns -> head )
1161
- appendPQExpBufferStr (query , "UNION ALL\n" );
1162
1174
appendPQExpBuffer (query ,
1163
1175
"SELECT oid FROM pg_catalog.pg_namespace n\n" );
1164
1176
processSQLNamePattern (GetConnection (fout ), query , cell -> val , false,
1165
1177
false, NULL , "n.nspname" , NULL , NULL );
1166
- }
1167
1178
1168
- res = ExecuteSqlQuery (fout , query -> data , PGRES_TUPLES_OK );
1179
+ res = ExecuteSqlQuery (fout , query -> data , PGRES_TUPLES_OK );
1180
+ if (strict_names && PQntuples (res ) == 0 )
1181
+ exit_horribly (NULL , "No matching table(s) were found for pattern \"%s\"\n" , cell -> val );
1169
1182
1170
- for (i = 0 ; i < PQntuples (res ); i ++ )
1171
- {
1172
- simple_oid_list_append (oids , atooid (PQgetvalue (res , i , 0 )));
1183
+ for (i = 0 ; i < PQntuples (res ); i ++ )
1184
+ {
1185
+ simple_oid_list_append (oids , atooid (PQgetvalue (res , i , 0 )));
1186
+ }
1187
+
1188
+ PQclear (res );
1189
+ resetPQExpBuffer (query );
1173
1190
}
1174
1191
1175
- PQclear (res );
1176
1192
destroyPQExpBuffer (query );
1177
1193
}
1178
1194
1179
1195
/*
1180
1196
* Find the OIDs of all tables matching the given list of patterns,
1181
- * and append them to the given OID list.
1197
+ * and append them to the given OID list.
1182
1198
*/
1183
1199
static void
1184
1200
expand_table_name_patterns (Archive * fout ,
1185
- SimpleStringList * patterns , SimpleOidList * oids )
1201
+ SimpleStringList * patterns , SimpleOidList * oids ,
1202
+ bool strict_names )
1186
1203
{
1187
1204
PQExpBuffer query ;
1188
1205
PGresult * res ;
@@ -1195,14 +1212,12 @@ expand_table_name_patterns(Archive *fout,
1195
1212
query = createPQExpBuffer ();
1196
1213
1197
1214
/*
1198
- * We use UNION ALL rather than UNION; this might sometimes result in
1199
- * duplicate entries in the OID list, but we don't care.
1215
+ * this might sometimes result in duplicate entries in the OID list,
1216
+ * but we don't care.
1200
1217
*/
1201
1218
1202
1219
for (cell = patterns -> head ; cell ; cell = cell -> next )
1203
1220
{
1204
- if (cell != patterns -> head )
1205
- appendPQExpBufferStr (query , "UNION ALL\n" );
1206
1221
appendPQExpBuffer (query ,
1207
1222
"SELECT c.oid"
1208
1223
"\nFROM pg_catalog.pg_class c"
@@ -1213,16 +1228,20 @@ expand_table_name_patterns(Archive *fout,
1213
1228
processSQLNamePattern (GetConnection (fout ), query , cell -> val , true,
1214
1229
false, "n.nspname" , "c.relname" , NULL ,
1215
1230
"pg_catalog.pg_table_is_visible(c.oid)" );
1216
- }
1217
1231
1218
- res = ExecuteSqlQuery (fout , query -> data , PGRES_TUPLES_OK );
1232
+ res = ExecuteSqlQuery (fout , query -> data , PGRES_TUPLES_OK );
1233
+ if (strict_names && PQntuples (res ) == 0 )
1234
+ exit_horribly (NULL , "No matching table(s) were found for pattern \"%s\"\n" , cell -> val );
1219
1235
1220
- for (i = 0 ; i < PQntuples (res ); i ++ )
1221
- {
1222
- simple_oid_list_append (oids , atooid (PQgetvalue (res , i , 0 )));
1236
+ for (i = 0 ; i < PQntuples (res ); i ++ )
1237
+ {
1238
+ simple_oid_list_append (oids , atooid (PQgetvalue (res , i , 0 )));
1239
+ }
1240
+
1241
+ PQclear (res );
1242
+ resetPQExpBuffer (query );
1223
1243
}
1224
1244
1225
- PQclear (res );
1226
1245
destroyPQExpBuffer (query );
1227
1246
}
1228
1247
0 commit comments