LCOV - code coverage report
Current view: top level - src/bin/pg_dump - pg_dump_sort.c (source / functions) Hit Total Coverage
Test: PostgreSQL 19devel Lines: 393 634 62.0 %
Date: 2025-08-31 01:17:28 Functions: 20 23 87.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * pg_dump_sort.c
       4             :  *    Sort the items of a dump into a safe order for dumping
       5             :  *
       6             :  *
       7             :  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
       8             :  * Portions Copyright (c) 1994, Regents of the University of California
       9             :  *
      10             :  *
      11             :  * IDENTIFICATION
      12             :  *    src/bin/pg_dump/pg_dump_sort.c
      13             :  *
      14             :  *-------------------------------------------------------------------------
      15             :  */
      16             : #include "postgres_fe.h"
      17             : 
      18             : #include "catalog/pg_class_d.h"
      19             : #include "common/int.h"
      20             : #include "lib/binaryheap.h"
      21             : #include "pg_backup_utils.h"
      22             : #include "pg_dump.h"
      23             : 
      24             : /*
      25             :  * Sort priority for database object types.
      26             :  * Objects are sorted by type, and within a type by name.
      27             :  *
      28             :  * Triggers, event triggers, and materialized views are intentionally sorted
      29             :  * late.  Triggers must be restored after all data modifications, so that
      30             :  * they don't interfere with loading data.  Event triggers are restored
      31             :  * next-to-last so that they don't interfere with object creations of any
      32             :  * kind.  Matview refreshes are last because they should execute in the
      33             :  * database's normal state (e.g., they must come after all ACLs are restored;
      34             :  * also, if they choose to look at system catalogs, they should see the final
      35             :  * restore state).  If you think to change this, see also the RestorePass
      36             :  * mechanism in pg_backup_archiver.c.
      37             :  *
      38             :  * On the other hand, casts are intentionally sorted earlier than you might
      39             :  * expect; logically they should come after functions, since they usually
      40             :  * depend on those.  This works around the backend's habit of recording
      41             :  * views that use casts as dependent on the cast's underlying function.
      42             :  * We initially sort casts first, and then any functions used by casts
      43             :  * will be hoisted above the casts, and in turn views that those functions
      44             :  * depend on will be hoisted above the functions.  But views not used that
      45             :  * way won't be hoisted.
      46             :  *
      47             :  * NOTE: object-type priorities must match the section assignments made in
      48             :  * pg_dump.c; that is, PRE_DATA objects must sort before DO_PRE_DATA_BOUNDARY,
      49             :  * POST_DATA objects must sort after DO_POST_DATA_BOUNDARY, and DATA objects
      50             :  * must sort between them.
      51             :  */
      52             : 
      53             : /* This enum lists the priority levels in order */
      54             : enum dbObjectTypePriorities
      55             : {
      56             :     PRIO_NAMESPACE = 1,
      57             :     PRIO_PROCLANG,
      58             :     PRIO_COLLATION,
      59             :     PRIO_TRANSFORM,
      60             :     PRIO_EXTENSION,
      61             :     PRIO_TYPE,                  /* used for DO_TYPE and DO_SHELL_TYPE */
      62             :     PRIO_CAST,
      63             :     PRIO_FUNC,
      64             :     PRIO_AGG,
      65             :     PRIO_ACCESS_METHOD,
      66             :     PRIO_OPERATOR,
      67             :     PRIO_OPFAMILY,              /* used for DO_OPFAMILY and DO_OPCLASS */
      68             :     PRIO_CONVERSION,
      69             :     PRIO_TSPARSER,
      70             :     PRIO_TSTEMPLATE,
      71             :     PRIO_TSDICT,
      72             :     PRIO_TSCONFIG,
      73             :     PRIO_FDW,
      74             :     PRIO_FOREIGN_SERVER,
      75             :     PRIO_TABLE,
      76             :     PRIO_TABLE_ATTACH,
      77             :     PRIO_DUMMY_TYPE,
      78             :     PRIO_ATTRDEF,
      79             :     PRIO_PRE_DATA_BOUNDARY,     /* boundary! */
      80             :     PRIO_TABLE_DATA,
      81             :     PRIO_SEQUENCE_SET,
      82             :     PRIO_LARGE_OBJECT,
      83             :     PRIO_LARGE_OBJECT_DATA,
      84             :     PRIO_STATISTICS_DATA_DATA,
      85             :     PRIO_POST_DATA_BOUNDARY,    /* boundary! */
      86             :     PRIO_CONSTRAINT,
      87             :     PRIO_INDEX,
      88             :     PRIO_INDEX_ATTACH,
      89             :     PRIO_STATSEXT,
      90             :     PRIO_RULE,
      91             :     PRIO_TRIGGER,
      92             :     PRIO_FK_CONSTRAINT,
      93             :     PRIO_POLICY,
      94             :     PRIO_PUBLICATION,
      95             :     PRIO_PUBLICATION_REL,
      96             :     PRIO_PUBLICATION_TABLE_IN_SCHEMA,
      97             :     PRIO_SUBSCRIPTION,
      98             :     PRIO_SUBSCRIPTION_REL,
      99             :     PRIO_DEFAULT_ACL,           /* done in ACL pass */
     100             :     PRIO_EVENT_TRIGGER,         /* must be next to last! */
     101             :     PRIO_REFRESH_MATVIEW        /* must be last! */
     102             : };
     103             : 
     104             : /* This table is indexed by enum DumpableObjectType */
     105             : static const int dbObjectTypePriority[] =
     106             : {
     107             :     [DO_NAMESPACE] = PRIO_NAMESPACE,
     108             :     [DO_EXTENSION] = PRIO_EXTENSION,
     109             :     [DO_TYPE] = PRIO_TYPE,
     110             :     [DO_SHELL_TYPE] = PRIO_TYPE,
     111             :     [DO_FUNC] = PRIO_FUNC,
     112             :     [DO_AGG] = PRIO_AGG,
     113             :     [DO_OPERATOR] = PRIO_OPERATOR,
     114             :     [DO_ACCESS_METHOD] = PRIO_ACCESS_METHOD,
     115             :     [DO_OPCLASS] = PRIO_OPFAMILY,
     116             :     [DO_OPFAMILY] = PRIO_OPFAMILY,
     117             :     [DO_COLLATION] = PRIO_COLLATION,
     118             :     [DO_CONVERSION] = PRIO_CONVERSION,
     119             :     [DO_TABLE] = PRIO_TABLE,
     120             :     [DO_TABLE_ATTACH] = PRIO_TABLE_ATTACH,
     121             :     [DO_ATTRDEF] = PRIO_ATTRDEF,
     122             :     [DO_INDEX] = PRIO_INDEX,
     123             :     [DO_INDEX_ATTACH] = PRIO_INDEX_ATTACH,
     124             :     [DO_STATSEXT] = PRIO_STATSEXT,
     125             :     [DO_RULE] = PRIO_RULE,
     126             :     [DO_TRIGGER] = PRIO_TRIGGER,
     127             :     [DO_CONSTRAINT] = PRIO_CONSTRAINT,
     128             :     [DO_FK_CONSTRAINT] = PRIO_FK_CONSTRAINT,
     129             :     [DO_PROCLANG] = PRIO_PROCLANG,
     130             :     [DO_CAST] = PRIO_CAST,
     131             :     [DO_TABLE_DATA] = PRIO_TABLE_DATA,
     132             :     [DO_SEQUENCE_SET] = PRIO_SEQUENCE_SET,
     133             :     [DO_DUMMY_TYPE] = PRIO_DUMMY_TYPE,
     134             :     [DO_TSPARSER] = PRIO_TSPARSER,
     135             :     [DO_TSDICT] = PRIO_TSDICT,
     136             :     [DO_TSTEMPLATE] = PRIO_TSTEMPLATE,
     137             :     [DO_TSCONFIG] = PRIO_TSCONFIG,
     138             :     [DO_FDW] = PRIO_FDW,
     139             :     [DO_FOREIGN_SERVER] = PRIO_FOREIGN_SERVER,
     140             :     [DO_DEFAULT_ACL] = PRIO_DEFAULT_ACL,
     141             :     [DO_TRANSFORM] = PRIO_TRANSFORM,
     142             :     [DO_LARGE_OBJECT] = PRIO_LARGE_OBJECT,
     143             :     [DO_LARGE_OBJECT_DATA] = PRIO_LARGE_OBJECT_DATA,
     144             :     [DO_PRE_DATA_BOUNDARY] = PRIO_PRE_DATA_BOUNDARY,
     145             :     [DO_POST_DATA_BOUNDARY] = PRIO_POST_DATA_BOUNDARY,
     146             :     [DO_EVENT_TRIGGER] = PRIO_EVENT_TRIGGER,
     147             :     [DO_REFRESH_MATVIEW] = PRIO_REFRESH_MATVIEW,
     148             :     [DO_POLICY] = PRIO_POLICY,
     149             :     [DO_PUBLICATION] = PRIO_PUBLICATION,
     150             :     [DO_PUBLICATION_REL] = PRIO_PUBLICATION_REL,
     151             :     [DO_PUBLICATION_TABLE_IN_SCHEMA] = PRIO_PUBLICATION_TABLE_IN_SCHEMA,
     152             :     [DO_REL_STATS] = PRIO_STATISTICS_DATA_DATA,
     153             :     [DO_SUBSCRIPTION] = PRIO_SUBSCRIPTION,
     154             :     [DO_SUBSCRIPTION_REL] = PRIO_SUBSCRIPTION_REL,
     155             : };
     156             : 
     157             : StaticAssertDecl(lengthof(dbObjectTypePriority) == NUM_DUMPABLE_OBJECT_TYPES,
     158             :                  "array length mismatch");
     159             : 
     160             : static DumpId preDataBoundId;
     161             : static DumpId postDataBoundId;
     162             : 
     163             : 
     164             : static int  DOTypeNameCompare(const void *p1, const void *p2);
     165             : static int  pgTypeNameCompare(Oid typid1, Oid typid2);
     166             : static int  accessMethodNameCompare(Oid am1, Oid am2);
     167             : static bool TopoSort(DumpableObject **objs,
     168             :                      int numObjs,
     169             :                      DumpableObject **ordering,
     170             :                      int *nOrdering);
     171             : static void findDependencyLoops(DumpableObject **objs, int nObjs, int totObjs);
     172             : static int  findLoop(DumpableObject *obj,
     173             :                      DumpId startPoint,
     174             :                      bool *processed,
     175             :                      DumpId *searchFailed,
     176             :                      DumpableObject **workspace,
     177             :                      int depth);
     178             : static void repairDependencyLoop(DumpableObject **loop,
     179             :                                  int nLoop);
     180             : static void describeDumpableObject(DumpableObject *obj,
     181             :                                    char *buf, int bufsize);
     182             : static int  int_cmp(void *a, void *b, void *arg);
     183             : 
     184             : 
     185             : /*
     186             :  * Sort the given objects into a type/name-based ordering
     187             :  *
     188             :  * Normally this is just the starting point for the dependency-based
     189             :  * ordering.
     190             :  */
     191             : void
     192         364 : sortDumpableObjectsByTypeName(DumpableObject **objs, int numObjs)
     193             : {
     194         364 :     if (numObjs > 1)
     195         364 :         qsort(objs, numObjs, sizeof(DumpableObject *),
     196             :               DOTypeNameCompare);
     197         364 : }
     198             : 
     199             : static int
     200    16966386 : DOTypeNameCompare(const void *p1, const void *p2)
     201             : {
     202    16966386 :     DumpableObject *obj1 = *(DumpableObject *const *) p1;
     203    16966386 :     DumpableObject *obj2 = *(DumpableObject *const *) p2;
     204             :     int         cmpval;
     205             : 
     206             :     /* Sort by type's priority */
     207    16966386 :     cmpval = dbObjectTypePriority[obj1->objType] -
     208    16966386 :         dbObjectTypePriority[obj2->objType];
     209             : 
     210    16966386 :     if (cmpval != 0)
     211     4240358 :         return cmpval;
     212             : 
     213             :     /*
     214             :      * Sort by namespace.  Typically, all objects of the same priority would
     215             :      * either have or not have a namespace link, but there are exceptions.
     216             :      * Sort NULL namespace after non-NULL in such cases.
     217             :      */
     218    12726028 :     if (obj1->namespace)
     219             :     {
     220    11979978 :         if (obj2->namespace)
     221             :         {
     222    11979852 :             cmpval = strcmp(obj1->namespace->dobj.name,
     223    11979852 :                             obj2->namespace->dobj.name);
     224    11979852 :             if (cmpval != 0)
     225      633840 :                 return cmpval;
     226             :         }
     227             :         else
     228         126 :             return -1;
     229             :     }
     230      746050 :     else if (obj2->namespace)
     231         170 :         return 1;
     232             : 
     233             :     /*
     234             :      * Sort by name.  With a few exceptions, names here are single catalog
     235             :      * columns.  To get a fuller picture, grep pg_dump.c for "dobj.name = ".
     236             :      * Names here don't match "Name:" in plain format output, which is a
     237             :      * _tocEntry.tag.  For example, DumpableObject.name of a constraint is
     238             :      * pg_constraint.conname, but _tocEntry.tag of a constraint is relname and
     239             :      * conname joined with a space.
     240             :      */
     241    12091892 :     cmpval = strcmp(obj1->name, obj2->name);
     242    12091892 :     if (cmpval != 0)
     243    10290582 :         return cmpval;
     244             : 
     245             :     /*
     246             :      * Sort by type.  This helps types that share a type priority without
     247             :      * sharing a unique name constraint, e.g. opclass and opfamily.
     248             :      */
     249     1801310 :     cmpval = obj1->objType - obj2->objType;
     250     1801310 :     if (cmpval != 0)
     251       67424 :         return cmpval;
     252             : 
     253             :     /*
     254             :      * To have a stable sort order, break ties for some object types.  Most
     255             :      * catalogs have a natural key, e.g. pg_proc_proname_args_nsp_index. Where
     256             :      * the above "namespace" and "name" comparisons don't cover all natural
     257             :      * key columns, compare the rest here.
     258             :      *
     259             :      * The natural key usually refers to other catalogs by surrogate keys.
     260             :      * Hence, this translates each of those references to the natural key of
     261             :      * the referenced catalog.  That may descend through multiple levels of
     262             :      * catalog references.  For example, to sort by pg_proc.proargtypes,
     263             :      * descend to each pg_type and then further to its pg_namespace, for an
     264             :      * overall sort by (nspname, typname).
     265             :      */
     266     1733886 :     if (obj1->objType == DO_FUNC || obj1->objType == DO_AGG)
     267           0 :     {
     268         134 :         FuncInfo   *fobj1 = *(FuncInfo *const *) p1;
     269         134 :         FuncInfo   *fobj2 = *(FuncInfo *const *) p2;
     270             :         int         i;
     271             : 
     272             :         /* Sort by number of arguments, then argument type names */
     273         134 :         cmpval = fobj1->nargs - fobj2->nargs;
     274         134 :         if (cmpval != 0)
     275          28 :             return cmpval;
     276         122 :         for (i = 0; i < fobj1->nargs; i++)
     277             :         {
     278         122 :             cmpval = pgTypeNameCompare(fobj1->argtypes[i],
     279         122 :                                        fobj2->argtypes[i]);
     280         122 :             if (cmpval != 0)
     281         106 :                 return cmpval;
     282             :         }
     283             :     }
     284     1733752 :     else if (obj1->objType == DO_OPERATOR)
     285             :     {
     286     1337222 :         OprInfo    *oobj1 = *(OprInfo *const *) p1;
     287     1337222 :         OprInfo    *oobj2 = *(OprInfo *const *) p2;
     288             : 
     289             :         /* oprkind is 'l', 'r', or 'b'; this sorts prefix, postfix, infix */
     290     1337222 :         cmpval = (oobj2->oprkind - oobj1->oprkind);
     291     1337222 :         if (cmpval != 0)
     292       34268 :             return cmpval;
     293             :         /* Within an oprkind, sort by argument type names */
     294     1302954 :         cmpval = pgTypeNameCompare(oobj1->oprleft, oobj2->oprleft);
     295     1302954 :         if (cmpval != 0)
     296     1148810 :             return cmpval;
     297      154144 :         cmpval = pgTypeNameCompare(oobj1->oprright, oobj2->oprright);
     298      154144 :         if (cmpval != 0)
     299      154144 :             return cmpval;
     300             :     }
     301      396530 :     else if (obj1->objType == DO_OPCLASS)
     302             :     {
     303       27510 :         OpclassInfo *opcobj1 = *(OpclassInfo *const *) p1;
     304       27510 :         OpclassInfo *opcobj2 = *(OpclassInfo *const *) p2;
     305             : 
     306             :         /* Sort by access method name, per pg_opclass_am_name_nsp_index */
     307       27510 :         cmpval = accessMethodNameCompare(opcobj1->opcmethod,
     308             :                                          opcobj2->opcmethod);
     309       27510 :         if (cmpval != 0)
     310       27510 :             return cmpval;
     311             :     }
     312      369020 :     else if (obj1->objType == DO_OPFAMILY)
     313             :     {
     314       22336 :         OpfamilyInfo *opfobj1 = *(OpfamilyInfo *const *) p1;
     315       22336 :         OpfamilyInfo *opfobj2 = *(OpfamilyInfo *const *) p2;
     316             : 
     317             :         /* Sort by access method name, per pg_opfamily_am_name_nsp_index */
     318       22336 :         cmpval = accessMethodNameCompare(opfobj1->opfmethod,
     319             :                                          opfobj2->opfmethod);
     320       22336 :         if (cmpval != 0)
     321       22336 :             return cmpval;
     322             :     }
     323      346684 :     else if (obj1->objType == DO_COLLATION)
     324             :     {
     325           0 :         CollInfo   *cobj1 = *(CollInfo *const *) p1;
     326           0 :         CollInfo   *cobj2 = *(CollInfo *const *) p2;
     327             : 
     328             :         /*
     329             :          * Sort by encoding, per pg_collation_name_enc_nsp_index. Technically,
     330             :          * this is not necessary, because wherever this changes dump order,
     331             :          * restoring the dump fails anyway.  CREATE COLLATION can't create a
     332             :          * tie for this to break, because it imposes restrictions to make
     333             :          * (nspname, collname) uniquely identify a collation within a given
     334             :          * DatabaseEncoding.  While pg_import_system_collations() can create a
     335             :          * tie, pg_dump+restore fails after
     336             :          * pg_import_system_collations('my_schema') does so. However, there's
     337             :          * little to gain by ignoring one natural key column on the basis of
     338             :          * those limitations elsewhere, so respect the full natural key like
     339             :          * we do for other object types.
     340             :          */
     341           0 :         cmpval = cobj1->collencoding - cobj2->collencoding;
     342           0 :         if (cmpval != 0)
     343           0 :             return cmpval;
     344             :     }
     345      346684 :     else if (obj1->objType == DO_ATTRDEF)
     346             :     {
     347         872 :         AttrDefInfo *adobj1 = *(AttrDefInfo *const *) p1;
     348         872 :         AttrDefInfo *adobj2 = *(AttrDefInfo *const *) p2;
     349             : 
     350             :         /* Sort by attribute number */
     351         872 :         cmpval = (adobj1->adnum - adobj2->adnum);
     352         872 :         if (cmpval != 0)
     353         872 :             return cmpval;
     354             :     }
     355      345812 :     else if (obj1->objType == DO_POLICY)
     356             :     {
     357          42 :         PolicyInfo *pobj1 = *(PolicyInfo *const *) p1;
     358          42 :         PolicyInfo *pobj2 = *(PolicyInfo *const *) p2;
     359             : 
     360             :         /* Sort by table name (table namespace was considered already) */
     361          42 :         cmpval = strcmp(pobj1->poltable->dobj.name,
     362          42 :                         pobj2->poltable->dobj.name);
     363          42 :         if (cmpval != 0)
     364          42 :             return cmpval;
     365             :     }
     366      345770 :     else if (obj1->objType == DO_RULE)
     367             :     {
     368      343776 :         RuleInfo   *robj1 = *(RuleInfo *const *) p1;
     369      343776 :         RuleInfo   *robj2 = *(RuleInfo *const *) p2;
     370             : 
     371             :         /* Sort by table name (table namespace was considered already) */
     372      343776 :         cmpval = strcmp(robj1->ruletable->dobj.name,
     373      343776 :                         robj2->ruletable->dobj.name);
     374      343776 :         if (cmpval != 0)
     375      343776 :             return cmpval;
     376             :     }
     377        1994 :     else if (obj1->objType == DO_TRIGGER)
     378             :     {
     379         680 :         TriggerInfo *tobj1 = *(TriggerInfo *const *) p1;
     380         680 :         TriggerInfo *tobj2 = *(TriggerInfo *const *) p2;
     381             : 
     382             :         /* Sort by table name (table namespace was considered already) */
     383         680 :         cmpval = strcmp(tobj1->tgtable->dobj.name,
     384         680 :                         tobj2->tgtable->dobj.name);
     385         680 :         if (cmpval != 0)
     386         680 :             return cmpval;
     387             :     }
     388        1314 :     else if (obj1->objType == DO_CONSTRAINT)
     389             :     {
     390         744 :         ConstraintInfo *robj1 = *(ConstraintInfo *const *) p1;
     391         744 :         ConstraintInfo *robj2 = *(ConstraintInfo *const *) p2;
     392             : 
     393             :         /*
     394             :          * Sort domain constraints before table constraints, for consistency
     395             :          * with our decision to sort CREATE DOMAIN before CREATE TABLE.
     396             :          */
     397         744 :         if (robj1->condomain)
     398             :         {
     399          16 :             if (robj2->condomain)
     400             :             {
     401             :                 /* Sort by domain name (domain namespace was considered) */
     402           0 :                 cmpval = strcmp(robj1->condomain->dobj.name,
     403           0 :                                 robj2->condomain->dobj.name);
     404           0 :                 if (cmpval != 0)
     405           0 :                     return cmpval;
     406             :             }
     407             :             else
     408          16 :                 return PRIO_TYPE - PRIO_TABLE;
     409             :         }
     410         728 :         else if (robj2->condomain)
     411          54 :             return PRIO_TABLE - PRIO_TYPE;
     412             :         else
     413             :         {
     414             :             /* Sort by table name (table namespace was considered already) */
     415         674 :             cmpval = strcmp(robj1->contable->dobj.name,
     416         674 :                             robj2->contable->dobj.name);
     417         674 :             if (cmpval != 0)
     418         674 :                 return cmpval;
     419             :         }
     420             :     }
     421         570 :     else if (obj1->objType == DO_DEFAULT_ACL)
     422             :     {
     423          22 :         DefaultACLInfo *daclobj1 = *(DefaultACLInfo *const *) p1;
     424          22 :         DefaultACLInfo *daclobj2 = *(DefaultACLInfo *const *) p2;
     425             : 
     426             :         /*
     427             :          * Sort by defaclrole, per pg_default_acl_role_nsp_obj_index.  The
     428             :          * (namespace, name) match (defaclnamespace, defaclobjtype).
     429             :          */
     430          22 :         cmpval = strcmp(daclobj1->defaclrole, daclobj2->defaclrole);
     431          22 :         if (cmpval != 0)
     432          22 :             return cmpval;
     433             :     }
     434         548 :     else if (obj1->objType == DO_PUBLICATION_REL)
     435             :     {
     436         482 :         PublicationRelInfo *probj1 = *(PublicationRelInfo *const *) p1;
     437         482 :         PublicationRelInfo *probj2 = *(PublicationRelInfo *const *) p2;
     438             : 
     439             :         /* Sort by publication name, since (namespace, name) match the rel */
     440         482 :         cmpval = strcmp(probj1->publication->dobj.name,
     441         482 :                         probj2->publication->dobj.name);
     442         482 :         if (cmpval != 0)
     443         482 :             return cmpval;
     444             :     }
     445          66 :     else if (obj1->objType == DO_PUBLICATION_TABLE_IN_SCHEMA)
     446             :     {
     447          66 :         PublicationSchemaInfo *psobj1 = *(PublicationSchemaInfo *const *) p1;
     448          66 :         PublicationSchemaInfo *psobj2 = *(PublicationSchemaInfo *const *) p2;
     449             : 
     450             :         /* Sort by publication name, since ->name is just nspname */
     451          66 :         cmpval = strcmp(psobj1->publication->dobj.name,
     452          66 :                         psobj2->publication->dobj.name);
     453          66 :         if (cmpval != 0)
     454          66 :             return cmpval;
     455             :     }
     456             : 
     457             :     /*
     458             :      * Shouldn't get here except after catalog corruption, but if we do, sort
     459             :      * by OID.  This may make logically-identical databases differ in the
     460             :      * order of objects in dump output.  Users will get spurious schema diffs.
     461             :      * Expect flaky failures of 002_pg_upgrade.pl test 'dump outputs from
     462             :      * original and restored regression databases match' if the regression
     463             :      * database contains objects allowing that test to reach here.  That's a
     464             :      * consequence of the test using "pg_restore -j", which doesn't fully
     465             :      * constrain OID assignment order.
     466             :      */
     467             :     Assert(false);
     468           0 :     return oidcmp(obj1->catId.oid, obj2->catId.oid);
     469             : }
     470             : 
     471             : /* Compare two OID-identified pg_type values by nspname, then by typname. */
     472             : static int
     473     1457220 : pgTypeNameCompare(Oid typid1, Oid typid2)
     474             : {
     475             :     TypeInfo   *typobj1;
     476             :     TypeInfo   *typobj2;
     477             :     int         cmpval;
     478             : 
     479     1457220 :     if (typid1 == typid2)
     480      154160 :         return 0;
     481             : 
     482     1303060 :     typobj1 = findTypeByOid(typid1);
     483     1303060 :     typobj2 = findTypeByOid(typid2);
     484             : 
     485     1303060 :     if (!typobj1 || !typobj2)
     486             :     {
     487             :         /*
     488             :          * getTypes() didn't find some OID.  Assume catalog corruption, e.g.
     489             :          * an oprright value without the corresponding OID in a pg_type row.
     490             :          * Report as "equal", so the caller uses the next available basis for
     491             :          * comparison, e.g. the next function argument.
     492             :          *
     493             :          * Unary operators have InvalidOid in oprleft (if oprkind='r') or in
     494             :          * oprright (if oprkind='l').  Caller already sorted by oprkind,
     495             :          * calling us only for like-kind operators.  Hence, "typid1 == typid2"
     496             :          * took care of InvalidOid.  (v14 removed postfix operator support.
     497             :          * Hence, when dumping from v14+, only oprleft can be InvalidOid.)
     498             :          */
     499             :         Assert(false);
     500           0 :         return 0;
     501             :     }
     502             : 
     503     1303060 :     if (!typobj1->dobj.namespace || !typobj2->dobj.namespace)
     504             :         Assert(false);          /* catalog corruption */
     505             :     else
     506             :     {
     507     1303060 :         cmpval = strcmp(typobj1->dobj.namespace->dobj.name,
     508     1303060 :                         typobj2->dobj.namespace->dobj.name);
     509     1303060 :         if (cmpval != 0)
     510          58 :             return cmpval;
     511             :     }
     512     1303002 :     return strcmp(typobj1->dobj.name, typobj2->dobj.name);
     513             : }
     514             : 
     515             : /* Compare two OID-identified pg_am values by amname. */
     516             : static int
     517       49846 : accessMethodNameCompare(Oid am1, Oid am2)
     518             : {
     519             :     AccessMethodInfo *amobj1;
     520             :     AccessMethodInfo *amobj2;
     521             : 
     522       49846 :     if (am1 == am2)
     523           0 :         return 0;
     524             : 
     525       49846 :     amobj1 = findAccessMethodByOid(am1);
     526       49846 :     amobj2 = findAccessMethodByOid(am2);
     527             : 
     528       49846 :     if (!amobj1 || !amobj2)
     529             :     {
     530             :         /* catalog corruption: handle like pgTypeNameCompare() does */
     531             :         Assert(false);
     532           0 :         return 0;
     533             :     }
     534             : 
     535       49846 :     return strcmp(amobj1->dobj.name, amobj2->dobj.name);
     536             : }
     537             : 
     538             : 
     539             : /*
     540             :  * Sort the given objects into a safe dump order using dependency
     541             :  * information (to the extent we have it available).
     542             :  *
     543             :  * The DumpIds of the PRE_DATA_BOUNDARY and POST_DATA_BOUNDARY objects are
     544             :  * passed in separately, in case we need them during dependency loop repair.
     545             :  */
     546             : void
     547         364 : sortDumpableObjects(DumpableObject **objs, int numObjs,
     548             :                     DumpId preBoundaryId, DumpId postBoundaryId)
     549             : {
     550             :     DumpableObject **ordering;
     551             :     int         nOrdering;
     552             : 
     553         364 :     if (numObjs <= 0)            /* can't happen anymore ... */
     554           0 :         return;
     555             : 
     556             :     /*
     557             :      * Saving the boundary IDs in static variables is a bit grotty, but seems
     558             :      * better than adding them to parameter lists of subsidiary functions.
     559             :      */
     560         364 :     preDataBoundId = preBoundaryId;
     561         364 :     postDataBoundId = postBoundaryId;
     562             : 
     563         364 :     ordering = (DumpableObject **) pg_malloc(numObjs * sizeof(DumpableObject *));
     564        1090 :     while (!TopoSort(objs, numObjs, ordering, &nOrdering))
     565         726 :         findDependencyLoops(ordering, nOrdering, numObjs);
     566             : 
     567         364 :     memcpy(objs, ordering, numObjs * sizeof(DumpableObject *));
     568             : 
     569         364 :     free(ordering);
     570             : }
     571             : 
     572             : /*
     573             :  * TopoSort -- topological sort of a dump list
     574             :  *
     575             :  * Generate a re-ordering of the dump list that satisfies all the dependency
     576             :  * constraints shown in the dump list.  (Each such constraint is a fact of a
     577             :  * partial ordering.)  Minimize rearrangement of the list not needed to
     578             :  * achieve the partial ordering.
     579             :  *
     580             :  * The input is the list of numObjs objects in objs[].  This list is not
     581             :  * modified.
     582             :  *
     583             :  * Returns true if able to build an ordering that satisfies all the
     584             :  * constraints, false if not (there are contradictory constraints).
     585             :  *
     586             :  * On success (true result), ordering[] is filled with a sorted array of
     587             :  * DumpableObject pointers, of length equal to the input list length.
     588             :  *
     589             :  * On failure (false result), ordering[] is filled with an unsorted array of
     590             :  * DumpableObject pointers of length *nOrdering, listing the objects that
     591             :  * prevented the sort from being completed.  In general, these objects either
     592             :  * participate directly in a dependency cycle, or are depended on by objects
     593             :  * that are in a cycle.  (The latter objects are not actually problematic,
     594             :  * but it takes further analysis to identify which are which.)
     595             :  *
     596             :  * The caller is responsible for allocating sufficient space at *ordering.
     597             :  */
     598             : static bool
     599        1090 : TopoSort(DumpableObject **objs,
     600             :          int numObjs,
     601             :          DumpableObject **ordering, /* output argument */
     602             :          int *nOrdering)        /* output argument */
     603             : {
     604        1090 :     DumpId      maxDumpId = getMaxDumpId();
     605             :     binaryheap *pendingHeap;
     606             :     int        *beforeConstraints;
     607             :     int        *idMap;
     608             :     DumpableObject *obj;
     609             :     int         i,
     610             :                 j,
     611             :                 k;
     612             : 
     613             :     /*
     614             :      * This is basically the same algorithm shown for topological sorting in
     615             :      * Knuth's Volume 1.  However, we would like to minimize unnecessary
     616             :      * rearrangement of the input ordering; that is, when we have a choice of
     617             :      * which item to output next, we always want to take the one highest in
     618             :      * the original list.  Therefore, instead of maintaining an unordered
     619             :      * linked list of items-ready-to-output as Knuth does, we maintain a heap
     620             :      * of their item numbers, which we can use as a priority queue.  This
     621             :      * turns the algorithm from O(N) to O(N log N) because each insertion or
     622             :      * removal of a heap item takes O(log N) time.  However, that's still
     623             :      * plenty fast enough for this application.
     624             :      */
     625             : 
     626        1090 :     *nOrdering = numObjs;       /* for success return */
     627             : 
     628             :     /* Eliminate the null case */
     629        1090 :     if (numObjs <= 0)
     630           0 :         return true;
     631             : 
     632             :     /* Create workspace for the above-described heap */
     633        1090 :     pendingHeap = binaryheap_allocate(numObjs, int_cmp, NULL);
     634             : 
     635             :     /*
     636             :      * Scan the constraints, and for each item in the input, generate a count
     637             :      * of the number of constraints that say it must be before something else.
     638             :      * The count for the item with dumpId j is stored in beforeConstraints[j].
     639             :      * We also make a map showing the input-order index of the item with
     640             :      * dumpId j.
     641             :      */
     642        1090 :     beforeConstraints = (int *) pg_malloc0((maxDumpId + 1) * sizeof(int));
     643        1090 :     idMap = (int *) pg_malloc((maxDumpId + 1) * sizeof(int));
     644     4321708 :     for (i = 0; i < numObjs; i++)
     645             :     {
     646     4320618 :         obj = objs[i];
     647     4320618 :         j = obj->dumpId;
     648     4320618 :         if (j <= 0 || j > maxDumpId)
     649           0 :             pg_fatal("invalid dumpId %d", j);
     650     4320618 :         idMap[j] = i;
     651    11219562 :         for (j = 0; j < obj->nDeps; j++)
     652             :         {
     653     6898944 :             k = obj->dependencies[j];
     654     6898944 :             if (k <= 0 || k > maxDumpId)
     655           0 :                 pg_fatal("invalid dependency %d", k);
     656     6898944 :             beforeConstraints[k]++;
     657             :         }
     658             :     }
     659             : 
     660             :     /*
     661             :      * Now initialize the heap of items-ready-to-output by filling it with the
     662             :      * indexes of items that already have beforeConstraints[id] == 0.
     663             :      *
     664             :      * We enter the indexes into pendingHeap in decreasing order so that the
     665             :      * heap invariant is satisfied at the completion of this loop.  This
     666             :      * reduces the amount of work that binaryheap_build() must do.
     667             :      */
     668     4321708 :     for (i = numObjs; --i >= 0;)
     669             :     {
     670     4320618 :         if (beforeConstraints[objs[i]->dumpId] == 0)
     671       63994 :             binaryheap_add_unordered(pendingHeap, (void *) (intptr_t) i);
     672             :     }
     673        1090 :     binaryheap_build(pendingHeap);
     674             : 
     675             :     /*--------------------
     676             :      * Now emit objects, working backwards in the output list.  At each step,
     677             :      * we use the priority heap to select the last item that has no remaining
     678             :      * before-constraints.  We remove that item from the heap, output it to
     679             :      * ordering[], and decrease the beforeConstraints count of each of the
     680             :      * items it was constrained against.  Whenever an item's beforeConstraints
     681             :      * count is thereby decreased to zero, we insert it into the priority heap
     682             :      * to show that it is a candidate to output.  We are done when the heap
     683             :      * becomes empty; if we have output every element then we succeeded,
     684             :      * otherwise we failed.
     685             :      * i = number of ordering[] entries left to output
     686             :      * j = objs[] index of item we are outputting
     687             :      * k = temp for scanning constraint list for item j
     688             :      *--------------------
     689             :      */
     690        1090 :     i = numObjs;
     691     2734594 :     while (!binaryheap_empty(pendingHeap))
     692             :     {
     693             :         /* Select object to output by removing largest heap member */
     694     2733504 :         j = (int) (intptr_t) binaryheap_remove_first(pendingHeap);
     695     2733504 :         obj = objs[j];
     696             :         /* Output candidate to ordering[] */
     697     2733504 :         ordering[--i] = obj;
     698             :         /* Update beforeConstraints counts of its predecessors */
     699     6831344 :         for (k = 0; k < obj->nDeps; k++)
     700             :         {
     701     4097840 :             int         id = obj->dependencies[k];
     702             : 
     703     4097840 :             if ((--beforeConstraints[id]) == 0)
     704     2669510 :                 binaryheap_add(pendingHeap, (void *) (intptr_t) idMap[id]);
     705             :         }
     706             :     }
     707             : 
     708             :     /*
     709             :      * If we failed, report the objects that couldn't be output; these are the
     710             :      * ones with beforeConstraints[] still nonzero.
     711             :      */
     712        1090 :     if (i != 0)
     713             :     {
     714         726 :         k = 0;
     715     2966202 :         for (j = 1; j <= maxDumpId; j++)
     716             :         {
     717     2965476 :             if (beforeConstraints[j] != 0)
     718     1587114 :                 ordering[k++] = objs[idMap[j]];
     719             :         }
     720         726 :         *nOrdering = k;
     721             :     }
     722             : 
     723             :     /* Done */
     724        1090 :     binaryheap_free(pendingHeap);
     725        1090 :     free(beforeConstraints);
     726        1090 :     free(idMap);
     727             : 
     728        1090 :     return (i == 0);
     729             : }
     730             : 
     731             : /*
     732             :  * findDependencyLoops - identify loops in TopoSort's failure output,
     733             :  *      and pass each such loop to repairDependencyLoop() for action
     734             :  *
     735             :  * In general there may be many loops in the set of objects returned by
     736             :  * TopoSort; for speed we should try to repair as many loops as we can
     737             :  * before trying TopoSort again.  We can safely repair loops that are
     738             :  * disjoint (have no members in common); if we find overlapping loops
     739             :  * then we repair only the first one found, because the action taken to
     740             :  * repair the first might have repaired the other as well.  (If not,
     741             :  * we'll fix it on the next go-round.)
     742             :  *
     743             :  * objs[] lists the objects TopoSort couldn't sort
     744             :  * nObjs is the number of such objects
     745             :  * totObjs is the total number of objects in the universe
     746             :  */
     747             : static void
     748         726 : findDependencyLoops(DumpableObject **objs, int nObjs, int totObjs)
     749             : {
     750             :     /*
     751             :      * We use three data structures here:
     752             :      *
     753             :      * processed[] is a bool array indexed by dump ID, marking the objects
     754             :      * already processed during this invocation of findDependencyLoops().
     755             :      *
     756             :      * searchFailed[] is another array indexed by dump ID.  searchFailed[j] is
     757             :      * set to dump ID k if we have proven that there is no dependency path
     758             :      * leading from object j back to start point k.  This allows us to skip
     759             :      * useless searching when there are multiple dependency paths from k to j,
     760             :      * which is a common situation.  We could use a simple bool array for
     761             :      * this, but then we'd need to re-zero it for each start point, resulting
     762             :      * in O(N^2) zeroing work.  Using the start point's dump ID as the "true"
     763             :      * value lets us skip clearing the array before we consider the next start
     764             :      * point.
     765             :      *
     766             :      * workspace[] is an array of DumpableObject pointers, in which we try to
     767             :      * build lists of objects constituting loops.  We make workspace[] large
     768             :      * enough to hold all the objects in TopoSort's output, which is huge
     769             :      * overkill in most cases but could theoretically be necessary if there is
     770             :      * a single dependency chain linking all the objects.
     771             :      */
     772             :     bool       *processed;
     773             :     DumpId     *searchFailed;
     774             :     DumpableObject **workspace;
     775             :     bool        fixedloop;
     776             :     int         i;
     777             : 
     778         726 :     processed = (bool *) pg_malloc0((getMaxDumpId() + 1) * sizeof(bool));
     779         726 :     searchFailed = (DumpId *) pg_malloc0((getMaxDumpId() + 1) * sizeof(DumpId));
     780         726 :     workspace = (DumpableObject **) pg_malloc(totObjs * sizeof(DumpableObject *));
     781         726 :     fixedloop = false;
     782             : 
     783     1587840 :     for (i = 0; i < nObjs; i++)
     784             :     {
     785     1587114 :         DumpableObject *obj = objs[i];
     786             :         int         looplen;
     787             :         int         j;
     788             : 
     789     1587114 :         looplen = findLoop(obj,
     790             :                            obj->dumpId,
     791             :                            processed,
     792             :                            searchFailed,
     793             :                            workspace,
     794             :                            0);
     795             : 
     796     1587114 :         if (looplen > 0)
     797             :         {
     798             :             /* Found a loop, repair it */
     799       61252 :             repairDependencyLoop(workspace, looplen);
     800       61252 :             fixedloop = true;
     801             :             /* Mark loop members as processed */
     802      184048 :             for (j = 0; j < looplen; j++)
     803      122796 :                 processed[workspace[j]->dumpId] = true;
     804             :         }
     805             :         else
     806             :         {
     807             :             /*
     808             :              * There's no loop starting at this object, but mark it processed
     809             :              * anyway.  This is not necessary for correctness, but saves later
     810             :              * invocations of findLoop() from uselessly chasing references to
     811             :              * such an object.
     812             :              */
     813     1525862 :             processed[obj->dumpId] = true;
     814             :         }
     815             :     }
     816             : 
     817             :     /* We'd better have fixed at least one loop */
     818         726 :     if (!fixedloop)
     819           0 :         pg_fatal("could not identify dependency loop");
     820             : 
     821         726 :     free(workspace);
     822         726 :     free(searchFailed);
     823         726 :     free(processed);
     824         726 : }
     825             : 
     826             : /*
     827             :  * Recursively search for a circular dependency loop that doesn't include
     828             :  * any already-processed objects.
     829             :  *
     830             :  *  obj: object we are examining now
     831             :  *  startPoint: dumpId of starting object for the hoped-for circular loop
     832             :  *  processed[]: flag array marking already-processed objects
     833             :  *  searchFailed[]: flag array marking already-unsuccessfully-visited objects
     834             :  *  workspace[]: work array in which we are building list of loop members
     835             :  *  depth: number of valid entries in workspace[] at call
     836             :  *
     837             :  * On success, the length of the loop is returned, and workspace[] is filled
     838             :  * with pointers to the members of the loop.  On failure, we return 0.
     839             :  *
     840             :  * Note: it is possible that the given starting object is a member of more
     841             :  * than one cycle; if so, we will find an arbitrary one of the cycles.
     842             :  */
     843             : static int
     844    45464680 : findLoop(DumpableObject *obj,
     845             :          DumpId startPoint,
     846             :          bool *processed,
     847             :          DumpId *searchFailed,
     848             :          DumpableObject **workspace,
     849             :          int depth)
     850             : {
     851             :     int         i;
     852             : 
     853             :     /*
     854             :      * Reject if obj is already processed.  This test prevents us from finding
     855             :      * loops that overlap previously-processed loops.
     856             :      */
     857    45464680 :     if (processed[obj->dumpId])
     858    42230656 :         return 0;
     859             : 
     860             :     /*
     861             :      * If we've already proven there is no path from this object back to the
     862             :      * startPoint, forget it.
     863             :      */
     864     3234024 :     if (searchFailed[obj->dumpId] == startPoint)
     865      340472 :         return 0;
     866             : 
     867             :     /*
     868             :      * Reject if obj is already present in workspace.  This test prevents us
     869             :      * from going into infinite recursion if we are given a startPoint object
     870             :      * that links to a cycle it's not a member of, and it guarantees that we
     871             :      * can't overflow the allocated size of workspace[].
     872             :      */
     873     5592688 :     for (i = 0; i < depth; i++)
     874             :     {
     875     2704624 :         if (workspace[i] == obj)
     876        5488 :             return 0;
     877             :     }
     878             : 
     879             :     /*
     880             :      * Okay, tentatively add obj to workspace
     881             :      */
     882     2888064 :     workspace[depth++] = obj;
     883             : 
     884             :     /*
     885             :      * See if we've found a loop back to the desired startPoint; if so, done
     886             :      */
     887    48034002 :     for (i = 0; i < obj->nDeps; i++)
     888             :     {
     889    45207190 :         if (obj->dependencies[i] == startPoint)
     890       61252 :             return depth;
     891             :     }
     892             : 
     893             :     /*
     894             :      * Recurse down each outgoing branch
     895             :      */
     896    46642834 :     for (i = 0; i < obj->nDeps; i++)
     897             :     {
     898    43877566 :         DumpableObject *nextobj = findObjectByDumpId(obj->dependencies[i]);
     899             :         int         newDepth;
     900             : 
     901    43877566 :         if (!nextobj)
     902           0 :             continue;           /* ignore dependencies on undumped objects */
     903    43877566 :         newDepth = findLoop(nextobj,
     904             :                             startPoint,
     905             :                             processed,
     906             :                             searchFailed,
     907             :                             workspace,
     908             :                             depth);
     909    43877566 :         if (newDepth > 0)
     910       61544 :             return newDepth;
     911             :     }
     912             : 
     913             :     /*
     914             :      * Remember there is no path from here back to startPoint
     915             :      */
     916     2765268 :     searchFailed[obj->dumpId] = startPoint;
     917             : 
     918     2765268 :     return 0;
     919             : }
     920             : 
     921             : /*
     922             :  * A user-defined datatype will have a dependency loop with each of its
     923             :  * I/O functions (since those have the datatype as input or output).
     924             :  * Similarly, a range type will have a loop with its canonicalize function,
     925             :  * if any.  Break the loop by making the function depend on the associated
     926             :  * shell type, instead.
     927             :  */
     928             : static void
     929         388 : repairTypeFuncLoop(DumpableObject *typeobj, DumpableObject *funcobj)
     930             : {
     931         388 :     TypeInfo   *typeInfo = (TypeInfo *) typeobj;
     932             : 
     933             :     /* remove function's dependency on type */
     934         388 :     removeObjectDependency(funcobj, typeobj->dumpId);
     935             : 
     936             :     /* add function's dependency on shell type, instead */
     937         388 :     if (typeInfo->shellType)
     938             :     {
     939         304 :         addObjectDependency(funcobj, typeInfo->shellType->dobj.dumpId);
     940             : 
     941             :         /*
     942             :          * Mark shell type (always including the definition, as we need the
     943             :          * shell type defined to identify the function fully) as to be dumped
     944             :          * if any such function is
     945             :          */
     946         304 :         if (funcobj->dump)
     947         304 :             typeInfo->shellType->dobj.dump = funcobj->dump |
     948             :                 DUMP_COMPONENT_DEFINITION;
     949             :     }
     950         388 : }
     951             : 
     952             : /*
     953             :  * Because we force a view to depend on its ON SELECT rule, while there
     954             :  * will be an implicit dependency in the other direction, we need to break
     955             :  * the loop.  If there are no other objects in the loop then we can remove
     956             :  * the implicit dependency and leave the ON SELECT rule non-separate.
     957             :  * This applies to matviews, as well.
     958             :  */
     959             : static void
     960       55024 : repairViewRuleLoop(DumpableObject *viewobj,
     961             :                    DumpableObject *ruleobj)
     962             : {
     963             :     /* remove rule's dependency on view */
     964       55024 :     removeObjectDependency(ruleobj, viewobj->dumpId);
     965             :     /* flags on the two objects are already set correctly for this case */
     966       55024 : }
     967             : 
     968             : /*
     969             :  * However, if there are other objects in the loop, we must break the loop
     970             :  * by making the ON SELECT rule a separately-dumped object.
     971             :  *
     972             :  * Because findLoop() finds shorter cycles before longer ones, it's likely
     973             :  * that we will have previously fired repairViewRuleLoop() and removed the
     974             :  * rule's dependency on the view.  Put it back to ensure the rule won't be
     975             :  * emitted before the view.
     976             :  *
     977             :  * Note: this approach does *not* work for matviews, at the moment.
     978             :  */
     979             : static void
     980          20 : repairViewRuleMultiLoop(DumpableObject *viewobj,
     981             :                         DumpableObject *ruleobj)
     982             : {
     983          20 :     TableInfo  *viewinfo = (TableInfo *) viewobj;
     984          20 :     RuleInfo   *ruleinfo = (RuleInfo *) ruleobj;
     985             : 
     986             :     /* remove view's dependency on rule */
     987          20 :     removeObjectDependency(viewobj, ruleobj->dumpId);
     988             :     /* mark view to be printed with a dummy definition */
     989          20 :     viewinfo->dummy_view = true;
     990             :     /* mark rule as needing its own dump */
     991          20 :     ruleinfo->separate = true;
     992             :     /* put back rule's dependency on view */
     993          20 :     addObjectDependency(ruleobj, viewobj->dumpId);
     994             :     /* now that rule is separate, it must be post-data */
     995          20 :     addObjectDependency(ruleobj, postDataBoundId);
     996          20 : }
     997             : 
     998             : /*
     999             :  * If a matview is involved in a multi-object loop, we can't currently fix
    1000             :  * that by splitting off the rule.  As a stopgap, we try to fix it by
    1001             :  * dropping the constraint that the matview be dumped in the pre-data section.
    1002             :  * This is sufficient to handle cases where a matview depends on some unique
    1003             :  * index, as can happen if it has a GROUP BY for example.
    1004             :  *
    1005             :  * Note that the "next object" is not necessarily the matview itself;
    1006             :  * it could be the matview's rowtype, for example.  We may come through here
    1007             :  * several times while removing all the pre-data linkages.  In particular,
    1008             :  * if there are other matviews that depend on the one with the circularity
    1009             :  * problem, we'll come through here for each such matview and mark them all
    1010             :  * as postponed.  (This works because all MVs have pre-data dependencies
    1011             :  * to begin with, so each of them will get visited.)
    1012             :  */
    1013             : static void
    1014         252 : repairMatViewBoundaryMultiLoop(DumpableObject *boundaryobj,
    1015             :                                DumpableObject *nextobj)
    1016             : {
    1017             :     /* remove boundary's dependency on object after it in loop */
    1018         252 :     removeObjectDependency(boundaryobj, nextobj->dumpId);
    1019             : 
    1020             :     /*
    1021             :      * If that object is a matview or matview stats, mark it as postponed into
    1022             :      * post-data.
    1023             :      */
    1024         252 :     if (nextobj->objType == DO_TABLE)
    1025             :     {
    1026          82 :         TableInfo  *nextinfo = (TableInfo *) nextobj;
    1027             : 
    1028          82 :         if (nextinfo->relkind == RELKIND_MATVIEW)
    1029          82 :             nextinfo->postponed_def = true;
    1030             :     }
    1031         170 :     else if (nextobj->objType == DO_REL_STATS)
    1032             :     {
    1033           6 :         RelStatsInfo *nextinfo = (RelStatsInfo *) nextobj;
    1034             : 
    1035           6 :         if (nextinfo->relkind == RELKIND_MATVIEW)
    1036           6 :             nextinfo->section = SECTION_POST_DATA;
    1037             :     }
    1038         252 : }
    1039             : 
    1040             : /*
    1041             :  * If a function is involved in a multi-object loop, we can't currently fix
    1042             :  * that by splitting it into two DumpableObjects.  As a stopgap, we try to fix
    1043             :  * it by dropping the constraint that the function be dumped in the pre-data
    1044             :  * section.  This is sufficient to handle cases where a function depends on
    1045             :  * some unique index, as can happen if it has a GROUP BY for example.
    1046             :  */
    1047             : static void
    1048          82 : repairFunctionBoundaryMultiLoop(DumpableObject *boundaryobj,
    1049             :                                 DumpableObject *nextobj)
    1050             : {
    1051             :     /* remove boundary's dependency on object after it in loop */
    1052          82 :     removeObjectDependency(boundaryobj, nextobj->dumpId);
    1053             :     /* if that object is a function, mark it as postponed into post-data */
    1054          82 :     if (nextobj->objType == DO_FUNC)
    1055             :     {
    1056          82 :         FuncInfo   *nextinfo = (FuncInfo *) nextobj;
    1057             : 
    1058          82 :         nextinfo->postponed_def = true;
    1059             :     }
    1060          82 : }
    1061             : 
    1062             : /*
    1063             :  * Because we make tables depend on their CHECK constraints, while there
    1064             :  * will be an automatic dependency in the other direction, we need to break
    1065             :  * the loop.  If there are no other objects in the loop then we can remove
    1066             :  * the automatic dependency and leave the CHECK constraint non-separate.
    1067             :  */
    1068             : static void
    1069        1174 : repairTableConstraintLoop(DumpableObject *tableobj,
    1070             :                           DumpableObject *constraintobj)
    1071             : {
    1072             :     /* remove constraint's dependency on table */
    1073        1174 :     removeObjectDependency(constraintobj, tableobj->dumpId);
    1074        1174 : }
    1075             : 
    1076             : /*
    1077             :  * However, if there are other objects in the loop, we must break the loop
    1078             :  * by making the CHECK constraint a separately-dumped object.
    1079             :  *
    1080             :  * Because findLoop() finds shorter cycles before longer ones, it's likely
    1081             :  * that we will have previously fired repairTableConstraintLoop() and
    1082             :  * removed the constraint's dependency on the table.  Put it back to ensure
    1083             :  * the constraint won't be emitted before the table...
    1084             :  */
    1085             : static void
    1086          10 : repairTableConstraintMultiLoop(DumpableObject *tableobj,
    1087             :                                DumpableObject *constraintobj)
    1088             : {
    1089             :     /* remove table's dependency on constraint */
    1090          10 :     removeObjectDependency(tableobj, constraintobj->dumpId);
    1091             :     /* mark constraint as needing its own dump */
    1092          10 :     ((ConstraintInfo *) constraintobj)->separate = true;
    1093             :     /* put back constraint's dependency on table */
    1094          10 :     addObjectDependency(constraintobj, tableobj->dumpId);
    1095             :     /* now that constraint is separate, it must be post-data */
    1096          10 :     addObjectDependency(constraintobj, postDataBoundId);
    1097          10 : }
    1098             : 
    1099             : /*
    1100             :  * Attribute defaults behave exactly the same as CHECK constraints...
    1101             :  */
    1102             : static void
    1103        2042 : repairTableAttrDefLoop(DumpableObject *tableobj,
    1104             :                        DumpableObject *attrdefobj)
    1105             : {
    1106             :     /* remove attrdef's dependency on table */
    1107        2042 :     removeObjectDependency(attrdefobj, tableobj->dumpId);
    1108        2042 : }
    1109             : 
    1110             : static void
    1111         316 : repairTableAttrDefMultiLoop(DumpableObject *tableobj,
    1112             :                             DumpableObject *attrdefobj)
    1113             : {
    1114             :     /* remove table's dependency on attrdef */
    1115         316 :     removeObjectDependency(tableobj, attrdefobj->dumpId);
    1116             :     /* mark attrdef as needing its own dump */
    1117         316 :     ((AttrDefInfo *) attrdefobj)->separate = true;
    1118             :     /* put back attrdef's dependency on table */
    1119         316 :     addObjectDependency(attrdefobj, tableobj->dumpId);
    1120         316 : }
    1121             : 
    1122             : /*
    1123             :  * CHECK, NOT NULL constraints on domains work just like those on tables ...
    1124             :  */
    1125             : static void
    1126         334 : repairDomainConstraintLoop(DumpableObject *domainobj,
    1127             :                            DumpableObject *constraintobj)
    1128             : {
    1129             :     /* remove constraint's dependency on domain */
    1130         334 :     removeObjectDependency(constraintobj, domainobj->dumpId);
    1131         334 : }
    1132             : 
    1133             : static void
    1134           0 : repairDomainConstraintMultiLoop(DumpableObject *domainobj,
    1135             :                                 DumpableObject *constraintobj)
    1136             : {
    1137             :     /* remove domain's dependency on constraint */
    1138           0 :     removeObjectDependency(domainobj, constraintobj->dumpId);
    1139             :     /* mark constraint as needing its own dump */
    1140           0 :     ((ConstraintInfo *) constraintobj)->separate = true;
    1141             :     /* put back constraint's dependency on domain */
    1142           0 :     addObjectDependency(constraintobj, domainobj->dumpId);
    1143             :     /* now that constraint is separate, it must be post-data */
    1144           0 :     addObjectDependency(constraintobj, postDataBoundId);
    1145           0 : }
    1146             : 
    1147             : static void
    1148           0 : repairIndexLoop(DumpableObject *partedindex,
    1149             :                 DumpableObject *partindex)
    1150             : {
    1151           0 :     removeObjectDependency(partedindex, partindex->dumpId);
    1152           0 : }
    1153             : 
    1154             : /*
    1155             :  * Fix a dependency loop, or die trying ...
    1156             :  *
    1157             :  * This routine is mainly concerned with reducing the multiple ways that
    1158             :  * a loop might appear to common cases, which it passes off to the
    1159             :  * "fixer" routines above.
    1160             :  */
    1161             : static void
    1162       61252 : repairDependencyLoop(DumpableObject **loop,
    1163             :                      int nLoop)
    1164             : {
    1165             :     int         i,
    1166             :                 j;
    1167             : 
    1168             :     /* Datatype and one of its I/O or canonicalize functions */
    1169       61252 :     if (nLoop == 2 &&
    1170       58962 :         loop[0]->objType == DO_TYPE &&
    1171         334 :         loop[1]->objType == DO_FUNC)
    1172             :     {
    1173           0 :         repairTypeFuncLoop(loop[0], loop[1]);
    1174           0 :         return;
    1175             :     }
    1176       61252 :     if (nLoop == 2 &&
    1177       58962 :         loop[1]->objType == DO_TYPE &&
    1178         388 :         loop[0]->objType == DO_FUNC)
    1179             :     {
    1180         388 :         repairTypeFuncLoop(loop[1], loop[0]);
    1181         388 :         return;
    1182             :     }
    1183             : 
    1184             :     /* View (including matview) and its ON SELECT rule */
    1185       60864 :     if (nLoop == 2 &&
    1186       58574 :         loop[0]->objType == DO_TABLE &&
    1187       58240 :         loop[1]->objType == DO_RULE &&
    1188       55024 :         (((TableInfo *) loop[0])->relkind == RELKIND_VIEW ||
    1189        1072 :          ((TableInfo *) loop[0])->relkind == RELKIND_MATVIEW) &&
    1190       55024 :         ((RuleInfo *) loop[1])->ev_type == '1' &&
    1191       55024 :         ((RuleInfo *) loop[1])->is_instead &&
    1192       55024 :         ((RuleInfo *) loop[1])->ruletable == (TableInfo *) loop[0])
    1193             :     {
    1194       55024 :         repairViewRuleLoop(loop[0], loop[1]);
    1195       55024 :         return;
    1196             :     }
    1197        5840 :     if (nLoop == 2 &&
    1198        3550 :         loop[1]->objType == DO_TABLE &&
    1199           0 :         loop[0]->objType == DO_RULE &&
    1200           0 :         (((TableInfo *) loop[1])->relkind == RELKIND_VIEW ||
    1201           0 :          ((TableInfo *) loop[1])->relkind == RELKIND_MATVIEW) &&
    1202           0 :         ((RuleInfo *) loop[0])->ev_type == '1' &&
    1203           0 :         ((RuleInfo *) loop[0])->is_instead &&
    1204           0 :         ((RuleInfo *) loop[0])->ruletable == (TableInfo *) loop[1])
    1205             :     {
    1206           0 :         repairViewRuleLoop(loop[1], loop[0]);
    1207           0 :         return;
    1208             :     }
    1209             : 
    1210             :     /* Indirect loop involving view (but not matview) and ON SELECT rule */
    1211        5840 :     if (nLoop > 2)
    1212             :     {
    1213        3830 :         for (i = 0; i < nLoop; i++)
    1214             :         {
    1215        3170 :             if (loop[i]->objType == DO_TABLE &&
    1216         914 :                 ((TableInfo *) loop[i])->relkind == RELKIND_VIEW)
    1217             :             {
    1218          48 :                 for (j = 0; j < nLoop; j++)
    1219             :                 {
    1220          48 :                     if (loop[j]->objType == DO_RULE &&
    1221          20 :                         ((RuleInfo *) loop[j])->ev_type == '1' &&
    1222          20 :                         ((RuleInfo *) loop[j])->is_instead &&
    1223          20 :                         ((RuleInfo *) loop[j])->ruletable == (TableInfo *) loop[i])
    1224             :                     {
    1225          20 :                         repairViewRuleMultiLoop(loop[i], loop[j]);
    1226          20 :                         return;
    1227             :                     }
    1228             :                 }
    1229             :             }
    1230             :         }
    1231             :     }
    1232             : 
    1233             :     /* Indirect loop involving matview and data boundary */
    1234        5820 :     if (nLoop > 2)
    1235             :     {
    1236        2710 :         for (i = 0; i < nLoop; i++)
    1237             :         {
    1238        2302 :             if (loop[i]->objType == DO_TABLE &&
    1239         894 :                 ((TableInfo *) loop[i])->relkind == RELKIND_MATVIEW)
    1240             :             {
    1241         666 :                 for (j = 0; j < nLoop; j++)
    1242             :                 {
    1243         660 :                     if (loop[j]->objType == DO_PRE_DATA_BOUNDARY)
    1244             :                     {
    1245             :                         DumpableObject *nextobj;
    1246             : 
    1247         246 :                         nextobj = (j < nLoop - 1) ? loop[j + 1] : loop[0];
    1248         246 :                         repairMatViewBoundaryMultiLoop(loop[j], nextobj);
    1249         246 :                         return;
    1250             :                     }
    1251             :                 }
    1252             :             }
    1253        2050 :             else if (loop[i]->objType == DO_REL_STATS &&
    1254         286 :                      ((RelStatsInfo *) loop[i])->relkind == RELKIND_MATVIEW)
    1255             :             {
    1256          24 :                 for (j = 0; j < nLoop; j++)
    1257             :                 {
    1258          24 :                     if (loop[j]->objType == DO_POST_DATA_BOUNDARY)
    1259             :                     {
    1260             :                         DumpableObject *nextobj;
    1261             : 
    1262           6 :                         nextobj = (j < nLoop - 1) ? loop[j + 1] : loop[0];
    1263           6 :                         repairMatViewBoundaryMultiLoop(loop[j], nextobj);
    1264           6 :                         return;
    1265             :                     }
    1266             :                 }
    1267             :             }
    1268             :         }
    1269             :     }
    1270             : 
    1271             :     /* Indirect loop involving function and data boundary */
    1272        5568 :     if (nLoop > 2)
    1273             :     {
    1274        1536 :         for (i = 0; i < nLoop; i++)
    1275             :         {
    1276        1210 :             if (loop[i]->objType == DO_FUNC)
    1277             :             {
    1278         248 :                 for (j = 0; j < nLoop; j++)
    1279             :                 {
    1280         238 :                     if (loop[j]->objType == DO_PRE_DATA_BOUNDARY)
    1281             :                     {
    1282             :                         DumpableObject *nextobj;
    1283             : 
    1284          82 :                         nextobj = (j < nLoop - 1) ? loop[j + 1] : loop[0];
    1285          82 :                         repairFunctionBoundaryMultiLoop(loop[j], nextobj);
    1286          82 :                         return;
    1287             :                     }
    1288             :                 }
    1289             :             }
    1290             :         }
    1291             :     }
    1292             : 
    1293             :     /* Table and CHECK constraint */
    1294        5486 :     if (nLoop == 2 &&
    1295        3550 :         loop[0]->objType == DO_TABLE &&
    1296        3216 :         loop[1]->objType == DO_CONSTRAINT &&
    1297        1174 :         ((ConstraintInfo *) loop[1])->contype == 'c' &&
    1298        1174 :         ((ConstraintInfo *) loop[1])->contable == (TableInfo *) loop[0])
    1299             :     {
    1300        1174 :         repairTableConstraintLoop(loop[0], loop[1]);
    1301        1174 :         return;
    1302             :     }
    1303        4312 :     if (nLoop == 2 &&
    1304        2376 :         loop[1]->objType == DO_TABLE &&
    1305           0 :         loop[0]->objType == DO_CONSTRAINT &&
    1306           0 :         ((ConstraintInfo *) loop[0])->contype == 'c' &&
    1307           0 :         ((ConstraintInfo *) loop[0])->contable == (TableInfo *) loop[1])
    1308             :     {
    1309           0 :         repairTableConstraintLoop(loop[1], loop[0]);
    1310           0 :         return;
    1311             :     }
    1312             : 
    1313             :     /* Indirect loop involving table and CHECK constraint */
    1314        4312 :     if (nLoop > 2)
    1315             :     {
    1316        1274 :         for (i = 0; i < nLoop; i++)
    1317             :         {
    1318         958 :             if (loop[i]->objType == DO_TABLE)
    1319             :             {
    1320        2548 :                 for (j = 0; j < nLoop; j++)
    1321             :                 {
    1322        1916 :                     if (loop[j]->objType == DO_CONSTRAINT &&
    1323          10 :                         ((ConstraintInfo *) loop[j])->contype == 'c' &&
    1324          10 :                         ((ConstraintInfo *) loop[j])->contable == (TableInfo *) loop[i])
    1325             :                     {
    1326          10 :                         repairTableConstraintMultiLoop(loop[i], loop[j]);
    1327          10 :                         return;
    1328             :                     }
    1329             :                 }
    1330             :             }
    1331             :         }
    1332             :     }
    1333             : 
    1334             :     /* Table and attribute default */
    1335        4302 :     if (nLoop == 2 &&
    1336        2376 :         loop[0]->objType == DO_TABLE &&
    1337        2042 :         loop[1]->objType == DO_ATTRDEF &&
    1338        2042 :         ((AttrDefInfo *) loop[1])->adtable == (TableInfo *) loop[0])
    1339             :     {
    1340        2042 :         repairTableAttrDefLoop(loop[0], loop[1]);
    1341        2042 :         return;
    1342             :     }
    1343        2260 :     if (nLoop == 2 &&
    1344         334 :         loop[1]->objType == DO_TABLE &&
    1345           0 :         loop[0]->objType == DO_ATTRDEF &&
    1346           0 :         ((AttrDefInfo *) loop[0])->adtable == (TableInfo *) loop[1])
    1347             :     {
    1348           0 :         repairTableAttrDefLoop(loop[1], loop[0]);
    1349           0 :         return;
    1350             :     }
    1351             : 
    1352             :     /* index on partitioned table and corresponding index on partition */
    1353        2260 :     if (nLoop == 2 &&
    1354         334 :         loop[0]->objType == DO_INDEX &&
    1355           0 :         loop[1]->objType == DO_INDEX)
    1356             :     {
    1357           0 :         if (((IndxInfo *) loop[0])->parentidx == loop[1]->catId.oid)
    1358             :         {
    1359           0 :             repairIndexLoop(loop[0], loop[1]);
    1360           0 :             return;
    1361             :         }
    1362           0 :         else if (((IndxInfo *) loop[1])->parentidx == loop[0]->catId.oid)
    1363             :         {
    1364           0 :             repairIndexLoop(loop[1], loop[0]);
    1365           0 :             return;
    1366             :         }
    1367             :     }
    1368             : 
    1369             :     /* Indirect loop involving table and attribute default */
    1370        2260 :     if (nLoop > 2)
    1371             :     {
    1372         632 :         for (i = 0; i < nLoop; i++)
    1373             :         {
    1374         632 :             if (loop[i]->objType == DO_TABLE)
    1375             :             {
    1376        2212 :                 for (j = 0; j < nLoop; j++)
    1377             :                 {
    1378        1896 :                     if (loop[j]->objType == DO_ATTRDEF &&
    1379         632 :                         ((AttrDefInfo *) loop[j])->adtable == (TableInfo *) loop[i])
    1380             :                     {
    1381         316 :                         repairTableAttrDefMultiLoop(loop[i], loop[j]);
    1382         316 :                         return;
    1383             :                     }
    1384             :                 }
    1385             :             }
    1386             :         }
    1387             :     }
    1388             : 
    1389             :     /* Domain and CHECK or NOT NULL constraint */
    1390        1944 :     if (nLoop == 2 &&
    1391         334 :         loop[0]->objType == DO_TYPE &&
    1392         334 :         loop[1]->objType == DO_CONSTRAINT &&
    1393         334 :         (((ConstraintInfo *) loop[1])->contype == 'c' ||
    1394         112 :          ((ConstraintInfo *) loop[1])->contype == 'n') &&
    1395         334 :         ((ConstraintInfo *) loop[1])->condomain == (TypeInfo *) loop[0])
    1396             :     {
    1397         334 :         repairDomainConstraintLoop(loop[0], loop[1]);
    1398         334 :         return;
    1399             :     }
    1400        1610 :     if (nLoop == 2 &&
    1401           0 :         loop[1]->objType == DO_TYPE &&
    1402           0 :         loop[0]->objType == DO_CONSTRAINT &&
    1403           0 :         (((ConstraintInfo *) loop[0])->contype == 'c' ||
    1404           0 :          ((ConstraintInfo *) loop[0])->contype == 'n') &&
    1405           0 :         ((ConstraintInfo *) loop[0])->condomain == (TypeInfo *) loop[1])
    1406             :     {
    1407           0 :         repairDomainConstraintLoop(loop[1], loop[0]);
    1408           0 :         return;
    1409             :     }
    1410             : 
    1411             :     /* Indirect loop involving domain and CHECK or NOT NULL constraint */
    1412        1610 :     if (nLoop > 2)
    1413             :     {
    1414           0 :         for (i = 0; i < nLoop; i++)
    1415             :         {
    1416           0 :             if (loop[i]->objType == DO_TYPE)
    1417             :             {
    1418           0 :                 for (j = 0; j < nLoop; j++)
    1419             :                 {
    1420           0 :                     if (loop[j]->objType == DO_CONSTRAINT &&
    1421           0 :                         (((ConstraintInfo *) loop[j])->contype == 'c' ||
    1422           0 :                          ((ConstraintInfo *) loop[j])->contype == 'n') &&
    1423           0 :                         ((ConstraintInfo *) loop[j])->condomain == (TypeInfo *) loop[i])
    1424             :                     {
    1425           0 :                         repairDomainConstraintMultiLoop(loop[i], loop[j]);
    1426           0 :                         return;
    1427             :                     }
    1428             :                 }
    1429             :             }
    1430             :         }
    1431             :     }
    1432             : 
    1433             :     /*
    1434             :      * Loop of table with itself --- just ignore it.
    1435             :      *
    1436             :      * (Actually, what this arises from is a dependency of a table column on
    1437             :      * another column, which happened with generated columns before v15; or a
    1438             :      * dependency of a table column on the whole table, which happens with
    1439             :      * partitioning.  But we didn't pay attention to sub-object IDs while
    1440             :      * collecting the dependency data, so we can't see that here.)
    1441             :      */
    1442        1610 :     if (nLoop == 1)
    1443             :     {
    1444        1610 :         if (loop[0]->objType == DO_TABLE)
    1445             :         {
    1446        1610 :             removeObjectDependency(loop[0], loop[0]->dumpId);
    1447        1610 :             return;
    1448             :         }
    1449             :     }
    1450             : 
    1451             :     /*
    1452             :      * If all the objects are TABLE_DATA items, what we must have is a
    1453             :      * circular set of foreign key constraints (or a single self-referential
    1454             :      * table).  Print an appropriate complaint and break the loop arbitrarily.
    1455             :      */
    1456           0 :     for (i = 0; i < nLoop; i++)
    1457             :     {
    1458           0 :         if (loop[i]->objType != DO_TABLE_DATA)
    1459           0 :             break;
    1460             :     }
    1461           0 :     if (i >= nLoop)
    1462             :     {
    1463           0 :         pg_log_warning(ngettext("there are circular foreign-key constraints on this table:",
    1464             :                                 "there are circular foreign-key constraints among these tables:",
    1465             :                                 nLoop));
    1466           0 :         for (i = 0; i < nLoop; i++)
    1467           0 :             pg_log_warning_detail("%s", loop[i]->name);
    1468           0 :         pg_log_warning_hint("You might not be able to restore the dump without using --disable-triggers or temporarily dropping the constraints.");
    1469           0 :         pg_log_warning_hint("Consider using a full dump instead of a --data-only dump to avoid this problem.");
    1470           0 :         if (nLoop > 1)
    1471           0 :             removeObjectDependency(loop[0], loop[1]->dumpId);
    1472             :         else                    /* must be a self-dependency */
    1473           0 :             removeObjectDependency(loop[0], loop[0]->dumpId);
    1474           0 :         return;
    1475             :     }
    1476             : 
    1477             :     /*
    1478             :      * If we can't find a principled way to break the loop, complain and break
    1479             :      * it in an arbitrary fashion.
    1480             :      */
    1481           0 :     pg_log_warning("could not resolve dependency loop among these items:");
    1482           0 :     for (i = 0; i < nLoop; i++)
    1483             :     {
    1484             :         char        buf[1024];
    1485             : 
    1486           0 :         describeDumpableObject(loop[i], buf, sizeof(buf));
    1487           0 :         pg_log_warning_detail("%s", buf);
    1488             :     }
    1489             : 
    1490           0 :     if (nLoop > 1)
    1491           0 :         removeObjectDependency(loop[0], loop[1]->dumpId);
    1492             :     else                        /* must be a self-dependency */
    1493           0 :         removeObjectDependency(loop[0], loop[0]->dumpId);
    1494             : }
    1495             : 
    1496             : /*
    1497             :  * Describe a dumpable object usefully for errors
    1498             :  *
    1499             :  * This should probably go somewhere else...
    1500             :  */
    1501             : static void
    1502           0 : describeDumpableObject(DumpableObject *obj, char *buf, int bufsize)
    1503             : {
    1504           0 :     switch (obj->objType)
    1505             :     {
    1506           0 :         case DO_NAMESPACE:
    1507           0 :             snprintf(buf, bufsize,
    1508             :                      "SCHEMA %s  (ID %d OID %u)",
    1509             :                      obj->name, obj->dumpId, obj->catId.oid);
    1510           0 :             return;
    1511           0 :         case DO_EXTENSION:
    1512           0 :             snprintf(buf, bufsize,
    1513             :                      "EXTENSION %s  (ID %d OID %u)",
    1514             :                      obj->name, obj->dumpId, obj->catId.oid);
    1515           0 :             return;
    1516           0 :         case DO_TYPE:
    1517           0 :             snprintf(buf, bufsize,
    1518             :                      "TYPE %s  (ID %d OID %u)",
    1519             :                      obj->name, obj->dumpId, obj->catId.oid);
    1520           0 :             return;
    1521           0 :         case DO_SHELL_TYPE:
    1522           0 :             snprintf(buf, bufsize,
    1523             :                      "SHELL TYPE %s  (ID %d OID %u)",
    1524             :                      obj->name, obj->dumpId, obj->catId.oid);
    1525           0 :             return;
    1526           0 :         case DO_FUNC:
    1527           0 :             snprintf(buf, bufsize,
    1528             :                      "FUNCTION %s  (ID %d OID %u)",
    1529             :                      obj->name, obj->dumpId, obj->catId.oid);
    1530           0 :             return;
    1531           0 :         case DO_AGG:
    1532           0 :             snprintf(buf, bufsize,
    1533             :                      "AGGREGATE %s  (ID %d OID %u)",
    1534             :                      obj->name, obj->dumpId, obj->catId.oid);
    1535           0 :             return;
    1536           0 :         case DO_OPERATOR:
    1537           0 :             snprintf(buf, bufsize,
    1538             :                      "OPERATOR %s  (ID %d OID %u)",
    1539             :                      obj->name, obj->dumpId, obj->catId.oid);
    1540           0 :             return;
    1541           0 :         case DO_ACCESS_METHOD:
    1542           0 :             snprintf(buf, bufsize,
    1543             :                      "ACCESS METHOD %s  (ID %d OID %u)",
    1544             :                      obj->name, obj->dumpId, obj->catId.oid);
    1545           0 :             return;
    1546           0 :         case DO_OPCLASS:
    1547           0 :             snprintf(buf, bufsize,
    1548             :                      "OPERATOR CLASS %s  (ID %d OID %u)",
    1549             :                      obj->name, obj->dumpId, obj->catId.oid);
    1550           0 :             return;
    1551           0 :         case DO_OPFAMILY:
    1552           0 :             snprintf(buf, bufsize,
    1553             :                      "OPERATOR FAMILY %s  (ID %d OID %u)",
    1554             :                      obj->name, obj->dumpId, obj->catId.oid);
    1555           0 :             return;
    1556           0 :         case DO_COLLATION:
    1557           0 :             snprintf(buf, bufsize,
    1558             :                      "COLLATION %s  (ID %d OID %u)",
    1559             :                      obj->name, obj->dumpId, obj->catId.oid);
    1560           0 :             return;
    1561           0 :         case DO_CONVERSION:
    1562           0 :             snprintf(buf, bufsize,
    1563             :                      "CONVERSION %s  (ID %d OID %u)",
    1564             :                      obj->name, obj->dumpId, obj->catId.oid);
    1565           0 :             return;
    1566           0 :         case DO_TABLE:
    1567           0 :             snprintf(buf, bufsize,
    1568             :                      "TABLE %s  (ID %d OID %u)",
    1569             :                      obj->name, obj->dumpId, obj->catId.oid);
    1570           0 :             return;
    1571           0 :         case DO_TABLE_ATTACH:
    1572           0 :             snprintf(buf, bufsize,
    1573             :                      "TABLE ATTACH %s  (ID %d)",
    1574             :                      obj->name, obj->dumpId);
    1575           0 :             return;
    1576           0 :         case DO_ATTRDEF:
    1577           0 :             snprintf(buf, bufsize,
    1578             :                      "ATTRDEF %s.%s  (ID %d OID %u)",
    1579           0 :                      ((AttrDefInfo *) obj)->adtable->dobj.name,
    1580           0 :                      ((AttrDefInfo *) obj)->adtable->attnames[((AttrDefInfo *) obj)->adnum - 1],
    1581             :                      obj->dumpId, obj->catId.oid);
    1582           0 :             return;
    1583           0 :         case DO_INDEX:
    1584           0 :             snprintf(buf, bufsize,
    1585             :                      "INDEX %s  (ID %d OID %u)",
    1586             :                      obj->name, obj->dumpId, obj->catId.oid);
    1587           0 :             return;
    1588           0 :         case DO_INDEX_ATTACH:
    1589           0 :             snprintf(buf, bufsize,
    1590             :                      "INDEX ATTACH %s  (ID %d)",
    1591             :                      obj->name, obj->dumpId);
    1592           0 :             return;
    1593           0 :         case DO_STATSEXT:
    1594           0 :             snprintf(buf, bufsize,
    1595             :                      "STATISTICS %s  (ID %d OID %u)",
    1596             :                      obj->name, obj->dumpId, obj->catId.oid);
    1597           0 :             return;
    1598           0 :         case DO_REFRESH_MATVIEW:
    1599           0 :             snprintf(buf, bufsize,
    1600             :                      "REFRESH MATERIALIZED VIEW %s  (ID %d OID %u)",
    1601             :                      obj->name, obj->dumpId, obj->catId.oid);
    1602           0 :             return;
    1603           0 :         case DO_RULE:
    1604           0 :             snprintf(buf, bufsize,
    1605             :                      "RULE %s  (ID %d OID %u)",
    1606             :                      obj->name, obj->dumpId, obj->catId.oid);
    1607           0 :             return;
    1608           0 :         case DO_TRIGGER:
    1609           0 :             snprintf(buf, bufsize,
    1610             :                      "TRIGGER %s  (ID %d OID %u)",
    1611             :                      obj->name, obj->dumpId, obj->catId.oid);
    1612           0 :             return;
    1613           0 :         case DO_EVENT_TRIGGER:
    1614           0 :             snprintf(buf, bufsize,
    1615             :                      "EVENT TRIGGER %s (ID %d OID %u)",
    1616             :                      obj->name, obj->dumpId, obj->catId.oid);
    1617           0 :             return;
    1618           0 :         case DO_CONSTRAINT:
    1619           0 :             snprintf(buf, bufsize,
    1620             :                      "CONSTRAINT %s  (ID %d OID %u)",
    1621             :                      obj->name, obj->dumpId, obj->catId.oid);
    1622           0 :             return;
    1623           0 :         case DO_FK_CONSTRAINT:
    1624           0 :             snprintf(buf, bufsize,
    1625             :                      "FK CONSTRAINT %s  (ID %d OID %u)",
    1626             :                      obj->name, obj->dumpId, obj->catId.oid);
    1627           0 :             return;
    1628           0 :         case DO_PROCLANG:
    1629           0 :             snprintf(buf, bufsize,
    1630             :                      "PROCEDURAL LANGUAGE %s  (ID %d OID %u)",
    1631             :                      obj->name, obj->dumpId, obj->catId.oid);
    1632           0 :             return;
    1633           0 :         case DO_CAST:
    1634           0 :             snprintf(buf, bufsize,
    1635             :                      "CAST %u to %u  (ID %d OID %u)",
    1636             :                      ((CastInfo *) obj)->castsource,
    1637             :                      ((CastInfo *) obj)->casttarget,
    1638             :                      obj->dumpId, obj->catId.oid);
    1639           0 :             return;
    1640           0 :         case DO_TRANSFORM:
    1641           0 :             snprintf(buf, bufsize,
    1642             :                      "TRANSFORM %u lang %u  (ID %d OID %u)",
    1643             :                      ((TransformInfo *) obj)->trftype,
    1644             :                      ((TransformInfo *) obj)->trflang,
    1645             :                      obj->dumpId, obj->catId.oid);
    1646           0 :             return;
    1647           0 :         case DO_TABLE_DATA:
    1648           0 :             snprintf(buf, bufsize,
    1649             :                      "TABLE DATA %s  (ID %d OID %u)",
    1650             :                      obj->name, obj->dumpId, obj->catId.oid);
    1651           0 :             return;
    1652           0 :         case DO_SEQUENCE_SET:
    1653           0 :             snprintf(buf, bufsize,
    1654             :                      "SEQUENCE SET %s  (ID %d OID %u)",
    1655             :                      obj->name, obj->dumpId, obj->catId.oid);
    1656           0 :             return;
    1657           0 :         case DO_DUMMY_TYPE:
    1658           0 :             snprintf(buf, bufsize,
    1659             :                      "DUMMY TYPE %s  (ID %d OID %u)",
    1660             :                      obj->name, obj->dumpId, obj->catId.oid);
    1661           0 :             return;
    1662           0 :         case DO_TSPARSER:
    1663           0 :             snprintf(buf, bufsize,
    1664             :                      "TEXT SEARCH PARSER %s  (ID %d OID %u)",
    1665             :                      obj->name, obj->dumpId, obj->catId.oid);
    1666           0 :             return;
    1667           0 :         case DO_TSDICT:
    1668           0 :             snprintf(buf, bufsize,
    1669             :                      "TEXT SEARCH DICTIONARY %s  (ID %d OID %u)",
    1670             :                      obj->name, obj->dumpId, obj->catId.oid);
    1671           0 :             return;
    1672           0 :         case DO_TSTEMPLATE:
    1673           0 :             snprintf(buf, bufsize,
    1674             :                      "TEXT SEARCH TEMPLATE %s  (ID %d OID %u)",
    1675             :                      obj->name, obj->dumpId, obj->catId.oid);
    1676           0 :             return;
    1677           0 :         case DO_TSCONFIG:
    1678           0 :             snprintf(buf, bufsize,
    1679             :                      "TEXT SEARCH CONFIGURATION %s  (ID %d OID %u)",
    1680             :                      obj->name, obj->dumpId, obj->catId.oid);
    1681           0 :             return;
    1682           0 :         case DO_FDW:
    1683           0 :             snprintf(buf, bufsize,
    1684             :                      "FOREIGN DATA WRAPPER %s  (ID %d OID %u)",
    1685             :                      obj->name, obj->dumpId, obj->catId.oid);
    1686           0 :             return;
    1687           0 :         case DO_FOREIGN_SERVER:
    1688           0 :             snprintf(buf, bufsize,
    1689             :                      "FOREIGN SERVER %s  (ID %d OID %u)",
    1690             :                      obj->name, obj->dumpId, obj->catId.oid);
    1691           0 :             return;
    1692           0 :         case DO_DEFAULT_ACL:
    1693           0 :             snprintf(buf, bufsize,
    1694             :                      "DEFAULT ACL %s  (ID %d OID %u)",
    1695             :                      obj->name, obj->dumpId, obj->catId.oid);
    1696           0 :             return;
    1697           0 :         case DO_LARGE_OBJECT:
    1698           0 :             snprintf(buf, bufsize,
    1699             :                      "LARGE OBJECT  (ID %d OID %u)",
    1700             :                      obj->dumpId, obj->catId.oid);
    1701           0 :             return;
    1702           0 :         case DO_LARGE_OBJECT_DATA:
    1703           0 :             snprintf(buf, bufsize,
    1704             :                      "LARGE OBJECT DATA  (ID %d)",
    1705             :                      obj->dumpId);
    1706           0 :             return;
    1707           0 :         case DO_POLICY:
    1708           0 :             snprintf(buf, bufsize,
    1709             :                      "POLICY (ID %d OID %u)",
    1710             :                      obj->dumpId, obj->catId.oid);
    1711           0 :             return;
    1712           0 :         case DO_PUBLICATION:
    1713           0 :             snprintf(buf, bufsize,
    1714             :                      "PUBLICATION (ID %d OID %u)",
    1715             :                      obj->dumpId, obj->catId.oid);
    1716           0 :             return;
    1717           0 :         case DO_PUBLICATION_REL:
    1718           0 :             snprintf(buf, bufsize,
    1719             :                      "PUBLICATION TABLE (ID %d OID %u)",
    1720             :                      obj->dumpId, obj->catId.oid);
    1721           0 :             return;
    1722           0 :         case DO_PUBLICATION_TABLE_IN_SCHEMA:
    1723           0 :             snprintf(buf, bufsize,
    1724             :                      "PUBLICATION TABLES IN SCHEMA (ID %d OID %u)",
    1725             :                      obj->dumpId, obj->catId.oid);
    1726           0 :             return;
    1727           0 :         case DO_SUBSCRIPTION:
    1728           0 :             snprintf(buf, bufsize,
    1729             :                      "SUBSCRIPTION (ID %d OID %u)",
    1730             :                      obj->dumpId, obj->catId.oid);
    1731           0 :             return;
    1732           0 :         case DO_SUBSCRIPTION_REL:
    1733           0 :             snprintf(buf, bufsize,
    1734             :                      "SUBSCRIPTION TABLE (ID %d OID %u)",
    1735             :                      obj->dumpId, obj->catId.oid);
    1736           0 :             return;
    1737           0 :         case DO_PRE_DATA_BOUNDARY:
    1738           0 :             snprintf(buf, bufsize,
    1739             :                      "PRE-DATA BOUNDARY  (ID %d)",
    1740             :                      obj->dumpId);
    1741           0 :             return;
    1742           0 :         case DO_POST_DATA_BOUNDARY:
    1743           0 :             snprintf(buf, bufsize,
    1744             :                      "POST-DATA BOUNDARY  (ID %d)",
    1745             :                      obj->dumpId);
    1746           0 :             return;
    1747           0 :         case DO_REL_STATS:
    1748           0 :             snprintf(buf, bufsize,
    1749             :                      "RELATION STATISTICS FOR %s  (ID %d OID %u)",
    1750             :                      obj->name, obj->dumpId, obj->catId.oid);
    1751           0 :             return;
    1752             :     }
    1753             :     /* shouldn't get here */
    1754           0 :     snprintf(buf, bufsize,
    1755             :              "object type %d  (ID %d OID %u)",
    1756           0 :              (int) obj->objType,
    1757             :              obj->dumpId, obj->catId.oid);
    1758             : }
    1759             : 
    1760             : /* binaryheap comparator that compares "a" and "b" as integers */
    1761             : static int
    1762    56567970 : int_cmp(void *a, void *b, void *arg)
    1763             : {
    1764    56567970 :     int         ai = (int) (intptr_t) a;
    1765    56567970 :     int         bi = (int) (intptr_t) b;
    1766             : 
    1767    56567970 :     return pg_cmp_s32(ai, bi);
    1768             : }

Generated by: LCOV version 1.16