1 /*-------------------------------------------------------------------------
4 * Common header file for the pg_dump utility
6 * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
9 * src/bin/pg_dump/pg_dump.h
11 *-------------------------------------------------------------------------
17 #include "pg_backup.h"
20 #define oidcmp(x,y) ( ((x) < (y) ? -1 : ((x) > (y)) ? 1 : 0) )
21 #define oideq(x,y) ( (x) == (y) )
22 #define oidle(x,y) ( (x) <= (y) )
23 #define oidge(x,y) ( (x) >= (y) )
24 #define oidzero(x) ( (x) == 0 )
27 * The data structures used to store system catalog information. Every
28 * dumpable object is a subclass of DumpableObject.
30 * NOTE: the structures described here live for the entire pg_dump run;
31 * and in most cases we make a struct for every object we can find in the
32 * catalogs, not only those we are actually going to dump. Hence, it's
33 * best to store a minimal amount of per-object info in these structs,
34 * and retrieve additional per-object info when and if we dump a specific
35 * object. In particular, try to avoid retrieving expensive-to-compute
36 * information until it's known to be needed. We do, however, have to
37 * store enough info to determine whether an object should be dumped and
38 * what order to dump in.
43 /* When modifying this enum, update priority tables in pg_dump_sort.c! */
64 DO_FK_CONSTRAINT
, /* see note for ConstraintInfo */
81 DO_POST_DATA_BOUNDARY
,
90 /* component types of an object which can be selected for dumping */
91 typedef uint32 DumpComponents
; /* a bitmask of dump object components */
92 #define DUMP_COMPONENT_NONE (0)
93 #define DUMP_COMPONENT_DEFINITION (1 << 0)
94 #define DUMP_COMPONENT_DATA (1 << 1)
95 #define DUMP_COMPONENT_COMMENT (1 << 2)
96 #define DUMP_COMPONENT_SECLABEL (1 << 3)
97 #define DUMP_COMPONENT_ACL (1 << 4)
98 #define DUMP_COMPONENT_POLICY (1 << 5)
99 #define DUMP_COMPONENT_USERMAP (1 << 6)
100 #define DUMP_COMPONENT_ALL (0xFFFF)
103 * component types which require us to obtain a lock on the table
105 * Note that some components only require looking at the information
106 * in the pg_catalog tables and, for those components, we do not need
107 * to lock the table. Be careful here though- some components use
108 * server-side functions which pull the latest information from
109 * SysCache and in those cases we *do* need to lock the table.
111 * We do not need locks for the COMMENT and SECLABEL components as
112 * those simply query their associated tables without using any
113 * server-side functions. We do not need locks for the ACL component
114 * as we pull that information from pg_class without using any
115 * server-side functions that use SysCache. The USERMAP component
116 * is only relevant for FOREIGN SERVERs and not tables, so no sense
117 * locking a table for that either (that can happen if we are going
118 * to dump "ALL" components for a table).
120 * We DO need locks for DEFINITION, due to various server-side
121 * functions that are used and POLICY due to pg_get_expr(). We set
122 * this up to grab the lock except in the cases we know to be safe.
124 #define DUMP_COMPONENTS_REQUIRING_LOCK (\
125 DUMP_COMPONENT_DEFINITION |\
126 DUMP_COMPONENT_DATA |\
127 DUMP_COMPONENT_POLICY)
129 typedef struct _dumpableObject
131 DumpableObjectType objType
;
132 CatalogId catId
; /* zero if not a cataloged object */
133 DumpId dumpId
; /* assigned by AssignDumpId() */
134 char *name
; /* object name (should never be NULL) */
135 struct _namespaceInfo
*namespace; /* containing namespace, or NULL */
136 DumpComponents dump
; /* bitmask of components to dump */
137 DumpComponents dump_contains
; /* as above, but for contained objects */
138 bool ext_member
; /* true if object is member of extension */
139 bool depends_on_ext
; /* true if object depends on an extension */
140 DumpId
*dependencies
; /* dumpIds of objects this one depends on */
141 int nDeps
; /* number of valid dependencies */
142 int allocDeps
; /* allocated size of dependencies[] */
145 typedef struct _namespaceInfo
148 char *rolname
; /* name of owner, or empty string */
155 typedef struct _extensionInfo
158 char *namespace; /* schema containing extension's objects */
161 char *extconfig
; /* info about configuration tables */
165 typedef struct _typeInfo
170 * Note: dobj.name is the raw pg_type.typname entry. ftypname is the
171 * result of format_type(), which will be quoted if needed, and might be
172 * schema-qualified too.
175 char *rolname
; /* name of owner, or empty string */
182 char typrelkind
; /* 'r', 'v', 'c', etc */
183 char typtype
; /* 'b', 'c', etc */
184 bool isArray
; /* true if auto-generated array type */
185 bool isDefined
; /* true if typisdefined */
186 /* If needed, we'll create a "shell type" entry for it; link that here: */
187 struct _shellTypeInfo
*shellType
; /* shell-type entry, or NULL */
188 /* If it's a domain, we store links to its constraints here: */
190 struct _constraintInfo
*domChecks
;
193 typedef struct _shellTypeInfo
197 TypeInfo
*baseType
; /* back link to associated base type */
200 typedef struct _funcInfo
203 char *rolname
; /* name of owner, or empty string */
214 /* AggInfo is a superset of FuncInfo */
215 typedef struct _aggInfo
218 /* we don't require any other fields at the moment */
221 typedef struct _oprInfo
229 typedef struct _accessMethodInfo
236 typedef struct _opclassInfo
242 typedef struct _opfamilyInfo
248 typedef struct _collInfo
254 typedef struct _convInfo
260 typedef struct _tableInfo
263 * These fields are collected for every table in the database.
266 char *rolname
; /* name of owner, or empty string */
272 char relpersistence
; /* relation persistence */
273 bool relispopulated
; /* relation is populated */
274 char relreplident
; /* replica identifier */
275 char *reltablespace
; /* relation tablespace */
276 char *reloptions
; /* options specified by WITH (...) */
277 char *checkoption
; /* WITH CHECK OPTION, if any */
278 char *toast_reloptions
; /* WITH options for the TOAST table */
279 bool hasindex
; /* does it have any indexes? */
280 bool hasrules
; /* does it have any rules? */
281 bool hastriggers
; /* does it have any triggers? */
282 bool rowsec
; /* is row security enabled? */
283 bool forcerowsec
; /* is row security forced? */
284 bool hasoids
; /* does it have OIDs? */
285 uint32 frozenxid
; /* table's relfrozenxid */
286 uint32 minmxid
; /* table's relminmxid */
287 Oid toast_oid
; /* toast table's OID, or 0 if none */
288 uint32 toast_frozenxid
; /* toast table's relfrozenxid, if any */
289 uint32 toast_minmxid
; /* toast table's relminmxid */
290 int ncheck
; /* # of CHECK expressions */
291 Oid reloftype
; /* underlying type for typed table */
292 /* these two are set only if table is a sequence owned by a column: */
293 Oid owning_tab
; /* OID of table owning sequence */
294 int owning_col
; /* attr # of column owning sequence */
295 bool is_identity_sequence
;
296 int relpages
; /* table's size in pages (from pg_class) */
298 bool interesting
; /* true if need to collect more data */
299 bool dummy_view
; /* view's real definition must be postponed */
300 bool postponed_def
; /* matview must be postponed into post-data */
301 bool ispartition
; /* is table a partition? */
302 bool unsafe_partitions
; /* is it an unsafe partitioned table? */
305 * These fields are computed only if we decide the table is interesting
306 * (it's either a table to dump, or a direct parent of a dumpable table).
308 int numatts
; /* number of attributes */
309 char **attnames
; /* the attribute names */
310 char **atttypnames
; /* attribute type names */
311 int *atttypmod
; /* type-specific type modifiers */
312 int *attstattarget
; /* attribute statistics targets */
313 char *attstorage
; /* attribute storage scheme */
314 char *typstorage
; /* type storage scheme */
315 bool *attisdropped
; /* true if attr is dropped; don't dump it */
318 int *attlen
; /* attribute length, used by binary_upgrade */
319 char *attalign
; /* attribute align, used by binary_upgrade */
320 bool *attislocal
; /* true if attr has local definition */
321 char **attoptions
; /* per-attribute options */
322 Oid
*attcollation
; /* per-attribute collation selection */
323 char **attfdwoptions
; /* per-attribute fdw options */
324 char **attmissingval
; /* per attribute missing value */
325 bool *notnull
; /* NOT NULL constraints on attributes */
326 bool *inhNotNull
; /* true if NOT NULL is inherited */
327 struct _attrDefInfo
**attrdefs
; /* DEFAULT expressions */
328 struct _constraintInfo
*checkexprs
; /* CHECK constraints */
329 bool needs_override
; /* has GENERATED ALWAYS AS IDENTITY */
330 char *amname
; /* relation access method */
333 * Stuff computed only for dumpable tables.
335 int numParents
; /* number of (immediate) parent tables */
336 struct _tableInfo
**parents
; /* TableInfos of immediate parents */
337 int numIndexes
; /* number of indexes */
338 struct _indxInfo
*indexes
; /* indexes */
339 struct _tableDataInfo
*dataObj
; /* TableDataInfo, if dumping its data */
340 int numTriggers
; /* number of triggers for table */
341 struct _triggerInfo
*triggers
; /* array of TriggerInfo structs */
344 typedef struct _attrDefInfo
346 DumpableObject dobj
; /* note: dobj.name is name of table */
347 TableInfo
*adtable
; /* link to table of attribute */
349 char *adef_expr
; /* decompiled DEFAULT expression */
350 bool separate
; /* true if must dump as separate item */
353 typedef struct _tableDataInfo
356 TableInfo
*tdtable
; /* link to table to dump */
357 char *filtercond
; /* WHERE condition to limit rows dumped */
360 typedef struct _indxInfo
363 TableInfo
*indextable
; /* link to table the index is for */
365 char *tablespace
; /* tablespace in which index is stored */
366 char *indreloptions
; /* options specified by WITH (...) */
367 char *indstatcols
; /* column numbers with statistics */
368 char *indstatvals
; /* statistic values for columns */
369 int indnkeyattrs
; /* number of index key attributes */
370 int indnattrs
; /* total number of index attributes */
371 Oid
*indkeys
; /* In spite of the name 'indkeys' this field
372 * contains both key and nonkey attributes */
375 Oid parentidx
; /* if a partition, parent index OID */
376 SimplePtrList partattaches
; /* if partitioned, partition attach objects */
378 /* if there is an associated constraint object, its dumpId: */
379 DumpId indexconstraint
;
382 typedef struct _indexAttachInfo
385 IndxInfo
*parentIdx
; /* link to index on partitioned table */
386 IndxInfo
*partitionIdx
; /* link to index on partition */
389 typedef struct _statsExtInfo
392 char *rolname
; /* name of owner, or empty string */
393 TableInfo
*stattable
; /* link to table the stats are for */
396 typedef struct _ruleInfo
399 TableInfo
*ruletable
; /* link to table the rule is for */
403 bool separate
; /* true if must dump as separate item */
404 /* separate is always true for non-ON SELECT rules */
407 typedef struct _triggerInfo
410 TableInfo
*tgtable
; /* link to table the trigger is for */
418 char *tgconstrrelname
;
426 typedef struct _evttriggerInfo
438 * struct ConstraintInfo is used for all constraint types. However we
439 * use a different objType for foreign key constraints, to make it easier
440 * to sort them the way we want.
442 * Note: condeferrable and condeferred are currently only valid for
443 * unique/primary-key constraints. Otherwise that info is in condef.
445 typedef struct _constraintInfo
448 TableInfo
*contable
; /* NULL if domain constraint */
449 TypeInfo
*condomain
; /* NULL if table constraint */
451 char *condef
; /* definition, if CHECK or FOREIGN KEY */
452 Oid confrelid
; /* referenced table, if FOREIGN KEY */
453 DumpId conindex
; /* identifies associated index if any */
454 bool condeferrable
; /* true if constraint is DEFERRABLE */
455 bool condeferred
; /* true if constraint is INITIALLY DEFERRED */
456 bool conislocal
; /* true if constraint has local definition */
457 bool separate
; /* true if must dump as separate item */
460 typedef struct _procLangInfo
471 char *lanowner
; /* name of owner, or empty string */
474 typedef struct _castInfo
484 typedef struct _transformInfo
493 /* InhInfo isn't a DumpableObject, just temporary state */
494 typedef struct _inhInfo
496 Oid inhrelid
; /* OID of a child table */
497 Oid inhparent
; /* OID of its parent */
500 typedef struct _prsInfo
510 typedef struct _dictInfo
515 char *dictinitoption
;
518 typedef struct _tmplInfo
525 typedef struct _cfgInfo
532 typedef struct _fdwInfo
545 typedef struct _foreignServerInfo
559 typedef struct _defaultACLInfo
567 char *initrdefaclacl
;
570 typedef struct _blobInfo
581 * The PolicyInfo struct is used to represent policies on a table and
582 * to indicate if a table has RLS enabled (ENABLE ROW SECURITY). If
583 * polname is NULL, then the record indicates ENABLE ROW SECURITY, while if
584 * it's non-NULL then this is a regular policy definition.
586 typedef struct _policyInfo
590 char *polname
; /* null indicates RLS is enabled on rel */
599 * The PublicationInfo struct is used to represent publications.
601 typedef struct _PublicationInfo
613 * The PublicationRelInfo struct is used to represent publication table
616 typedef struct _PublicationRelInfo
619 PublicationInfo
*publication
;
621 } PublicationRelInfo
;
624 * The SubscriptionInfo struct is used to represent subscription.
626 typedef struct _SubscriptionInfo
633 char *subpublications
;
637 * We build an array of these with an entry for each object that is an
638 * extension member according to pg_depend.
640 typedef struct _extensionMemberId
642 CatalogId catId
; /* tableoid+oid of some member object */
643 ExtensionInfo
*ext
; /* owning extension */
647 extern bool force_quotes
; /* double-quotes for identifiers flag */
649 /* placeholders for comment starting and ending delimiters */
650 extern char g_comment_start
[10];
651 extern char g_comment_end
[10];
653 extern char g_opaque_type
[10]; /* name for the opaque type */
656 * common utility functions
659 extern TableInfo
*getSchemaData(Archive
*fout
, int *numTablesPtr
);
661 extern void AssignDumpId(DumpableObject
*dobj
);
662 extern DumpId
createDumpId(void);
663 extern DumpId
getMaxDumpId(void);
664 extern DumpableObject
*findObjectByDumpId(DumpId dumpId
);
665 extern DumpableObject
*findObjectByCatalogId(CatalogId catalogId
);
666 extern void getDumpableObjects(DumpableObject
***objs
, int *numObjs
);
668 extern void addObjectDependency(DumpableObject
*dobj
, DumpId refId
);
669 extern void removeObjectDependency(DumpableObject
*dobj
, DumpId refId
);
671 extern TableInfo
*findTableByOid(Oid oid
);
672 extern TypeInfo
*findTypeByOid(Oid oid
);
673 extern FuncInfo
*findFuncByOid(Oid oid
);
674 extern OprInfo
*findOprByOid(Oid oid
);
675 extern CollInfo
*findCollationByOid(Oid oid
);
676 extern NamespaceInfo
*findNamespaceByOid(Oid oid
);
677 extern ExtensionInfo
*findExtensionByOid(Oid oid
);
678 extern PublicationInfo
*findPublicationByOid(Oid oid
);
680 extern void setExtensionMembership(ExtensionMemberId
*extmems
, int nextmems
);
681 extern ExtensionInfo
*findOwningExtension(CatalogId catalogId
);
683 extern void parseOidArray(const char *str
, Oid
*array
, int arraysize
);
685 extern void sortDumpableObjects(DumpableObject
**objs
, int numObjs
,
686 DumpId preBoundaryId
, DumpId postBoundaryId
);
687 extern void sortDumpableObjectsByTypeName(DumpableObject
**objs
, int numObjs
);
690 * version specific routines
692 extern NamespaceInfo
*getNamespaces(Archive
*fout
, int *numNamespaces
);
693 extern ExtensionInfo
*getExtensions(Archive
*fout
, int *numExtensions
);
694 extern TypeInfo
*getTypes(Archive
*fout
, int *numTypes
);
695 extern FuncInfo
*getFuncs(Archive
*fout
, int *numFuncs
);
696 extern AggInfo
*getAggregates(Archive
*fout
, int *numAggregates
);
697 extern OprInfo
*getOperators(Archive
*fout
, int *numOperators
);
698 extern AccessMethodInfo
*getAccessMethods(Archive
*fout
, int *numAccessMethods
);
699 extern OpclassInfo
*getOpclasses(Archive
*fout
, int *numOpclasses
);
700 extern OpfamilyInfo
*getOpfamilies(Archive
*fout
, int *numOpfamilies
);
701 extern CollInfo
*getCollations(Archive
*fout
, int *numCollations
);
702 extern ConvInfo
*getConversions(Archive
*fout
, int *numConversions
);
703 extern TableInfo
*getTables(Archive
*fout
, int *numTables
);
704 extern void getOwnedSeqs(Archive
*fout
, TableInfo tblinfo
[], int numTables
);
705 extern InhInfo
*getInherits(Archive
*fout
, int *numInherits
);
706 extern void getPartitioningInfo(Archive
*fout
);
707 extern void getIndexes(Archive
*fout
, TableInfo tblinfo
[], int numTables
);
708 extern void getExtendedStatistics(Archive
*fout
);
709 extern void getConstraints(Archive
*fout
, TableInfo tblinfo
[], int numTables
);
710 extern RuleInfo
*getRules(Archive
*fout
, int *numRules
);
711 extern void getTriggers(Archive
*fout
, TableInfo tblinfo
[], int numTables
);
712 extern ProcLangInfo
*getProcLangs(Archive
*fout
, int *numProcLangs
);
713 extern CastInfo
*getCasts(Archive
*fout
, int *numCasts
);
714 extern TransformInfo
*getTransforms(Archive
*fout
, int *numTransforms
);
715 extern void getTableAttrs(Archive
*fout
, TableInfo
*tbinfo
, int numTables
);
716 extern bool shouldPrintColumn(DumpOptions
*dopt
, TableInfo
*tbinfo
, int colno
);
717 extern TSParserInfo
*getTSParsers(Archive
*fout
, int *numTSParsers
);
718 extern TSDictInfo
*getTSDictionaries(Archive
*fout
, int *numTSDicts
);
719 extern TSTemplateInfo
*getTSTemplates(Archive
*fout
, int *numTSTemplates
);
720 extern TSConfigInfo
*getTSConfigurations(Archive
*fout
, int *numTSConfigs
);
721 extern FdwInfo
*getForeignDataWrappers(Archive
*fout
,
722 int *numForeignDataWrappers
);
723 extern ForeignServerInfo
*getForeignServers(Archive
*fout
,
724 int *numForeignServers
);
725 extern DefaultACLInfo
*getDefaultACLs(Archive
*fout
, int *numDefaultACLs
);
726 extern void getExtensionMembership(Archive
*fout
, ExtensionInfo extinfo
[],
728 extern void processExtensionTables(Archive
*fout
, ExtensionInfo extinfo
[],
730 extern EventTriggerInfo
*getEventTriggers(Archive
*fout
, int *numEventTriggers
);
731 extern void getPolicies(Archive
*fout
, TableInfo tblinfo
[], int numTables
);
732 extern PublicationInfo
*getPublications(Archive
*fout
,
733 int *numPublications
);
734 extern void getPublicationTables(Archive
*fout
, TableInfo tblinfo
[],
736 extern void getSubscriptions(Archive
*fout
);
738 #endif /* PG_DUMP_H */