OSDN Git Service

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