Debian Codegraph @

src/bin/pg_dump/pg_backup_archiver.h

raw
1 /*-------------------------------------------------------------------------
2 *
3 * pg_backup_archiver.h
4 *
5 * Private interface to the pg_dump archiver routines.
6 * It is NOT intended that these routines be called by any
7 * dumper directly.
8 *
9 * See the headers to pg_restore for more details.
10 *
11 * Copyright (c) 2000, Philip Warner
12 * Rights are granted to use this software in any way so long
13 * as this notice is not removed.
14 *
15 * The author is not responsible for loss or damages that may
16 * result from its use.
17 *
18 *
19 * IDENTIFICATION
20 * src/bin/pg_dump/pg_backup_archiver.h
21 *
22 *-------------------------------------------------------------------------
23 */
24 #ifndef __PG_BACKUP_ARCHIVE__
25 #define __PG_BACKUP_ARCHIVE__
26
27 #include <time.h>
28
29 #include "libpq-fe.h"
30 #include "pg_backup.h"
31 #include "pqexpbuffer.h"
32
33 #define LOBBUFSIZE 16384
34
35 /* Data block types */
36 #define BLK_DATA 1
37 #define BLK_BLOBS 3
38
39 /* Encode version components into a convenient integer <maj><min><rev> */
40 #define MAKE_ARCHIVE_VERSION(major, minor, rev) (((major) * 256 + (minor)) * 256 + (rev))
41
42 #define ARCHIVE_MAJOR(version) (((version) >> 16) & 255)
43 #define ARCHIVE_MINOR(version) (((version) >> 8) & 255)
44 #define ARCHIVE_REV(version) (((version) ) & 255)
45
46 /* Historical version numbers (checked in code) */
47 #define K_VERS_1_0 MAKE_ARCHIVE_VERSION(1, 0, 0)
48 #define K_VERS_1_2 MAKE_ARCHIVE_VERSION(1, 2, 0) /* Allow No ZLIB */
49 #define K_VERS_1_3 MAKE_ARCHIVE_VERSION(1, 3, 0) /* BLOBS */
50 #define K_VERS_1_4 MAKE_ARCHIVE_VERSION(1, 4, 0) /* Date & name in header */
51 #define K_VERS_1_5 MAKE_ARCHIVE_VERSION(1, 5, 0) /* Handle dependencies */
52 #define K_VERS_1_6 MAKE_ARCHIVE_VERSION(1, 6, 0) /* Schema field in TOCs */
53 #define K_VERS_1_7 MAKE_ARCHIVE_VERSION(1, 7, 0) /* File Offset size in
54 * header */
55 #define K_VERS_1_8 MAKE_ARCHIVE_VERSION(1, 8, 0) /* change interpretation
56 * of ID numbers and
57 * dependencies */
58 #define K_VERS_1_9 MAKE_ARCHIVE_VERSION(1, 9, 0) /* add default_with_oids
59 * tracking */
60 #define K_VERS_1_10 MAKE_ARCHIVE_VERSION(1, 10, 0) /* add tablespace */
61 #define K_VERS_1_11 MAKE_ARCHIVE_VERSION(1, 11, 0) /* add toc section
62 * indicator */
63 #define K_VERS_1_12 MAKE_ARCHIVE_VERSION(1, 12, 0) /* add separate BLOB
64 * entries */
65 #define K_VERS_1_13 MAKE_ARCHIVE_VERSION(1, 13, 0) /* change search_path
66 * behavior */
67 #define K_VERS_1_14 MAKE_ARCHIVE_VERSION(1, 14, 0) /* add tableam */
68 #define K_VERS_1_15 MAKE_ARCHIVE_VERSION(1, 15, 0) /* add
69 * compression_algorithm
70 * in header */
71 #define K_VERS_1_16 MAKE_ARCHIVE_VERSION(1, 16, 0) /* BLOB METADATA entries
72 * and multiple BLOBS,
73 * relkind */
74
75 /* Current archive version number (the format we can output) */
76 #define K_VERS_MAJOR 1
77 #define K_VERS_MINOR 16
78 #define K_VERS_REV 0
79 #define K_VERS_SELF MAKE_ARCHIVE_VERSION(K_VERS_MAJOR, K_VERS_MINOR, K_VERS_REV)
80
81 /* Newest format we can read */
82 #define K_VERS_MAX MAKE_ARCHIVE_VERSION(K_VERS_MAJOR, K_VERS_MINOR, 255)
83
84
85 /* Flags to indicate disposition of offsets stored in files */
86 #define K_OFFSET_POS_NOT_SET 1
87 #define K_OFFSET_POS_SET 2
88 #define K_OFFSET_NO_DATA 3
89
90 /*
91 * Special exit values from worker children. We reserve 0 for normal
92 * success; 1 and other small values should be interpreted as crashes.
93 */
94 #define WORKER_OK 0
95 #define WORKER_CREATE_DONE 10
96 #define WORKER_INHIBIT_DATA 11
97 #define WORKER_IGNORED_ERRORS 12
98
99 typedef struct _archiveHandle ArchiveHandle;
100 typedef struct _tocEntry TocEntry;
101 struct ParallelState;
102
103 #define READ_ERROR_EXIT(fd) \
104 do { \
105 if (feof(fd)) \
106 pg_fatal("could not read from input file: end of file"); \
107 else \
108 pg_fatal("could not read from input file: %m"); \
109 } while (0)
110
111 #define WRITE_ERROR_EXIT \
112 do { \
113 pg_fatal("could not write to output file: %m"); \
114 } while (0)
115
116 typedef enum T_Action
117 {
118 ACT_DUMP,
119 ACT_RESTORE,
120 } T_Action;
121
122 typedef void (*ClosePtrType) (ArchiveHandle *AH);
123 typedef void (*ReopenPtrType) (ArchiveHandle *AH);
124 typedef void (*ArchiveEntryPtrType) (ArchiveHandle *AH, TocEntry *te);
125
126 typedef void (*StartDataPtrType) (ArchiveHandle *AH, TocEntry *te);
127 typedef void (*WriteDataPtrType) (ArchiveHandle *AH, const void *data, size_t dLen);
128 typedef void (*EndDataPtrType) (ArchiveHandle *AH, TocEntry *te);
129
130 typedef void (*StartLOsPtrType) (ArchiveHandle *AH, TocEntry *te);
131 typedef void (*StartLOPtrType) (ArchiveHandle *AH, TocEntry *te, Oid oid);
132 typedef void (*EndLOPtrType) (ArchiveHandle *AH, TocEntry *te, Oid oid);
133 typedef void (*EndLOsPtrType) (ArchiveHandle *AH, TocEntry *te);
134
135 typedef int (*WriteBytePtrType) (ArchiveHandle *AH, const int i);
136 typedef int (*ReadBytePtrType) (ArchiveHandle *AH);
137 typedef void (*WriteBufPtrType) (ArchiveHandle *AH, const void *c, size_t len);
138 typedef void (*ReadBufPtrType) (ArchiveHandle *AH, void *buf, size_t len);
139 typedef void (*WriteExtraTocPtrType) (ArchiveHandle *AH, TocEntry *te);
140 typedef void (*ReadExtraTocPtrType) (ArchiveHandle *AH, TocEntry *te);
141 typedef void (*PrintExtraTocPtrType) (ArchiveHandle *AH, TocEntry *te);
142 typedef void (*PrintTocDataPtrType) (ArchiveHandle *AH, TocEntry *te);
143
144 typedef void (*PrepParallelRestorePtrType) (ArchiveHandle *AH);
145 typedef void (*ClonePtrType) (ArchiveHandle *AH);
146 typedef void (*DeClonePtrType) (ArchiveHandle *AH);
147
148 typedef int (*WorkerJobDumpPtrType) (ArchiveHandle *AH, TocEntry *te);
149 typedef int (*WorkerJobRestorePtrType) (ArchiveHandle *AH, TocEntry *te);
150
151 typedef size_t (*CustomOutPtrType) (ArchiveHandle *AH, const void *buf, size_t len);
152
153 typedef enum
154 {
155 SQL_SCAN = 0, /* normal */
156 SQL_IN_SINGLE_QUOTE, /* '...' literal */
157 SQL_IN_DOUBLE_QUOTE, /* "..." identifier */
158 } sqlparseState;
159
160 typedef struct
161 {
162 sqlparseState state; /* see above */
163 bool backSlash; /* next char is backslash quoted? */
164 PQExpBuffer curCmd; /* incomplete line (NULL if not created) */
165 } sqlparseInfo;
166
167 typedef enum
168 {
169 STAGE_NONE = 0,
170 STAGE_INITIALIZING,
171 STAGE_PROCESSING,
172 STAGE_FINALIZING,
173 } ArchiverStage;
174
175 typedef enum
176 {
177 OUTPUT_SQLCMDS = 0, /* emitting general SQL commands */
178 OUTPUT_COPYDATA, /* writing COPY data */
179 OUTPUT_OTHERDATA, /* writing data as INSERT commands */
180 } ArchiverOutput;
181
182 /*
183 * For historical reasons, ACL items are interspersed with everything else in
184 * a dump file's TOC; typically they're right after the object they're for.
185 * However, we need to restore data before ACLs, as otherwise a read-only
186 * table (ie one where the owner has revoked her own INSERT privilege) causes
187 * data restore failures. On the other hand, matview REFRESH commands should
188 * come out after ACLs, as otherwise non-superuser-owned matviews might not
189 * be able to execute. (If the permissions at the time of dumping would not
190 * allow a REFRESH, too bad; we won't fix that for you.) We also want event
191 * triggers to be restored after ACLs, so that they can't mess those up.
192 *
193 * These considerations force us to make three passes over the TOC,
194 * restoring the appropriate subset of items in each pass. We assume that
195 * the dependency sort resulted in an appropriate ordering of items within
196 * each subset.
197 *
198 * XXX This mechanism should be superseded by tracking dependencies on ACLs
199 * properly; but we'll still need it for old dump files even after that.
200 */
201 typedef enum
202 {
203 RESTORE_PASS_MAIN = 0, /* Main pass (most TOC item types) */
204 RESTORE_PASS_ACL, /* ACL item types */
205 RESTORE_PASS_POST_ACL, /* Event trigger and matview refresh items */
206
207 #define RESTORE_PASS_LAST RESTORE_PASS_POST_ACL
208 } RestorePass;
209
210 #define REQ_SCHEMA 0x01 /* want schema */
211 #define REQ_DATA 0x02 /* want data */
212 #define REQ_STATS 0x04
213 #define REQ_SPECIAL 0x08 /* for special TOC entries */
214
215 struct _archiveHandle
216 {
217 Archive public; /* Public part of archive */
218 int version; /* Version of file */
219
220 char *archiveRemoteVersion; /* When reading an archive, the
221 * version of the dumped DB */
222 char *archiveDumpVersion; /* When reading an archive, the version of
223 * the dumper */
224
225 size_t intSize; /* Size of an integer in the archive */
226 size_t offSize; /* Size of a file offset in the archive -
227 * Added V1.7 */
228 ArchiveFormat format; /* Archive format */
229
230 sqlparseInfo sqlparse; /* state for parsing INSERT data */
231
232 time_t createDate; /* Date archive created */
233
234 /*
235 * Fields used when discovering archive format. For tar format, we load
236 * the first block into the lookahead buffer, and verify that it looks
237 * like a tar header. The tar module must then consume bytes from the
238 * lookahead buffer before reading any more from the file. For custom
239 * format, we load only the "PGDMP" marker into the buffer, and then set
240 * readHeader after confirming it matches. The buffer is vestigial in
241 * this case, as the subsequent code just checks readHeader and doesn't
242 * examine the buffer.
243 */
244 int readHeader; /* Set if we already read "PGDMP" marker */
245 char *lookahead; /* Buffer used when reading header to discover
246 * format */
247 size_t lookaheadSize; /* Allocated size of buffer */
248 size_t lookaheadLen; /* Length of valid data in lookahead */
249 size_t lookaheadPos; /* Current read position in lookahead buffer */
250
251 ArchiveEntryPtrType ArchiveEntryPtr; /* Called for each metadata object */
252 StartDataPtrType StartDataPtr; /* Called when table data is about to be
253 * dumped */
254 WriteDataPtrType WriteDataPtr; /* Called to send some table data to the
255 * archive */
256 EndDataPtrType EndDataPtr; /* Called when table data dump is finished */
257 WriteBytePtrType WriteBytePtr; /* Write a byte to output */
258 ReadBytePtrType ReadBytePtr; /* Read a byte from an archive */
259 WriteBufPtrType WriteBufPtr; /* Write a buffer of output to the archive */
260 ReadBufPtrType ReadBufPtr; /* Read a buffer of input from the archive */
261 ClosePtrType ClosePtr; /* Close the archive */
262 ReopenPtrType ReopenPtr; /* Reopen the archive */
263 WriteExtraTocPtrType WriteExtraTocPtr; /* Write extra TOC entry data
264 * associated with the current
265 * archive format */
266 ReadExtraTocPtrType ReadExtraTocPtr; /* Read extra info associated with
267 * archive format */
268 PrintExtraTocPtrType PrintExtraTocPtr; /* Extra TOC info for format */
269 PrintTocDataPtrType PrintTocDataPtr;
270
271 StartLOsPtrType StartLOsPtr;
272 EndLOsPtrType EndLOsPtr;
273 StartLOPtrType StartLOPtr;
274 EndLOPtrType EndLOPtr;
275
276 SetupWorkerPtrType SetupWorkerPtr;
277 WorkerJobDumpPtrType WorkerJobDumpPtr;
278 WorkerJobRestorePtrType WorkerJobRestorePtr;
279
280 PrepParallelRestorePtrType PrepParallelRestorePtr;
281 ClonePtrType ClonePtr; /* Clone format-specific fields */
282 DeClonePtrType DeClonePtr; /* Clean up cloned fields */
283
284 CustomOutPtrType CustomOutPtr; /* Alternative script output routine */
285
286 /* Stuff for direct DB connection */
287 char *archdbname; /* DB name *read* from archive */
288 char *savedPassword; /* password for ropt->username, if known */
289 char *use_role;
290 PGconn *connection;
291 /* If connCancel isn't NULL, SIGINT handler will send a cancel */
292 PGcancel *volatile connCancel;
293
294 int connectToDB; /* Flag to indicate if direct DB connection is
295 * required */
296 ArchiverOutput outputKind; /* Flag for what we're currently writing */
297 bool pgCopyIn; /* Currently in libpq 'COPY IN' mode. */
298
299 int loFd;
300 bool writingLO;
301 int loCount; /* # of LOs restored */
302
303 char *fSpec; /* Archive File Spec */
304 FILE *FH; /* General purpose file handle */
305 void *OF; /* Output file */
306
307 struct _tocEntry *toc; /* Header of circular list of TOC entries */
308 int tocCount; /* Number of TOC entries */
309 DumpId maxDumpId; /* largest DumpId among all TOC entries */
310
311 /* arrays created after the TOC list is complete: */
312 struct _tocEntry **tocsByDumpId; /* TOCs indexed by dumpId */
313 DumpId *tableDataId; /* TABLE DATA ids, indexed by table dumpId */
314
315 struct _tocEntry *currToc; /* Used when dumping data */
316 pg_compress_specification compression_spec; /* Requested specification for
317 * compression */
318 bool dosync; /* data requested to be synced on sight */
319 DataDirSyncMethod sync_method;
320 ArchiveMode mode; /* File mode - r or w */
321 void *formatData; /* Header data specific to file format */
322
323 /* these vars track state to avoid sending redundant SET commands */
324 char *currUser; /* current username, or NULL if unknown */
325 char *currSchema; /* current schema, or NULL */
326 char *currTablespace; /* current tablespace, or NULL */
327 char *currTableAm; /* current table access method, or NULL */
328
329 /* in --transaction-size mode, this counts objects emitted in cur xact */
330 int txnCount;
331
332 void *lo_buf;
333 size_t lo_buf_used;
334 size_t lo_buf_size;
335
336 int noTocComments;
337 ArchiverStage stage;
338 ArchiverStage lastErrorStage;
339 RestorePass restorePass; /* used only during parallel restore */
340 struct _tocEntry *currentTE;
341 struct _tocEntry *lastErrorTE;
342 };
343
344
345 typedef char *(*DefnDumperPtr) (Archive *AH, const void *userArg, const TocEntry *te);
346 typedef int (*DataDumperPtr) (Archive *AH, const void *userArg);
347
348 struct _tocEntry
349 {
350 struct _tocEntry *prev;
351 struct _tocEntry *next;
352 CatalogId catalogId;
353 DumpId dumpId;
354 teSection section;
355 bool hadDumper; /* Archiver was passed a dumper routine (used
356 * in restore) */
357 char *tag; /* index tag */
358 char *namespace; /* null or empty string if not in a schema */
359 char *tablespace; /* null if not in a tablespace; empty string
360 * means use database default */
361 char *tableam; /* table access method, only for TABLE tags */
362 char relkind; /* relation kind, only for TABLE tags */
363 char *owner;
364 char *desc;
365 char *defn;
366 char *dropStmt;
367 char *copyStmt;
368 DumpId *dependencies; /* dumpIds of objects this one depends on */
369 int nDeps; /* number of dependencies */
370
371 DataDumperPtr dataDumper; /* Routine to dump data for object */
372 const void *dataDumperArg; /* Arg for above routine */
373 void *formatData; /* TOC Entry data specific to file format */
374
375 DefnDumperPtr defnDumper; /* routine to dump definition statement */
376 const void *defnDumperArg; /* arg for above routine */
377 size_t defnLen; /* length of dumped definition */
378
379 /* working state while dumping/restoring */
380 pgoff_t dataLength; /* item's data size; 0 if none or unknown */
381 int reqs; /* do we need schema and/or data of object
382 * (REQ_* bit mask) */
383 bool created; /* set for DATA member if TABLE was created */
384
385 /* working state (needed only for parallel restore) */
386 struct _tocEntry *pending_prev; /* list links for pending-items list; */
387 struct _tocEntry *pending_next; /* NULL if not in that list */
388 int depCount; /* number of dependencies not yet restored */
389 DumpId *revDeps; /* dumpIds of objects depending on this one */
390 int nRevDeps; /* number of such dependencies */
391 DumpId *lockDeps; /* dumpIds of objects this one needs lock on */
392 int nLockDeps; /* number of such dependencies */
393 };
394
395 extern int parallel_restore(ArchiveHandle *AH, TocEntry *te);
396 extern void on_exit_close_archive(Archive *AHX);
397
398 extern void warn_or_exit_horribly(ArchiveHandle *AH, const char *fmt,...) pg_attribute_printf(2, 3);
399
400 /* Options for ArchiveEntry */
401 typedef struct _archiveOpts
402 {
403 const char *tag;
404 const char *namespace;
405 const char *tablespace;
406 const char *tableam;
407 char relkind;
408 const char *owner;
409 const char *description;
410 teSection section;
411 const char *createStmt;
412 const char *dropStmt;
413 const char *copyStmt;
414 const DumpId *deps;
415 int nDeps;
416 DataDumperPtr dumpFn;
417 const void *dumpArg;
418 DefnDumperPtr defnFn;
419 const void *defnArg;
420 } ArchiveOpts;
421 #define ARCHIVE_OPTS(...) &(ArchiveOpts){__VA_ARGS__}
422 /* Called to add a TOC entry */
423 extern TocEntry *ArchiveEntry(Archive *AHX, CatalogId catalogId,
424 DumpId dumpId, ArchiveOpts *opts);
425
426 extern void WriteHead(ArchiveHandle *AH);
427 extern void ReadHead(ArchiveHandle *AH);
428 extern void WriteToc(ArchiveHandle *AH);
429 extern void ReadToc(ArchiveHandle *AH);
430 extern void WriteDataChunks(ArchiveHandle *AH, struct ParallelState *pstate);
431 extern void WriteDataChunksForTocEntry(ArchiveHandle *AH, TocEntry *te);
432 extern ArchiveHandle *CloneArchive(ArchiveHandle *AH);
433 extern void DeCloneArchive(ArchiveHandle *AH);
434
435 extern int TocIDRequired(ArchiveHandle *AH, DumpId id);
436 TocEntry *getTocEntryByDumpId(ArchiveHandle *AH, DumpId id);
437 extern bool checkSeek(FILE *fp);
438
439 #define appendStringLiteralAHX(buf,str,AH) \
440 appendStringLiteral(buf, str, (AH)->public.encoding, (AH)->public.std_strings)
441
442 #define appendByteaLiteralAHX(buf,str,len,AH) \
443 appendByteaLiteral(buf, str, len, (AH)->public.std_strings)
444
445 /*
446 * Mandatory routines for each supported format
447 */
448
449 extern size_t WriteInt(ArchiveHandle *AH, int i);
450 extern int ReadInt(ArchiveHandle *AH);
451 extern char *ReadStr(ArchiveHandle *AH);
452 extern size_t WriteStr(ArchiveHandle *AH, const char *c);
453
454 int ReadOffset(ArchiveHandle *, pgoff_t *);
455 size_t WriteOffset(ArchiveHandle *, pgoff_t, int);
456
457 extern void StartRestoreLOs(ArchiveHandle *AH);
458 extern void StartRestoreLO(ArchiveHandle *AH, Oid oid, bool drop);
459 extern void EndRestoreLO(ArchiveHandle *AH, Oid oid);
460 extern void EndRestoreLOs(ArchiveHandle *AH);
461
462 extern void InitArchiveFmt_Custom(ArchiveHandle *AH);
463 extern void InitArchiveFmt_Null(ArchiveHandle *AH);
464 extern void InitArchiveFmt_Directory(ArchiveHandle *AH);
465 extern void InitArchiveFmt_Tar(ArchiveHandle *AH);
466
467 extern void ReconnectToServer(ArchiveHandle *AH, const char *dbname);
468 extern void IssueCommandPerBlob(ArchiveHandle *AH, TocEntry *te,
469 const char *cmdBegin, const char *cmdEnd);
470 extern void IssueACLPerBlob(ArchiveHandle *AH, TocEntry *te);
471 extern void DropLOIfExists(ArchiveHandle *AH, Oid oid);
472
473 void ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH);
474 int ahprintf(ArchiveHandle *AH, const char *fmt,...) pg_attribute_printf(2, 3);
475
476 #endif