@@ -97,6 +97,9 @@ static const char *username_subquery;
9797/* obsolete as of 7.3: */
9898static Oid g_last_builtin_oid ; /* value of the last builtin oid */
9999
100+ /* The specified names/patterns should to match at least one entity */
101+ static int strict_names = 0 ;
102+
100103/*
101104 * Object inclusion/exclusion lists
102105 *
@@ -131,10 +134,12 @@ static void setup_connection(Archive *AH, DumpOptions *dopt,
131134static ArchiveFormat parseArchiveFormat (const char * format , ArchiveMode * mode );
132135static void expand_schema_name_patterns (Archive * fout ,
133136 SimpleStringList * patterns ,
134- SimpleOidList * oids );
137+ SimpleOidList * oids ,
138+ bool strict_names );
135139static void expand_table_name_patterns (Archive * fout ,
136140 SimpleStringList * patterns ,
137- SimpleOidList * oids );
141+ SimpleOidList * oids ,
142+ bool strict_names );
138143static NamespaceInfo * findNamespace (Archive * fout , Oid nsoid , Oid objoid );
139144static void dumpTableData (Archive * fout , DumpOptions * dopt , TableDataInfo * tdinfo );
140145static void refreshMatViewData (Archive * fout , TableDataInfo * tdinfo );
@@ -333,6 +338,7 @@ main(int argc, char **argv)
333338 {"section" , required_argument , NULL , 5 },
334339 {"serializable-deferrable" , no_argument , & dopt .serializable_deferrable , 1 },
335340 {"snapshot" , required_argument , NULL , 6 },
341+ {"strict-names" , no_argument , & strict_names , 1 },
336342 {"use-set-session-authorization" , no_argument , & dopt .use_setsessauth , 1 },
337343 {"no-security-labels" , no_argument , & dopt .no_security_labels , 1 },
338344 {"no-synchronized-snapshots" , no_argument , & dopt .no_synchronized_snapshots , 1 },
@@ -690,27 +696,32 @@ main(int argc, char **argv)
690696 if (schema_include_patterns .head != NULL )
691697 {
692698 expand_schema_name_patterns (fout , & schema_include_patterns ,
693- & schema_include_oids );
699+ & schema_include_oids ,
700+ strict_names );
694701 if (schema_include_oids .head == NULL )
695702 exit_horribly (NULL , "No matching schemas were found\n" );
696703 }
697704 expand_schema_name_patterns (fout , & schema_exclude_patterns ,
698- & schema_exclude_oids );
705+ & schema_exclude_oids ,
706+ false);
699707 /* non-matching exclusion patterns aren't an error */
700708
701709 /* Expand table selection patterns into OID lists */
702710 if (table_include_patterns .head != NULL )
703711 {
704712 expand_table_name_patterns (fout , & table_include_patterns ,
705- & table_include_oids );
713+ & table_include_oids ,
714+ strict_names );
706715 if (table_include_oids .head == NULL )
707716 exit_horribly (NULL , "No matching tables were found\n" );
708717 }
709718 expand_table_name_patterns (fout , & table_exclude_patterns ,
710- & table_exclude_oids );
719+ & table_exclude_oids ,
720+ false);
711721
712722 expand_table_name_patterns (fout , & tabledata_exclude_patterns ,
713- & tabledata_exclude_oids );
723+ & tabledata_exclude_oids ,
724+ false);
714725
715726 /* non-matching exclusion patterns aren't an error */
716727
@@ -905,6 +916,8 @@ help(const char *progname)
905916 printf (_ (" --section=SECTION dump named section (pre-data, data, or post-data)\n" ));
906917 printf (_ (" --serializable-deferrable wait until the dump can run without anomalies\n" ));
907918 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" ));
908921 printf (_ (" --use-set-session-authorization\n"
909922 " use SET SESSION AUTHORIZATION commands instead of\n"
910923 " ALTER OWNER commands to set ownership\n" ));
@@ -1135,7 +1148,8 @@ parseArchiveFormat(const char *format, ArchiveMode *mode)
11351148static void
11361149expand_schema_name_patterns (Archive * fout ,
11371150 SimpleStringList * patterns ,
1138- SimpleOidList * oids )
1151+ SimpleOidList * oids ,
1152+ bool strict_names )
11391153{
11401154 PQExpBuffer query ;
11411155 PGresult * res ;
@@ -1151,38 +1165,41 @@ expand_schema_name_patterns(Archive *fout,
11511165 query = createPQExpBuffer ();
11521166
11531167 /*
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.
11561170 */
11571171
11581172 for (cell = patterns -> head ; cell ; cell = cell -> next )
11591173 {
1160- if (cell != patterns -> head )
1161- appendPQExpBufferStr (query , "UNION ALL\n" );
11621174 appendPQExpBuffer (query ,
11631175 "SELECT oid FROM pg_catalog.pg_namespace n\n" );
11641176 processSQLNamePattern (GetConnection (fout ), query , cell -> val , false,
11651177 false, NULL , "n.nspname" , NULL , NULL );
1166- }
11671178
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 );
11691182
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 );
11731190 }
11741191
1175- PQclear (res );
11761192 destroyPQExpBuffer (query );
11771193}
11781194
11791195/*
11801196 * 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.
11821198 */
11831199static void
11841200expand_table_name_patterns (Archive * fout ,
1185- SimpleStringList * patterns , SimpleOidList * oids )
1201+ SimpleStringList * patterns , SimpleOidList * oids ,
1202+ bool strict_names )
11861203{
11871204 PQExpBuffer query ;
11881205 PGresult * res ;
@@ -1195,14 +1212,12 @@ expand_table_name_patterns(Archive *fout,
11951212 query = createPQExpBuffer ();
11961213
11971214 /*
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.
12001217 */
12011218
12021219 for (cell = patterns -> head ; cell ; cell = cell -> next )
12031220 {
1204- if (cell != patterns -> head )
1205- appendPQExpBufferStr (query , "UNION ALL\n" );
12061221 appendPQExpBuffer (query ,
12071222 "SELECT c.oid"
12081223 "\nFROM pg_catalog.pg_class c"
@@ -1213,16 +1228,20 @@ expand_table_name_patterns(Archive *fout,
12131228 processSQLNamePattern (GetConnection (fout ), query , cell -> val , true,
12141229 false, "n.nspname" , "c.relname" , NULL ,
12151230 "pg_catalog.pg_table_is_visible(c.oid)" );
1216- }
12171231
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 );
12191235
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 );
12231243 }
12241244
1225- PQclear (res );
12261245 destroyPQExpBuffer (query );
12271246}
12281247
0 commit comments