Header And Logo

PostgreSQL
| The world's most advanced open source database.

pg_backup_archiver.h

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * pg_backup_archiver.h
00004  *
00005  *  Private interface to the pg_dump archiver routines.
00006  *  It is NOT intended that these routines be called by any
00007  *  dumper directly.
00008  *
00009  *  See the headers to pg_restore for more details.
00010  *
00011  * Copyright (c) 2000, Philip Warner
00012  *      Rights are granted to use this software in any way so long
00013  *      as this notice is not removed.
00014  *
00015  *  The author is not responsible for loss or damages that may
00016  *  result from it's use.
00017  *
00018  *
00019  * IDENTIFICATION
00020  *      src/bin/pg_dump/pg_backup_archiver.h
00021  *
00022  *-------------------------------------------------------------------------
00023  */
00024 
00025 #ifndef __PG_BACKUP_ARCHIVE__
00026 #define __PG_BACKUP_ARCHIVE__
00027 
00028 #include "postgres_fe.h"
00029 
00030 #include <time.h>
00031 
00032 #include "pg_backup.h"
00033 
00034 #include "libpq-fe.h"
00035 #include "pqexpbuffer.h"
00036 
00037 #define LOBBUFSIZE 16384
00038 
00039 /*
00040  * Note: zlib.h must be included *after* libpq-fe.h, because the latter may
00041  * include ssl.h, which has a naming conflict with zlib.h.
00042  */
00043 #ifdef HAVE_LIBZ
00044 #include <zlib.h>
00045 #define GZCLOSE(fh) gzclose(fh)
00046 #define GZWRITE(p, s, n, fh) gzwrite(fh, p, (n) * (s))
00047 #define GZREAD(p, s, n, fh) gzread(fh, p, (n) * (s))
00048 #else
00049 #define GZCLOSE(fh) fclose(fh)
00050 #define GZWRITE(p, s, n, fh) (fwrite(p, s, n, fh) * (s))
00051 #define GZREAD(p, s, n, fh) fread(p, s, n, fh)
00052 /* this is just the redefinition of a libz constant */
00053 #define Z_DEFAULT_COMPRESSION (-1)
00054 
00055 typedef struct _z_stream
00056 {
00057     void       *next_in;
00058     void       *next_out;
00059     size_t      avail_in;
00060     size_t      avail_out;
00061 } z_stream;
00062 typedef z_stream *z_streamp;
00063 #endif
00064 
00065 /* Current archive version number (the format we can output) */
00066 #define K_VERS_MAJOR 1
00067 #define K_VERS_MINOR 12
00068 #define K_VERS_REV 0
00069 
00070 /* Data block types */
00071 #define BLK_DATA 1
00072 #define BLK_BLOBS 3
00073 
00074 /* Historical version numbers (checked in code) */
00075 #define K_VERS_1_0 (( (1 * 256 + 0) * 256 + 0) * 256 + 0)
00076 #define K_VERS_1_2 (( (1 * 256 + 2) * 256 + 0) * 256 + 0)       /* Allow No ZLIB */
00077 #define K_VERS_1_3 (( (1 * 256 + 3) * 256 + 0) * 256 + 0)       /* BLOBs */
00078 #define K_VERS_1_4 (( (1 * 256 + 4) * 256 + 0) * 256 + 0)       /* Date & name in header */
00079 #define K_VERS_1_5 (( (1 * 256 + 5) * 256 + 0) * 256 + 0)       /* Handle dependencies */
00080 #define K_VERS_1_6 (( (1 * 256 + 6) * 256 + 0) * 256 + 0)       /* Schema field in TOCs */
00081 #define K_VERS_1_7 (( (1 * 256 + 7) * 256 + 0) * 256 + 0)       /* File Offset size in
00082                                                                  * header */
00083 #define K_VERS_1_8 (( (1 * 256 + 8) * 256 + 0) * 256 + 0)       /* change interpretation
00084                                                                  * of ID numbers and
00085                                                                  * dependencies */
00086 #define K_VERS_1_9 (( (1 * 256 + 9) * 256 + 0) * 256 + 0)       /* add default_with_oids
00087                                                                  * tracking */
00088 #define K_VERS_1_10 (( (1 * 256 + 10) * 256 + 0) * 256 + 0)     /* add tablespace */
00089 #define K_VERS_1_11 (( (1 * 256 + 11) * 256 + 0) * 256 + 0)     /* add toc section
00090                                                                  * indicator */
00091 #define K_VERS_1_12 (( (1 * 256 + 12) * 256 + 0) * 256 + 0)     /* add separate BLOB
00092                                                                  * entries */
00093 
00094 /* Newest format we can read */
00095 #define K_VERS_MAX (( (1 * 256 + 12) * 256 + 255) * 256 + 0)
00096 
00097 
00098 /* Flags to indicate disposition of offsets stored in files */
00099 #define K_OFFSET_POS_NOT_SET 1
00100 #define K_OFFSET_POS_SET 2
00101 #define K_OFFSET_NO_DATA 3
00102 
00103 /*
00104  * Special exit values from worker children.  We reserve 0 for normal
00105  * success; 1 and other small values should be interpreted as crashes.
00106  */
00107 #define WORKER_OK                     0
00108 #define WORKER_CREATE_DONE            10
00109 #define WORKER_INHIBIT_DATA           11
00110 #define WORKER_IGNORED_ERRORS         12
00111 
00112 struct _archiveHandle;
00113 struct _tocEntry;
00114 struct _restoreList;
00115 struct ParallelArgs;
00116 struct ParallelState;
00117 
00118 typedef enum T_Action
00119 {
00120     ACT_DUMP,
00121     ACT_RESTORE
00122 } T_Action;
00123 
00124 typedef void (*ClosePtr) (struct _archiveHandle * AH);
00125 typedef void (*ReopenPtr) (struct _archiveHandle * AH);
00126 typedef void (*ArchiveEntryPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
00127 
00128 typedef void (*StartDataPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
00129 typedef size_t (*WriteDataPtr) (struct _archiveHandle * AH, const void *data, size_t dLen);
00130 typedef void (*EndDataPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
00131 
00132 typedef void (*StartBlobsPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
00133 typedef void (*StartBlobPtr) (struct _archiveHandle * AH, struct _tocEntry * te, Oid oid);
00134 typedef void (*EndBlobPtr) (struct _archiveHandle * AH, struct _tocEntry * te, Oid oid);
00135 typedef void (*EndBlobsPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
00136 
00137 typedef int (*WriteBytePtr) (struct _archiveHandle * AH, const int i);
00138 typedef int (*ReadBytePtr) (struct _archiveHandle * AH);
00139 typedef size_t (*WriteBufPtr) (struct _archiveHandle * AH, const void *c, size_t len);
00140 typedef size_t (*ReadBufPtr) (struct _archiveHandle * AH, void *buf, size_t len);
00141 typedef void (*SaveArchivePtr) (struct _archiveHandle * AH);
00142 typedef void (*WriteExtraTocPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
00143 typedef void (*ReadExtraTocPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
00144 typedef void (*PrintExtraTocPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
00145 typedef void (*PrintTocDataPtr) (struct _archiveHandle * AH, struct _tocEntry * te, RestoreOptions *ropt);
00146 
00147 typedef void (*ClonePtr) (struct _archiveHandle * AH);
00148 typedef void (*DeClonePtr) (struct _archiveHandle * AH);
00149 
00150 typedef char *(*WorkerJobRestorePtr) (struct _archiveHandle * AH, struct _tocEntry * te);
00151 typedef char *(*WorkerJobDumpPtr) (struct _archiveHandle * AH, struct _tocEntry * te);
00152 typedef char *(*MasterStartParallelItemPtr) (struct _archiveHandle * AH, struct _tocEntry * te,
00153                                                          T_Action act);
00154 typedef int (*MasterEndParallelItemPtr) (struct _archiveHandle * AH, struct _tocEntry * te,
00155                                          const char *str, T_Action act);
00156 
00157 typedef size_t (*CustomOutPtr) (struct _archiveHandle * AH, const void *buf, size_t len);
00158 
00159 typedef enum
00160 {
00161     SQL_SCAN = 0,               /* normal */
00162     SQL_IN_SINGLE_QUOTE,        /* '...' literal */
00163     SQL_IN_DOUBLE_QUOTE         /* "..." identifier */
00164 } sqlparseState;
00165 
00166 typedef struct
00167 {
00168     sqlparseState state;        /* see above */
00169     bool        backSlash;      /* next char is backslash quoted? */
00170     PQExpBuffer curCmd;         /* incomplete line (NULL if not created) */
00171 } sqlparseInfo;
00172 
00173 typedef enum
00174 {
00175     STAGE_NONE = 0,
00176     STAGE_INITIALIZING,
00177     STAGE_PROCESSING,
00178     STAGE_FINALIZING
00179 } ArchiverStage;
00180 
00181 typedef enum
00182 {
00183     OUTPUT_SQLCMDS = 0,         /* emitting general SQL commands */
00184     OUTPUT_COPYDATA,            /* writing COPY data */
00185     OUTPUT_OTHERDATA            /* writing data as INSERT commands */
00186 } ArchiverOutput;
00187 
00188 typedef enum
00189 {
00190     REQ_SCHEMA = 0x01,          /* want schema */
00191     REQ_DATA = 0x02,            /* want data */
00192     REQ_SPECIAL = 0x04          /* for special TOC entries */
00193 } teReqs;
00194 
00195 typedef struct _archiveHandle
00196 {
00197     Archive     public;         /* Public part of archive */
00198     char        vmaj;           /* Version of file */
00199     char        vmin;
00200     char        vrev;
00201     int         version;        /* Conveniently formatted version */
00202 
00203     char       *archiveRemoteVersion;   /* When reading an archive, the
00204                                          * version of the dumped DB */
00205     char       *archiveDumpVersion;     /* When reading an archive, the
00206                                          * version of the dumper */
00207 
00208     int         debugLevel;     /* Used for logging (currently only by
00209                                  * --verbose) */
00210     size_t      intSize;        /* Size of an integer in the archive */
00211     size_t      offSize;        /* Size of a file offset in the archive -
00212                                  * Added V1.7 */
00213     ArchiveFormat format;       /* Archive format */
00214 
00215     sqlparseInfo sqlparse;      /* state for parsing INSERT data */
00216 
00217     time_t      createDate;     /* Date archive created */
00218 
00219     /*
00220      * Fields used when discovering header. A format can always get the
00221      * previous read bytes from here...
00222      */
00223     int         readHeader;     /* Used if file header has been read already */
00224     char       *lookahead;      /* Buffer used when reading header to discover
00225                                  * format */
00226     size_t      lookaheadSize;  /* Size of allocated buffer */
00227     size_t      lookaheadLen;   /* Length of data in lookahead */
00228     pgoff_t     lookaheadPos;   /* Current read position in lookahead buffer */
00229 
00230     ArchiveEntryPtr ArchiveEntryPtr;    /* Called for each metadata object */
00231     StartDataPtr StartDataPtr;  /* Called when table data is about to be
00232                                  * dumped */
00233     WriteDataPtr WriteDataPtr;  /* Called to send some table data to the
00234                                  * archive */
00235     EndDataPtr EndDataPtr;      /* Called when table data dump is finished */
00236     WriteBytePtr WriteBytePtr;  /* Write a byte to output */
00237     ReadBytePtr ReadBytePtr;    /* Read a byte from an archive */
00238     WriteBufPtr WriteBufPtr;    /* Write a buffer of output to the archive */
00239     ReadBufPtr ReadBufPtr;      /* Read a buffer of input from the archive */
00240     ClosePtr ClosePtr;          /* Close the archive */
00241     ReopenPtr ReopenPtr;        /* Reopen the archive */
00242     WriteExtraTocPtr WriteExtraTocPtr;  /* Write extra TOC entry data
00243                                          * associated with the current archive
00244                                          * format */
00245     ReadExtraTocPtr ReadExtraTocPtr;    /* Read extr info associated with
00246                                          * archie format */
00247     PrintExtraTocPtr PrintExtraTocPtr;  /* Extra TOC info for format */
00248     PrintTocDataPtr PrintTocDataPtr;
00249 
00250     StartBlobsPtr StartBlobsPtr;
00251     EndBlobsPtr EndBlobsPtr;
00252     StartBlobPtr StartBlobPtr;
00253     EndBlobPtr EndBlobPtr;
00254 
00255     MasterStartParallelItemPtr MasterStartParallelItemPtr;
00256     MasterEndParallelItemPtr MasterEndParallelItemPtr;
00257 
00258     SetupWorkerPtr SetupWorkerPtr;
00259     WorkerJobDumpPtr WorkerJobDumpPtr;
00260     WorkerJobRestorePtr WorkerJobRestorePtr;
00261 
00262     ClonePtr ClonePtr;          /* Clone format-specific fields */
00263     DeClonePtr DeClonePtr;      /* Clean up cloned fields */
00264 
00265     CustomOutPtr CustomOutPtr;  /* Alternative script output routine */
00266 
00267     /* Stuff for direct DB connection */
00268     char       *archdbname;     /* DB name *read* from archive */
00269     enum trivalue promptPassword;
00270     char       *savedPassword;  /* password for ropt->username, if known */
00271     char       *use_role;
00272     PGconn     *connection;
00273     int         connectToDB;    /* Flag to indicate if direct DB connection is
00274                                  * required */
00275     ArchiverOutput outputKind;  /* Flag for what we're currently writing */
00276     bool        pgCopyIn;       /* Currently in libpq 'COPY IN' mode. */
00277 
00278     int         loFd;           /* BLOB fd */
00279     int         writingBlob;    /* Flag */
00280     int         blobCount;      /* # of blobs restored */
00281 
00282     char       *fSpec;          /* Archive File Spec */
00283     FILE       *FH;             /* General purpose file handle */
00284     void       *OF;
00285     int         gzOut;          /* Output file */
00286 
00287     struct _tocEntry *toc;      /* Header of circular list of TOC entries */
00288     int         tocCount;       /* Number of TOC entries */
00289     DumpId      maxDumpId;      /* largest DumpId among all TOC entries */
00290 
00291     /* arrays created after the TOC list is complete: */
00292     struct _tocEntry **tocsByDumpId;    /* TOCs indexed by dumpId */
00293     DumpId     *tableDataId;    /* TABLE DATA ids, indexed by table dumpId */
00294 
00295     struct _tocEntry *currToc;  /* Used when dumping data */
00296     int         compression;    /* Compression requested on open Possible
00297                                  * values for compression: -1
00298                                  * Z_DEFAULT_COMPRESSION 0  COMPRESSION_NONE
00299                                  * 1-9 levels for gzip compression */
00300     ArchiveMode mode;           /* File mode - r or w */
00301     void       *formatData;     /* Header data specific to file format */
00302 
00303     RestoreOptions *ropt;       /* Used to check restore options in ahwrite
00304                                  * etc */
00305 
00306     /* these vars track state to avoid sending redundant SET commands */
00307     char       *currUser;       /* current username, or NULL if unknown */
00308     char       *currSchema;     /* current schema, or NULL */
00309     char       *currTablespace; /* current tablespace, or NULL */
00310     bool        currWithOids;   /* current default_with_oids setting */
00311 
00312     void       *lo_buf;
00313     size_t      lo_buf_used;
00314     size_t      lo_buf_size;
00315 
00316     int         noTocComments;
00317     ArchiverStage stage;
00318     ArchiverStage lastErrorStage;
00319     struct _tocEntry *currentTE;
00320     struct _tocEntry *lastErrorTE;
00321 } ArchiveHandle;
00322 
00323 typedef struct _tocEntry
00324 {
00325     struct _tocEntry *prev;
00326     struct _tocEntry *next;
00327     CatalogId   catalogId;
00328     DumpId      dumpId;
00329     teSection   section;
00330     bool        hadDumper;      /* Archiver was passed a dumper routine (used
00331                                  * in restore) */
00332     char       *tag;            /* index tag */
00333     char       *namespace;      /* null or empty string if not in a schema */
00334     char       *tablespace;     /* null if not in a tablespace; empty string
00335                                  * means use database default */
00336     char       *owner;
00337     bool        withOids;       /* Used only by "TABLE" tags */
00338     char       *desc;
00339     char       *defn;
00340     char       *dropStmt;
00341     char       *copyStmt;
00342     DumpId     *dependencies;   /* dumpIds of objects this one depends on */
00343     int         nDeps;          /* number of dependencies */
00344 
00345     DataDumperPtr dataDumper;   /* Routine to dump data for object */
00346     void       *dataDumperArg;  /* Arg for above routine */
00347     void       *formatData;     /* TOC Entry data specific to file format */
00348 
00349     /* working state while dumping/restoring */
00350     teReqs      reqs;           /* do we need schema and/or data of object */
00351     bool        created;        /* set for DATA member if TABLE was created */
00352 
00353     /* working state (needed only for parallel restore) */
00354     struct _tocEntry *par_prev; /* list links for pending/ready items; */
00355     struct _tocEntry *par_next; /* these are NULL if not in either list */
00356     int         depCount;       /* number of dependencies not yet restored */
00357     DumpId     *revDeps;        /* dumpIds of objects depending on this one */
00358     int         nRevDeps;       /* number of such dependencies */
00359     DumpId     *lockDeps;       /* dumpIds of objects this one needs lock on */
00360     int         nLockDeps;      /* number of such dependencies */
00361 } TocEntry;
00362 
00363 extern int  parallel_restore(struct ParallelArgs * args);
00364 extern void on_exit_close_archive(Archive *AHX);
00365 
00366 extern void warn_or_exit_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt,...) __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4)));
00367 
00368 extern void WriteTOC(ArchiveHandle *AH);
00369 extern void ReadTOC(ArchiveHandle *AH);
00370 extern void WriteHead(ArchiveHandle *AH);
00371 extern void ReadHead(ArchiveHandle *AH);
00372 extern void WriteToc(ArchiveHandle *AH);
00373 extern void ReadToc(ArchiveHandle *AH);
00374 extern void WriteDataChunks(ArchiveHandle *AH, struct ParallelState *pstate);
00375 extern void WriteDataChunksForTocEntry(ArchiveHandle *AH, TocEntry *te);
00376 extern ArchiveHandle *CloneArchive(ArchiveHandle *AH);
00377 extern void DeCloneArchive(ArchiveHandle *AH);
00378 
00379 extern teReqs TocIDRequired(ArchiveHandle *AH, DumpId id);
00380 TocEntry   *getTocEntryByDumpId(ArchiveHandle *AH, DumpId id);
00381 extern bool checkSeek(FILE *fp);
00382 
00383 #define appendStringLiteralAHX(buf,str,AH) \
00384     appendStringLiteral(buf, str, (AH)->public.encoding, (AH)->public.std_strings)
00385 
00386 #define appendByteaLiteralAHX(buf,str,len,AH) \
00387     appendByteaLiteral(buf, str, len, (AH)->public.std_strings)
00388 
00389 /*
00390  * Mandatory routines for each supported format
00391  */
00392 
00393 extern size_t WriteInt(ArchiveHandle *AH, int i);
00394 extern int  ReadInt(ArchiveHandle *AH);
00395 extern char *ReadStr(ArchiveHandle *AH);
00396 extern size_t WriteStr(ArchiveHandle *AH, const char *s);
00397 
00398 int         ReadOffset(ArchiveHandle *, pgoff_t *);
00399 size_t      WriteOffset(ArchiveHandle *, pgoff_t, int);
00400 
00401 extern void StartRestoreBlobs(ArchiveHandle *AH);
00402 extern void StartRestoreBlob(ArchiveHandle *AH, Oid oid, bool drop);
00403 extern void EndRestoreBlob(ArchiveHandle *AH, Oid oid);
00404 extern void EndRestoreBlobs(ArchiveHandle *AH);
00405 
00406 extern void InitArchiveFmt_Custom(ArchiveHandle *AH);
00407 extern void InitArchiveFmt_Null(ArchiveHandle *AH);
00408 extern void InitArchiveFmt_Directory(ArchiveHandle *AH);
00409 extern void InitArchiveFmt_Tar(ArchiveHandle *AH);
00410 
00411 extern bool isValidTarHeader(char *header);
00412 
00413 extern int  ReconnectToServer(ArchiveHandle *AH, const char *dbname, const char *newUser);
00414 extern void DropBlobIfExists(ArchiveHandle *AH, Oid oid);
00415 
00416 int         ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH);
00417 int         ahprintf(ArchiveHandle *AH, const char *fmt,...) __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
00418 
00419 void        ahlog(ArchiveHandle *AH, int level, const char *fmt,...) __attribute__((format(PG_PRINTF_ATTRIBUTE, 3, 4)));
00420 
00421 #endif