@@ -83,8 +83,6 @@ static void flagInhTables(Archive *fout, TableInfo *tblinfo, int numTables,
83
83
InhInfo * inhinfo , int numInherits );
84
84
static void flagInhIndexes (Archive * fout , TableInfo * tblinfo , int numTables );
85
85
static void flagInhAttrs (DumpOptions * dopt , TableInfo * tblinfo , int numTables );
86
- static void findParentsByOid (TableInfo * self ,
87
- InhInfo * inhinfo , int numInherits );
88
86
static int strInArray (const char * pattern , char * * arr , int arr_size );
89
87
static IndxInfo * findIndexByOid (Oid oid );
90
88
@@ -288,45 +286,70 @@ static void
288
286
flagInhTables (Archive * fout , TableInfo * tblinfo , int numTables ,
289
287
InhInfo * inhinfo , int numInherits )
290
288
{
289
+ TableInfo * child = NULL ;
290
+ TableInfo * parent = NULL ;
291
291
int i ,
292
292
j ;
293
293
294
- for (i = 0 ; i < numTables ; i ++ )
294
+ /*
295
+ * Set up links from child tables to their parents.
296
+ *
297
+ * We used to attempt to skip this work for tables that are not to be
298
+ * dumped; but the optimizable cases are rare in practice, and setting up
299
+ * these links in bulk is cheaper than the old way. (Note in particular
300
+ * that it's very rare for a child to have more than one parent.)
301
+ */
302
+ for (i = 0 ; i < numInherits ; i ++ )
295
303
{
296
- bool find_parents = true;
297
- bool mark_parents = true;
298
-
299
- /* Some kinds never have parents */
300
- if (tblinfo [i ].relkind == RELKIND_SEQUENCE ||
301
- tblinfo [i ].relkind == RELKIND_VIEW ||
302
- tblinfo [i ].relkind == RELKIND_MATVIEW )
303
- continue ;
304
-
305
304
/*
306
- * Normally, we don't bother computing anything for non-target tables.
307
- * However, we must find the parents of non-root partitioned tables in
308
- * any case, so that we can trace from leaf partitions up to the root
309
- * (in case a leaf is to be dumped but its parents are not). We need
310
- * not mark such parents interesting for getTableAttrs, though.
305
+ * Skip a hashtable lookup if it's same table as last time. This is
306
+ * unlikely for the child, but less so for the parent. (Maybe we
307
+ * should ask the backend for a sorted array to make it more likely?
308
+ * Not clear the sorting effort would be repaid, though.)
311
309
*/
312
- if (!tblinfo [i ].dobj .dump )
310
+ if (child == NULL ||
311
+ child -> dobj .catId .oid != inhinfo [i ].inhrelid )
313
312
{
314
- mark_parents = false ;
313
+ child = findTableByOid ( inhinfo [ i ]. inhrelid ) ;
315
314
316
- if (!(tblinfo [i ].relkind == RELKIND_PARTITIONED_TABLE &&
317
- tblinfo [i ].ispartition ))
318
- find_parents = false;
315
+ /*
316
+ * If we find no TableInfo, assume the pg_inherits entry is for a
317
+ * partitioned index, which we don't need to track.
318
+ */
319
+ if (child == NULL )
320
+ continue ;
319
321
}
322
+ if (parent == NULL ||
323
+ parent -> dobj .catId .oid != inhinfo [i ].inhparent )
324
+ {
325
+ parent = findTableByOid (inhinfo [i ].inhparent );
326
+ if (parent == NULL )
327
+ pg_fatal ("failed sanity check, parent OID %u of table \"%s\" (OID %u) not found" ,
328
+ inhinfo [i ].inhparent ,
329
+ child -> dobj .name ,
330
+ child -> dobj .catId .oid );
331
+ }
332
+ /* Add this parent to the child's list of parents. */
333
+ if (child -> numParents > 0 )
334
+ child -> parents = pg_realloc_array (child -> parents ,
335
+ TableInfo * ,
336
+ child -> numParents + 1 );
337
+ else
338
+ child -> parents = pg_malloc_array (TableInfo * , 1 );
339
+ child -> parents [child -> numParents ++ ] = parent ;
340
+ }
320
341
321
- /* If needed, find all the immediate parent tables. */
322
- if (find_parents )
323
- findParentsByOid (& tblinfo [i ], inhinfo , numInherits );
324
-
342
+ /*
343
+ * Now consider all child tables and mark parents interesting as needed.
344
+ */
345
+ for (i = 0 ; i < numTables ; i ++ )
346
+ {
325
347
/*
326
348
* If needed, mark the parents as interesting for getTableAttrs and
327
- * getIndexes.
349
+ * getIndexes. We only need this for direct parents of dumpable
350
+ * tables.
328
351
*/
329
- if (mark_parents )
352
+ if (tblinfo [ i ]. dobj . dump )
330
353
{
331
354
int numParents = tblinfo [i ].numParents ;
332
355
TableInfo * * parents = tblinfo [i ].parents ;
@@ -336,7 +359,8 @@ flagInhTables(Archive *fout, TableInfo *tblinfo, int numTables,
336
359
}
337
360
338
361
/* Create TableAttachInfo object if needed */
339
- if (tblinfo [i ].dobj .dump && tblinfo [i ].ispartition )
362
+ if ((tblinfo [i ].dobj .dump & DUMP_COMPONENT_DEFINITION ) &&
363
+ tblinfo [i ].ispartition )
340
364
{
341
365
TableAttachInfo * attachinfo ;
342
366
@@ -995,52 +1019,6 @@ findOwningExtension(CatalogId catalogId)
995
1019
}
996
1020
997
1021
998
- /*
999
- * findParentsByOid
1000
- * find a table's parents in tblinfo[]
1001
- */
1002
- static void
1003
- findParentsByOid (TableInfo * self ,
1004
- InhInfo * inhinfo , int numInherits )
1005
- {
1006
- Oid oid = self -> dobj .catId .oid ;
1007
- int i ,
1008
- j ;
1009
- int numParents ;
1010
-
1011
- numParents = 0 ;
1012
- for (i = 0 ; i < numInherits ; i ++ )
1013
- {
1014
- if (inhinfo [i ].inhrelid == oid )
1015
- numParents ++ ;
1016
- }
1017
-
1018
- self -> numParents = numParents ;
1019
-
1020
- if (numParents > 0 )
1021
- {
1022
- self -> parents = pg_malloc_array (TableInfo * , numParents );
1023
- j = 0 ;
1024
- for (i = 0 ; i < numInherits ; i ++ )
1025
- {
1026
- if (inhinfo [i ].inhrelid == oid )
1027
- {
1028
- TableInfo * parent ;
1029
-
1030
- parent = findTableByOid (inhinfo [i ].inhparent );
1031
- if (parent == NULL )
1032
- pg_fatal ("failed sanity check, parent OID %u of table \"%s\" (OID %u) not found" ,
1033
- inhinfo [i ].inhparent ,
1034
- self -> dobj .name ,
1035
- oid );
1036
- self -> parents [j ++ ] = parent ;
1037
- }
1038
- }
1039
- }
1040
- else
1041
- self -> parents = NULL ;
1042
- }
1043
-
1044
1022
/*
1045
1023
* parseOidArray
1046
1024
* parse a string of numbers delimited by spaces into a character array
0 commit comments