Make pg_dump's table of object-type priorities more maintainable.
authorTom Lane
Tue, 12 Jan 2021 00:58:07 +0000 (19:58 -0500)
committerTom Lane
Tue, 12 Jan 2021 02:09:18 +0000 (21:09 -0500)
Wedging a new object type into this table has historically required
manually renumbering a lot of existing entries.  (Although it appears
that some people got lazy and re-used the priority level of an
existing object type, even if it wasn't particularly related.)
We can let the compiler do the counting by inventing an enum type that
lists the desired priority levels in order.  Now, if you want to add
or remove a priority level, that's a one-liner.

This patch is not purely cosmetic, because I split apart the priorities
of DO_COLLATION and DO_TRANSFORM, as well as those of DO_ACCESS_METHOD
and DO_OPERATOR, which look to me to have been merged out of expediency
rather than because it was a good idea.  Shell types continue to be
sorted interchangeably with full types, and opclasses interchangeably
with opfamilies.

src/bin/pg_dump/pg_dump_sort.c

index f990b863942299189cfa3659759973be13527e97..022413812ea56624776a359f3e333bff83e13af0 100644 (file)
  * POST_DATA objects must sort after DO_POST_DATA_BOUNDARY, and DATA objects
  * must sort between them.
  */
+
+/* This enum lists the priority levels in order */
+enum dbObjectTypePriorities
+{
+   PRIO_NAMESPACE = 1,
+   PRIO_PROCLANG,
+   PRIO_COLLATION,
+   PRIO_TRANSFORM,
+   PRIO_EXTENSION,
+   PRIO_TYPE,                  /* used for DO_TYPE and DO_SHELL_TYPE */
+   PRIO_FUNC,
+   PRIO_AGG,
+   PRIO_ACCESS_METHOD,
+   PRIO_OPERATOR,
+   PRIO_OPFAMILY,              /* used for DO_OPFAMILY and DO_OPCLASS */
+   PRIO_CAST,
+   PRIO_CONVERSION,
+   PRIO_TSPARSER,
+   PRIO_TSTEMPLATE,
+   PRIO_TSDICT,
+   PRIO_TSCONFIG,
+   PRIO_FDW,
+   PRIO_FOREIGN_SERVER,
+   PRIO_TABLE,
+   PRIO_DUMMY_TYPE,
+   PRIO_ATTRDEF,
+   PRIO_BLOB,
+   PRIO_PRE_DATA_BOUNDARY,     /* boundary! */
+   PRIO_TABLE_DATA,
+   PRIO_SEQUENCE_SET,
+   PRIO_BLOB_DATA,
+   PRIO_POST_DATA_BOUNDARY,    /* boundary! */
+   PRIO_CONSTRAINT,
+   PRIO_INDEX,
+   PRIO_INDEX_ATTACH,
+   PRIO_STATSEXT,
+   PRIO_RULE,
+   PRIO_TRIGGER,
+   PRIO_FK_CONSTRAINT,
+   PRIO_POLICY,
+   PRIO_PUBLICATION,
+   PRIO_PUBLICATION_REL,
+   PRIO_SUBSCRIPTION,
+   PRIO_DEFAULT_ACL,           /* done in ACL pass */
+   PRIO_EVENT_TRIGGER,         /* must be next to last! */
+   PRIO_REFRESH_MATVIEW        /* must be last! */
+};
+
+/* This table is indexed by enum DumpableObjectType */
 static const int dbObjectTypePriority[] =
 {
-   1,                          /* DO_NAMESPACE */
-   4,                          /* DO_EXTENSION */
-   5,                          /* DO_TYPE */
-   5,                          /* DO_SHELL_TYPE */
-   6,                          /* DO_FUNC */
-   7,                          /* DO_AGG */
-   8,                          /* DO_OPERATOR */
-   8,                          /* DO_ACCESS_METHOD */
-   9,                          /* DO_OPCLASS */
-   9,                          /* DO_OPFAMILY */
-   3,                          /* DO_COLLATION */
-   11,                         /* DO_CONVERSION */
-   18,                         /* DO_TABLE */
-   20,                         /* DO_ATTRDEF */
-   28,                         /* DO_INDEX */
-   29,                         /* DO_INDEX_ATTACH */
-   30,                         /* DO_STATSEXT */
-   31,                         /* DO_RULE */
-   32,                         /* DO_TRIGGER */
-   27,                         /* DO_CONSTRAINT */
-   33,                         /* DO_FK_CONSTRAINT */
-   2,                          /* DO_PROCLANG */
-   10,                         /* DO_CAST */
-   23,                         /* DO_TABLE_DATA */
-   24,                         /* DO_SEQUENCE_SET */
-   19,                         /* DO_DUMMY_TYPE */
-   12,                         /* DO_TSPARSER */
-   14,                         /* DO_TSDICT */
-   13,                         /* DO_TSTEMPLATE */
-   15,                         /* DO_TSCONFIG */
-   16,                         /* DO_FDW */
-   17,                         /* DO_FOREIGN_SERVER */
-   38,                         /* DO_DEFAULT_ACL --- done in ACL pass */
-   3,                          /* DO_TRANSFORM */
-   21,                         /* DO_BLOB */
-   25,                         /* DO_BLOB_DATA */
-   22,                         /* DO_PRE_DATA_BOUNDARY */
-   26,                         /* DO_POST_DATA_BOUNDARY */
-   39,                         /* DO_EVENT_TRIGGER --- next to last! */
-   40,                         /* DO_REFRESH_MATVIEW --- last! */
-   34,                         /* DO_POLICY */
-   35,                         /* DO_PUBLICATION */
-   36,                         /* DO_PUBLICATION_REL */
-   37                          /* DO_SUBSCRIPTION */
+   PRIO_NAMESPACE,             /* DO_NAMESPACE */
+   PRIO_EXTENSION,             /* DO_EXTENSION */
+   PRIO_TYPE,                  /* DO_TYPE */
+   PRIO_TYPE,                  /* DO_SHELL_TYPE */
+   PRIO_FUNC,                  /* DO_FUNC */
+   PRIO_AGG,                   /* DO_AGG */
+   PRIO_OPERATOR,              /* DO_OPERATOR */
+   PRIO_ACCESS_METHOD,         /* DO_ACCESS_METHOD */
+   PRIO_OPFAMILY,              /* DO_OPCLASS */
+   PRIO_OPFAMILY,              /* DO_OPFAMILY */
+   PRIO_COLLATION,             /* DO_COLLATION */
+   PRIO_CONVERSION,            /* DO_CONVERSION */
+   PRIO_TABLE,                 /* DO_TABLE */
+   PRIO_ATTRDEF,               /* DO_ATTRDEF */
+   PRIO_INDEX,                 /* DO_INDEX */
+   PRIO_INDEX_ATTACH,          /* DO_INDEX_ATTACH */
+   PRIO_STATSEXT,              /* DO_STATSEXT */
+   PRIO_RULE,                  /* DO_RULE */
+   PRIO_TRIGGER,               /* DO_TRIGGER */
+   PRIO_CONSTRAINT,            /* DO_CONSTRAINT */
+   PRIO_FK_CONSTRAINT,         /* DO_FK_CONSTRAINT */
+   PRIO_PROCLANG,              /* DO_PROCLANG */
+   PRIO_CAST,                  /* DO_CAST */
+   PRIO_TABLE_DATA,            /* DO_TABLE_DATA */
+   PRIO_SEQUENCE_SET,          /* DO_SEQUENCE_SET */
+   PRIO_DUMMY_TYPE,            /* DO_DUMMY_TYPE */
+   PRIO_TSPARSER,              /* DO_TSPARSER */
+   PRIO_TSDICT,                /* DO_TSDICT */
+   PRIO_TSTEMPLATE,            /* DO_TSTEMPLATE */
+   PRIO_TSCONFIG,              /* DO_TSCONFIG */
+   PRIO_FDW,                   /* DO_FDW */
+   PRIO_FOREIGN_SERVER,        /* DO_FOREIGN_SERVER */
+   PRIO_DEFAULT_ACL,           /* DO_DEFAULT_ACL */
+   PRIO_TRANSFORM,             /* DO_TRANSFORM */
+   PRIO_BLOB,                  /* DO_BLOB */
+   PRIO_BLOB_DATA,             /* DO_BLOB_DATA */
+   PRIO_PRE_DATA_BOUNDARY,     /* DO_PRE_DATA_BOUNDARY */
+   PRIO_POST_DATA_BOUNDARY,    /* DO_POST_DATA_BOUNDARY */
+   PRIO_EVENT_TRIGGER,         /* DO_EVENT_TRIGGER */
+   PRIO_REFRESH_MATVIEW,       /* DO_REFRESH_MATVIEW */
+   PRIO_POLICY,                /* DO_POLICY */
+   PRIO_PUBLICATION,           /* DO_PUBLICATION */
+   PRIO_PUBLICATION_REL,       /* DO_PUBLICATION_REL */
+   PRIO_SUBSCRIPTION           /* DO_SUBSCRIPTION */
 };
 
 StaticAssertDecl(lengthof(dbObjectTypePriority) == (DO_SUBSCRIPTION + 1),