]> granicus.if.org Git - postgresql/blob - src/bin/pg_dump/pg_dump.h
Revise psql pattern-matching switches as per discussion. The rule is now
[postgresql] / src / bin / pg_dump / pg_dump.h
1 /*-------------------------------------------------------------------------
2  *
3  * pg_dump.h
4  *        Common header file for the pg_dump utility
5  *
6  * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.h,v 1.130 2006/10/09 23:36:59 tgl Exp $
10  *
11  *-------------------------------------------------------------------------
12  */
13
14 #ifndef PG_DUMP_H
15 #define PG_DUMP_H
16
17 #include "postgres_fe.h"
18
19
20 /*
21  * pg_dump uses two different mechanisms for identifying database objects:
22  *
23  * CatalogId represents an object by the tableoid and oid of its defining
24  * entry in the system catalogs.  We need this to interpret pg_depend entries,
25  * for instance.
26  *
27  * DumpId is a simple sequential integer counter assigned as dumpable objects
28  * are identified during a pg_dump run.  We use DumpId internally in preference
29  * to CatalogId for two reasons: it's more compact, and we can assign DumpIds
30  * to "objects" that don't have a separate CatalogId.  For example, it is
31  * convenient to consider a table, its data, and its ACL as three separate
32  * dumpable "objects" with distinct DumpIds --- this lets us reason about the
33  * order in which to dump these things.
34  */
35
36 typedef struct
37 {
38         Oid                     tableoid;
39         Oid                     oid;
40 } CatalogId;
41
42 typedef int DumpId;
43
44 /*
45  * Data structures for simple lists of OIDs and strings.  The support for
46  * these is very primitive compared to the backend's List facilities, but
47  * it's all we need in pg_dump.
48  */
49
50 typedef struct SimpleOidListCell
51 {
52         struct SimpleOidListCell *next;
53         Oid                     val;
54 } SimpleOidListCell;
55
56 typedef struct SimpleOidList
57 {
58         SimpleOidListCell *head;
59         SimpleOidListCell *tail;
60 } SimpleOidList;
61
62 typedef struct SimpleStringListCell
63 {
64         struct SimpleStringListCell *next;
65         char            val[1];                 /* VARIABLE LENGTH FIELD */
66 } SimpleStringListCell;
67
68 typedef struct SimpleStringList
69 {
70         SimpleStringListCell *head;
71         SimpleStringListCell *tail;
72 } SimpleStringList;
73
74 /*
75  * The data structures used to store system catalog information.  Every
76  * dumpable object is a subclass of DumpableObject.
77  *
78  * NOTE: the structures described here live for the entire pg_dump run;
79  * and in most cases we make a struct for every object we can find in the
80  * catalogs, not only those we are actually going to dump.      Hence, it's
81  * best to store a minimal amount of per-object info in these structs,
82  * and retrieve additional per-object info when and if we dump a specific
83  * object.      In particular, try to avoid retrieving expensive-to-compute
84  * information until it's known to be needed.  We do, however, have to
85  * store enough info to determine whether an object should be dumped and
86  * what order to dump in.
87  */
88
89 typedef enum
90 {
91         /* When modifying this enum, update priority tables in pg_dump_sort.c! */
92         DO_NAMESPACE,
93         DO_TYPE,
94         DO_SHELL_TYPE,
95         DO_FUNC,
96         DO_AGG,
97         DO_OPERATOR,
98         DO_OPCLASS,
99         DO_CONVERSION,
100         DO_TABLE,
101         DO_ATTRDEF,
102         DO_INDEX,
103         DO_RULE,
104         DO_TRIGGER,
105         DO_CONSTRAINT,
106         DO_FK_CONSTRAINT,                       /* see note for ConstraintInfo */
107         DO_PROCLANG,
108         DO_CAST,
109         DO_TABLE_DATA,
110         DO_TABLE_TYPE,
111         DO_BLOBS,
112         DO_BLOB_COMMENTS
113 } DumpableObjectType;
114
115 typedef struct _dumpableObject
116 {
117         DumpableObjectType objType;
118         CatalogId       catId;                  /* zero if not a cataloged object */
119         DumpId          dumpId;                 /* assigned by AssignDumpId() */
120         char       *name;                       /* object name (should never be NULL) */
121         struct _namespaceInfo *namespace;       /* containing namespace, or NULL */
122         bool            dump;                   /* true if we want to dump this object */
123         DumpId     *dependencies;       /* dumpIds of objects this one depends on */
124         int                     nDeps;                  /* number of valid dependencies */
125         int                     allocDeps;              /* allocated size of dependencies[] */
126 } DumpableObject;
127
128 typedef struct _namespaceInfo
129 {
130         DumpableObject dobj;
131         char       *rolname;            /* name of owner, or empty string */
132         char       *nspacl;
133 } NamespaceInfo;
134
135 typedef struct _typeInfo
136 {
137         DumpableObject dobj;
138
139         /*
140          * Note: dobj.name is the pg_type.typname entry.  format_type() might
141          * produce something different than typname
142          */
143         char       *rolname;            /* name of owner, or empty string */
144         Oid                     typelem;
145         Oid                     typrelid;
146         char            typrelkind;             /* 'r', 'v', 'c', etc */
147         char            typtype;                /* 'b', 'c', etc */
148         bool            isArray;                /* true if user-defined array type */
149         bool            isDefined;              /* true if typisdefined */
150         /* If it's a dumpable base type, we create a "shell type" entry for it */
151         struct _shellTypeInfo *shellType;       /* shell-type entry, or NULL */
152         /* If it's a domain, we store links to its constraints here: */
153         int                     nDomChecks;
154         struct _constraintInfo *domChecks;
155 } TypeInfo;
156
157 typedef struct _shellTypeInfo
158 {
159         DumpableObject dobj;
160
161         TypeInfo   *baseType;           /* back link to associated base type */
162 } ShellTypeInfo;
163
164 typedef struct _funcInfo
165 {
166         DumpableObject dobj;
167         char       *rolname;            /* name of owner, or empty string */
168         Oid                     lang;
169         int                     nargs;
170         Oid                *argtypes;
171         Oid                     prorettype;
172         char       *proacl;
173 } FuncInfo;
174
175 /* AggInfo is a superset of FuncInfo */
176 typedef struct _aggInfo
177 {
178         FuncInfo        aggfn;
179         /* we don't require any other fields at the moment */
180 } AggInfo;
181
182 typedef struct _oprInfo
183 {
184         DumpableObject dobj;
185         char       *rolname;
186         Oid                     oprcode;
187 } OprInfo;
188
189 typedef struct _opclassInfo
190 {
191         DumpableObject dobj;
192         char       *rolname;
193 } OpclassInfo;
194
195 typedef struct _convInfo
196 {
197         DumpableObject dobj;
198         char       *rolname;
199 } ConvInfo;
200
201 typedef struct _tableInfo
202 {
203         /*
204          * These fields are collected for every table in the database.
205          */
206         DumpableObject dobj;
207         char       *rolname;            /* name of owner, or empty string */
208         char       *relacl;
209         char            relkind;
210         char       *reltablespace;      /* relation tablespace */
211         char       *reloptions;         /* options specified by WITH (...) */
212         bool            hasindex;               /* does it have any indexes? */
213         bool            hasrules;               /* does it have any rules? */
214         bool            hasoids;                /* does it have OIDs? */
215         int                     ncheck;                 /* # of CHECK expressions */
216         int                     ntrig;                  /* # of triggers */
217         /* these two are set only if table is a sequence owned by a column: */
218         Oid                     owning_tab;             /* OID of table owning sequence */
219         int                     owning_col;             /* attr # of column owning sequence */
220
221         bool            interesting;    /* true if need to collect more data */
222
223         /*
224          * These fields are computed only if we decide the table is interesting
225          * (it's either a table to dump, or a direct parent of a dumpable table).
226          */
227         int                     numatts;                /* number of attributes */
228         char      **attnames;           /* the attribute names */
229         char      **atttypnames;        /* attribute type names */
230         int                *atttypmod;          /* type-specific type modifiers */
231         int                *attstattarget;      /* attribute statistics targets */
232         char       *attstorage;         /* attribute storage scheme */
233         char       *typstorage;         /* type storage scheme */
234         bool       *attisdropped;       /* true if attr is dropped; don't dump it */
235         bool       *attislocal;         /* true if attr has local definition */
236
237         /*
238          * Note: we need to store per-attribute notnull, default, and constraint
239          * stuff for all interesting tables so that we can tell which constraints
240          * were inherited.
241          */
242         bool       *notnull;            /* Not null constraints on attributes */
243         struct _attrDefInfo **attrdefs;         /* DEFAULT expressions */
244         bool       *inhAttrs;           /* true if each attribute is inherited */
245         bool       *inhAttrDef;         /* true if attr's default is inherited */
246         bool       *inhNotNull;         /* true if NOT NULL is inherited */
247         struct _constraintInfo *checkexprs; /* CHECK constraints */
248
249         /*
250          * Stuff computed only for dumpable tables.
251          */
252         int                     numParents;             /* number of (immediate) parent tables */
253         struct _tableInfo **parents;    /* TableInfos of immediate parents */
254 } TableInfo;
255
256 typedef struct _attrDefInfo
257 {
258         DumpableObject dobj;
259         TableInfo  *adtable;            /* link to table of attribute */
260         int                     adnum;
261         char       *adef_expr;          /* decompiled DEFAULT expression */
262         bool            separate;               /* TRUE if must dump as separate item */
263 } AttrDefInfo;
264
265 typedef struct _tableDataInfo
266 {
267         DumpableObject dobj;
268         TableInfo  *tdtable;            /* link to table to dump */
269         bool            oids;                   /* include OIDs in data? */
270 } TableDataInfo;
271
272 typedef struct _indxInfo
273 {
274         DumpableObject dobj;
275         TableInfo  *indextable;         /* link to table the index is for */
276         char       *indexdef;
277         char       *tablespace;         /* tablespace in which index is stored */
278         char       *options;            /* options specified by WITH (...) */
279         int                     indnkeys;
280         Oid                *indkeys;
281         bool            indisclustered;
282         /* if there is an associated constraint object, its dumpId: */
283         DumpId          indexconstraint;
284 } IndxInfo;
285
286 typedef struct _ruleInfo
287 {
288         DumpableObject dobj;
289         TableInfo  *ruletable;          /* link to table the rule is for */
290         char            ev_type;
291         bool            is_instead;
292         bool            separate;               /* TRUE if must dump as separate item */
293         /* separate is always true for non-ON SELECT rules */
294 } RuleInfo;
295
296 typedef struct _triggerInfo
297 {
298         DumpableObject dobj;
299         TableInfo  *tgtable;            /* link to table the trigger is for */
300         char       *tgfname;
301         int                     tgtype;
302         int                     tgnargs;
303         char       *tgargs;
304         bool            tgisconstraint;
305         char       *tgconstrname;
306         Oid                     tgconstrrelid;
307         char       *tgconstrrelname;
308         bool            tgenabled;
309         bool            tgdeferrable;
310         bool            tginitdeferred;
311 } TriggerInfo;
312
313 /*
314  * struct ConstraintInfo is used for all constraint types.      However we
315  * use a different objType for foreign key constraints, to make it easier
316  * to sort them the way we want.
317  */
318 typedef struct _constraintInfo
319 {
320         DumpableObject dobj;
321         TableInfo  *contable;           /* NULL if domain constraint */
322         TypeInfo   *condomain;          /* NULL if table constraint */
323         char            contype;
324         char       *condef;                     /* definition, if CHECK or FOREIGN KEY */
325         DumpId          conindex;               /* identifies associated index if any */
326         bool            coninherited;   /* TRUE if appears to be inherited */
327         bool            separate;               /* TRUE if must dump as separate item */
328 } ConstraintInfo;
329
330 typedef struct _procLangInfo
331 {
332         DumpableObject dobj;
333         bool            lanpltrusted;
334         Oid                     lanplcallfoid;
335         Oid                     lanvalidator;
336         char       *lanacl;
337         char       *lanowner;           /* name of owner, or empty string */
338 } ProcLangInfo;
339
340 typedef struct _castInfo
341 {
342         DumpableObject dobj;
343         Oid                     castsource;
344         Oid                     casttarget;
345         Oid                     castfunc;
346         char            castcontext;
347 } CastInfo;
348
349 /* InhInfo isn't a DumpableObject, just temporary state */
350 typedef struct _inhInfo
351 {
352         Oid                     inhrelid;               /* OID of a child table */
353         Oid                     inhparent;              /* OID of its parent */
354 } InhInfo;
355
356
357 /* global decls */
358 extern bool force_quotes;               /* double-quotes for identifiers flag */
359 extern bool g_verbose;                  /* verbose flag */
360
361 /* placeholders for comment starting and ending delimiters */
362 extern char g_comment_start[10];
363 extern char g_comment_end[10];
364
365 extern char g_opaque_type[10];  /* name for the opaque type */
366
367 /*
368  *      common utility functions
369  */
370
371 extern TableInfo *getSchemaData(int *numTablesPtr);
372
373 typedef enum _OidOptions
374 {
375         zeroAsOpaque = 1,
376         zeroAsAny = 2,
377         zeroAsStar = 4,
378         zeroAsNone = 8
379 } OidOptions;
380
381 extern void AssignDumpId(DumpableObject *dobj);
382 extern DumpId createDumpId(void);
383 extern DumpId getMaxDumpId(void);
384 extern DumpableObject *findObjectByDumpId(DumpId dumpId);
385 extern DumpableObject *findObjectByCatalogId(CatalogId catalogId);
386 extern void getDumpableObjects(DumpableObject ***objs, int *numObjs);
387
388 extern void addObjectDependency(DumpableObject *dobj, DumpId refId);
389 extern void removeObjectDependency(DumpableObject *dobj, DumpId refId);
390
391 extern TableInfo *findTableByOid(Oid oid);
392 extern TypeInfo *findTypeByOid(Oid oid);
393 extern FuncInfo *findFuncByOid(Oid oid);
394 extern OprInfo *findOprByOid(Oid oid);
395
396 extern void simple_oid_list_append(SimpleOidList *list, Oid val);
397 extern void simple_string_list_append(SimpleStringList *list, const char *val);
398 extern bool simple_oid_list_member(SimpleOidList *list, Oid val);
399 extern bool simple_string_list_member(SimpleStringList *list, const char *val);
400
401 extern char *pg_strdup(const char *string);
402 extern void *pg_malloc(size_t size);
403 extern void *pg_calloc(size_t nmemb, size_t size);
404 extern void *pg_realloc(void *ptr, size_t size);
405
406 extern void check_conn_and_db(void);
407 extern void exit_nicely(void);
408
409 extern void parseOidArray(const char *str, Oid *array, int arraysize);
410
411 extern void sortDumpableObjects(DumpableObject **objs, int numObjs);
412 extern void sortDumpableObjectsByTypeName(DumpableObject **objs, int numObjs);
413 extern void sortDumpableObjectsByTypeOid(DumpableObject **objs, int numObjs);
414
415 /*
416  * version specific routines
417  */
418 extern NamespaceInfo *getNamespaces(int *numNamespaces);
419 extern TypeInfo *getTypes(int *numTypes);
420 extern FuncInfo *getFuncs(int *numFuncs);
421 extern AggInfo *getAggregates(int *numAggregates);
422 extern OprInfo *getOperators(int *numOperators);
423 extern OpclassInfo *getOpclasses(int *numOpclasses);
424 extern ConvInfo *getConversions(int *numConversions);
425 extern TableInfo *getTables(int *numTables);
426 extern InhInfo *getInherits(int *numInherits);
427 extern void getIndexes(TableInfo tblinfo[], int numTables);
428 extern void getConstraints(TableInfo tblinfo[], int numTables);
429 extern RuleInfo *getRules(int *numRules);
430 extern void getTriggers(TableInfo tblinfo[], int numTables);
431 extern ProcLangInfo *getProcLangs(int *numProcLangs);
432 extern CastInfo *getCasts(int *numCasts);
433 extern void getTableAttrs(TableInfo *tbinfo, int numTables);
434
435 #endif   /* PG_DUMP_H */