| 1 |
/*------------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* pg_backup_archiver.c |
| 4 |
* |
| 5 |
* Private implementation of the archiver routines. |
| 6 |
* |
| 7 |
* See the headers to pg_restore for more details. |
| 8 |
* |
| 9 |
* Copyright (c) 2000, Philip Warner |
| 10 |
* Rights are granted to use this software in any way so long |
| 11 |
* as this notice is not removed. |
| 12 |
* |
| 13 |
* The author is not responsible for loss or damages that may |
| 14 |
* result from its use. |
| 15 |
* |
| 16 |
* |
| 17 |
* IDENTIFICATION |
| 18 |
* src/bin/pg_dump/pg_backup_archiver.c |
| 19 |
* |
| 20 |
*------------------------------------------------------------------------- |
| 21 |
*/ |
| 22 |
#include "postgres_fe.h" |
| 23 |
|
| 24 |
#include <ctype.h> |
| 25 |
#include <fcntl.h> |
| 26 |
#include <unistd.h> |
| 27 |
#include <sys/stat.h> |
| 28 |
#include <sys/wait.h> |
| 29 |
#ifdef WIN32 |
| 30 |
#include <io.h> |
| 31 |
#endif |
| 32 |
|
| 33 |
#include "catalog/pg_class_d.h" |
| 34 |
#include "common/string.h" |
| 35 |
#include "compress_io.h" |
| 36 |
#include "dumputils.h" |
| 37 |
#include "fe_utils/string_utils.h" |
| 38 |
#include "lib/binaryheap.h" |
| 39 |
#include "lib/stringinfo.h" |
| 40 |
#include "libpq/libpq-fs.h" |
| 41 |
#include "parallel.h" |
| 42 |
#include "pg_backup_archiver.h" |
| 43 |
#include "pg_backup_db.h" |
| 44 |
#include "pg_backup_utils.h" |
| 45 |
#include "pgtar.h" |
| 46 |
|
| 47 |
#define TEXT_DUMP_HEADER "--\n-- PostgreSQL database dump\n--\n\n" |
| 48 |
#define TEXT_DUMPALL_HEADER "--\n-- PostgreSQL database cluster dump\n--\n\n" |
| 49 |
|
| 50 |
#define TOC_PREFIX_NONE "" |
| 51 |
#define TOC_PREFIX_DATA "Data for " |
| 52 |
#define TOC_PREFIX_STATS "Statistics for " |
| 53 |
|
| 54 |
static ArchiveHandle *_allocAH(const char *FileSpec, const ArchiveFormat fmt, |
| 55 |
const pg_compress_specification compression_spec, |
| 56 |
bool dosync, ArchiveMode mode, |
| 57 |
SetupWorkerPtrType setupWorkerPtr, |
| 58 |
DataDirSyncMethod sync_method); |
| 59 |
static void _getObjectDescription(PQExpBuffer buf, const TocEntry *te); |
| 60 |
static void _printTocEntry(ArchiveHandle *AH, TocEntry *te, const char *pfx); |
| 61 |
static void _doSetFixedOutputState(ArchiveHandle *AH); |
| 62 |
static void _doSetSessionAuth(ArchiveHandle *AH, const char *user); |
| 63 |
static void _reconnectToDB(ArchiveHandle *AH, const char *dbname); |
| 64 |
static void _becomeUser(ArchiveHandle *AH, const char *user); |
| 65 |
static void _becomeOwner(ArchiveHandle *AH, TocEntry *te); |
| 66 |
static void _selectOutputSchema(ArchiveHandle *AH, const char *schemaName); |
| 67 |
static void _selectTablespace(ArchiveHandle *AH, const char *tablespace); |
| 68 |
static void _selectTableAccessMethod(ArchiveHandle *AH, const char *tableam); |
| 69 |
static void _printTableAccessMethodNoStorage(ArchiveHandle *AH, |
| 70 |
TocEntry *te); |
| 71 |
static void processEncodingEntry(ArchiveHandle *AH, TocEntry *te); |
| 72 |
static void processStdStringsEntry(ArchiveHandle *AH, TocEntry *te); |
| 73 |
static void processSearchPathEntry(ArchiveHandle *AH, TocEntry *te); |
| 74 |
static int _tocEntryRequired(TocEntry *te, teSection curSection, ArchiveHandle *AH); |
| 75 |
static RestorePass _tocEntryRestorePass(TocEntry *te); |
| 76 |
static bool _tocEntryIsACL(TocEntry *te); |
| 77 |
static void _disableTriggersIfNecessary(ArchiveHandle *AH, TocEntry *te); |
| 78 |
static void _enableTriggersIfNecessary(ArchiveHandle *AH, TocEntry *te); |
| 79 |
static bool is_load_via_partition_root(TocEntry *te); |
| 80 |
static void buildTocEntryArrays(ArchiveHandle *AH); |
| 81 |
static void _moveBefore(TocEntry *pos, TocEntry *te); |
| 82 |
static int _discoverArchiveFormat(ArchiveHandle *AH); |
| 83 |
|
| 84 |
static int RestoringToDB(ArchiveHandle *AH); |
| 85 |
static void dump_lo_buf(ArchiveHandle *AH); |
| 86 |
static void dumpTimestamp(ArchiveHandle *AH, const char *msg, time_t tim); |
| 87 |
static void SetOutput(ArchiveHandle *AH, const char *filename, |
| 88 |
const pg_compress_specification compression_spec); |
| 89 |
static CompressFileHandle *SaveOutput(ArchiveHandle *AH); |
| 90 |
static void RestoreOutput(ArchiveHandle *AH, CompressFileHandle *savedOutput); |
| 91 |
|
| 92 |
static int restore_toc_entry(ArchiveHandle *AH, TocEntry *te, bool is_parallel); |
| 93 |
static void restore_toc_entries_prefork(ArchiveHandle *AH, |
| 94 |
TocEntry *pending_list); |
| 95 |
static void restore_toc_entries_parallel(ArchiveHandle *AH, |
| 96 |
ParallelState *pstate, |
| 97 |
TocEntry *pending_list); |
| 98 |
static void restore_toc_entries_postfork(ArchiveHandle *AH, |
| 99 |
TocEntry *pending_list); |
| 100 |
static void pending_list_header_init(TocEntry *l); |
| 101 |
static void pending_list_append(TocEntry *l, TocEntry *te); |
| 102 |
static void pending_list_remove(TocEntry *te); |
| 103 |
static int TocEntrySizeCompareQsort(const void *p1, const void *p2); |
| 104 |
static int TocEntrySizeCompareBinaryheap(void *p1, void *p2, void *arg); |
| 105 |
static void move_to_ready_heap(TocEntry *pending_list, |
| 106 |
binaryheap *ready_heap, |
| 107 |
RestorePass pass); |
| 108 |
static TocEntry *pop_next_work_item(binaryheap *ready_heap, |
| 109 |
ParallelState *pstate); |
| 110 |
static void mark_dump_job_done(ArchiveHandle *AH, |
| 111 |
TocEntry *te, |
| 112 |
int status, |
| 113 |
void *callback_data); |
| 114 |
static void mark_restore_job_done(ArchiveHandle *AH, |
| 115 |
TocEntry *te, |
| 116 |
int status, |
| 117 |
void *callback_data); |
| 118 |
static void fix_dependencies(ArchiveHandle *AH); |
| 119 |
static bool has_lock_conflicts(TocEntry *te1, TocEntry *te2); |
| 120 |
static void repoint_table_dependencies(ArchiveHandle *AH); |
| 121 |
static void identify_locking_dependencies(ArchiveHandle *AH, TocEntry *te); |
| 122 |
static void reduce_dependencies(ArchiveHandle *AH, TocEntry *te, |
| 123 |
binaryheap *ready_heap); |
| 124 |
static void mark_create_done(ArchiveHandle *AH, TocEntry *te); |
| 125 |
static void inhibit_data_for_failed_table(ArchiveHandle *AH, TocEntry *te); |
| 126 |
|
| 127 |
static void StrictNamesCheck(RestoreOptions *ropt); |
| 128 |
|
| 129 |
|
| 130 |
/* |
| 131 |
* Allocate a new DumpOptions block containing all default values. |
| 132 |
*/ |
| 133 |
DumpOptions * |
| 134 |
NewDumpOptions(void) |
| 135 |
{ |
| 136 |
DumpOptions *opts = (DumpOptions *) pg_malloc(sizeof(DumpOptions)); |
| 137 |
|
| 138 |
InitDumpOptions(opts); |
| 139 |
return opts; |
| 140 |
} |
| 141 |
|
| 142 |
/* |
| 143 |
* Initialize a DumpOptions struct to all default values |
| 144 |
*/ |
| 145 |
void |
| 146 |
InitDumpOptions(DumpOptions *opts) |
| 147 |
{ |
| 148 |
memset(opts, 0, sizeof(DumpOptions)); |
| 149 |
/* set any fields that shouldn't default to zeroes */ |
| 150 |
opts->include_everything = true; |
| 151 |
opts->cparams.promptPassword = TRI_DEFAULT; |
| 152 |
opts->dumpSections = DUMP_UNSECTIONED; |
| 153 |
opts->dumpSchema = true; |
| 154 |
opts->dumpData = true; |
| 155 |
opts->dumpStatistics = false; |
| 156 |
} |
| 157 |
|
| 158 |
/* |
| 159 |
* Create a freshly allocated DumpOptions with options equivalent to those |
| 160 |
* found in the given RestoreOptions. |
| 161 |
*/ |
| 162 |
DumpOptions * |
| 163 |
dumpOptionsFromRestoreOptions(RestoreOptions *ropt) |
| 164 |
{ |
| 165 |
DumpOptions *dopt = NewDumpOptions(); |
| 166 |
|
| 167 |
/* this is the inverse of what's at the end of pg_dump.c's main() */ |
| 168 |
dopt->cparams.dbname = ropt->cparams.dbname ? pg_strdup(ropt->cparams.dbname) : NULL; |
| 169 |
dopt->cparams.pgport = ropt->cparams.pgport ? pg_strdup(ropt->cparams.pgport) : NULL; |
| 170 |
dopt->cparams.pghost = ropt->cparams.pghost ? pg_strdup(ropt->cparams.pghost) : NULL; |
| 171 |
dopt->cparams.username = ropt->cparams.username ? pg_strdup(ropt->cparams.username) : NULL; |
| 172 |
dopt->cparams.promptPassword = ropt->cparams.promptPassword; |
| 173 |
dopt->outputClean = ropt->dropSchema; |
| 174 |
dopt->dumpData = ropt->dumpData; |
| 175 |
dopt->dumpSchema = ropt->dumpSchema; |
| 176 |
dopt->dumpSections = ropt->dumpSections; |
| 177 |
dopt->dumpStatistics = ropt->dumpStatistics; |
| 178 |
dopt->if_exists = ropt->if_exists; |
| 179 |
dopt->column_inserts = ropt->column_inserts; |
| 180 |
dopt->aclsSkip = ropt->aclsSkip; |
| 181 |
dopt->outputSuperuser = ropt->superuser; |
| 182 |
dopt->outputCreateDB = ropt->createDB; |
| 183 |
dopt->outputNoOwner = ropt->noOwner; |
| 184 |
dopt->outputNoTableAm = ropt->noTableAm; |
| 185 |
dopt->outputNoTablespaces = ropt->noTablespace; |
| 186 |
dopt->disable_triggers = ropt->disable_triggers; |
| 187 |
dopt->use_setsessauth = ropt->use_setsessauth; |
| 188 |
dopt->disable_dollar_quoting = ropt->disable_dollar_quoting; |
| 189 |
dopt->dump_inserts = ropt->dump_inserts; |
| 190 |
dopt->no_comments = ropt->no_comments; |
| 191 |
dopt->no_policies = ropt->no_policies; |
| 192 |
dopt->no_publications = ropt->no_publications; |
| 193 |
dopt->no_security_labels = ropt->no_security_labels; |
| 194 |
dopt->no_subscriptions = ropt->no_subscriptions; |
| 195 |
dopt->lockWaitTimeout = ropt->lockWaitTimeout; |
| 196 |
dopt->include_everything = ropt->include_everything; |
| 197 |
dopt->enable_row_security = ropt->enable_row_security; |
| 198 |
dopt->sequence_data = ropt->sequence_data; |
| 199 |
dopt->restrict_key = ropt->restrict_key ? pg_strdup(ropt->restrict_key) : NULL; |
| 200 |
|
| 201 |
return dopt; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
/* |
| 206 |
* Wrapper functions. |
| 207 |
* |
| 208 |
* The objective is to make writing new formats and dumpers as simple |
| 209 |
* as possible, if necessary at the expense of extra function calls etc. |
| 210 |
* |
| 211 |
*/ |
| 212 |
|
| 213 |
/* |
| 214 |
* The dump worker setup needs lots of knowledge of the internals of pg_dump, |
| 215 |
* so it's defined in pg_dump.c and passed into OpenArchive. The restore worker |
| 216 |
* setup doesn't need to know anything much, so it's defined here. |
| 217 |
*/ |
| 218 |
static void |
| 219 |
setupRestoreWorker(Archive *AHX) |
| 220 |
{ |
| 221 |
ArchiveHandle *AH = (ArchiveHandle *) AHX; |
| 222 |
|
| 223 |
AH->ReopenPtr(AH); |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
/* Create a new archive */ |
| 228 |
/* Public */ |
| 229 |
Archive * |
| 230 |
CreateArchive(const char *FileSpec, const ArchiveFormat fmt, |
| 231 |
const pg_compress_specification compression_spec, |
| 232 |
bool dosync, ArchiveMode mode, |
| 233 |
SetupWorkerPtrType setupDumpWorker, |
| 234 |
DataDirSyncMethod sync_method) |
| 235 |
|
| 236 |
{ |
| 237 |
ArchiveHandle *AH = _allocAH(FileSpec, fmt, compression_spec, |
| 238 |
dosync, mode, setupDumpWorker, sync_method); |
| 239 |
|
| 240 |
return (Archive *) AH; |
| 241 |
} |
| 242 |
|
| 243 |
/* Open an existing archive */ |
| 244 |
/* Public */ |
| 245 |
Archive * |
| 246 |
OpenArchive(const char *FileSpec, const ArchiveFormat fmt) |
| 247 |
{ |
| 248 |
ArchiveHandle *AH; |
| 249 |
pg_compress_specification compression_spec = {0}; |
| 250 |
|
| 251 |
compression_spec.algorithm = PG_COMPRESSION_NONE; |
| 252 |
AH = _allocAH(FileSpec, fmt, compression_spec, true, |
| 253 |
archModeRead, setupRestoreWorker, |
| 254 |
DATA_DIR_SYNC_METHOD_FSYNC); |
| 255 |
|
| 256 |
return (Archive *) AH; |
| 257 |
} |
| 258 |
|
| 259 |
/* Public */ |
| 260 |
void |
| 261 |
CloseArchive(Archive *AHX) |
| 262 |
{ |
| 263 |
ArchiveHandle *AH = (ArchiveHandle *) AHX; |
| 264 |
|
| 265 |
AH->ClosePtr(AH); |
| 266 |
|
| 267 |
/* Close the output */ |
| 268 |
errno = 0; |
| 269 |
if (!EndCompressFileHandle(AH->OF)) |
| 270 |
pg_fatal("could not close output file: %m"); |
| 271 |
} |
| 272 |
|
| 273 |
/* Public */ |
| 274 |
void |
| 275 |
SetArchiveOptions(Archive *AH, DumpOptions *dopt, RestoreOptions *ropt) |
| 276 |
{ |
| 277 |
/* Caller can omit dump options, in which case we synthesize them */ |
| 278 |
if (dopt == NULL && ropt != NULL) |
| 279 |
dopt = dumpOptionsFromRestoreOptions(ropt); |
| 280 |
|
| 281 |
/* Save options for later access */ |
| 282 |
AH->dopt = dopt; |
| 283 |
AH->ropt = ropt; |
| 284 |
} |
| 285 |
|
| 286 |
/* Public */ |
| 287 |
void |
| 288 |
ProcessArchiveRestoreOptions(Archive *AHX) |
| 289 |
{ |
| 290 |
ArchiveHandle *AH = (ArchiveHandle *) AHX; |
| 291 |
RestoreOptions *ropt = AH->public.ropt; |
| 292 |
TocEntry *te; |
| 293 |
teSection curSection; |
| 294 |
|
| 295 |
/* Decide which TOC entries will be dumped/restored, and mark them */ |
| 296 |
curSection = SECTION_PRE_DATA; |
| 297 |
for (te = AH->toc->next; te != AH->toc; te = te->next) |
| 298 |
{ |
| 299 |
/* |
| 300 |
* When writing an archive, we also take this opportunity to check |
| 301 |
* that we have generated the entries in a sane order that respects |
| 302 |
* the section divisions. When reading, don't complain, since buggy |
| 303 |
* old versions of pg_dump might generate out-of-order archives. |
| 304 |
*/ |
| 305 |
if (AH->mode != archModeRead) |
| 306 |
{ |
| 307 |
switch (te->section) |
| 308 |
{ |
| 309 |
case SECTION_NONE: |
| 310 |
/* ok to be anywhere */ |
| 311 |
break; |
| 312 |
case SECTION_PRE_DATA: |
| 313 |
if (curSection != SECTION_PRE_DATA) |
| 314 |
pg_log_warning("archive items not in correct section order"); |
| 315 |
break; |
| 316 |
case SECTION_DATA: |
| 317 |
if (curSection == SECTION_POST_DATA) |
| 318 |
pg_log_warning("archive items not in correct section order"); |
| 319 |
break; |
| 320 |
case SECTION_POST_DATA: |
| 321 |
/* ok no matter which section we were in */ |
| 322 |
break; |
| 323 |
default: |
| 324 |
pg_fatal("unexpected section code %d", |
| 325 |
(int) te->section); |
| 326 |
break; |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
if (te->section != SECTION_NONE) |
| 331 |
curSection = te->section; |
| 332 |
|
| 333 |
te->reqs = _tocEntryRequired(te, curSection, AH); |
| 334 |
} |
| 335 |
|
| 336 |
/* Enforce strict names checking */ |
| 337 |
if (ropt->strict_names) |
| 338 |
StrictNamesCheck(ropt); |
| 339 |
} |
| 340 |
|
| 341 |
/* Public */ |
| 342 |
void |
| 343 |
RestoreArchive(Archive *AHX) |
| 344 |
{ |
| 345 |
ArchiveHandle *AH = (ArchiveHandle *) AHX; |
| 346 |
RestoreOptions *ropt = AH->public.ropt; |
| 347 |
bool parallel_mode; |
| 348 |
TocEntry *te; |
| 349 |
CompressFileHandle *sav; |
| 350 |
|
| 351 |
AH->stage = STAGE_INITIALIZING; |
| 352 |
|
| 353 |
/* |
| 354 |
* If we're going to do parallel restore, there are some restrictions. |
| 355 |
*/ |
| 356 |
parallel_mode = (AH->public.numWorkers > 1 && ropt->useDB); |
| 357 |
if (parallel_mode) |
| 358 |
{ |
| 359 |
/* We haven't got round to making this work for all archive formats */ |
| 360 |
if (AH->ClonePtr == NULL || AH->ReopenPtr == NULL) |
| 361 |
pg_fatal("parallel restore is not supported with this archive file format"); |
| 362 |
|
| 363 |
/* Doesn't work if the archive represents dependencies as OIDs */ |
| 364 |
if (AH->version < K_VERS_1_8) |
| 365 |
pg_fatal("parallel restore is not supported with archives made by pre-8.0 pg_dump"); |
| 366 |
|
| 367 |
/* |
| 368 |
* It's also not gonna work if we can't reopen the input file, so |
| 369 |
* let's try that immediately. |
| 370 |
*/ |
| 371 |
AH->ReopenPtr(AH); |
| 372 |
} |
| 373 |
|
| 374 |
/* |
| 375 |
* Make sure we won't need (de)compression we haven't got |
| 376 |
*/ |
| 377 |
if (AH->PrintTocDataPtr != NULL) |
| 378 |
{ |
| 379 |
for (te = AH->toc->next; te != AH->toc; te = te->next) |
| 380 |
{ |
| 381 |
if (te->hadDumper && (te->reqs & REQ_DATA) != 0) |
| 382 |
{ |
| 383 |
char *errmsg = supports_compression(AH->compression_spec); |
| 384 |
|
| 385 |
if (errmsg) |
| 386 |
pg_fatal("cannot restore from compressed archive (%s)", |
| 387 |
errmsg); |
| 388 |
else |
| 389 |
break; |
| 390 |
} |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
/* |
| 395 |
* Prepare index arrays, so we can assume we have them throughout restore. |
| 396 |
* It's possible we already did this, though. |
| 397 |
*/ |
| 398 |
if (AH->tocsByDumpId == NULL) |
| 399 |
buildTocEntryArrays(AH); |
| 400 |
|
| 401 |
/* |
| 402 |
* If we're using a DB connection, then connect it. |
| 403 |
*/ |
| 404 |
if (ropt->useDB) |
| 405 |
{ |
| 406 |
pg_log_info("connecting to database for restore"); |
| 407 |
if (AH->version < K_VERS_1_3) |
| 408 |
pg_fatal("direct database connections are not supported in pre-1.3 archives"); |
| 409 |
|
| 410 |
/* |
| 411 |
* We don't want to guess at whether the dump will successfully |
| 412 |
* restore; allow the attempt regardless of the version of the restore |
| 413 |
* target. |
| 414 |
*/ |
| 415 |
AHX->minRemoteVersion = 0; |
| 416 |
AHX->maxRemoteVersion = 9999999; |
| 417 |
|
| 418 |
ConnectDatabaseAhx(AHX, &ropt->cparams, false); |
| 419 |
|
| 420 |
/* |
| 421 |
* If we're talking to the DB directly, don't send comments since they |
| 422 |
* obscure SQL when displaying errors |
| 423 |
*/ |
| 424 |
AH->noTocComments = 1; |
| 425 |
} |
| 426 |
|
| 427 |
/* |
| 428 |
* Work out if we have an implied schema-less restore. This can happen if |
| 429 |
* the dump excluded the schema or the user has used a toc list to exclude |
| 430 |
* all of the schema data. All we do is look for schema entries - if none |
| 431 |
* are found then we unset the dumpSchema flag. |
| 432 |
* |
| 433 |
* We could scan for wanted TABLE entries, but that is not the same as |
| 434 |
* data-only. At this stage, it seems unnecessary (6-Mar-2001). |
| 435 |
*/ |
| 436 |
if (ropt->dumpSchema) |
| 437 |
{ |
| 438 |
bool no_schema_found = true; |
| 439 |
|
| 440 |
for (te = AH->toc->next; te != AH->toc; te = te->next) |
| 441 |
{ |
| 442 |
if ((te->reqs & REQ_SCHEMA) != 0) |
| 443 |
{ |
| 444 |
no_schema_found = false; |
| 445 |
break; |
| 446 |
} |
| 447 |
} |
| 448 |
if (no_schema_found) |
| 449 |
{ |
| 450 |
ropt->dumpSchema = false; |
| 451 |
pg_log_info("implied no-schema restore"); |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
/* |
| 456 |
* Setup the output file if necessary. |
| 457 |
*/ |
| 458 |
sav = SaveOutput(AH); |
| 459 |
if (ropt->filename || ropt->compression_spec.algorithm != PG_COMPRESSION_NONE) |
| 460 |
SetOutput(AH, ropt->filename, ropt->compression_spec); |
| 461 |
|
| 462 |
ahprintf(AH, "--\n-- PostgreSQL database dump\n--\n\n"); |
| 463 |
|
| 464 |
/* |
| 465 |
* If generating plain-text output, enter restricted mode to block any |
| 466 |
* unexpected psql meta-commands. A malicious source might try to inject |
| 467 |
* a variety of things via bogus responses to queries. While we cannot |
| 468 |
* prevent such sources from affecting the destination at restore time, we |
| 469 |
* can block psql meta-commands so that the client machine that runs psql |
| 470 |
* with the dump output remains unaffected. |
| 471 |
*/ |
| 472 |
if (ropt->restrict_key) |
| 473 |
ahprintf(AH, "\\restrict %s\n\n", ropt->restrict_key); |
| 474 |
|
| 475 |
if (AH->archiveRemoteVersion) |
| 476 |
ahprintf(AH, "-- Dumped from database version %s\n", |
| 477 |
AH->archiveRemoteVersion); |
| 478 |
if (AH->archiveDumpVersion) |
| 479 |
ahprintf(AH, "-- Dumped by pg_dump version %s\n", |
| 480 |
AH->archiveDumpVersion); |
| 481 |
|
| 482 |
ahprintf(AH, "\n"); |
| 483 |
|
| 484 |
if (AH->public.verbose) |
| 485 |
dumpTimestamp(AH, "Started on", AH->createDate); |
| 486 |
|
| 487 |
if (ropt->single_txn) |
| 488 |
{ |
| 489 |
if (AH->connection) |
| 490 |
StartTransaction(AHX); |
| 491 |
else |
| 492 |
ahprintf(AH, "BEGIN;\n\n"); |
| 493 |
} |
| 494 |
|
| 495 |
/* |
| 496 |
* Establish important parameter values right away. |
| 497 |
*/ |
| 498 |
_doSetFixedOutputState(AH); |
| 499 |
|
| 500 |
AH->stage = STAGE_PROCESSING; |
| 501 |
|
| 502 |
/* |
| 503 |
* Drop the items at the start, in reverse order |
| 504 |
*/ |
| 505 |
if (ropt->dropSchema) |
| 506 |
{ |
| 507 |
for (te = AH->toc->prev; te != AH->toc; te = te->prev) |
| 508 |
{ |
| 509 |
AH->currentTE = te; |
| 510 |
|
| 511 |
/* |
| 512 |
* In createDB mode, issue a DROP *only* for the database as a |
| 513 |
* whole. Issuing drops against anything else would be wrong, |
| 514 |
* because at this point we're connected to the wrong database. |
| 515 |
* (The DATABASE PROPERTIES entry, if any, should be treated like |
| 516 |
* the DATABASE entry.) |
| 517 |
*/ |
| 518 |
if (ropt->createDB) |
| 519 |
{ |
| 520 |
if (strcmp(te->desc, "DATABASE") != 0 && |
| 521 |
strcmp(te->desc, "DATABASE PROPERTIES") != 0) |
| 522 |
continue; |
| 523 |
} |
| 524 |
|
| 525 |
/* Otherwise, drop anything that's selected and has a dropStmt */ |
| 526 |
if (((te->reqs & (REQ_SCHEMA | REQ_DATA)) != 0) && te->dropStmt) |
| 527 |
{ |
| 528 |
bool not_allowed_in_txn = false; |
| 529 |
|
| 530 |
pg_log_info("dropping %s %s", te->desc, te->tag); |
| 531 |
|
| 532 |
/* |
| 533 |
* In --transaction-size mode, we have to temporarily exit our |
| 534 |
* transaction block to drop objects that can't be dropped |
| 535 |
* within a transaction. |
| 536 |
*/ |
| 537 |
if (ropt->txn_size > 0) |
| 538 |
{ |
| 539 |
if (strcmp(te->desc, "DATABASE") == 0 || |
| 540 |
strcmp(te->desc, "DATABASE PROPERTIES") == 0) |
| 541 |
{ |
| 542 |
not_allowed_in_txn = true; |
| 543 |
if (AH->connection) |
| 544 |
CommitTransaction(AHX); |
| 545 |
else |
| 546 |
ahprintf(AH, "COMMIT;\n"); |
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
/* Select owner and schema as necessary */ |
| 551 |
_becomeOwner(AH, te); |
| 552 |
_selectOutputSchema(AH, te->namespace); |
| 553 |
|
| 554 |
/* |
| 555 |
* Now emit the DROP command, if the object has one. Note we |
| 556 |
* don't necessarily emit it verbatim; at this point we add an |
| 557 |
* appropriate IF EXISTS clause, if the user requested it. |
| 558 |
*/ |
| 559 |
if (strcmp(te->desc, "BLOB METADATA") == 0) |
| 560 |
{ |
| 561 |
/* We must generate the per-blob commands */ |
| 562 |
if (ropt->if_exists) |
| 563 |
IssueCommandPerBlob(AH, te, |
| 564 |
"SELECT pg_catalog.lo_unlink(oid) " |
| 565 |
"FROM pg_catalog.pg_largeobject_metadata " |
| 566 |
"WHERE oid = '", "'"); |
| 567 |
else |
| 568 |
IssueCommandPerBlob(AH, te, |
| 569 |
"SELECT pg_catalog.lo_unlink('", |
| 570 |
"')"); |
| 571 |
} |
| 572 |
else if (*te->dropStmt != '\0') |
| 573 |
{ |
| 574 |
if (!ropt->if_exists || |
| 575 |
strncmp(te->dropStmt, "--", 2) == 0) |
| 576 |
{ |
| 577 |
/* |
| 578 |
* Without --if-exists, or if it's just a comment (as |
| 579 |
* happens for the public schema), print the dropStmt |
| 580 |
* as-is. |
| 581 |
*/ |
| 582 |
ahprintf(AH, "%s", te->dropStmt); |
| 583 |
} |
| 584 |
else |
| 585 |
{ |
| 586 |
/* |
| 587 |
* Inject an appropriate spelling of "if exists". For |
| 588 |
* old-style large objects, we have a routine that |
| 589 |
* knows how to do it, without depending on |
| 590 |
* te->dropStmt; use that. For other objects we need |
| 591 |
* to parse the command. |
| 592 |
*/ |
| 593 |
if (strcmp(te->desc, "BLOB") == 0) |
| 594 |
{ |
| 595 |
DropLOIfExists(AH, te->catalogId.oid); |
| 596 |
} |
| 597 |
else |
| 598 |
{ |
| 599 |
char *dropStmt = pg_strdup(te->dropStmt); |
| 600 |
char *dropStmtOrig = dropStmt; |
| 601 |
PQExpBuffer ftStmt = createPQExpBuffer(); |
| 602 |
|
| 603 |
/* |
| 604 |
* Need to inject IF EXISTS clause after ALTER |
| 605 |
* TABLE part in ALTER TABLE .. DROP statement |
| 606 |
*/ |
| 607 |
if (strncmp(dropStmt, "ALTER TABLE", 11) == 0) |
| 608 |
{ |
| 609 |
appendPQExpBufferStr(ftStmt, |
| 610 |
"ALTER TABLE IF EXISTS"); |
| 611 |
dropStmt = dropStmt + 11; |
| 612 |
} |
| 613 |
|
| 614 |
/* |
| 615 |
* ALTER TABLE..ALTER COLUMN..DROP DEFAULT does |
| 616 |
* not support the IF EXISTS clause, and therefore |
| 617 |
* we simply emit the original command for DEFAULT |
| 618 |
* objects (modulo the adjustment made above). |
| 619 |
* |
| 620 |
* Likewise, don't mess with DATABASE PROPERTIES. |
| 621 |
* |
| 622 |
* If we used CREATE OR REPLACE VIEW as a means of |
| 623 |
* quasi-dropping an ON SELECT rule, that should |
| 624 |
* be emitted unchanged as well. |
| 625 |
* |
| 626 |
* For other object types, we need to extract the |
| 627 |
* first part of the DROP which includes the |
| 628 |
* object type. Most of the time this matches |
| 629 |
* te->desc, so search for that; however for the |
| 630 |
* different kinds of CONSTRAINTs, we know to |
| 631 |
* search for hardcoded "DROP CONSTRAINT" instead. |
| 632 |
*/ |
| 633 |
if (strcmp(te->desc, "DEFAULT") == 0 || |
| 634 |
strcmp(te->desc, "DATABASE PROPERTIES") == 0 || |
| 635 |
strncmp(dropStmt, "CREATE OR REPLACE VIEW", 22) == 0) |
| 636 |
appendPQExpBufferStr(ftStmt, dropStmt); |
| 637 |
else |
| 638 |
{ |
| 639 |
char buffer[40]; |
| 640 |
char *mark; |
| 641 |
|
| 642 |
if (strcmp(te->desc, "CONSTRAINT") == 0 || |
| 643 |
strcmp(te->desc, "CHECK CONSTRAINT") == 0 || |
| 644 |
strcmp(te->desc, "FK CONSTRAINT") == 0) |
| 645 |
strcpy(buffer, "DROP CONSTRAINT"); |
| 646 |
else |
| 647 |
snprintf(buffer, sizeof(buffer), "DROP %s", |
| 648 |
te->desc); |
| 649 |
|
| 650 |
mark = strstr(dropStmt, buffer); |
| 651 |
|
| 652 |
if (mark) |
| 653 |
{ |
| 654 |
*mark = '\0'; |
| 655 |
appendPQExpBuffer(ftStmt, "%s%s IF EXISTS%s", |
| 656 |
dropStmt, buffer, |
| 657 |
mark + strlen(buffer)); |
| 658 |
} |
| 659 |
else |
| 660 |
{ |
| 661 |
/* complain and emit unmodified command */ |
| 662 |
pg_log_warning("could not find where to insert IF EXISTS in statement \"%s\"", |
| 663 |
dropStmtOrig); |
| 664 |
appendPQExpBufferStr(ftStmt, dropStmt); |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
ahprintf(AH, "%s", ftStmt->data); |
| 669 |
|
| 670 |
destroyPQExpBuffer(ftStmt); |
| 671 |
pg_free(dropStmtOrig); |
| 672 |
} |
| 673 |
} |
| 674 |
} |
| 675 |
|
| 676 |
/* |
| 677 |
* In --transaction-size mode, re-establish the transaction |
| 678 |
* block if needed; otherwise, commit after every N drops. |
| 679 |
*/ |
| 680 |
if (ropt->txn_size > 0) |
| 681 |
{ |
| 682 |
if (not_allowed_in_txn) |
| 683 |
{ |
| 684 |
if (AH->connection) |
| 685 |
StartTransaction(AHX); |
| 686 |
else |
| 687 |
ahprintf(AH, "BEGIN;\n"); |
| 688 |
AH->txnCount = 0; |
| 689 |
} |
| 690 |
else if (++AH->txnCount >= ropt->txn_size) |
| 691 |
{ |
| 692 |
if (AH->connection) |
| 693 |
{ |
| 694 |
CommitTransaction(AHX); |
| 695 |
StartTransaction(AHX); |
| 696 |
} |
| 697 |
else |
| 698 |
ahprintf(AH, "COMMIT;\nBEGIN;\n"); |
| 699 |
AH->txnCount = 0; |
| 700 |
} |
| 701 |
} |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
/* |
| 706 |
* _selectOutputSchema may have set currSchema to reflect the effect |
| 707 |
* of a "SET search_path" command it emitted. However, by now we may |
| 708 |
* have dropped that schema; or it might not have existed in the first |
| 709 |
* place. In either case the effective value of search_path will not |
| 710 |
* be what we think. Forcibly reset currSchema so that we will |
| 711 |
* re-establish the search_path setting when needed (after creating |
| 712 |
* the schema). |
| 713 |
* |
| 714 |
* If we treated users as pg_dump'able objects then we'd need to reset |
| 715 |
* currUser here too. |
| 716 |
*/ |
| 717 |
free(AH->currSchema); |
| 718 |
AH->currSchema = NULL; |
| 719 |
} |
| 720 |
|
| 721 |
if (parallel_mode) |
| 722 |
{ |
| 723 |
/* |
| 724 |
* In parallel mode, turn control over to the parallel-restore logic. |
| 725 |
*/ |
| 726 |
ParallelState *pstate; |
| 727 |
TocEntry pending_list; |
| 728 |
|
| 729 |
/* The archive format module may need some setup for this */ |
| 730 |
if (AH->PrepParallelRestorePtr) |
| 731 |
AH->PrepParallelRestorePtr(AH); |
| 732 |
|
| 733 |
pending_list_header_init(&pending_list); |
| 734 |
|
| 735 |
/* This runs PRE_DATA items and then disconnects from the database */ |
| 736 |
restore_toc_entries_prefork(AH, &pending_list); |
| 737 |
Assert(AH->connection == NULL); |
| 738 |
|
| 739 |
/* ParallelBackupStart() will actually fork the processes */ |
| 740 |
pstate = ParallelBackupStart(AH); |
| 741 |
restore_toc_entries_parallel(AH, pstate, &pending_list); |
| 742 |
ParallelBackupEnd(AH, pstate); |
| 743 |
|
| 744 |
/* reconnect the leader and see if we missed something */ |
| 745 |
restore_toc_entries_postfork(AH, &pending_list); |
| 746 |
Assert(AH->connection != NULL); |
| 747 |
} |
| 748 |
else |
| 749 |
{ |
| 750 |
/* |
| 751 |
* In serial mode, process everything in three phases: normal items, |
| 752 |
* then ACLs, then post-ACL items. We might be able to skip one or |
| 753 |
* both extra phases in some cases, eg data-only restores. |
| 754 |
*/ |
| 755 |
bool haveACL = false; |
| 756 |
bool havePostACL = false; |
| 757 |
|
| 758 |
for (te = AH->toc->next; te != AH->toc; te = te->next) |
| 759 |
{ |
| 760 |
if ((te->reqs & (REQ_SCHEMA | REQ_DATA | REQ_STATS)) == 0) |
| 761 |
continue; /* ignore if not to be dumped at all */ |
| 762 |
|
| 763 |
switch (_tocEntryRestorePass(te)) |
| 764 |
{ |
| 765 |
case RESTORE_PASS_MAIN: |
| 766 |
(void) restore_toc_entry(AH, te, false); |
| 767 |
break; |
| 768 |
case RESTORE_PASS_ACL: |
| 769 |
haveACL = true; |
| 770 |
break; |
| 771 |
case RESTORE_PASS_POST_ACL: |
| 772 |
havePostACL = true; |
| 773 |
break; |
| 774 |
} |
| 775 |
} |
| 776 |
|
| 777 |
if (haveACL) |
| 778 |
{ |
| 779 |
for (te = AH->toc->next; te != AH->toc; te = te->next) |
| 780 |
{ |
| 781 |
if ((te->reqs & (REQ_SCHEMA | REQ_DATA | REQ_STATS)) != 0 && |
| 782 |
_tocEntryRestorePass(te) == RESTORE_PASS_ACL) |
| 783 |
(void) restore_toc_entry(AH, te, false); |
| 784 |
} |
| 785 |
} |
| 786 |
|
| 787 |
if (havePostACL) |
| 788 |
{ |
| 789 |
for (te = AH->toc->next; te != AH->toc; te = te->next) |
| 790 |
{ |
| 791 |
if ((te->reqs & (REQ_SCHEMA | REQ_DATA | REQ_STATS)) != 0 && |
| 792 |
_tocEntryRestorePass(te) == RESTORE_PASS_POST_ACL) |
| 793 |
(void) restore_toc_entry(AH, te, false); |
| 794 |
} |
| 795 |
} |
| 796 |
} |
| 797 |
|
| 798 |
/* |
| 799 |
* Close out any persistent transaction we may have. While these two |
| 800 |
* cases are started in different places, we can end both cases here. |
| 801 |
*/ |
| 802 |
if (ropt->single_txn || ropt->txn_size > 0) |
| 803 |
{ |
| 804 |
if (AH->connection) |
| 805 |
CommitTransaction(AHX); |
| 806 |
else |
| 807 |
ahprintf(AH, "COMMIT;\n\n"); |
| 808 |
} |
| 809 |
|
| 810 |
if (AH->public.verbose) |
| 811 |
dumpTimestamp(AH, "Completed on", time(NULL)); |
| 812 |
|
| 813 |
ahprintf(AH, "--\n-- PostgreSQL database dump complete\n--\n\n"); |
| 814 |
|
| 815 |
/* |
| 816 |
* If generating plain-text output, exit restricted mode at the very end |
| 817 |
* of the script. This is not pro forma; in particular, pg_dumpall |
| 818 |
* requires this when transitioning from one database to another. |
| 819 |
*/ |
| 820 |
if (ropt->restrict_key) |
| 821 |
ahprintf(AH, "\\unrestrict %s\n\n", ropt->restrict_key); |
| 822 |
|
| 823 |
/* |
| 824 |
* Clean up & we're done. |
| 825 |
*/ |
| 826 |
AH->stage = STAGE_FINALIZING; |
| 827 |
|
| 828 |
if (ropt->filename || ropt->compression_spec.algorithm != PG_COMPRESSION_NONE) |
| 829 |
RestoreOutput(AH, sav); |
| 830 |
|
| 831 |
if (ropt->useDB) |
| 832 |
DisconnectDatabase(&AH->public); |
| 833 |
} |
| 834 |
|
| 835 |
/* |
| 836 |
* Restore a single TOC item. Used in both parallel and non-parallel restore; |
| 837 |
* is_parallel is true if we are in a worker child process. |
| 838 |
* |
| 839 |
* Returns 0 normally, but WORKER_CREATE_DONE or WORKER_INHIBIT_DATA if |
| 840 |
* the parallel parent has to make the corresponding status update. |
| 841 |
*/ |
| 842 |
static int |
| 843 |
restore_toc_entry(ArchiveHandle *AH, TocEntry *te, bool is_parallel) |
| 844 |
{ |
| 845 |
RestoreOptions *ropt = AH->public.ropt; |
| 846 |
int status = WORKER_OK; |
| 847 |
int reqs; |
| 848 |
bool defnDumped; |
| 849 |
|
| 850 |
AH->currentTE = te; |
| 851 |
|
| 852 |
/* Dump any relevant dump warnings to stderr */ |
| 853 |
if (!ropt->suppressDumpWarnings && strcmp(te->desc, "WARNING") == 0) |
| 854 |
{ |
| 855 |
if (ropt->dumpSchema && te->defn != NULL && strlen(te->defn) != 0) |
| 856 |
pg_log_warning("warning from original dump file: %s", te->defn); |
| 857 |
else if (te->copyStmt != NULL && strlen(te->copyStmt) != 0) |
| 858 |
pg_log_warning("warning from original dump file: %s", te->copyStmt); |
| 859 |
} |
| 860 |
|
| 861 |
/* Work out what, if anything, we want from this entry */ |
| 862 |
reqs = te->reqs; |
| 863 |
|
| 864 |
defnDumped = false; |
| 865 |
|
| 866 |
/* |
| 867 |
* If it has a schema component that we want, then process that |
| 868 |
*/ |
| 869 |
if ((reqs & REQ_SCHEMA) != 0) |
| 870 |
{ |
| 871 |
bool object_is_db = false; |
| 872 |
|
| 873 |
/* |
| 874 |
* In --transaction-size mode, must exit our transaction block to |
| 875 |
* create a database or set its properties. |
| 876 |
*/ |
| 877 |
if (strcmp(te->desc, "DATABASE") == 0 || |
| 878 |
strcmp(te->desc, "DATABASE PROPERTIES") == 0) |
| 879 |
{ |
| 880 |
object_is_db = true; |
| 881 |
if (ropt->txn_size > 0) |
| 882 |
{ |
| 883 |
if (AH->connection) |
| 884 |
CommitTransaction(&AH->public); |
| 885 |
else |
| 886 |
ahprintf(AH, "COMMIT;\n\n"); |
| 887 |
} |
| 888 |
} |
| 889 |
|
| 890 |
/* Show namespace in log message if available */ |
| 891 |
if (te->namespace) |
| 892 |
pg_log_info("creating %s \"%s.%s\"", |
| 893 |
te->desc, te->namespace, te->tag); |
| 894 |
else |
| 895 |
pg_log_info("creating %s \"%s\"", |
| 896 |
te->desc, te->tag); |
| 897 |
|
| 898 |
_printTocEntry(AH, te, TOC_PREFIX_NONE); |
| 899 |
defnDumped = true; |
| 900 |
|
| 901 |
if (strcmp(te->desc, "TABLE") == 0) |
| 902 |
{ |
| 903 |
if (AH->lastErrorTE == te) |
| 904 |
{ |
| 905 |
/* |
| 906 |
* We failed to create the table. If |
| 907 |
* --no-data-for-failed-tables was given, mark the |
| 908 |
* corresponding TABLE DATA to be ignored. |
| 909 |
* |
| 910 |
* In the parallel case this must be done in the parent, so we |
| 911 |
* just set the return value. |
| 912 |
*/ |
| 913 |
if (ropt->noDataForFailedTables) |
| 914 |
{ |
| 915 |
if (is_parallel) |
| 916 |
status = WORKER_INHIBIT_DATA; |
| 917 |
else |
| 918 |
inhibit_data_for_failed_table(AH, te); |
| 919 |
} |
| 920 |
} |
| 921 |
else |
| 922 |
{ |
| 923 |
/* |
| 924 |
* We created the table successfully. Mark the corresponding |
| 925 |
* TABLE DATA for possible truncation. |
| 926 |
* |
| 927 |
* In the parallel case this must be done in the parent, so we |
| 928 |
* just set the return value. |
| 929 |
*/ |
| 930 |
if (is_parallel) |
| 931 |
status = WORKER_CREATE_DONE; |
| 932 |
else |
| 933 |
mark_create_done(AH, te); |
| 934 |
} |
| 935 |
} |
| 936 |
|
| 937 |
/* |
| 938 |
* If we created a DB, connect to it. Also, if we changed DB |
| 939 |
* properties, reconnect to ensure that relevant GUC settings are |
| 940 |
* applied to our session. (That also restarts the transaction block |
| 941 |
* in --transaction-size mode.) |
| 942 |
*/ |
| 943 |
if (object_is_db) |
| 944 |
{ |
| 945 |
pg_log_info("connecting to new database \"%s\"", te->tag); |
| 946 |
_reconnectToDB(AH, te->tag); |
| 947 |
} |
| 948 |
} |
| 949 |
|
| 950 |
/* |
| 951 |
* If it has a data component that we want, then process that |
| 952 |
*/ |
| 953 |
if ((reqs & REQ_DATA) != 0) |
| 954 |
{ |
| 955 |
/* |
| 956 |
* hadDumper will be set if there is genuine data component for this |
| 957 |
* node. Otherwise, we need to check the defn field for statements |
| 958 |
* that need to be executed in data-only restores. |
| 959 |
*/ |
| 960 |
if (te->hadDumper) |
| 961 |
{ |
| 962 |
/* |
| 963 |
* If we can output the data, then restore it. |
| 964 |
*/ |
| 965 |
if (AH->PrintTocDataPtr != NULL) |
| 966 |
{ |
| 967 |
_printTocEntry(AH, te, TOC_PREFIX_DATA); |
| 968 |
|
| 969 |
if (strcmp(te->desc, "BLOBS") == 0 || |
| 970 |
strcmp(te->desc, "BLOB COMMENTS") == 0) |
| 971 |
{ |
| 972 |
pg_log_info("processing %s", te->desc); |
| 973 |
|
| 974 |
_selectOutputSchema(AH, "pg_catalog"); |
| 975 |
|
| 976 |
/* Send BLOB COMMENTS data to ExecuteSimpleCommands() */ |
| 977 |
if (strcmp(te->desc, "BLOB COMMENTS") == 0) |
| 978 |
AH->outputKind = OUTPUT_OTHERDATA; |
| 979 |
|
| 980 |
AH->PrintTocDataPtr(AH, te); |
| 981 |
|
| 982 |
AH->outputKind = OUTPUT_SQLCMDS; |
| 983 |
} |
| 984 |
else |
| 985 |
{ |
| 986 |
bool use_truncate; |
| 987 |
|
| 988 |
_disableTriggersIfNecessary(AH, te); |
| 989 |
|
| 990 |
/* Select owner and schema as necessary */ |
| 991 |
_becomeOwner(AH, te); |
| 992 |
_selectOutputSchema(AH, te->namespace); |
| 993 |
|
| 994 |
pg_log_info("processing data for table \"%s.%s\"", |
| 995 |
te->namespace, te->tag); |
| 996 |
|
| 997 |
/* |
| 998 |
* In parallel restore, if we created the table earlier in |
| 999 |
* this run (so that we know it is empty) and we are not |
| 1000 |
* restoring a load-via-partition-root data item then we |
| 1001 |
* wrap the COPY in a transaction and precede it with a |
| 1002 |
* TRUNCATE. If wal_level is set to minimal this prevents |
| 1003 |
* WAL-logging the COPY. This obtains a speedup similar |
| 1004 |
* to that from using single_txn mode in non-parallel |
| 1005 |
* restores. |
| 1006 |
* |
| 1007 |
* We mustn't do this for load-via-partition-root cases |
| 1008 |
* because some data might get moved across partition |
| 1009 |
* boundaries, risking deadlock and/or loss of previously |
| 1010 |
* loaded data. (We assume that all partitions of a |
| 1011 |
* partitioned table will be treated the same way.) |
| 1012 |
*/ |
| 1013 |
use_truncate = is_parallel && te->created && |
| 1014 |
!is_load_via_partition_root(te); |
| 1015 |
|
| 1016 |
if (use_truncate) |
| 1017 |
{ |
| 1018 |
/* |
| 1019 |
* Parallel restore is always talking directly to a |
| 1020 |
* server, so no need to see if we should issue BEGIN. |
| 1021 |
*/ |
| 1022 |
StartTransaction(&AH->public); |
| 1023 |
|
| 1024 |
/* |
| 1025 |
* Issue TRUNCATE with ONLY so that child tables are |
| 1026 |
* not wiped. |
| 1027 |
*/ |
| 1028 |
ahprintf(AH, "TRUNCATE TABLE ONLY %s;\n\n", |
| 1029 |
fmtQualifiedId(te->namespace, te->tag)); |
| 1030 |
} |
| 1031 |
|
| 1032 |
/* |
| 1033 |
* If we have a copy statement, use it. |
| 1034 |
*/ |
| 1035 |
if (te->copyStmt && strlen(te->copyStmt) > 0) |
| 1036 |
{ |
| 1037 |
ahprintf(AH, "%s", te->copyStmt); |
| 1038 |
AH->outputKind = OUTPUT_COPYDATA; |
| 1039 |
} |
| 1040 |
else |
| 1041 |
AH->outputKind = OUTPUT_OTHERDATA; |
| 1042 |
|
| 1043 |
AH->PrintTocDataPtr(AH, te); |
| 1044 |
|
| 1045 |
/* |
| 1046 |
* Terminate COPY if needed. |
| 1047 |
*/ |
| 1048 |
if (AH->outputKind == OUTPUT_COPYDATA && |
| 1049 |
RestoringToDB(AH)) |
| 1050 |
EndDBCopyMode(&AH->public, te->tag); |
| 1051 |
AH->outputKind = OUTPUT_SQLCMDS; |
| 1052 |
|
| 1053 |
/* close out the transaction started above */ |
| 1054 |
if (use_truncate) |
| 1055 |
CommitTransaction(&AH->public); |
| 1056 |
|
| 1057 |
_enableTriggersIfNecessary(AH, te); |
| 1058 |
} |
| 1059 |
} |
| 1060 |
} |
| 1061 |
else if (!defnDumped) |
| 1062 |
{ |
| 1063 |
/* If we haven't already dumped the defn part, do so now */ |
| 1064 |
pg_log_info("executing %s %s", te->desc, te->tag); |
| 1065 |
_printTocEntry(AH, te, TOC_PREFIX_NONE); |
| 1066 |
} |
| 1067 |
} |
| 1068 |
|
| 1069 |
/* |
| 1070 |
* If it has a statistics component that we want, then process that |
| 1071 |
*/ |
| 1072 |
if ((reqs & REQ_STATS) != 0) |
| 1073 |
_printTocEntry(AH, te, TOC_PREFIX_STATS); |
| 1074 |
|
| 1075 |
/* |
| 1076 |
* If we emitted anything for this TOC entry, that counts as one action |
| 1077 |
* against the transaction-size limit. Commit if it's time to. |
| 1078 |
*/ |
| 1079 |
if ((reqs & (REQ_SCHEMA | REQ_DATA | REQ_STATS)) != 0 && ropt->txn_size > 0) |
| 1080 |
{ |
| 1081 |
if (++AH->txnCount >= ropt->txn_size) |
| 1082 |
{ |
| 1083 |
if (AH->connection) |
| 1084 |
{ |
| 1085 |
CommitTransaction(&AH->public); |
| 1086 |
StartTransaction(&AH->public); |
| 1087 |
} |
| 1088 |
else |
| 1089 |
ahprintf(AH, "COMMIT;\nBEGIN;\n\n"); |
| 1090 |
AH->txnCount = 0; |
| 1091 |
} |
| 1092 |
} |
| 1093 |
|
| 1094 |
if (AH->public.n_errors > 0 && status == WORKER_OK) |
| 1095 |
status = WORKER_IGNORED_ERRORS; |
| 1096 |
|
| 1097 |
return status; |
| 1098 |
} |
| 1099 |
|
| 1100 |
/* |
| 1101 |
* Allocate a new RestoreOptions block. |
| 1102 |
* This is mainly so we can initialize it, but also for future expansion, |
| 1103 |
*/ |
| 1104 |
RestoreOptions * |
| 1105 |
NewRestoreOptions(void) |
| 1106 |
{ |
| 1107 |
RestoreOptions *opts; |
| 1108 |
|
| 1109 |
opts = (RestoreOptions *) pg_malloc0(sizeof(RestoreOptions)); |
| 1110 |
|
| 1111 |
/* set any fields that shouldn't default to zeroes */ |
| 1112 |
opts->format = archUnknown; |
| 1113 |
opts->cparams.promptPassword = TRI_DEFAULT; |
| 1114 |
opts->dumpSections = DUMP_UNSECTIONED; |
| 1115 |
opts->compression_spec.algorithm = PG_COMPRESSION_NONE; |
| 1116 |
opts->compression_spec.level = 0; |
| 1117 |
opts->dumpSchema = true; |
| 1118 |
opts->dumpData = true; |
| 1119 |
opts->dumpStatistics = true; |
| 1120 |
|
| 1121 |
return opts; |
| 1122 |
} |
| 1123 |
|
| 1124 |
static void |
| 1125 |
_disableTriggersIfNecessary(ArchiveHandle *AH, TocEntry *te) |
| 1126 |
{ |
| 1127 |
RestoreOptions *ropt = AH->public.ropt; |
| 1128 |
|
| 1129 |
/* This hack is only needed in a data-only restore */ |
| 1130 |
if (ropt->dumpSchema || !ropt->disable_triggers) |
| 1131 |
return; |
| 1132 |
|
| 1133 |
pg_log_info("disabling triggers for %s", te->tag); |
| 1134 |
|
| 1135 |
/* |
| 1136 |
* Become superuser if possible, since they are the only ones who can |
| 1137 |
* disable constraint triggers. If -S was not given, assume the initial |
| 1138 |
* user identity is a superuser. (XXX would it be better to become the |
| 1139 |
* table owner?) |
| 1140 |
*/ |
| 1141 |
_becomeUser(AH, ropt->superuser); |
| 1142 |
|
| 1143 |
/* |
| 1144 |
* Disable them. |
| 1145 |
*/ |
| 1146 |
ahprintf(AH, "ALTER TABLE %s DISABLE TRIGGER ALL;\n\n", |
| 1147 |
fmtQualifiedId(te->namespace, te->tag)); |
| 1148 |
} |
| 1149 |
|
| 1150 |
static void |
| 1151 |
_enableTriggersIfNecessary(ArchiveHandle *AH, TocEntry *te) |
| 1152 |
{ |
| 1153 |
RestoreOptions *ropt = AH->public.ropt; |
| 1154 |
|
| 1155 |
/* This hack is only needed in a data-only restore */ |
| 1156 |
if (ropt->dumpSchema || !ropt->disable_triggers) |
| 1157 |
return; |
| 1158 |
|
| 1159 |
pg_log_info("enabling triggers for %s", te->tag); |
| 1160 |
|
| 1161 |
/* |
| 1162 |
* Become superuser if possible, since they are the only ones who can |
| 1163 |
* disable constraint triggers. If -S was not given, assume the initial |
| 1164 |
* user identity is a superuser. (XXX would it be better to become the |
| 1165 |
* table owner?) |
| 1166 |
*/ |
| 1167 |
_becomeUser(AH, ropt->superuser); |
| 1168 |
|
| 1169 |
/* |
| 1170 |
* Enable them. |
| 1171 |
*/ |
| 1172 |
ahprintf(AH, "ALTER TABLE %s ENABLE TRIGGER ALL;\n\n", |
| 1173 |
fmtQualifiedId(te->namespace, te->tag)); |
| 1174 |
} |
| 1175 |
|
| 1176 |
/* |
| 1177 |
* Detect whether a TABLE DATA TOC item is performing "load via partition |
| 1178 |
* root", that is the target table is an ancestor partition rather than the |
| 1179 |
* table the TOC item is nominally for. |
| 1180 |
* |
| 1181 |
* In newer archive files this can be detected by checking for a special |
| 1182 |
* comment placed in te->defn. In older files we have to fall back to seeing |
| 1183 |
* if the COPY statement targets the named table or some other one. This |
| 1184 |
* will not work for data dumped as INSERT commands, so we could give a false |
| 1185 |
* negative in that case; fortunately, that's a rarely-used option. |
| 1186 |
*/ |
| 1187 |
static bool |
| 1188 |
is_load_via_partition_root(TocEntry *te) |
| 1189 |
{ |
| 1190 |
if (te->defn && |
| 1191 |
strncmp(te->defn, "-- load via partition root ", 27) == 0) |
| 1192 |
return true; |
| 1193 |
if (te->copyStmt && *te->copyStmt) |
| 1194 |
{ |
| 1195 |
PQExpBuffer copyStmt = createPQExpBuffer(); |
| 1196 |
bool result; |
| 1197 |
|
| 1198 |
/* |
| 1199 |
* Build the initial part of the COPY as it would appear if the |
| 1200 |
* nominal target table is the actual target. If we see anything |
| 1201 |
* else, it must be a load-via-partition-root case. |
| 1202 |
*/ |
| 1203 |
appendPQExpBuffer(copyStmt, "COPY %s ", |
| 1204 |
fmtQualifiedId(te->namespace, te->tag)); |
| 1205 |
result = strncmp(te->copyStmt, copyStmt->data, copyStmt->len) != 0; |
| 1206 |
destroyPQExpBuffer(copyStmt); |
| 1207 |
return result; |
| 1208 |
} |
| 1209 |
/* Assume it's not load-via-partition-root */ |
| 1210 |
return false; |
| 1211 |
} |
| 1212 |
|
| 1213 |
/* |
| 1214 |
* This is a routine that is part of the dumper interface, hence the 'Archive*' parameter. |
| 1215 |
*/ |
| 1216 |
|
| 1217 |
/* Public */ |
| 1218 |
void |
| 1219 |
WriteData(Archive *AHX, const void *data, size_t dLen) |
| 1220 |
{ |
| 1221 |
ArchiveHandle *AH = (ArchiveHandle *) AHX; |
| 1222 |
|
| 1223 |
if (!AH->currToc) |
| 1224 |
pg_fatal("internal error -- WriteData cannot be called outside the context of a DataDumper routine"); |
| 1225 |
|
| 1226 |
AH->WriteDataPtr(AH, data, dLen); |
| 1227 |
} |
| 1228 |
|
| 1229 |
/* |
| 1230 |
* Create a new TOC entry. The TOC was designed as a TOC, but is now the |
| 1231 |
* repository for all metadata. But the name has stuck. |
| 1232 |
* |
| 1233 |
* The new entry is added to the Archive's TOC list. Most callers can ignore |
| 1234 |
* the result value because nothing else need be done, but a few want to |
| 1235 |
* manipulate the TOC entry further. |
| 1236 |
*/ |
| 1237 |
|
| 1238 |
/* Public */ |
| 1239 |
TocEntry * |
| 1240 |
ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, |
| 1241 |
ArchiveOpts *opts) |
| 1242 |
{ |
| 1243 |
ArchiveHandle *AH = (ArchiveHandle *) AHX; |
| 1244 |
TocEntry *newToc; |
| 1245 |
|
| 1246 |
newToc = (TocEntry *) pg_malloc0(sizeof(TocEntry)); |
| 1247 |
|
| 1248 |
AH->tocCount++; |
| 1249 |
if (dumpId > AH->maxDumpId) |
| 1250 |
AH->maxDumpId = dumpId; |
| 1251 |
|
| 1252 |
newToc->prev = AH->toc->prev; |
| 1253 |
newToc->next = AH->toc; |
| 1254 |
AH->toc->prev->next = newToc; |
| 1255 |
AH->toc->prev = newToc; |
| 1256 |
|
| 1257 |
newToc->catalogId = catalogId; |
| 1258 |
newToc->dumpId = dumpId; |
| 1259 |
newToc->section = opts->section; |
| 1260 |
|
| 1261 |
newToc->tag = pg_strdup(opts->tag); |
| 1262 |
newToc->namespace = opts->namespace ? pg_strdup(opts->namespace) : NULL; |
| 1263 |
newToc->tablespace = opts->tablespace ? pg_strdup(opts->tablespace) : NULL; |
| 1264 |
newToc->tableam = opts->tableam ? pg_strdup(opts->tableam) : NULL; |
| 1265 |
newToc->relkind = opts->relkind; |
| 1266 |
newToc->owner = opts->owner ? pg_strdup(opts->owner) : NULL; |
| 1267 |
newToc->desc = pg_strdup(opts->description); |
| 1268 |
newToc->defn = opts->createStmt ? pg_strdup(opts->createStmt) : NULL; |
| 1269 |
newToc->dropStmt = opts->dropStmt ? pg_strdup(opts->dropStmt) : NULL; |
| 1270 |
newToc->copyStmt = opts->copyStmt ? pg_strdup(opts->copyStmt) : NULL; |
| 1271 |
|
| 1272 |
if (opts->nDeps > 0) |
| 1273 |
{ |
| 1274 |
newToc->dependencies = (DumpId *) pg_malloc(opts->nDeps * sizeof(DumpId)); |
| 1275 |
memcpy(newToc->dependencies, opts->deps, opts->nDeps * sizeof(DumpId)); |
| 1276 |
newToc->nDeps = opts->nDeps; |
| 1277 |
} |
| 1278 |
else |
| 1279 |
{ |
| 1280 |
newToc->dependencies = NULL; |
| 1281 |
newToc->nDeps = 0; |
| 1282 |
} |
| 1283 |
|
| 1284 |
newToc->dataDumper = opts->dumpFn; |
| 1285 |
newToc->dataDumperArg = opts->dumpArg; |
| 1286 |
newToc->hadDumper = opts->dumpFn ? true : false; |
| 1287 |
|
| 1288 |
newToc->defnDumper = opts->defnFn; |
| 1289 |
newToc->defnDumperArg = opts->defnArg; |
| 1290 |
|
| 1291 |
newToc->formatData = NULL; |
| 1292 |
newToc->dataLength = 0; |
| 1293 |
|
| 1294 |
if (AH->ArchiveEntryPtr != NULL) |
| 1295 |
AH->ArchiveEntryPtr(AH, newToc); |
| 1296 |
|
| 1297 |
return newToc; |
| 1298 |
} |
| 1299 |
|
| 1300 |
/* Public */ |
| 1301 |
void |
| 1302 |
PrintTOCSummary(Archive *AHX) |
| 1303 |
{ |
| 1304 |
ArchiveHandle *AH = (ArchiveHandle *) AHX; |
| 1305 |
RestoreOptions *ropt = AH->public.ropt; |
| 1306 |
TocEntry *te; |
| 1307 |
pg_compress_specification out_compression_spec = {0}; |
| 1308 |
teSection curSection; |
| 1309 |
CompressFileHandle *sav; |
| 1310 |
const char *fmtName; |
| 1311 |
char stamp_str[64]; |
| 1312 |
|
| 1313 |
/* TOC is always uncompressed */ |
| 1314 |
out_compression_spec.algorithm = PG_COMPRESSION_NONE; |
| 1315 |
|
| 1316 |
sav = SaveOutput(AH); |
| 1317 |
if (ropt->filename) |
| 1318 |
SetOutput(AH, ropt->filename, out_compression_spec); |
| 1319 |
|
| 1320 |
if (strftime(stamp_str, sizeof(stamp_str), PGDUMP_STRFTIME_FMT, |
| 1321 |
localtime(&AH->createDate)) == 0) |
| 1322 |
strcpy(stamp_str, "[unknown]"); |
| 1323 |
|
| 1324 |
ahprintf(AH, ";\n; Archive created at %s\n", stamp_str); |
| 1325 |
ahprintf(AH, "; dbname: %s\n; TOC Entries: %d\n; Compression: %s\n", |
| 1326 |
sanitize_line(AH->archdbname, false), |
| 1327 |
AH->tocCount, |
| 1328 |
get_compress_algorithm_name(AH->compression_spec.algorithm)); |
| 1329 |
|
| 1330 |
switch (AH->format) |
| 1331 |
{ |
| 1332 |
case archCustom: |
| 1333 |
fmtName = "CUSTOM"; |
| 1334 |
break; |
| 1335 |
case archDirectory: |
| 1336 |
fmtName = "DIRECTORY"; |
| 1337 |
break; |
| 1338 |
case archTar: |
| 1339 |
fmtName = "TAR"; |
| 1340 |
break; |
| 1341 |
default: |
| 1342 |
fmtName = "UNKNOWN"; |
| 1343 |
} |
| 1344 |
|
| 1345 |
ahprintf(AH, "; Dump Version: %d.%d-%d\n", |
| 1346 |
ARCHIVE_MAJOR(AH->version), ARCHIVE_MINOR(AH->version), ARCHIVE_REV(AH->version)); |
| 1347 |
ahprintf(AH, "; Format: %s\n", fmtName); |
| 1348 |
ahprintf(AH, "; Integer: %d bytes\n", (int) AH->intSize); |
| 1349 |
ahprintf(AH, "; Offset: %d bytes\n", (int) AH->offSize); |
| 1350 |
if (AH->archiveRemoteVersion) |
| 1351 |
ahprintf(AH, "; Dumped from database version: %s\n", |
| 1352 |
AH->archiveRemoteVersion); |
| 1353 |
if (AH->archiveDumpVersion) |
| 1354 |
ahprintf(AH, "; Dumped by pg_dump version: %s\n", |
| 1355 |
AH->archiveDumpVersion); |
| 1356 |
|
| 1357 |
ahprintf(AH, ";\n;\n; Selected TOC Entries:\n;\n"); |
| 1358 |
|
| 1359 |
curSection = SECTION_PRE_DATA; |
| 1360 |
for (te = AH->toc->next; te != AH->toc; te = te->next) |
| 1361 |
{ |
| 1362 |
/* This bit must match ProcessArchiveRestoreOptions' marking logic */ |
| 1363 |
if (te->section != SECTION_NONE) |
| 1364 |
curSection = te->section; |
| 1365 |
te->reqs = _tocEntryRequired(te, curSection, AH); |
| 1366 |
/* Now, should we print it? */ |
| 1367 |
if (ropt->verbose || |
| 1368 |
(te->reqs & (REQ_SCHEMA | REQ_DATA | REQ_STATS)) != 0) |
| 1369 |
{ |
| 1370 |
char *sanitized_name; |
| 1371 |
char *sanitized_schema; |
| 1372 |
char *sanitized_owner; |
| 1373 |
|
| 1374 |
/* |
| 1375 |
*/ |
| 1376 |
sanitized_name = sanitize_line(te->tag, false); |
| 1377 |
sanitized_schema = sanitize_line(te->namespace, true); |
| 1378 |
sanitized_owner = sanitize_line(te->owner, false); |
| 1379 |
|
| 1380 |
ahprintf(AH, "%d; %u %u %s %s %s %s\n", te->dumpId, |
| 1381 |
te->catalogId.tableoid, te->catalogId.oid, |
| 1382 |
te->desc, sanitized_schema, sanitized_name, |
| 1383 |
sanitized_owner); |
| 1384 |
|
| 1385 |
free(sanitized_name); |
| 1386 |
free(sanitized_schema); |
| 1387 |
free(sanitized_owner); |
| 1388 |
} |
| 1389 |
if (ropt->verbose && te->nDeps > 0) |
| 1390 |
{ |
| 1391 |
int i; |
| 1392 |
|
| 1393 |
ahprintf(AH, ";\tdepends on:"); |
| 1394 |
for (i = 0; i < te->nDeps; i++) |
| 1395 |
ahprintf(AH, " %d", te->dependencies[i]); |
| 1396 |
ahprintf(AH, "\n"); |
| 1397 |
} |
| 1398 |
} |
| 1399 |
|
| 1400 |
/* Enforce strict names checking */ |
| 1401 |
if (ropt->strict_names) |
| 1402 |
StrictNamesCheck(ropt); |
| 1403 |
|
| 1404 |
if (ropt->filename) |
| 1405 |
RestoreOutput(AH, sav); |
| 1406 |
} |
| 1407 |
|
| 1408 |
/*********** |
| 1409 |
* Large Object Archival |
| 1410 |
***********/ |
| 1411 |
|
| 1412 |
/* Called by a dumper to signal start of a LO */ |
| 1413 |
int |
| 1414 |
StartLO(Archive *AHX, Oid oid) |
| 1415 |
{ |
| 1416 |
ArchiveHandle *AH = (ArchiveHandle *) AHX; |
| 1417 |
|
| 1418 |
if (!AH->StartLOPtr) |
| 1419 |
pg_fatal("large-object output not supported in chosen format"); |
| 1420 |
|
| 1421 |
AH->StartLOPtr(AH, AH->currToc, oid); |
| 1422 |
|
| 1423 |
return 1; |
| 1424 |
} |
| 1425 |
|
| 1426 |
/* Called by a dumper to signal end of a LO */ |
| 1427 |
int |
| 1428 |
EndLO(Archive *AHX, Oid oid) |
| 1429 |
{ |
| 1430 |
ArchiveHandle *AH = (ArchiveHandle *) AHX; |
| 1431 |
|
| 1432 |
if (AH->EndLOPtr) |
| 1433 |
AH->EndLOPtr(AH, AH->currToc, oid); |
| 1434 |
|
| 1435 |
return 1; |
| 1436 |
} |
| 1437 |
|
| 1438 |
/********** |
| 1439 |
* Large Object Restoration |
| 1440 |
**********/ |
| 1441 |
|
| 1442 |
/* |
| 1443 |
* Called by a format handler before a group of LOs is restored |
| 1444 |
*/ |
| 1445 |
void |
| 1446 |
StartRestoreLOs(ArchiveHandle *AH) |
| 1447 |
{ |
| 1448 |
RestoreOptions *ropt = AH->public.ropt; |
| 1449 |
|
| 1450 |
/* |
| 1451 |
* LOs must be restored within a transaction block, since we need the LO |
| 1452 |
* handle to stay open while we write it. Establish a transaction unless |
| 1453 |
* there's one being used globally. |
| 1454 |
*/ |
| 1455 |
if (!(ropt->single_txn || ropt->txn_size > 0)) |
| 1456 |
{ |
| 1457 |
if (AH->connection) |
| 1458 |
StartTransaction(&AH->public); |
| 1459 |
else |
| 1460 |
ahprintf(AH, "BEGIN;\n\n"); |
| 1461 |
} |
| 1462 |
|
| 1463 |
AH->loCount = 0; |
| 1464 |
} |
| 1465 |
|
| 1466 |
/* |
| 1467 |
* Called by a format handler after a group of LOs is restored |
| 1468 |
*/ |
| 1469 |
void |
| 1470 |
EndRestoreLOs(ArchiveHandle *AH) |
| 1471 |
{ |
| 1472 |
RestoreOptions *ropt = AH->public.ropt; |
| 1473 |
|
| 1474 |
if (!(ropt->single_txn || ropt->txn_size > 0)) |
| 1475 |
{ |
| 1476 |
if (AH->connection) |
| 1477 |
CommitTransaction(&AH->public); |
| 1478 |
else |
| 1479 |
ahprintf(AH, "COMMIT;\n\n"); |
| 1480 |
} |
| 1481 |
|
| 1482 |
pg_log_info(ngettext("restored %d large object", |
| 1483 |
"restored %d large objects", |
| 1484 |
AH->loCount), |
| 1485 |
AH->loCount); |
| 1486 |
} |
| 1487 |
|
| 1488 |
|
| 1489 |
/* |
| 1490 |
* Called by a format handler to initiate restoration of a LO |
| 1491 |
*/ |
| 1492 |
void |
| 1493 |
StartRestoreLO(ArchiveHandle *AH, Oid oid, bool drop) |
| 1494 |
{ |
| 1495 |
bool old_lo_style = (AH->version < K_VERS_1_12); |
| 1496 |
Oid loOid; |
| 1497 |
|
| 1498 |
AH->loCount++; |
| 1499 |
|
| 1500 |
/* Initialize the LO Buffer */ |
| 1501 |
if (AH->lo_buf == NULL) |
| 1502 |
{ |
| 1503 |
/* First time through (in this process) so allocate the buffer */ |
| 1504 |
AH->lo_buf_size = LOBBUFSIZE; |
| 1505 |
AH->lo_buf = pg_malloc(LOBBUFSIZE); |
| 1506 |
} |
| 1507 |
AH->lo_buf_used = 0; |
| 1508 |
|
| 1509 |
pg_log_info("restoring large object with OID %u", oid); |
| 1510 |
|
| 1511 |
/* With an old archive we must do drop and create logic here */ |
| 1512 |
if (old_lo_style && drop) |
| 1513 |
DropLOIfExists(AH, oid); |
| 1514 |
|
| 1515 |
if (AH->connection) |
| 1516 |
{ |
| 1517 |
if (old_lo_style) |
| 1518 |
{ |
| 1519 |
loOid = lo_create(AH->connection, oid); |
| 1520 |
if (loOid == 0 || loOid != oid) |
| 1521 |
pg_fatal("could not create large object %u: %s", |
| 1522 |
oid, PQerrorMessage(AH->connection)); |
| 1523 |
} |
| 1524 |
AH->loFd = lo_open(AH->connection, oid, INV_WRITE); |
| 1525 |
if (AH->loFd == -1) |
| 1526 |
pg_fatal("could not open large object %u: %s", |
| 1527 |
oid, PQerrorMessage(AH->connection)); |
| 1528 |
} |
| 1529 |
else |
| 1530 |
{ |
| 1531 |
if (old_lo_style) |
| 1532 |
ahprintf(AH, "SELECT pg_catalog.lo_open(pg_catalog.lo_create('%u'), %d);\n", |
| 1533 |
oid, INV_WRITE); |
| 1534 |
else |
| 1535 |
ahprintf(AH, "SELECT pg_catalog.lo_open('%u', %d);\n", |
| 1536 |
oid, INV_WRITE); |
| 1537 |
} |
| 1538 |
|
| 1539 |
AH->writingLO = true; |
| 1540 |
} |
| 1541 |
|
| 1542 |
void |
| 1543 |
EndRestoreLO(ArchiveHandle *AH, Oid oid) |
| 1544 |
{ |
| 1545 |
if (AH->lo_buf_used > 0) |
| 1546 |
{ |
| 1547 |
/* Write remaining bytes from the LO buffer */ |
| 1548 |
dump_lo_buf(AH); |
| 1549 |
} |
| 1550 |
|
| 1551 |
AH->writingLO = false; |
| 1552 |
|
| 1553 |
if (AH->connection) |
| 1554 |
{ |
| 1555 |
lo_close(AH->connection, AH->loFd); |
| 1556 |
AH->loFd = -1; |
| 1557 |
} |
| 1558 |
else |
| 1559 |
{ |
| 1560 |
ahprintf(AH, "SELECT pg_catalog.lo_close(0);\n\n"); |
| 1561 |
} |
| 1562 |
} |
| 1563 |
|
| 1564 |
/*********** |
| 1565 |
* Sorting and Reordering |
| 1566 |
***********/ |
| 1567 |
|
| 1568 |
void |
| 1569 |
SortTocFromFile(Archive *AHX) |
| 1570 |
{ |
| 1571 |
ArchiveHandle *AH = (ArchiveHandle *) AHX; |
| 1572 |
RestoreOptions *ropt = AH->public.ropt; |
| 1573 |
FILE *fh; |
| 1574 |
StringInfoData linebuf; |
| 1575 |
|
| 1576 |
/* Allocate space for the 'wanted' array, and init it */ |
| 1577 |
ropt->idWanted = (bool *) pg_malloc0(sizeof(bool) * AH->maxDumpId); |
| 1578 |
|
| 1579 |
/* Setup the file */ |
| 1580 |
fh = fopen(ropt->tocFile, PG_BINARY_R); |
| 1581 |
if (!fh) |
| 1582 |
pg_fatal("could not open TOC file \"%s\": %m", ropt->tocFile); |
| 1583 |
|
| 1584 |
initStringInfo(&linebuf); |
| 1585 |
|
| 1586 |
while (pg_get_line_buf(fh, &linebuf)) |
| 1587 |
{ |
| 1588 |
char *cmnt; |
| 1589 |
char *endptr; |
| 1590 |
DumpId id; |
| 1591 |
TocEntry *te; |
| 1592 |
|
| 1593 |
/* Truncate line at comment, if any */ |
| 1594 |
cmnt = strchr(linebuf.data, ';'); |
| 1595 |
if (cmnt != NULL) |
| 1596 |
{ |
| 1597 |
cmnt[0] = '\0'; |
| 1598 |
linebuf.len = cmnt - linebuf.data; |
| 1599 |
} |
| 1600 |
|
| 1601 |
/* Ignore if all blank */ |
| 1602 |
if (strspn(linebuf.data, " \t\r\n") == linebuf.len) |
| 1603 |
continue; |
| 1604 |
|
| 1605 |
/* Get an ID, check it's valid and not already seen */ |
| 1606 |
id = strtol(linebuf.data, &endptr, 10); |
| 1607 |
if (endptr == linebuf.data || id <= 0 || id > AH->maxDumpId || |
| 1608 |
ropt->idWanted[id - 1]) |
| 1609 |
{ |
| 1610 |
pg_log_warning("line ignored: %s", linebuf.data); |
| 1611 |
continue; |
| 1612 |
} |
| 1613 |
|
| 1614 |
/* Find TOC entry */ |
| 1615 |
te = getTocEntryByDumpId(AH, id); |
| 1616 |
if (!te) |
| 1617 |
pg_fatal("could not find entry for ID %d", |
| 1618 |
id); |
| 1619 |
|
| 1620 |
/* Mark it wanted */ |
| 1621 |
ropt->idWanted[id - 1] = true; |
| 1622 |
|
| 1623 |
/* |
| 1624 |
* Move each item to the end of the list as it is selected, so that |
| 1625 |
* they are placed in the desired order. Any unwanted items will end |
| 1626 |
* up at the front of the list, which may seem unintuitive but it's |
| 1627 |
* what we need. In an ordinary serial restore that makes no |
| 1628 |
* difference, but in a parallel restore we need to mark unrestored |
| 1629 |
* items' dependencies as satisfied before we start examining |
| 1630 |
* restorable items. Otherwise they could have surprising |
| 1631 |
* side-effects on the order in which restorable items actually get |
| 1632 |
* restored. |
| 1633 |
*/ |
| 1634 |
_moveBefore(AH->toc, te); |
| 1635 |
} |
| 1636 |
|
| 1637 |
pg_free(linebuf.data); |
| 1638 |
|
| 1639 |
if (fclose(fh) != 0) |
| 1640 |
pg_fatal("could not close TOC file: %m"); |
| 1641 |
} |
| 1642 |
|
| 1643 |
/********************** |
| 1644 |
* Convenience functions that look like standard IO functions |
| 1645 |
* for writing data when in dump mode. |
| 1646 |
**********************/ |
| 1647 |
|
| 1648 |
/* Public */ |
| 1649 |
void |
| 1650 |
archputs(const char *s, Archive *AH) |
| 1651 |
{ |
| 1652 |
WriteData(AH, s, strlen(s)); |
| 1653 |
} |
| 1654 |
|
| 1655 |
/* Public */ |
| 1656 |
int |
| 1657 |
archprintf(Archive *AH, const char *fmt,...) |
| 1658 |
{ |
| 1659 |
int save_errno = errno; |
| 1660 |
char *p; |
| 1661 |
size_t len = 128; /* initial assumption about buffer size */ |
| 1662 |
size_t cnt; |
| 1663 |
|
| 1664 |
for (;;) |
| 1665 |
{ |
| 1666 |
va_list args; |
| 1667 |
|
| 1668 |
/* Allocate work buffer. */ |
| 1669 |
p = (char *) pg_malloc(len); |
| 1670 |
|
| 1671 |
/* Try to format the data. */ |
| 1672 |
errno = save_errno; |
| 1673 |
va_start(args, fmt); |
| 1674 |
cnt = pvsnprintf(p, len, fmt, args); |
| 1675 |
va_end(args); |
| 1676 |
|
| 1677 |
if (cnt < len) |
| 1678 |
break; /* success */ |
| 1679 |
|
| 1680 |
/* Release buffer and loop around to try again with larger len. */ |
| 1681 |
free(p); |
| 1682 |
len = cnt; |
| 1683 |
} |
| 1684 |
|
| 1685 |
WriteData(AH, p, cnt); |
| 1686 |
free(p); |
| 1687 |
return (int) cnt; |
| 1688 |
} |
| 1689 |
|
| 1690 |
|
| 1691 |
/******************************* |
| 1692 |
* Stuff below here should be 'private' to the archiver routines |
| 1693 |
*******************************/ |
| 1694 |
|
| 1695 |
static void |
| 1696 |
SetOutput(ArchiveHandle *AH, const char *filename, |
| 1697 |
const pg_compress_specification compression_spec) |
| 1698 |
{ |
| 1699 |
CompressFileHandle *CFH; |
| 1700 |
const char *mode; |
| 1701 |
int fn = -1; |
| 1702 |
|
| 1703 |
if (filename) |
| 1704 |
{ |
| 1705 |
if (strcmp(filename, "-") == 0) |
| 1706 |
fn = fileno(stdout); |
| 1707 |
} |
| 1708 |
else if (AH->FH) |
| 1709 |
fn = fileno(AH->FH); |
| 1710 |
else if (AH->fSpec) |
| 1711 |
{ |
| 1712 |
filename = AH->fSpec; |
| 1713 |
} |
| 1714 |
else |
| 1715 |
fn = fileno(stdout); |
| 1716 |
|
| 1717 |
if (AH->mode == archModeAppend) |
| 1718 |
mode = PG_BINARY_A; |
| 1719 |
else |
| 1720 |
mode = PG_BINARY_W; |
| 1721 |
|
| 1722 |
CFH = InitCompressFileHandle(compression_spec); |
| 1723 |
|
| 1724 |
if (!CFH->open_func(filename, fn, mode, CFH)) |
| 1725 |
{ |
| 1726 |
if (filename) |
| 1727 |
pg_fatal("could not open output file \"%s\": %m", filename); |
| 1728 |
else |
| 1729 |
pg_fatal("could not open output file: %m"); |
| 1730 |
} |
| 1731 |
|
| 1732 |
AH->OF = CFH; |
| 1733 |
} |
| 1734 |
|
| 1735 |
static CompressFileHandle * |
| 1736 |
SaveOutput(ArchiveHandle *AH) |
| 1737 |
{ |
| 1738 |
return (CompressFileHandle *) AH->OF; |
| 1739 |
} |
| 1740 |
|
| 1741 |
static void |
| 1742 |
RestoreOutput(ArchiveHandle *AH, CompressFileHandle *savedOutput) |
| 1743 |
{ |
| 1744 |
errno = 0; |
| 1745 |
if (!EndCompressFileHandle(AH->OF)) |
| 1746 |
pg_fatal("could not close output file: %m"); |
| 1747 |
|
| 1748 |
AH->OF = savedOutput; |
| 1749 |
} |
| 1750 |
|
| 1751 |
|
| 1752 |
|
| 1753 |
/* |
| 1754 |
* Print formatted text to the output file (usually stdout). |
| 1755 |
*/ |
| 1756 |
int |
| 1757 |
ahprintf(ArchiveHandle *AH, const char *fmt,...) |
| 1758 |
{ |
| 1759 |
int save_errno = errno; |
| 1760 |
char *p; |
| 1761 |
size_t len = 128; /* initial assumption about buffer size */ |
| 1762 |
size_t cnt; |
| 1763 |
|
| 1764 |
for (;;) |
| 1765 |
{ |
| 1766 |
va_list args; |
| 1767 |
|
| 1768 |
/* Allocate work buffer. */ |
| 1769 |
p = (char *) pg_malloc(len); |
| 1770 |
|
| 1771 |
/* Try to format the data. */ |
| 1772 |
errno = save_errno; |
| 1773 |
va_start(args, fmt); |
| 1774 |
cnt = pvsnprintf(p, len, fmt, args); |
| 1775 |
va_end(args); |
| 1776 |
|
| 1777 |
if (cnt < len) |
| 1778 |
break; /* success */ |
| 1779 |
|
| 1780 |
/* Release buffer and loop around to try again with larger len. */ |
| 1781 |
free(p); |
| 1782 |
len = cnt; |
| 1783 |
} |
| 1784 |
|
| 1785 |
ahwrite(p, 1, cnt, AH); |
| 1786 |
free(p); |
| 1787 |
return (int) cnt; |
| 1788 |
} |
| 1789 |
|
| 1790 |
/* |
| 1791 |
* Single place for logic which says 'We are restoring to a direct DB connection'. |
| 1792 |
*/ |
| 1793 |
static int |
| 1794 |
RestoringToDB(ArchiveHandle *AH) |
| 1795 |
{ |
| 1796 |
RestoreOptions *ropt = AH->public.ropt; |
| 1797 |
|
| 1798 |
return (ropt && ropt->useDB && AH->connection); |
| 1799 |
} |
| 1800 |
|
| 1801 |
/* |
| 1802 |
* Dump the current contents of the LO data buffer while writing a LO |
| 1803 |
*/ |
| 1804 |
static void |
| 1805 |
dump_lo_buf(ArchiveHandle *AH) |
| 1806 |
{ |
| 1807 |
if (AH->connection) |
| 1808 |
{ |
| 1809 |
int res; |
| 1810 |
|
| 1811 |
res = lo_write(AH->connection, AH->loFd, AH->lo_buf, AH->lo_buf_used); |
| 1812 |
pg_log_debug(ngettext("wrote %zu byte of large object data (result = %d)", |
| 1813 |
"wrote %zu bytes of large object data (result = %d)", |
| 1814 |
AH->lo_buf_used), |
| 1815 |
AH->lo_buf_used, res); |
| 1816 |
/* We assume there are no short writes, only errors */ |
| 1817 |
if (res != AH->lo_buf_used) |
| 1818 |
warn_or_exit_horribly(AH, "could not write to large object: %s", |
| 1819 |
PQerrorMessage(AH->connection)); |
| 1820 |
} |
| 1821 |
else |
| 1822 |
{ |
| 1823 |
PQExpBuffer buf = createPQExpBuffer(); |
| 1824 |
|
| 1825 |
appendByteaLiteralAHX(buf, |
| 1826 |
(const unsigned char *) AH->lo_buf, |
| 1827 |
AH->lo_buf_used, |
| 1828 |
AH); |
| 1829 |
|
| 1830 |
/* Hack: turn off writingLO so ahwrite doesn't recurse to here */ |
| 1831 |
AH->writingLO = false; |
| 1832 |
ahprintf(AH, "SELECT pg_catalog.lowrite(0, %s);\n", buf->data); |
| 1833 |
AH->writingLO = true; |
| 1834 |
|
| 1835 |
destroyPQExpBuffer(buf); |
| 1836 |
} |
| 1837 |
AH->lo_buf_used = 0; |
| 1838 |
} |
| 1839 |
|
| 1840 |
|
| 1841 |
/* |
| 1842 |
* Write buffer to the output file (usually stdout). This is used for |
| 1843 |
* outputting 'restore' scripts etc. It is even possible for an archive |
| 1844 |
* format to create a custom output routine to 'fake' a restore if it |
| 1845 |
* wants to generate a script (see TAR output). |
| 1846 |
*/ |
| 1847 |
void |
| 1848 |
ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH) |
| 1849 |
{ |
| 1850 |
int bytes_written = 0; |
| 1851 |
|
| 1852 |
if (AH->writingLO) |
| 1853 |
{ |
| 1854 |
size_t remaining = size * nmemb; |
| 1855 |
|
| 1856 |
while (AH->lo_buf_used + remaining > AH->lo_buf_size) |
| 1857 |
{ |
| 1858 |
size_t avail = AH->lo_buf_size - AH->lo_buf_used; |
| 1859 |
|
| 1860 |
memcpy((char *) AH->lo_buf + AH->lo_buf_used, ptr, avail); |
| 1861 |
ptr = (const char *) ptr + avail; |
| 1862 |
remaining -= avail; |
| 1863 |
AH->lo_buf_used += avail; |
| 1864 |
dump_lo_buf(AH); |
| 1865 |
} |
| 1866 |
|
| 1867 |
memcpy((char *) AH->lo_buf + AH->lo_buf_used, ptr, remaining); |
| 1868 |
AH->lo_buf_used += remaining; |
| 1869 |
|
| 1870 |
bytes_written = size * nmemb; |
| 1871 |
} |
| 1872 |
else if (AH->CustomOutPtr) |
| 1873 |
bytes_written = AH->CustomOutPtr(AH, ptr, size * nmemb); |
| 1874 |
|
| 1875 |
/* |
| 1876 |
* If we're doing a restore, and it's direct to DB, and we're connected |
| 1877 |
* then send it to the DB. |
| 1878 |
*/ |
| 1879 |
else if (RestoringToDB(AH)) |
| 1880 |
bytes_written = ExecuteSqlCommandBuf(&AH->public, (const char *) ptr, size * nmemb); |
| 1881 |
else |
| 1882 |
{ |
| 1883 |
CompressFileHandle *CFH = (CompressFileHandle *) AH->OF; |
| 1884 |
|
| 1885 |
CFH->write_func(ptr, size * nmemb, CFH); |
| 1886 |
bytes_written = size * nmemb; |
| 1887 |
} |
| 1888 |
|
| 1889 |
if (bytes_written != size * nmemb) |
| 1890 |
WRITE_ERROR_EXIT; |
| 1891 |
} |
| 1892 |
|
| 1893 |
/* on some error, we may decide to go on... */ |
| 1894 |
void |
| 1895 |
warn_or_exit_horribly(ArchiveHandle *AH, const char *fmt,...) |
| 1896 |
{ |
| 1897 |
va_list ap; |
| 1898 |
|
| 1899 |
switch (AH->stage) |
| 1900 |
{ |
| 1901 |
|
| 1902 |
case STAGE_NONE: |
| 1903 |
/* Do nothing special */ |
| 1904 |
break; |
| 1905 |
|
| 1906 |
case STAGE_INITIALIZING: |
| 1907 |
if (AH->stage != AH->lastErrorStage) |
| 1908 |
pg_log_info("while INITIALIZING:"); |
| 1909 |
break; |
| 1910 |
|
| 1911 |
case STAGE_PROCESSING: |
| 1912 |
if (AH->stage != AH->lastErrorStage) |
| 1913 |
pg_log_info("while PROCESSING TOC:"); |
| 1914 |
break; |
| 1915 |
|
| 1916 |
case STAGE_FINALIZING: |
| 1917 |
if (AH->stage != AH->lastErrorStage) |
| 1918 |
pg_log_info("while FINALIZING:"); |
| 1919 |
break; |
| 1920 |
} |
| 1921 |
if (AH->currentTE != NULL && AH->currentTE != AH->lastErrorTE) |
| 1922 |
{ |
| 1923 |
pg_log_info("from TOC entry %d; %u %u %s %s %s", |
| 1924 |
AH->currentTE->dumpId, |
| 1925 |
AH->currentTE->catalogId.tableoid, |
| 1926 |
AH->currentTE->catalogId.oid, |
| 1927 |
AH->currentTE->desc ? AH->currentTE->desc : "(no desc)", |
| 1928 |
AH->currentTE->tag ? AH->currentTE->tag : "(no tag)", |
| 1929 |
AH->currentTE->owner ? AH->currentTE->owner : "(no owner)"); |
| 1930 |
} |
| 1931 |
AH->lastErrorStage = AH->stage; |
| 1932 |
AH->lastErrorTE = AH->currentTE; |
| 1933 |
|
| 1934 |
va_start(ap, fmt); |
| 1935 |
pg_log_generic_v(PG_LOG_ERROR, PG_LOG_PRIMARY, fmt, ap); |
| 1936 |
va_end(ap); |
| 1937 |
|
| 1938 |
if (AH->public.exit_on_error) |
| 1939 |
exit_nicely(1); |
| 1940 |
else |
| 1941 |
AH->public.n_errors++; |
| 1942 |
} |
| 1943 |
|
| 1944 |
#ifdef NOT_USED |
| 1945 |
|
| 1946 |
static void |
| 1947 |
_moveAfter(ArchiveHandle *AH, TocEntry *pos, TocEntry *te) |
| 1948 |
{ |
| 1949 |
/* Unlink te from list */ |
| 1950 |
te->prev->next = te->next; |
| 1951 |
te->next->prev = te->prev; |
| 1952 |
|
| 1953 |
/* and insert it after "pos" */ |
| 1954 |
te->prev = pos; |
| 1955 |
te->next = pos->next; |
| 1956 |
pos->next->prev = te; |
| 1957 |
pos->next = te; |
| 1958 |
} |
| 1959 |
#endif |
| 1960 |
|
| 1961 |
static void |
| 1962 |
_moveBefore(TocEntry *pos, TocEntry *te) |
| 1963 |
{ |
| 1964 |
/* Unlink te from list */ |
| 1965 |
te->prev->next = te->next; |
| 1966 |
te->next->prev = te->prev; |
| 1967 |
|
| 1968 |
/* and insert it before "pos" */ |
| 1969 |
te->prev = pos->prev; |
| 1970 |
te->next = pos; |
| 1971 |
pos->prev->next = te; |
| 1972 |
pos->prev = te; |
| 1973 |
} |
| 1974 |
|
| 1975 |
/* |
| 1976 |
* Build index arrays for the TOC list |
| 1977 |
* |
| 1978 |
* This should be invoked only after we have created or read in all the TOC |
| 1979 |
* items. |
| 1980 |
* |
| 1981 |
* The arrays are indexed by dump ID (so entry zero is unused). Note that the |
| 1982 |
* array entries run only up to maxDumpId. We might see dependency dump IDs |
| 1983 |
* beyond that (if the dump was partial); so always check the array bound |
| 1984 |
* before trying to touch an array entry. |
| 1985 |
*/ |
| 1986 |
static void |
| 1987 |
buildTocEntryArrays(ArchiveHandle *AH) |
| 1988 |
{ |
| 1989 |
DumpId maxDumpId = AH->maxDumpId; |
| 1990 |
TocEntry *te; |
| 1991 |
|
| 1992 |
AH->tocsByDumpId = (TocEntry **) pg_malloc0((maxDumpId + 1) * sizeof(TocEntry *)); |
| 1993 |
AH->tableDataId = (DumpId *) pg_malloc0((maxDumpId + 1) * sizeof(DumpId)); |
| 1994 |
|
| 1995 |
for (te = AH->toc->next; te != AH->toc; te = te->next) |
| 1996 |
{ |
| 1997 |
/* this check is purely paranoia, maxDumpId should be correct */ |
| 1998 |
if (te->dumpId <= 0 || te->dumpId > maxDumpId) |
| 1999 |
pg_fatal("bad dumpId"); |
| 2000 |
|
| 2001 |
/* tocsByDumpId indexes all TOCs by their dump ID */ |
| 2002 |
AH->tocsByDumpId[te->dumpId] = te; |
| 2003 |
|
| 2004 |
/* |
| 2005 |
* tableDataId provides the TABLE DATA item's dump ID for each TABLE |
| 2006 |
* TOC entry that has a DATA item. We compute this by reversing the |
| 2007 |
* TABLE DATA item's dependency, knowing that a TABLE DATA item has |
| 2008 |
* just one dependency and it is the TABLE item. |
| 2009 |
*/ |
| 2010 |
if (strcmp(te->desc, "TABLE DATA") == 0 && te->nDeps > 0) |
| 2011 |
{ |
| 2012 |
DumpId tableId = te->dependencies[0]; |
| 2013 |
|
| 2014 |
/* |
| 2015 |
* The TABLE item might not have been in the archive, if this was |
| 2016 |
* a data-only dump; but its dump ID should be less than its data |
| 2017 |
* item's dump ID, so there should be a place for it in the array. |
| 2018 |
*/ |
| 2019 |
if (tableId <= 0 || tableId > maxDumpId) |
| 2020 |
pg_fatal("bad table dumpId for TABLE DATA item"); |
| 2021 |
|
| 2022 |
AH->tableDataId[tableId] = te->dumpId; |
| 2023 |
} |
| 2024 |
} |
| 2025 |
} |
| 2026 |
|
| 2027 |
TocEntry * |
| 2028 |
getTocEntryByDumpId(ArchiveHandle *AH, DumpId id) |
| 2029 |
{ |
| 2030 |
/* build index arrays if we didn't already */ |
| 2031 |
if (AH->tocsByDumpId == NULL) |
| 2032 |
buildTocEntryArrays(AH); |
| 2033 |
|
| 2034 |
if (id > 0 && id <= AH->maxDumpId) |
| 2035 |
return AH->tocsByDumpId[id]; |
| 2036 |
|
| 2037 |
return NULL; |
| 2038 |
} |
| 2039 |
|
| 2040 |
int |
| 2041 |
TocIDRequired(ArchiveHandle *AH, DumpId id) |
| 2042 |
{ |
| 2043 |
TocEntry *te = getTocEntryByDumpId(AH, id); |
| 2044 |
|
| 2045 |
if (!te) |
| 2046 |
return 0; |
| 2047 |
|
| 2048 |
return te->reqs; |
| 2049 |
} |
| 2050 |
|
| 2051 |
size_t |
| 2052 |
WriteOffset(ArchiveHandle *AH, pgoff_t o, int wasSet) |
| 2053 |
{ |
| 2054 |
int off; |
| 2055 |
|
| 2056 |
/* Save the flag */ |
| 2057 |
AH->WriteBytePtr(AH, wasSet); |
| 2058 |
|
| 2059 |
/* Write out pgoff_t smallest byte first, prevents endian mismatch */ |
| 2060 |
for (off = 0; off < sizeof(pgoff_t); off++) |
| 2061 |
{ |
| 2062 |
AH->WriteBytePtr(AH, o & 0xFF); |
| 2063 |
o >>= 8; |
| 2064 |
} |
| 2065 |
return sizeof(pgoff_t) + 1; |
| 2066 |
} |
| 2067 |
|
| 2068 |
int |
| 2069 |
ReadOffset(ArchiveHandle *AH, pgoff_t * o) |
| 2070 |
{ |
| 2071 |
int i; |
| 2072 |
int off; |
| 2073 |
int offsetFlg; |
| 2074 |
|
| 2075 |
/* Initialize to zero */ |
| 2076 |
*o = 0; |
| 2077 |
|
| 2078 |
/* Check for old version */ |
| 2079 |
if (AH->version < K_VERS_1_7) |
| 2080 |
{ |
| 2081 |
/* Prior versions wrote offsets using WriteInt */ |
| 2082 |
i = ReadInt(AH); |
| 2083 |
/* -1 means not set */ |
| 2084 |
if (i < 0) |
| 2085 |
return K_OFFSET_POS_NOT_SET; |
| 2086 |
else if (i == 0) |
| 2087 |
return K_OFFSET_NO_DATA; |
| 2088 |
|
| 2089 |
/* Cast to pgoff_t because it was written as an int. */ |
| 2090 |
*o = (pgoff_t) i; |
| 2091 |
return K_OFFSET_POS_SET; |
| 2092 |
} |
| 2093 |
|
| 2094 |
/* |
| 2095 |
* Read the flag indicating the state of the data pointer. Check if valid |
| 2096 |
* and die if not. |
| 2097 |
* |
| 2098 |
* This used to be handled by a negative or zero pointer, now we use an |
| 2099 |
* extra byte specifically for the state. |
| 2100 |
*/ |
| 2101 |
offsetFlg = AH->ReadBytePtr(AH) & 0xFF; |
| 2102 |
|
| 2103 |
switch (offsetFlg) |
| 2104 |
{ |
| 2105 |
case K_OFFSET_POS_NOT_SET: |
| 2106 |
case K_OFFSET_NO_DATA: |
| 2107 |
case K_OFFSET_POS_SET: |
| 2108 |
|
| 2109 |
break; |
| 2110 |
|
| 2111 |
default: |
| 2112 |
pg_fatal("unexpected data offset flag %d", offsetFlg); |
| 2113 |
} |
| 2114 |
|
| 2115 |
/* |
| 2116 |
* Read the bytes |
| 2117 |
*/ |
| 2118 |
for (off = 0; off < AH->offSize; off++) |
| 2119 |
{ |
| 2120 |
if (off < sizeof(pgoff_t)) |
| 2121 |
*o |= ((pgoff_t) (AH->ReadBytePtr(AH))) << (off * 8); |
| 2122 |
else |
| 2123 |
{ |
| 2124 |
if (AH->ReadBytePtr(AH) != 0) |
| 2125 |
pg_fatal("file offset in dump file is too large"); |
| 2126 |
} |
| 2127 |
} |
| 2128 |
|
| 2129 |
return offsetFlg; |
| 2130 |
} |
| 2131 |
|
| 2132 |
size_t |
| 2133 |
WriteInt(ArchiveHandle *AH, int i) |
| 2134 |
{ |
| 2135 |
int b; |
| 2136 |
|
| 2137 |
/* |
| 2138 |
* This is a bit yucky, but I don't want to make the binary format very |
| 2139 |
* dependent on representation, and not knowing much about it, I write out |
| 2140 |
* a sign byte. If you change this, don't forget to change the file |
| 2141 |
* version #, and modify ReadInt to read the new format AS WELL AS the old |
| 2142 |
* formats. |
| 2143 |
*/ |
| 2144 |
|
| 2145 |
/* SIGN byte */ |
| 2146 |
if (i < 0) |
| 2147 |
{ |
| 2148 |
AH->WriteBytePtr(AH, 1); |
| 2149 |
i = -i; |
| 2150 |
} |
| 2151 |
else |
| 2152 |
AH->WriteBytePtr(AH, 0); |
| 2153 |
|
| 2154 |
for (b = 0; b < AH->intSize; b++) |
| 2155 |
{ |
| 2156 |
AH->WriteBytePtr(AH, i & 0xFF); |
| 2157 |
i >>= 8; |
| 2158 |
} |
| 2159 |
|
| 2160 |
return AH->intSize + 1; |
| 2161 |
} |
| 2162 |
|
| 2163 |
int |
| 2164 |
ReadInt(ArchiveHandle *AH) |
| 2165 |
{ |
| 2166 |
int res = 0; |
| 2167 |
int bv, |
| 2168 |
b; |
| 2169 |
int sign = 0; /* Default positive */ |
| 2170 |
int bitShift = 0; |
| 2171 |
|
| 2172 |
if (AH->version > K_VERS_1_0) |
| 2173 |
/* Read a sign byte */ |
| 2174 |
sign = AH->ReadBytePtr(AH); |
| 2175 |
|
| 2176 |
for (b = 0; b < AH->intSize; b++) |
| 2177 |
{ |
| 2178 |
bv = AH->ReadBytePtr(AH) & 0xFF; |
| 2179 |
if (bv != 0) |
| 2180 |
res = res + (bv << bitShift); |
| 2181 |
bitShift += 8; |
| 2182 |
} |
| 2183 |
|
| 2184 |
if (sign) |
| 2185 |
res = -res; |
| 2186 |
|
| 2187 |
return res; |
| 2188 |
} |
| 2189 |
|
| 2190 |
size_t |
| 2191 |
WriteStr(ArchiveHandle *AH, const char *c) |
| 2192 |
{ |
| 2193 |
size_t res; |
| 2194 |
|
| 2195 |
if (c) |
| 2196 |
{ |
| 2197 |
int len = strlen(c); |
| 2198 |
|
| 2199 |
res = WriteInt(AH, len); |
| 2200 |
AH->WriteBufPtr(AH, c, len); |
| 2201 |
res += len; |
| 2202 |
} |
| 2203 |
else |
| 2204 |
res = WriteInt(AH, -1); |
| 2205 |
|
| 2206 |
return res; |
| 2207 |
} |
| 2208 |
|
| 2209 |
char * |
| 2210 |
ReadStr(ArchiveHandle *AH) |
| 2211 |
{ |
| 2212 |
char *buf; |
| 2213 |
int l; |
| 2214 |
|
| 2215 |
l = ReadInt(AH); |
| 2216 |
if (l < 0) |
| 2217 |
buf = NULL; |
| 2218 |
else |
| 2219 |
{ |
| 2220 |
buf = (char *) pg_malloc(l + 1); |
| 2221 |
AH->ReadBufPtr(AH, buf, l); |
| 2222 |
|
| 2223 |
buf[l] = '\0'; |
| 2224 |
} |
| 2225 |
|
| 2226 |
return buf; |
| 2227 |
} |
| 2228 |
|
| 2229 |
static bool |
| 2230 |
_fileExistsInDirectory(const char *dir, const char *filename) |
| 2231 |
{ |
| 2232 |
struct stat st; |
| 2233 |
char buf[MAXPGPATH]; |
| 2234 |
|
| 2235 |
if (snprintf(buf, MAXPGPATH, "%s/%s", dir, filename) >= MAXPGPATH) |
| 2236 |
pg_fatal("directory name too long: \"%s\"", dir); |
| 2237 |
|
| 2238 |
return (stat(buf, &st) == 0 && S_ISREG(st.st_mode)); |
| 2239 |
} |
| 2240 |
|
| 2241 |
static int |
| 2242 |
_discoverArchiveFormat(ArchiveHandle *AH) |
| 2243 |
{ |
| 2244 |
FILE *fh; |
| 2245 |
char sig[6]; /* More than enough */ |
| 2246 |
size_t cnt; |
| 2247 |
int wantClose = 0; |
| 2248 |
|
| 2249 |
pg_log_debug("attempting to ascertain archive format"); |
| 2250 |
|
| 2251 |
free(AH->lookahead); |
| 2252 |
|
| 2253 |
AH->readHeader = 0; |
| 2254 |
AH->lookaheadSize = 512; |
| 2255 |
AH->lookahead = pg_malloc0(512); |
| 2256 |
AH->lookaheadLen = 0; |
| 2257 |
AH->lookaheadPos = 0; |
| 2258 |
|
| 2259 |
if (AH->fSpec) |
| 2260 |
{ |
| 2261 |
struct stat st; |
| 2262 |
|
| 2263 |
wantClose = 1; |
| 2264 |
|
| 2265 |
/* |
| 2266 |
* Check if the specified archive is a directory. If so, check if |
| 2267 |
* there's a "toc.dat" (or "toc.dat.{gz,lz4,zst}") file in it. |
| 2268 |
*/ |
| 2269 |
if (stat(AH->fSpec, &st) == 0 && S_ISDIR(st.st_mode)) |
| 2270 |
{ |
| 2271 |
AH->format = archDirectory; |
| 2272 |
if (_fileExistsInDirectory(AH->fSpec, "toc.dat")) |
| 2273 |
return AH->format; |
| 2274 |
#ifdef HAVE_LIBZ |
| 2275 |
if (_fileExistsInDirectory(AH->fSpec, "toc.dat.gz")) |
| 2276 |
return AH->format; |
| 2277 |
#endif |
| 2278 |
#ifdef USE_LZ4 |
| 2279 |
if (_fileExistsInDirectory(AH->fSpec, "toc.dat.lz4")) |
| 2280 |
return AH->format; |
| 2281 |
#endif |
| 2282 |
#ifdef USE_ZSTD |
| 2283 |
if (_fileExistsInDirectory(AH->fSpec, "toc.dat.zst")) |
| 2284 |
return AH->format; |
| 2285 |
#endif |
| 2286 |
pg_fatal("directory \"%s\" does not appear to be a valid archive (\"toc.dat\" does not exist)", |
| 2287 |
AH->fSpec); |
| 2288 |
fh = NULL; /* keep compiler quiet */ |
| 2289 |
} |
| 2290 |
else |
| 2291 |
{ |
| 2292 |
fh = fopen(AH->fSpec, PG_BINARY_R); |
| 2293 |
if (!fh) |
| 2294 |
pg_fatal("could not open input file \"%s\": %m", AH->fSpec); |
| 2295 |
} |
| 2296 |
} |
| 2297 |
else |
| 2298 |
{ |
| 2299 |
fh = stdin; |
| 2300 |
if (!fh) |
| 2301 |
pg_fatal("could not open input file: %m"); |
| 2302 |
} |
| 2303 |
|
| 2304 |
if ((cnt = fread(sig, 1, 5, fh)) != 5) |
| 2305 |
{ |
| 2306 |
if (ferror(fh)) |
| 2307 |
pg_fatal("could not read input file: %m"); |
| 2308 |
else |
| 2309 |
pg_fatal("input file is too short (read %lu, expected 5)", |
| 2310 |
(unsigned long) cnt); |
| 2311 |
} |
| 2312 |
|
| 2313 |
/* Save it, just in case we need it later */ |
| 2314 |
memcpy(&AH->lookahead[0], sig, 5); |
| 2315 |
AH->lookaheadLen = 5; |
| 2316 |
|
| 2317 |
if (strncmp(sig, "PGDMP", 5) == 0) |
| 2318 |
{ |
| 2319 |
/* It's custom format, stop here */ |
| 2320 |
AH->format = archCustom; |
| 2321 |
AH->readHeader = 1; |
| 2322 |
} |
| 2323 |
else |
| 2324 |
{ |
| 2325 |
/* |
| 2326 |
* *Maybe* we have a tar archive format file or a text dump ... So, |
| 2327 |
* read first 512 byte header... |
| 2328 |
*/ |
| 2329 |
cnt = fread(&AH->lookahead[AH->lookaheadLen], 1, 512 - AH->lookaheadLen, fh); |
| 2330 |
/* read failure is checked below */ |
| 2331 |
AH->lookaheadLen += cnt; |
| 2332 |
|
| 2333 |
if (AH->lookaheadLen >= strlen(TEXT_DUMPALL_HEADER) && |
| 2334 |
(strncmp(AH->lookahead, TEXT_DUMP_HEADER, strlen(TEXT_DUMP_HEADER)) == 0 || |
| 2335 |
strncmp(AH->lookahead, TEXT_DUMPALL_HEADER, strlen(TEXT_DUMPALL_HEADER)) == 0)) |
| 2336 |
{ |
| 2337 |
/* |
| 2338 |
* looks like it's probably a text format dump. so suggest they |
| 2339 |
* try psql |
| 2340 |
*/ |
| 2341 |
pg_fatal("input file appears to be a text format dump. Please use psql."); |
| 2342 |
} |
| 2343 |
|
| 2344 |
if (AH->lookaheadLen != 512) |
| 2345 |
{ |
| 2346 |
if (feof(fh)) |
| 2347 |
pg_fatal("input file does not appear to be a valid archive (too short?)"); |
| 2348 |
else |
| 2349 |
READ_ERROR_EXIT(fh); |
| 2350 |
} |
| 2351 |
|
| 2352 |
if (!isValidTarHeader(AH->lookahead)) |
| 2353 |
pg_fatal("input file does not appear to be a valid tar archive"); |
| 2354 |
|
| 2355 |
AH->format = archTar; |
| 2356 |
} |
| 2357 |
|
| 2358 |
/* Close the file if we opened it */ |
| 2359 |
if (wantClose) |
| 2360 |
{ |
| 2361 |
if (fclose(fh) != 0) |
| 2362 |
pg_fatal("could not close input file: %m"); |
| 2363 |
/* Forget lookahead, since we'll re-read header after re-opening */ |
| 2364 |
AH->readHeader = 0; |
| 2365 |
AH->lookaheadLen = 0; |
| 2366 |
} |
| 2367 |
|
| 2368 |
return AH->format; |
| 2369 |
} |
| 2370 |
|
| 2371 |
|
| 2372 |
/* |
| 2373 |
* Allocate an archive handle |
| 2374 |
*/ |
| 2375 |
static ArchiveHandle * |
| 2376 |
_allocAH(const char *FileSpec, const ArchiveFormat fmt, |
| 2377 |
const pg_compress_specification compression_spec, |
| 2378 |
bool dosync, ArchiveMode mode, |
| 2379 |
SetupWorkerPtrType setupWorkerPtr, DataDirSyncMethod sync_method) |
| 2380 |
{ |
| 2381 |
ArchiveHandle *AH; |
| 2382 |
CompressFileHandle *CFH; |
| 2383 |
pg_compress_specification out_compress_spec = {0}; |
| 2384 |
|
| 2385 |
pg_log_debug("allocating AH for %s, format %d", |
| 2386 |
FileSpec ? FileSpec : "(stdio)", fmt); |
| 2387 |
|
| 2388 |
AH = (ArchiveHandle *) pg_malloc0(sizeof(ArchiveHandle)); |
| 2389 |
|
| 2390 |
AH->version = K_VERS_SELF; |
| 2391 |
|
| 2392 |
/* initialize for backwards compatible string processing */ |
| 2393 |
AH->public.encoding = 0; /* PG_SQL_ASCII */ |
| 2394 |
AH->public.std_strings = false; |
| 2395 |
|
| 2396 |
/* sql error handling */ |
| 2397 |
AH->public.exit_on_error = true; |
| 2398 |
AH->public.n_errors = 0; |
| 2399 |
|
| 2400 |
AH->archiveDumpVersion = PG_VERSION; |
| 2401 |
|
| 2402 |
AH->createDate = time(NULL); |
| 2403 |
|
| 2404 |
AH->intSize = sizeof(int); |
| 2405 |
AH->offSize = sizeof(pgoff_t); |
| 2406 |
if (FileSpec) |
| 2407 |
{ |
| 2408 |
AH->fSpec = pg_strdup(FileSpec); |
| 2409 |
|
| 2410 |
/* |
| 2411 |
* Not used; maybe later.... |
| 2412 |
* |
| 2413 |
* AH->workDir = pg_strdup(FileSpec); for(i=strlen(FileSpec) ; i > 0 ; |
| 2414 |
* i--) if (AH->workDir[i-1] == '/') |
| 2415 |
*/ |
| 2416 |
} |
| 2417 |
else |
| 2418 |
AH->fSpec = NULL; |
| 2419 |
|
| 2420 |
AH->currUser = NULL; /* unknown */ |
| 2421 |
AH->currSchema = NULL; /* ditto */ |
| 2422 |
AH->currTablespace = NULL; /* ditto */ |
| 2423 |
AH->currTableAm = NULL; /* ditto */ |
| 2424 |
|
| 2425 |
AH->toc = (TocEntry *) pg_malloc0(sizeof(TocEntry)); |
| 2426 |
|
| 2427 |
AH->toc->next = AH->toc; |
| 2428 |
AH->toc->prev = AH->toc; |
| 2429 |
|
| 2430 |
AH->mode = mode; |
| 2431 |
AH->compression_spec = compression_spec; |
| 2432 |
AH->dosync = dosync; |
| 2433 |
AH->sync_method = sync_method; |
| 2434 |
|
| 2435 |
memset(&(AH->sqlparse), 0, sizeof(AH->sqlparse)); |
| 2436 |
|
| 2437 |
/* Open stdout with no compression for AH output handle */ |
| 2438 |
out_compress_spec.algorithm = PG_COMPRESSION_NONE; |
| 2439 |
CFH = InitCompressFileHandle(out_compress_spec); |
| 2440 |
if (!CFH->open_func(NULL, fileno(stdout), PG_BINARY_A, CFH)) |
| 2441 |
pg_fatal("could not open stdout for appending: %m"); |
| 2442 |
AH->OF = CFH; |
| 2443 |
|
| 2444 |
/* |
| 2445 |
* On Windows, we need to use binary mode to read/write non-text files, |
| 2446 |
* which include all archive formats as well as compressed plain text. |
| 2447 |
* Force stdin/stdout into binary mode if that is what we are using. |
| 2448 |
*/ |
| 2449 |
#ifdef WIN32 |
| 2450 |
if ((fmt != archNull || compression_spec.algorithm != PG_COMPRESSION_NONE) && |
| 2451 |
(AH->fSpec == NULL || strcmp(AH->fSpec, "") == 0)) |
| 2452 |
{ |
| 2453 |
if (mode == archModeWrite) |
| 2454 |
_setmode(fileno(stdout), O_BINARY); |
| 2455 |
else |
| 2456 |
_setmode(fileno(stdin), O_BINARY); |
| 2457 |
} |
| 2458 |
#endif |
| 2459 |
|
| 2460 |
AH->SetupWorkerPtr = setupWorkerPtr; |
| 2461 |
|
| 2462 |
if (fmt == archUnknown) |
| 2463 |
AH->format = _discoverArchiveFormat(AH); |
| 2464 |
else |
| 2465 |
AH->format = fmt; |
| 2466 |
|
| 2467 |
switch (AH->format) |
| 2468 |
{ |
| 2469 |
case archCustom: |
| 2470 |
InitArchiveFmt_Custom(AH); |
| 2471 |
break; |
| 2472 |
|
| 2473 |
case archNull: |
| 2474 |
InitArchiveFmt_Null(AH); |
| 2475 |
break; |
| 2476 |
|
| 2477 |
case archDirectory: |
| 2478 |
InitArchiveFmt_Directory(AH); |
| 2479 |
break; |
| 2480 |
|
| 2481 |
case archTar: |
| 2482 |
InitArchiveFmt_Tar(AH); |
| 2483 |
break; |
| 2484 |
|
| 2485 |
default: |
| 2486 |
pg_fatal("unrecognized file format \"%d\"", AH->format); |
| 2487 |
} |
| 2488 |
|
| 2489 |
return AH; |
| 2490 |
} |
| 2491 |
|
| 2492 |
/* |
| 2493 |
* Write out all data (tables & LOs) |
| 2494 |
*/ |
| 2495 |
void |
| 2496 |
WriteDataChunks(ArchiveHandle *AH, ParallelState *pstate) |
| 2497 |
{ |
| 2498 |
TocEntry *te; |
| 2499 |
|
| 2500 |
if (pstate && pstate->numWorkers > 1) |
| 2501 |
{ |
| 2502 |
/* |
| 2503 |
* In parallel mode, this code runs in the leader process. We |
| 2504 |
* construct an array of candidate TEs, then sort it into decreasing |
| 2505 |
* size order, then dispatch each TE to a data-transfer worker. By |
| 2506 |
* dumping larger tables first, we avoid getting into a situation |
| 2507 |
* where we're down to one job and it's big, losing parallelism. |
| 2508 |
*/ |
| 2509 |
TocEntry **tes; |
| 2510 |
int ntes; |
| 2511 |
|
| 2512 |
tes = (TocEntry **) pg_malloc(AH->tocCount * sizeof(TocEntry *)); |
| 2513 |
ntes = 0; |
| 2514 |
for (te = AH->toc->next; te != AH->toc; te = te->next) |
| 2515 |
{ |
| 2516 |
/* Consider only TEs with dataDumper functions ... */ |
| 2517 |
if (!te->dataDumper) |
| 2518 |
continue; |
| 2519 |
/* ... and ignore ones not enabled for dump */ |
| 2520 |
if ((te->reqs & REQ_DATA) == 0) |
| 2521 |
continue; |
| 2522 |
|
| 2523 |
tes[ntes++] = te; |
| 2524 |
} |
| 2525 |
|
| 2526 |
if (ntes > 1) |
| 2527 |
qsort(tes, ntes, sizeof(TocEntry *), TocEntrySizeCompareQsort); |
| 2528 |
|
| 2529 |
for (int i = 0; i < ntes; i++) |
| 2530 |
DispatchJobForTocEntry(AH, pstate, tes[i], ACT_DUMP, |
| 2531 |
mark_dump_job_done, NULL); |
| 2532 |
|
| 2533 |
pg_free(tes); |
| 2534 |
|
| 2535 |
/* Now wait for workers to finish. */ |
| 2536 |
WaitForWorkers(AH, pstate, WFW_ALL_IDLE); |
| 2537 |
} |
| 2538 |
else |
| 2539 |
{ |
| 2540 |
/* Non-parallel mode: just dump all candidate TEs sequentially. */ |
| 2541 |
for (te = AH->toc->next; te != AH->toc; te = te->next) |
| 2542 |
{ |
| 2543 |
/* Must have same filter conditions as above */ |
| 2544 |
if (!te->dataDumper) |
| 2545 |
continue; |
| 2546 |
if ((te->reqs & REQ_DATA) == 0) |
| 2547 |
continue; |
| 2548 |
|
| 2549 |
WriteDataChunksForTocEntry(AH, te); |
| 2550 |
} |
| 2551 |
} |
| 2552 |
} |
| 2553 |
|
| 2554 |
|
| 2555 |
/* |
| 2556 |
* Callback function that's invoked in the leader process after a step has |
| 2557 |
* been parallel dumped. |
| 2558 |
* |
| 2559 |
* We don't need to do anything except check for worker failure. |
| 2560 |
*/ |
| 2561 |
static void |
| 2562 |
mark_dump_job_done(ArchiveHandle *AH, |
| 2563 |
TocEntry *te, |
| 2564 |
int status, |
| 2565 |
void *callback_data) |
| 2566 |
{ |
| 2567 |
pg_log_info("finished item %d %s %s", |
| 2568 |
te->dumpId, te->desc, te->tag); |
| 2569 |
|
| 2570 |
if (status != 0) |
| 2571 |
pg_fatal("worker process failed: exit code %d", |
| 2572 |
status); |
| 2573 |
} |
| 2574 |
|
| 2575 |
|
| 2576 |
void |
| 2577 |
WriteDataChunksForTocEntry(ArchiveHandle *AH, TocEntry *te) |
| 2578 |
{ |
| 2579 |
StartDataPtrType startPtr; |
| 2580 |
EndDataPtrType endPtr; |
| 2581 |
|
| 2582 |
AH->currToc = te; |
| 2583 |
|
| 2584 |
if (strcmp(te->desc, "BLOBS") == 0) |
| 2585 |
{ |
| 2586 |
startPtr = AH->StartLOsPtr; |
| 2587 |
endPtr = AH->EndLOsPtr; |
| 2588 |
} |
| 2589 |
else |
| 2590 |
{ |
| 2591 |
startPtr = AH->StartDataPtr; |
| 2592 |
endPtr = AH->EndDataPtr; |
| 2593 |
} |
| 2594 |
|
| 2595 |
if (startPtr != NULL) |
| 2596 |
(*startPtr) (AH, te); |
| 2597 |
|
| 2598 |
/* |
| 2599 |
* The user-provided DataDumper routine needs to call AH->WriteData |
| 2600 |
*/ |
| 2601 |
te->dataDumper((Archive *) AH, te->dataDumperArg); |
| 2602 |
|
| 2603 |
if (endPtr != NULL) |
| 2604 |
(*endPtr) (AH, te); |
| 2605 |
|
| 2606 |
AH->currToc = NULL; |
| 2607 |
} |
| 2608 |
|
| 2609 |
void |
| 2610 |
WriteToc(ArchiveHandle *AH) |
| 2611 |
{ |
| 2612 |
TocEntry *te; |
| 2613 |
char workbuf[32]; |
| 2614 |
int tocCount; |
| 2615 |
int i; |
| 2616 |
|
| 2617 |
/* count entries that will actually be dumped */ |
| 2618 |
tocCount = 0; |
| 2619 |
for (te = AH->toc->next; te != AH->toc; te = te->next) |
| 2620 |
{ |
| 2621 |
if ((te->reqs & (REQ_SCHEMA | REQ_DATA | REQ_STATS | REQ_SPECIAL)) != 0) |
| 2622 |
tocCount++; |
| 2623 |
} |
| 2624 |
|
| 2625 |
/* printf("%d TOC Entries to save\n", tocCount); */ |
| 2626 |
|
| 2627 |
WriteInt(AH, tocCount); |
| 2628 |
|
| 2629 |
for (te = AH->toc->next; te != AH->toc; te = te->next) |
| 2630 |
{ |
| 2631 |
if ((te->reqs & (REQ_SCHEMA | REQ_DATA | REQ_STATS | REQ_SPECIAL)) == 0) |
| 2632 |
continue; |
| 2633 |
|
| 2634 |
WriteInt(AH, te->dumpId); |
| 2635 |
WriteInt(AH, te->dataDumper ? 1 : 0); |
| 2636 |
|
| 2637 |
/* OID is recorded as a string for historical reasons */ |
| 2638 |
sprintf(workbuf, "%u", te->catalogId.tableoid); |
| 2639 |
WriteStr(AH, workbuf); |
| 2640 |
sprintf(workbuf, "%u", te->catalogId.oid); |
| 2641 |
WriteStr(AH, workbuf); |
| 2642 |
|
| 2643 |
WriteStr(AH, te->tag); |
| 2644 |
WriteStr(AH, te->desc); |
| 2645 |
WriteInt(AH, te->section); |
| 2646 |
|
| 2647 |
if (te->defnLen) |
| 2648 |
{ |
| 2649 |
/* |
| 2650 |
* defnLen should only be set for custom format's second call to |
| 2651 |
* WriteToc(), which rewrites the TOC in place to update data |
| 2652 |
* offsets. Instead of calling the defnDumper a second time |
| 2653 |
* (which could involve re-executing queries), just skip writing |
| 2654 |
* the entry. While regenerating the definition should |
| 2655 |
* theoretically produce the same result as before, it's expensive |
| 2656 |
* and feels risky. |
| 2657 |
* |
| 2658 |
* The custom format only calls WriteToc() a second time if |
| 2659 |
* fseeko() is usable (see _CloseArchive() in pg_backup_custom.c), |
| 2660 |
* so we can safely use it without checking. For other formats, |
| 2661 |
* we fail because one of our assumptions must no longer hold |
| 2662 |
* true. |
| 2663 |
* |
| 2664 |
* XXX This is a layering violation, but the alternative is an |
| 2665 |
* awkward and complicated callback infrastructure for this |
| 2666 |
* special case. This might be worth revisiting in the future. |
| 2667 |
*/ |
| 2668 |
if (AH->format != archCustom) |
| 2669 |
pg_fatal("unexpected TOC entry in WriteToc(): %d %s %s", |
| 2670 |
te->dumpId, te->desc, te->tag); |
| 2671 |
|
| 2672 |
if (fseeko(AH->FH, te->defnLen, SEEK_CUR) != 0) |
| 2673 |
pg_fatal("error during file seek: %m"); |
| 2674 |
} |
| 2675 |
else if (te->defnDumper) |
| 2676 |
{ |
| 2677 |
char *defn = te->defnDumper((Archive *) AH, te->defnDumperArg, te); |
| 2678 |
|
| 2679 |
te->defnLen = WriteStr(AH, defn); |
| 2680 |
pg_free(defn); |
| 2681 |
} |
| 2682 |
else |
| 2683 |
WriteStr(AH, te->defn); |
| 2684 |
|
| 2685 |
WriteStr(AH, te->dropStmt); |
| 2686 |
WriteStr(AH, te->copyStmt); |
| 2687 |
WriteStr(AH, te->namespace); |
| 2688 |
WriteStr(AH, te->tablespace); |
| 2689 |
WriteStr(AH, te->tableam); |
| 2690 |
WriteInt(AH, te->relkind); |
| 2691 |
WriteStr(AH, te->owner); |
| 2692 |
WriteStr(AH, "false"); |
| 2693 |
|
| 2694 |
/* Dump list of dependencies */ |
| 2695 |
for (i = 0; i < te->nDeps; i++) |
| 2696 |
{ |
| 2697 |
sprintf(workbuf, "%d", te->dependencies[i]); |
| 2698 |
WriteStr(AH, workbuf); |
| 2699 |
} |
| 2700 |
WriteStr(AH, NULL); /* Terminate List */ |
| 2701 |
|
| 2702 |
if (AH->WriteExtraTocPtr) |
| 2703 |
AH->WriteExtraTocPtr(AH, te); |
| 2704 |
} |
| 2705 |
} |
| 2706 |
|
| 2707 |
void |
| 2708 |
ReadToc(ArchiveHandle *AH) |
| 2709 |
{ |
| 2710 |
int i; |
| 2711 |
char *tmp; |
| 2712 |
DumpId *deps; |
| 2713 |
int depIdx; |
| 2714 |
int depSize; |
| 2715 |
TocEntry *te; |
| 2716 |
bool is_supported; |
| 2717 |
|
| 2718 |
AH->tocCount = ReadInt(AH); |
| 2719 |
AH->maxDumpId = 0; |
| 2720 |
|
| 2721 |
for (i = 0; i < AH->tocCount; i++) |
| 2722 |
{ |
| 2723 |
te = (TocEntry *) pg_malloc0(sizeof(TocEntry)); |
| 2724 |
te->dumpId = ReadInt(AH); |
| 2725 |
|
| 2726 |
if (te->dumpId > AH->maxDumpId) |
| 2727 |
AH->maxDumpId = te->dumpId; |
| 2728 |
|
| 2729 |
/* Sanity check */ |
| 2730 |
if (te->dumpId <= 0) |
| 2731 |
pg_fatal("entry ID %d out of range -- perhaps a corrupt TOC", |
| 2732 |
te->dumpId); |
| 2733 |
|
| 2734 |
te->hadDumper = ReadInt(AH); |
| 2735 |
|
| 2736 |
if (AH->version >= K_VERS_1_8) |
| 2737 |
{ |
| 2738 |
tmp = ReadStr(AH); |
| 2739 |
sscanf(tmp, "%u", &te->catalogId.tableoid); |
| 2740 |
free(tmp); |
| 2741 |
} |
| 2742 |
else |
| 2743 |
te->catalogId.tableoid = InvalidOid; |
| 2744 |
tmp = ReadStr(AH); |
| 2745 |
sscanf(tmp, "%u", &te->catalogId.oid); |
| 2746 |
free(tmp); |
| 2747 |
|
| 2748 |
te->tag = ReadStr(AH); |
| 2749 |
te->desc = ReadStr(AH); |
| 2750 |
|
| 2751 |
if (AH->version >= K_VERS_1_11) |
| 2752 |
{ |
| 2753 |
te->section = ReadInt(AH); |
| 2754 |
} |
| 2755 |
else |
| 2756 |
{ |
| 2757 |
/* |
| 2758 |
* Rules for pre-8.4 archives wherein pg_dump hasn't classified |
| 2759 |
* the entries into sections. This list need not cover entry |
| 2760 |
* types added later than 8.4. |
| 2761 |
*/ |
| 2762 |
if (strcmp(te->desc, "COMMENT") == 0 || |
| 2763 |
strcmp(te->desc, "ACL") == 0 || |
| 2764 |
strcmp(te->desc, "ACL LANGUAGE") == 0) |
| 2765 |
te->section = SECTION_NONE; |
| 2766 |
else if (strcmp(te->desc, "TABLE DATA") == 0 || |
| 2767 |
strcmp(te->desc, "BLOBS") == 0 || |
| 2768 |
strcmp(te->desc, "BLOB COMMENTS") == 0) |
| 2769 |
te->section = SECTION_DATA; |
| 2770 |
else if (strcmp(te->desc, "CONSTRAINT") == 0 || |
| 2771 |
strcmp(te->desc, "CHECK CONSTRAINT") == 0 || |
| 2772 |
strcmp(te->desc, "FK CONSTRAINT") == 0 || |
| 2773 |
strcmp(te->desc, "INDEX") == 0 || |
| 2774 |
strcmp(te->desc, "RULE") == 0 || |
| 2775 |
strcmp(te->desc, "TRIGGER") == 0) |
| 2776 |
te->section = SECTION_POST_DATA; |
| 2777 |
else |
| 2778 |
te->section = SECTION_PRE_DATA; |
| 2779 |
} |
| 2780 |
|
| 2781 |
te->defn = ReadStr(AH); |
| 2782 |
te->dropStmt = ReadStr(AH); |
| 2783 |
|
| 2784 |
if (AH->version >= K_VERS_1_3) |
| 2785 |
te->copyStmt = ReadStr(AH); |
| 2786 |
|
| 2787 |
if (AH->version >= K_VERS_1_6) |
| 2788 |
te->namespace = ReadStr(AH); |
| 2789 |
|
| 2790 |
if (AH->version >= K_VERS_1_10) |
| 2791 |
te->tablespace = ReadStr(AH); |
| 2792 |
|
| 2793 |
if (AH->version >= K_VERS_1_14) |
| 2794 |
te->tableam = ReadStr(AH); |
| 2795 |
|
| 2796 |
if (AH->version >= K_VERS_1_16) |
| 2797 |
te->relkind = ReadInt(AH); |
| 2798 |
|
| 2799 |
te->owner = ReadStr(AH); |
| 2800 |
is_supported = true; |
| 2801 |
if (AH->version < K_VERS_1_9) |
| 2802 |
is_supported = false; |
| 2803 |
else |
| 2804 |
{ |
| 2805 |
tmp = ReadStr(AH); |
| 2806 |
|
| 2807 |
if (strcmp(tmp, "true") == 0) |
| 2808 |
is_supported = false; |
| 2809 |
|
| 2810 |
free(tmp); |
| 2811 |
} |
| 2812 |
|
| 2813 |
if (!is_supported) |
| 2814 |
pg_log_warning("restoring tables WITH OIDS is not supported anymore"); |
| 2815 |
|
| 2816 |
/* Read TOC entry dependencies */ |
| 2817 |
if (AH->version >= K_VERS_1_5) |
| 2818 |
{ |
| 2819 |
depSize = 100; |
| 2820 |
deps = (DumpId *) pg_malloc(sizeof(DumpId) * depSize); |
| 2821 |
depIdx = 0; |
| 2822 |
for (;;) |
| 2823 |
{ |
| 2824 |
tmp = ReadStr(AH); |
| 2825 |
if (!tmp) |
| 2826 |
break; /* end of list */ |
| 2827 |
if (depIdx >= depSize) |
| 2828 |
{ |
| 2829 |
depSize *= 2; |
| 2830 |
deps = (DumpId *) pg_realloc(deps, sizeof(DumpId) * depSize); |
| 2831 |
} |
| 2832 |
sscanf(tmp, "%d", &deps[depIdx]); |
| 2833 |
free(tmp); |
| 2834 |
depIdx++; |
| 2835 |
} |
| 2836 |
|
| 2837 |
if (depIdx > 0) /* We have a non-null entry */ |
| 2838 |
{ |
| 2839 |
deps = (DumpId *) pg_realloc(deps, sizeof(DumpId) * depIdx); |
| 2840 |
te->dependencies = deps; |
| 2841 |
te->nDeps = depIdx; |
| 2842 |
} |
| 2843 |
else |
| 2844 |
{ |
| 2845 |
free(deps); |
| 2846 |
te->dependencies = NULL; |
| 2847 |
te->nDeps = 0; |
| 2848 |
} |
| 2849 |
} |
| 2850 |
else |
| 2851 |
{ |
| 2852 |
te->dependencies = NULL; |
| 2853 |
te->nDeps = 0; |
| 2854 |
} |
| 2855 |
te->dataLength = 0; |
| 2856 |
|
| 2857 |
if (AH->ReadExtraTocPtr) |
| 2858 |
AH->ReadExtraTocPtr(AH, te); |
| 2859 |
|
| 2860 |
pg_log_debug("read TOC entry %d (ID %d) for %s %s", |
| 2861 |
i, te->dumpId, te->desc, te->tag); |
| 2862 |
|
| 2863 |
/* link completed entry into TOC circular list */ |
| 2864 |
te->prev = AH->toc->prev; |
| 2865 |
AH->toc->prev->next = te; |
| 2866 |
AH->toc->prev = te; |
| 2867 |
te->next = AH->toc; |
| 2868 |
|
| 2869 |
/* special processing immediately upon read for some items */ |
| 2870 |
if (strcmp(te->desc, "ENCODING") == 0) |
| 2871 |
processEncodingEntry(AH, te); |
| 2872 |
else if (strcmp(te->desc, "STDSTRINGS") == 0) |
| 2873 |
processStdStringsEntry(AH, te); |
| 2874 |
else if (strcmp(te->desc, "SEARCHPATH") == 0) |
| 2875 |
processSearchPathEntry(AH, te); |
| 2876 |
} |
| 2877 |
} |
| 2878 |
|
| 2879 |
static void |
| 2880 |
processEncodingEntry(ArchiveHandle *AH, TocEntry *te) |
| 2881 |
{ |
| 2882 |
/* te->defn should have the form SET client_encoding = 'foo'; */ |
| 2883 |
char *defn = pg_strdup(te->defn); |
| 2884 |
char *ptr1; |
| 2885 |
char *ptr2 = NULL; |
| 2886 |
int encoding; |
| 2887 |
|
| 2888 |
ptr1 = strchr(defn, '\''); |
| 2889 |
if (ptr1) |
| 2890 |
ptr2 = strchr(++ptr1, '\''); |
| 2891 |
if (ptr2) |
| 2892 |
{ |
| 2893 |
*ptr2 = '\0'; |
| 2894 |
encoding = pg_char_to_encoding(ptr1); |
| 2895 |
if (encoding < 0) |
| 2896 |
pg_fatal("unrecognized encoding \"%s\"", |
| 2897 |
ptr1); |
| 2898 |
AH->public.encoding = encoding; |
| 2899 |
setFmtEncoding(encoding); |
| 2900 |
} |
| 2901 |
else |
| 2902 |
pg_fatal("invalid ENCODING item: %s", |
| 2903 |
te->defn); |
| 2904 |
|
| 2905 |
free(defn); |
| 2906 |
} |
| 2907 |
|
| 2908 |
static void |
| 2909 |
processStdStringsEntry(ArchiveHandle *AH, TocEntry *te) |
| 2910 |
{ |
| 2911 |
/* te->defn should have the form SET standard_conforming_strings = 'x'; */ |
| 2912 |
char *ptr1; |
| 2913 |
|
| 2914 |
ptr1 = strchr(te->defn, '\''); |
| 2915 |
if (ptr1 && strncmp(ptr1, "'on'", 4) == 0) |
| 2916 |
AH->public.std_strings = true; |
| 2917 |
else if (ptr1 && strncmp(ptr1, "'off'", 5) == 0) |
| 2918 |
AH->public.std_strings = false; |
| 2919 |
else |
| 2920 |
pg_fatal("invalid STDSTRINGS item: %s", |
| 2921 |
te->defn); |
| 2922 |
} |
| 2923 |
|
| 2924 |
static void |
| 2925 |
processSearchPathEntry(ArchiveHandle *AH, TocEntry *te) |
| 2926 |
{ |
| 2927 |
/* |
| 2928 |
* te->defn should contain a command to set search_path. We just copy it |
| 2929 |
* verbatim for use later. |
| 2930 |
*/ |
| 2931 |
AH->public.searchpath = pg_strdup(te->defn); |
| 2932 |
} |
| 2933 |
|
| 2934 |
static void |
| 2935 |
StrictNamesCheck(RestoreOptions *ropt) |
| 2936 |
{ |
| 2937 |
const char *missing_name; |
| 2938 |
|
| 2939 |
Assert(ropt->strict_names); |
| 2940 |
|
| 2941 |
if (ropt->schemaNames.head != NULL) |
| 2942 |
{ |
| 2943 |
missing_name = simple_string_list_not_touched(&ropt->schemaNames); |
| 2944 |
if (missing_name != NULL) |
| 2945 |
pg_fatal("schema \"%s\" not found", missing_name); |
| 2946 |
} |
| 2947 |
|
| 2948 |
if (ropt->tableNames.head != NULL) |
| 2949 |
{ |
| 2950 |
missing_name = simple_string_list_not_touched(&ropt->tableNames); |
| 2951 |
if (missing_name != NULL) |
| 2952 |
pg_fatal("table \"%s\" not found", missing_name); |
| 2953 |
} |
| 2954 |
|
| 2955 |
if (ropt->indexNames.head != NULL) |
| 2956 |
{ |
| 2957 |
missing_name = simple_string_list_not_touched(&ropt->indexNames); |
| 2958 |
if (missing_name != NULL) |
| 2959 |
pg_fatal("index \"%s\" not found", missing_name); |
| 2960 |
} |
| 2961 |
|
| 2962 |
if (ropt->functionNames.head != NULL) |
| 2963 |
{ |
| 2964 |
missing_name = simple_string_list_not_touched(&ropt->functionNames); |
| 2965 |
if (missing_name != NULL) |
| 2966 |
pg_fatal("function \"%s\" not found", missing_name); |
| 2967 |
} |
| 2968 |
|
| 2969 |
if (ropt->triggerNames.head != NULL) |
| 2970 |
{ |
| 2971 |
missing_name = simple_string_list_not_touched(&ropt->triggerNames); |
| 2972 |
if (missing_name != NULL) |
| 2973 |
pg_fatal("trigger \"%s\" not found", missing_name); |
| 2974 |
} |
| 2975 |
} |
| 2976 |
|
| 2977 |
/* |
| 2978 |
* Determine whether we want to restore this TOC entry. |
| 2979 |
* |
| 2980 |
* Returns 0 if entry should be skipped, or some combination of the |
| 2981 |
* REQ_SCHEMA, REQ_DATA, and REQ_STATS bits if we want to restore schema, data |
| 2982 |
* and/or statistics portions of this TOC entry, or REQ_SPECIAL if it's a |
| 2983 |
* special entry. |
| 2984 |
*/ |
| 2985 |
static int |
| 2986 |
_tocEntryRequired(TocEntry *te, teSection curSection, ArchiveHandle *AH) |
| 2987 |
{ |
| 2988 |
int res = REQ_SCHEMA | REQ_DATA; |
| 2989 |
RestoreOptions *ropt = AH->public.ropt; |
| 2990 |
|
| 2991 |
/* These items are treated specially */ |
| 2992 |
if (strcmp(te->desc, "ENCODING") == 0 || |
| 2993 |
strcmp(te->desc, "STDSTRINGS") == 0 || |
| 2994 |
strcmp(te->desc, "SEARCHPATH") == 0) |
| 2995 |
return REQ_SPECIAL; |
| 2996 |
|
| 2997 |
if (strcmp(te->desc, "STATISTICS DATA") == 0) |
| 2998 |
{ |
| 2999 |
if (!ropt->dumpStatistics) |
| 3000 |
return 0; |
| 3001 |
|
| 3002 |
res = REQ_STATS; |
| 3003 |
} |
| 3004 |
|
| 3005 |
/* |
| 3006 |
* DATABASE and DATABASE PROPERTIES also have a special rule: they are |
| 3007 |
* restored in createDB mode, and not restored otherwise, independently of |
| 3008 |
* all else. |
| 3009 |
*/ |
| 3010 |
if (strcmp(te->desc, "DATABASE") == 0 || |
| 3011 |
strcmp(te->desc, "DATABASE PROPERTIES") == 0) |
| 3012 |
{ |
| 3013 |
if (ropt->createDB) |
| 3014 |
return REQ_SCHEMA; |
| 3015 |
else |
| 3016 |
return 0; |
| 3017 |
} |
| 3018 |
|
| 3019 |
/* |
| 3020 |
* Process exclusions that affect certain classes of TOC entries. |
| 3021 |
*/ |
| 3022 |
|
| 3023 |
/* If it's an ACL, maybe ignore it */ |
| 3024 |
if (ropt->aclsSkip && _tocEntryIsACL(te)) |
| 3025 |
return 0; |
| 3026 |
|
| 3027 |
/* If it's a comment, maybe ignore it */ |
| 3028 |
if (ropt->no_comments && strcmp(te->desc, "COMMENT") == 0) |
| 3029 |
return 0; |
| 3030 |
|
| 3031 |
/* If it's a policy, maybe ignore it */ |
| 3032 |
if (ropt->no_policies && |
| 3033 |
(strcmp(te->desc, "POLICY") == 0 || |
| 3034 |
strcmp(te->desc, "ROW SECURITY") == 0)) |
| 3035 |
return 0; |
| 3036 |
|
| 3037 |
/* |
| 3038 |
* If it's a comment on a policy, a publication, or a subscription, maybe |
| 3039 |
* ignore it. |
| 3040 |
*/ |
| 3041 |
if (strcmp(te->desc, "COMMENT") == 0) |
| 3042 |
{ |
| 3043 |
if (ropt->no_policies && |
| 3044 |
strncmp(te->tag, "POLICY", strlen("POLICY")) == 0) |
| 3045 |
return 0; |
| 3046 |
|
| 3047 |
if (ropt->no_publications && |
| 3048 |
strncmp(te->tag, "PUBLICATION", strlen("PUBLICATION")) == 0) |
| 3049 |
return 0; |
| 3050 |
|
| 3051 |
if (ropt->no_subscriptions && |
| 3052 |
strncmp(te->tag, "SUBSCRIPTION", strlen("SUBSCRIPTION")) == 0) |
| 3053 |
return 0; |
| 3054 |
} |
| 3055 |
|
| 3056 |
/* |
| 3057 |
* If it's a publication or a table part of a publication, maybe ignore |
| 3058 |
* it. |
| 3059 |
*/ |
| 3060 |
if (ropt->no_publications && |
| 3061 |
(strcmp(te->desc, "PUBLICATION") == 0 || |
| 3062 |
strcmp(te->desc, "PUBLICATION TABLE") == 0 || |
| 3063 |
strcmp(te->desc, "PUBLICATION TABLES IN SCHEMA") == 0)) |
| 3064 |
return 0; |
| 3065 |
|
| 3066 |
/* If it's a security label, maybe ignore it */ |
| 3067 |
if (ropt->no_security_labels && strcmp(te->desc, "SECURITY LABEL") == 0) |
| 3068 |
return 0; |
| 3069 |
|
| 3070 |
/* |
| 3071 |
* If it's a security label on a publication or a subscription, maybe |
| 3072 |
* ignore it. |
| 3073 |
*/ |
| 3074 |
if (strcmp(te->desc, "SECURITY LABEL") == 0) |
| 3075 |
{ |
| 3076 |
if (ropt->no_publications && |
| 3077 |
strncmp(te->tag, "PUBLICATION", strlen("PUBLICATION")) == 0) |
| 3078 |
return 0; |
| 3079 |
|
| 3080 |
if (ropt->no_subscriptions && |
| 3081 |
strncmp(te->tag, "SUBSCRIPTION", strlen("SUBSCRIPTION")) == 0) |
| 3082 |
return 0; |
| 3083 |
} |
| 3084 |
|
| 3085 |
/* If it's a subscription, maybe ignore it */ |
| 3086 |
if (ropt->no_subscriptions && |
| 3087 |
(strcmp(te->desc, "SUBSCRIPTION") == 0 || |
| 3088 |
strcmp(te->desc, "SUBSCRIPTION TABLE") == 0)) |
| 3089 |
return 0; |
| 3090 |
|
| 3091 |
/* Ignore it if section is not to be dumped/restored */ |
| 3092 |
switch (curSection) |
| 3093 |
{ |
| 3094 |
case SECTION_PRE_DATA: |
| 3095 |
if (!(ropt->dumpSections & DUMP_PRE_DATA)) |
| 3096 |
return 0; |
| 3097 |
break; |
| 3098 |
case SECTION_DATA: |
| 3099 |
if (!(ropt->dumpSections & DUMP_DATA)) |
| 3100 |
return 0; |
| 3101 |
break; |
| 3102 |
case SECTION_POST_DATA: |
| 3103 |
if (!(ropt->dumpSections & DUMP_POST_DATA)) |
| 3104 |
return 0; |
| 3105 |
break; |
| 3106 |
default: |
| 3107 |
/* shouldn't get here, really, but ignore it */ |
| 3108 |
return 0; |
| 3109 |
} |
| 3110 |
|
| 3111 |
/* Ignore it if rejected by idWanted[] (cf. SortTocFromFile) */ |
| 3112 |
if (ropt->idWanted && !ropt->idWanted[te->dumpId - 1]) |
| 3113 |
return 0; |
| 3114 |
|
| 3115 |
/* |
| 3116 |
* Check options for selective dump/restore. |
| 3117 |
*/ |
| 3118 |
if (strcmp(te->desc, "ACL") == 0 || |
| 3119 |
strcmp(te->desc, "COMMENT") == 0 || |
| 3120 |
strcmp(te->desc, "SECURITY LABEL") == 0) |
| 3121 |
{ |
| 3122 |
/* Database properties react to createDB, not selectivity options. */ |
| 3123 |
if (strncmp(te->tag, "DATABASE ", 9) == 0) |
| 3124 |
{ |
| 3125 |
if (!ropt->createDB) |
| 3126 |
return 0; |
| 3127 |
} |
| 3128 |
else if (ropt->schemaNames.head != NULL || |
| 3129 |
ropt->schemaExcludeNames.head != NULL || |
| 3130 |
ropt->selTypes) |
| 3131 |
{ |
| 3132 |
/* |
| 3133 |
* In a selective dump/restore, we want to restore these dependent |
| 3134 |
* TOC entry types only if their parent object is being restored. |
| 3135 |
* Without selectivity options, we let through everything in the |
| 3136 |
* archive. Note there may be such entries with no parent, eg |
| 3137 |
* non-default ACLs for built-in objects. Also, we make |
| 3138 |
* per-column ACLs additionally depend on the table's ACL if any |
| 3139 |
* to ensure correct restore order, so those dependencies should |
| 3140 |
* be ignored in this check. |
| 3141 |
* |
| 3142 |
* This code depends on the parent having been marked already, |
| 3143 |
* which should be the case; if it isn't, perhaps due to |
| 3144 |
* SortTocFromFile rearrangement, skipping the dependent entry |
| 3145 |
* seems prudent anyway. |
| 3146 |
* |
| 3147 |
* Ideally we'd handle, eg, table CHECK constraints this way too. |
| 3148 |
* But it's hard to tell which of their dependencies is the one to |
| 3149 |
* consult. |
| 3150 |
*/ |
| 3151 |
bool dumpthis = false; |
| 3152 |
|
| 3153 |
for (int i = 0; i < te->nDeps; i++) |
| 3154 |
{ |
| 3155 |
TocEntry *pte = getTocEntryByDumpId(AH, te->dependencies[i]); |
| 3156 |
|
| 3157 |
if (!pte) |
| 3158 |
continue; /* probably shouldn't happen */ |
| 3159 |
if (strcmp(pte->desc, "ACL") == 0) |
| 3160 |
continue; /* ignore dependency on another ACL */ |
| 3161 |
if (pte->reqs == 0) |
| 3162 |
continue; /* this object isn't marked, so ignore it */ |
| 3163 |
/* Found a parent to be dumped, so we want to dump this too */ |
| 3164 |
dumpthis = true; |
| 3165 |
break; |
| 3166 |
} |
| 3167 |
if (!dumpthis) |
| 3168 |
return 0; |
| 3169 |
} |
| 3170 |
} |
| 3171 |
else |
| 3172 |
{ |
| 3173 |
/* Apply selective-restore rules for standalone TOC entries. */ |
| 3174 |
if (ropt->schemaNames.head != NULL) |
| 3175 |
{ |
| 3176 |
/* If no namespace is specified, it means all. */ |
| 3177 |
if (!te->namespace) |
| 3178 |
return 0; |
| 3179 |
if (!simple_string_list_member(&ropt->schemaNames, te->namespace)) |
| 3180 |
return 0; |
| 3181 |
} |
| 3182 |
|
| 3183 |
if (ropt->schemaExcludeNames.head != NULL && |
| 3184 |
te->namespace && |
| 3185 |
simple_string_list_member(&ropt->schemaExcludeNames, te->namespace)) |
| 3186 |
return 0; |
| 3187 |
|
| 3188 |
if (ropt->selTypes) |
| 3189 |
{ |
| 3190 |
if (strcmp(te->desc, "STATISTICS DATA") == 0) |
| 3191 |
{ |
| 3192 |
bool dumpthis = false; |
| 3193 |
|
| 3194 |
/* |
| 3195 |
* Statistics data entries can be for tables or indexes. Check |
| 3196 |
* the parent dependency to determine which type this entry |
| 3197 |
* belongs to, then apply the appropriate name filter. |
| 3198 |
*/ |
| 3199 |
for (int i = 0; i < te->nDeps; i++) |
| 3200 |
{ |
| 3201 |
TocEntry *pte = getTocEntryByDumpId(AH, te->dependencies[i]); |
| 3202 |
|
| 3203 |
if (!pte) |
| 3204 |
continue; |
| 3205 |
|
| 3206 |
if (ropt->selTable && |
| 3207 |
(strcmp(pte->desc, "TABLE") == 0 || |
| 3208 |
strcmp(pte->desc, "VIEW") == 0 || |
| 3209 |
strcmp(pte->desc, "FOREIGN TABLE") == 0 || |
| 3210 |
strcmp(pte->desc, "MATERIALIZED VIEW") == 0)) |
| 3211 |
{ |
| 3212 |
if (ropt->tableNames.head == NULL || |
| 3213 |
simple_string_list_member(&ropt->tableNames, pte->tag)) |
| 3214 |
dumpthis = true; |
| 3215 |
} |
| 3216 |
|
| 3217 |
if (ropt->selIndex && |
| 3218 |
strcmp(pte->desc, "INDEX") == 0) |
| 3219 |
{ |
| 3220 |
if (ropt->indexNames.head == NULL || |
| 3221 |
simple_string_list_member(&ropt->indexNames, pte->tag)) |
| 3222 |
dumpthis = true; |
| 3223 |
} |
| 3224 |
} |
| 3225 |
if (!dumpthis) |
| 3226 |
return 0; |
| 3227 |
} |
| 3228 |
else if (strcmp(te->desc, "TABLE") == 0 || |
| 3229 |
strcmp(te->desc, "TABLE DATA") == 0 || |
| 3230 |
strcmp(te->desc, "VIEW") == 0 || |
| 3231 |
strcmp(te->desc, "FOREIGN TABLE") == 0 || |
| 3232 |
strcmp(te->desc, "MATERIALIZED VIEW") == 0 || |
| 3233 |
strcmp(te->desc, "MATERIALIZED VIEW DATA") == 0 || |
| 3234 |
strcmp(te->desc, "SEQUENCE") == 0 || |
| 3235 |
strcmp(te->desc, "SEQUENCE SET") == 0) |
| 3236 |
{ |
| 3237 |
if (!ropt->selTable) |
| 3238 |
return 0; |
| 3239 |
if (ropt->tableNames.head != NULL && |
| 3240 |
!simple_string_list_member(&ropt->tableNames, te->tag)) |
| 3241 |
return 0; |
| 3242 |
} |
| 3243 |
else if (strcmp(te->desc, "INDEX") == 0) |
| 3244 |
{ |
| 3245 |
if (!ropt->selIndex) |
| 3246 |
return 0; |
| 3247 |
if (ropt->indexNames.head != NULL && |
| 3248 |
!simple_string_list_member(&ropt->indexNames, te->tag)) |
| 3249 |
return 0; |
| 3250 |
} |
| 3251 |
else if (strcmp(te->desc, "FUNCTION") == 0 || |
| 3252 |
strcmp(te->desc, "AGGREGATE") == 0 || |
| 3253 |
strcmp(te->desc, "PROCEDURE") == 0) |
| 3254 |
{ |
| 3255 |
if (!ropt->selFunction) |
| 3256 |
return 0; |
| 3257 |
if (ropt->functionNames.head != NULL && |
| 3258 |
!simple_string_list_member(&ropt->functionNames, te->tag)) |
| 3259 |
return 0; |
| 3260 |
} |
| 3261 |
else if (strcmp(te->desc, "TRIGGER") == 0) |
| 3262 |
{ |
| 3263 |
if (!ropt->selTrigger) |
| 3264 |
return 0; |
| 3265 |
if (ropt->triggerNames.head != NULL && |
| 3266 |
!simple_string_list_member(&ropt->triggerNames, te->tag)) |
| 3267 |
return 0; |
| 3268 |
} |
| 3269 |
else |
| 3270 |
return 0; |
| 3271 |
} |
| 3272 |
} |
| 3273 |
|
| 3274 |
|
| 3275 |
/* |
| 3276 |
* Determine whether the TOC entry contains schema and/or data components, |
| 3277 |
* and mask off inapplicable REQ bits. If it had a dataDumper, assume |
| 3278 |
* it's both schema and data. Otherwise it's probably schema-only, but |
| 3279 |
* there are exceptions. |
| 3280 |
*/ |
| 3281 |
if (!te->hadDumper) |
| 3282 |
{ |
| 3283 |
/* |
| 3284 |
* Special Case: If 'SEQUENCE SET' or anything to do with LOs, then it |
| 3285 |
* is considered a data entry. We don't need to check for BLOBS or |
| 3286 |
* old-style BLOB COMMENTS entries, because they will have hadDumper = |
| 3287 |
* true ... but we do need to check new-style BLOB ACLs, comments, |
| 3288 |
* etc. |
| 3289 |
*/ |
| 3290 |
if (strcmp(te->desc, "SEQUENCE SET") == 0 || |
| 3291 |
strcmp(te->desc, "BLOB") == 0 || |
| 3292 |
strcmp(te->desc, "BLOB METADATA") == 0 || |
| 3293 |
(strcmp(te->desc, "ACL") == 0 && |
| 3294 |
strncmp(te->tag, "LARGE OBJECT", 12) == 0) || |
| 3295 |
(strcmp(te->desc, "COMMENT") == 0 && |
| 3296 |
strncmp(te->tag, "LARGE OBJECT", 12) == 0) || |
| 3297 |
(strcmp(te->desc, "SECURITY LABEL") == 0 && |
| 3298 |
strncmp(te->tag, "LARGE OBJECT", 12) == 0)) |
| 3299 |
res = res & REQ_DATA; |
| 3300 |
else |
| 3301 |
res = res & ~REQ_DATA; |
| 3302 |
} |
| 3303 |
|
| 3304 |
/* |
| 3305 |
* If there's no definition command, there's no schema component. Treat |
| 3306 |
* "load via partition root" comments as not schema. |
| 3307 |
*/ |
| 3308 |
if (!te->defn || !te->defn[0] || |
| 3309 |
strncmp(te->defn, "-- load via partition root ", 27) == 0) |
| 3310 |
res = res & ~REQ_SCHEMA; |
| 3311 |
|
| 3312 |
/* |
| 3313 |
* Special case: <Init> type with <Max OID> tag; this is obsolete and we |
| 3314 |
* always ignore it. |
| 3315 |
*/ |
| 3316 |
if ((strcmp(te->desc, "<Init>") == 0) && (strcmp(te->tag, "Max OID") == 0)) |
| 3317 |
return 0; |
| 3318 |
|
| 3319 |
/* Mask it if we don't want data */ |
| 3320 |
if (!ropt->dumpData) |
| 3321 |
{ |
| 3322 |
/* |
| 3323 |
* The sequence_data option overrides dumpData for SEQUENCE SET. |
| 3324 |
* |
| 3325 |
* In binary-upgrade mode, even with dumpData unset, we do not mask |
| 3326 |
* out large objects. (Only large object definitions, comments and |
| 3327 |
* other metadata should be generated in binary-upgrade mode, not the |
| 3328 |
* actual data, but that need not concern us here.) |
| 3329 |
*/ |
| 3330 |
if (!(ropt->sequence_data && strcmp(te->desc, "SEQUENCE SET") == 0) && |
| 3331 |
!(ropt->binary_upgrade && |
| 3332 |
(strcmp(te->desc, "BLOB") == 0 || |
| 3333 |
strcmp(te->desc, "BLOB METADATA") == 0 || |
| 3334 |
(strcmp(te->desc, "ACL") == 0 && |
| 3335 |
strncmp(te->tag, "LARGE OBJECT", 12) == 0) || |
| 3336 |
(strcmp(te->desc, "COMMENT") == 0 && |
| 3337 |
strncmp(te->tag, "LARGE OBJECT", 12) == 0) || |
| 3338 |
(strcmp(te->desc, "SECURITY LABEL") == 0 && |
| 3339 |
strncmp(te->tag, "LARGE OBJECT", 12) == 0)))) |
| 3340 |
res = res & (REQ_SCHEMA | REQ_STATS); |
| 3341 |
} |
| 3342 |
|
| 3343 |
/* Mask it if we don't want schema */ |
| 3344 |
if (!ropt->dumpSchema) |
| 3345 |
res = res & (REQ_DATA | REQ_STATS); |
| 3346 |
|
| 3347 |
return res; |
| 3348 |
} |
| 3349 |
|
| 3350 |
/* |
| 3351 |
* Identify which pass we should restore this TOC entry in. |
| 3352 |
* |
| 3353 |
* See notes with the RestorePass typedef in pg_backup_archiver.h. |
| 3354 |
*/ |
| 3355 |
static RestorePass |
| 3356 |
_tocEntryRestorePass(TocEntry *te) |
| 3357 |
{ |
| 3358 |
/* "ACL LANGUAGE" was a crock emitted only in PG 7.4 */ |
| 3359 |
if (strcmp(te->desc, "ACL") == 0 || |
| 3360 |
strcmp(te->desc, "ACL LANGUAGE") == 0 || |
| 3361 |
strcmp(te->desc, "DEFAULT ACL") == 0) |
| 3362 |
return RESTORE_PASS_ACL; |
| 3363 |
if (strcmp(te->desc, "EVENT TRIGGER") == 0 || |
| 3364 |
strcmp(te->desc, "MATERIALIZED VIEW DATA") == 0) |
| 3365 |
return RESTORE_PASS_POST_ACL; |
| 3366 |
|
| 3367 |
/* |
| 3368 |
* Comments and security labels need to be emitted in the same pass as |
| 3369 |
* their parent objects. ACLs haven't got comments and security labels, |
| 3370 |
* and neither do matview data objects, but event triggers do. |
| 3371 |
* (Fortunately, event triggers haven't got ACLs, or we'd need yet another |
| 3372 |
* weird special case.) |
| 3373 |
*/ |
| 3374 |
if ((strcmp(te->desc, "COMMENT") == 0 || |
| 3375 |
strcmp(te->desc, "SECURITY LABEL") == 0) && |
| 3376 |
strncmp(te->tag, "EVENT TRIGGER ", 14) == 0) |
| 3377 |
return RESTORE_PASS_POST_ACL; |
| 3378 |
|
| 3379 |
/* |
| 3380 |
* If statistics data is dependent on materialized view data, it must be |
| 3381 |
* deferred to RESTORE_PASS_POST_ACL. Those entries are already marked as |
| 3382 |
* SECTION_POST_DATA, and some other stats entries (e.g., index stats) |
| 3383 |
* will also be marked as SECTION_POST_DATA. Additionally, our lookahead |
| 3384 |
* code in fetchAttributeStats() assumes that we dump all statistics data |
| 3385 |
* entries in TOC order. To ensure this assumption holds, we move all |
| 3386 |
* statistics data entries in SECTION_POST_DATA to RESTORE_PASS_POST_ACL. |
| 3387 |
*/ |
| 3388 |
if (strcmp(te->desc, "STATISTICS DATA") == 0 && |
| 3389 |
te->section == SECTION_POST_DATA) |
| 3390 |
return RESTORE_PASS_POST_ACL; |
| 3391 |
|
| 3392 |
/* All else can be handled in the main pass. */ |
| 3393 |
return RESTORE_PASS_MAIN; |
| 3394 |
} |
| 3395 |
|
| 3396 |
/* |
| 3397 |
* Identify TOC entries that are ACLs. |
| 3398 |
* |
| 3399 |
* Note: it seems worth duplicating some code here to avoid a hard-wired |
| 3400 |
* assumption that these are exactly the same entries that we restore during |
| 3401 |
* the RESTORE_PASS_ACL phase. |
| 3402 |
*/ |
| 3403 |
static bool |
| 3404 |
_tocEntryIsACL(TocEntry *te) |
| 3405 |
{ |
| 3406 |
/* "ACL LANGUAGE" was a crock emitted only in PG 7.4 */ |
| 3407 |
if (strcmp(te->desc, "ACL") == 0 || |
| 3408 |
strcmp(te->desc, "ACL LANGUAGE") == 0 || |
| 3409 |
strcmp(te->desc, "DEFAULT ACL") == 0) |
| 3410 |
return true; |
| 3411 |
return false; |
| 3412 |
} |
| 3413 |
|
| 3414 |
/* |
| 3415 |
* Issue SET commands for parameters that we want to have set the same way |
| 3416 |
* at all times during execution of a restore script. |
| 3417 |
*/ |
| 3418 |
static void |
| 3419 |
_doSetFixedOutputState(ArchiveHandle *AH) |
| 3420 |
{ |
| 3421 |
RestoreOptions *ropt = AH->public.ropt; |
| 3422 |
|
| 3423 |
/* |
| 3424 |
* Disable timeouts to allow for slow commands, idle parallel workers, etc |
| 3425 |
*/ |
| 3426 |
ahprintf(AH, "SET statement_timeout = 0;\n"); |
| 3427 |
ahprintf(AH, "SET lock_timeout = 0;\n"); |
| 3428 |
ahprintf(AH, "SET idle_in_transaction_session_timeout = 0;\n"); |
| 3429 |
ahprintf(AH, "SET transaction_timeout = 0;\n"); |
| 3430 |
|
| 3431 |
/* Select the correct character set encoding */ |
| 3432 |
ahprintf(AH, "SET client_encoding = '%s';\n", |
| 3433 |
pg_encoding_to_char(AH->public.encoding)); |
| 3434 |
|
| 3435 |
/* Select the correct string literal syntax */ |
| 3436 |
ahprintf(AH, "SET standard_conforming_strings = %s;\n", |
| 3437 |
AH->public.std_strings ? "on" : "off"); |
| 3438 |
|
| 3439 |
/* Select the role to be used during restore */ |
| 3440 |
if (ropt && ropt->use_role) |
| 3441 |
ahprintf(AH, "SET ROLE %s;\n", fmtId(ropt->use_role)); |
| 3442 |
|
| 3443 |
/* Select the dump-time search_path */ |
| 3444 |
if (AH->public.searchpath) |
| 3445 |
ahprintf(AH, "%s", AH->public.searchpath); |
| 3446 |
|
| 3447 |
/* Make sure function checking is disabled */ |
| 3448 |
ahprintf(AH, "SET check_function_bodies = false;\n"); |
| 3449 |
|
| 3450 |
/* Ensure that all valid XML data will be accepted */ |
| 3451 |
ahprintf(AH, "SET xmloption = content;\n"); |
| 3452 |
|
| 3453 |
/* Avoid annoying notices etc */ |
| 3454 |
ahprintf(AH, "SET client_min_messages = warning;\n"); |
| 3455 |
if (!AH->public.std_strings) |
| 3456 |
ahprintf(AH, "SET escape_string_warning = off;\n"); |
| 3457 |
|
| 3458 |
/* Adjust row-security state */ |
| 3459 |
if (ropt && ropt->enable_row_security) |
| 3460 |
ahprintf(AH, "SET row_security = on;\n"); |
| 3461 |
else |
| 3462 |
ahprintf(AH, "SET row_security = off;\n"); |
| 3463 |
|
| 3464 |
/* |
| 3465 |
* In --transaction-size mode, we should always be in a transaction when |
| 3466 |
* we begin to restore objects. |
| 3467 |
*/ |
| 3468 |
if (ropt && ropt->txn_size > 0) |
| 3469 |
{ |
| 3470 |
if (AH->connection) |
| 3471 |
StartTransaction(&AH->public); |
| 3472 |
else |
| 3473 |
ahprintf(AH, "\nBEGIN;\n"); |
| 3474 |
AH->txnCount = 0; |
| 3475 |
} |
| 3476 |
|
| 3477 |
ahprintf(AH, "\n"); |
| 3478 |
} |
| 3479 |
|
| 3480 |
/* |
| 3481 |
* Issue a SET SESSION AUTHORIZATION command. Caller is responsible |
| 3482 |
* for updating state if appropriate. If user is NULL or an empty string, |
| 3483 |
* the specification DEFAULT will be used. |
| 3484 |
*/ |
| 3485 |
static void |
| 3486 |
_doSetSessionAuth(ArchiveHandle *AH, const char *user) |
| 3487 |
{ |
| 3488 |
PQExpBuffer cmd = createPQExpBuffer(); |
| 3489 |
|
| 3490 |
appendPQExpBufferStr(cmd, "SET SESSION AUTHORIZATION "); |
| 3491 |
|
| 3492 |
/* |
| 3493 |
* SQL requires a string literal here. Might as well be correct. |
| 3494 |
*/ |
| 3495 |
if (user && *user) |
| 3496 |
appendStringLiteralAHX(cmd, user, AH); |
| 3497 |
else |
| 3498 |
appendPQExpBufferStr(cmd, "DEFAULT"); |
| 3499 |
appendPQExpBufferChar(cmd, ';'); |
| 3500 |
|
| 3501 |
if (RestoringToDB(AH)) |
| 3502 |
{ |
| 3503 |
PGresult *res; |
| 3504 |
|
| 3505 |
res = PQexec(AH->connection, cmd->data); |
| 3506 |
|
| 3507 |
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) |
| 3508 |
/* NOT warn_or_exit_horribly... use -O instead to skip this. */ |
| 3509 |
pg_fatal("could not set session user to \"%s\": %s", |
| 3510 |
user, PQerrorMessage(AH->connection)); |
| 3511 |
|
| 3512 |
PQclear(res); |
| 3513 |
} |
| 3514 |
else |
| 3515 |
ahprintf(AH, "%s\n\n", cmd->data); |
| 3516 |
|
| 3517 |
destroyPQExpBuffer(cmd); |
| 3518 |
} |
| 3519 |
|
| 3520 |
|
| 3521 |
/* |
| 3522 |
* Issue the commands to connect to the specified database. |
| 3523 |
* |
| 3524 |
* If we're currently restoring right into a database, this will |
| 3525 |
* actually establish a connection. Otherwise it puts a \connect into |
| 3526 |
* the script output. |
| 3527 |
*/ |
| 3528 |
static void |
| 3529 |
_reconnectToDB(ArchiveHandle *AH, const char *dbname) |
| 3530 |
{ |
| 3531 |
if (RestoringToDB(AH)) |
| 3532 |
ReconnectToServer(AH, dbname); |
| 3533 |
else |
| 3534 |
{ |
| 3535 |
PQExpBufferData connectbuf; |
| 3536 |
RestoreOptions *ropt = AH->public.ropt; |
| 3537 |
|
| 3538 |
/* |
| 3539 |
* We must temporarily exit restricted mode for \connect, etc. |
| 3540 |
* Anything added between this line and the following \restrict must |
| 3541 |
* be careful to avoid any possible meta-command injection vectors. |
| 3542 |
*/ |
| 3543 |
ahprintf(AH, "\\unrestrict %s\n", ropt->restrict_key); |
| 3544 |
|
| 3545 |
initPQExpBuffer(&connectbuf); |
| 3546 |
appendPsqlMetaConnect(&connectbuf, dbname); |
| 3547 |
ahprintf(AH, "%s", connectbuf.data); |
| 3548 |
termPQExpBuffer(&connectbuf); |
| 3549 |
|
| 3550 |
ahprintf(AH, "\\restrict %s\n\n", ropt->restrict_key); |
| 3551 |
} |
| 3552 |
|
| 3553 |
/* |
| 3554 |
* NOTE: currUser keeps track of what the imaginary session user in our |
| 3555 |
* script is. It's now effectively reset to the original userID. |
| 3556 |
*/ |
| 3557 |
free(AH->currUser); |
| 3558 |
AH->currUser = NULL; |
| 3559 |
|
| 3560 |
/* don't assume we still know the output schema, tablespace, etc either */ |
| 3561 |
free(AH->currSchema); |
| 3562 |
AH->currSchema = NULL; |
| 3563 |
|
| 3564 |
free(AH->currTableAm); |
| 3565 |
AH->currTableAm = NULL; |
| 3566 |
|
| 3567 |
free(AH->currTablespace); |
| 3568 |
AH->currTablespace = NULL; |
| 3569 |
|
| 3570 |
/* re-establish fixed state */ |
| 3571 |
_doSetFixedOutputState(AH); |
| 3572 |
} |
| 3573 |
|
| 3574 |
/* |
| 3575 |
* Become the specified user, and update state to avoid redundant commands |
| 3576 |
* |
| 3577 |
* NULL or empty argument is taken to mean restoring the session default |
| 3578 |
*/ |
| 3579 |
static void |
| 3580 |
_becomeUser(ArchiveHandle *AH, const char *user) |
| 3581 |
{ |
| 3582 |
if (!user) |
| 3583 |
user = ""; /* avoid null pointers */ |
| 3584 |
|
| 3585 |
if (AH->currUser && strcmp(AH->currUser, user) == 0) |
| 3586 |
return; /* no need to do anything */ |
| 3587 |
|
| 3588 |
_doSetSessionAuth(AH, user); |
| 3589 |
|
| 3590 |
/* |
| 3591 |
* NOTE: currUser keeps track of what the imaginary session user in our |
| 3592 |
* script is |
| 3593 |
*/ |
| 3594 |
free(AH->currUser); |
| 3595 |
AH->currUser = pg_strdup(user); |
| 3596 |
} |
| 3597 |
|
| 3598 |
/* |
| 3599 |
* Become the owner of the given TOC entry object. If |
| 3600 |
* changes in ownership are not allowed, this doesn't do anything. |
| 3601 |
*/ |
| 3602 |
static void |
| 3603 |
_becomeOwner(ArchiveHandle *AH, TocEntry *te) |
| 3604 |
{ |
| 3605 |
RestoreOptions *ropt = AH->public.ropt; |
| 3606 |
|
| 3607 |
if (ropt && (ropt->noOwner || !ropt->use_setsessauth)) |
| 3608 |
return; |
| 3609 |
|
| 3610 |
_becomeUser(AH, te->owner); |
| 3611 |
} |
| 3612 |
|
| 3613 |
|
| 3614 |
/* |
| 3615 |
* Issue the commands to select the specified schema as the current schema |
| 3616 |
* in the target database. |
| 3617 |
*/ |
| 3618 |
static void |
| 3619 |
_selectOutputSchema(ArchiveHandle *AH, const char *schemaName) |
| 3620 |
{ |
| 3621 |
PQExpBuffer qry; |
| 3622 |
|
| 3623 |
/* |
| 3624 |
* If there was a SEARCHPATH TOC entry, we're supposed to just stay with |
| 3625 |
* that search_path rather than switching to entry-specific paths. |
| 3626 |
* Otherwise, it's an old archive that will not restore correctly unless |
| 3627 |
* we set the search_path as it's expecting. |
| 3628 |
*/ |
| 3629 |
if (AH->public.searchpath) |
| 3630 |
return; |
| 3631 |
|
| 3632 |
if (!schemaName || *schemaName == '\0' || |
| 3633 |
(AH->currSchema && strcmp(AH->currSchema, schemaName) == 0)) |
| 3634 |
return; /* no need to do anything */ |
| 3635 |
|
| 3636 |
qry = createPQExpBuffer(); |
| 3637 |
|
| 3638 |
appendPQExpBuffer(qry, "SET search_path = %s", |
| 3639 |
fmtId(schemaName)); |
| 3640 |
if (strcmp(schemaName, "pg_catalog") != 0) |
| 3641 |
appendPQExpBufferStr(qry, ", pg_catalog"); |
| 3642 |
|
| 3643 |
if (RestoringToDB(AH)) |
| 3644 |
{ |
| 3645 |
PGresult *res; |
| 3646 |
|
| 3647 |
res = PQexec(AH->connection, qry->data); |
| 3648 |
|
| 3649 |
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) |
| 3650 |
warn_or_exit_horribly(AH, |
| 3651 |
"could not set \"search_path\" to \"%s\": %s", |
| 3652 |
schemaName, PQerrorMessage(AH->connection)); |
| 3653 |
|
| 3654 |
PQclear(res); |
| 3655 |
} |
| 3656 |
else |
| 3657 |
ahprintf(AH, "%s;\n\n", qry->data); |
| 3658 |
|
| 3659 |
free(AH->currSchema); |
| 3660 |
AH->currSchema = pg_strdup(schemaName); |
| 3661 |
|
| 3662 |
destroyPQExpBuffer(qry); |
| 3663 |
} |
| 3664 |
|
| 3665 |
/* |
| 3666 |
* Issue the commands to select the specified tablespace as the current one |
| 3667 |
* in the target database. |
| 3668 |
*/ |
| 3669 |
static void |
| 3670 |
_selectTablespace(ArchiveHandle *AH, const char *tablespace) |
| 3671 |
{ |
| 3672 |
RestoreOptions *ropt = AH->public.ropt; |
| 3673 |
PQExpBuffer qry; |
| 3674 |
const char *want, |
| 3675 |
*have; |
| 3676 |
|
| 3677 |
/* do nothing in --no-tablespaces mode */ |
| 3678 |
if (ropt->noTablespace) |
| 3679 |
return; |
| 3680 |
|
| 3681 |
have = AH->currTablespace; |
| 3682 |
want = tablespace; |
| 3683 |
|
| 3684 |
/* no need to do anything for non-tablespace object */ |
| 3685 |
if (!want) |
| 3686 |
return; |
| 3687 |
|
| 3688 |
if (have && strcmp(want, have) == 0) |
| 3689 |
return; /* no need to do anything */ |
| 3690 |
|
| 3691 |
qry = createPQExpBuffer(); |
| 3692 |
|
| 3693 |
if (strcmp(want, "") == 0) |
| 3694 |
{ |
| 3695 |
/* We want the tablespace to be the database's default */ |
| 3696 |
appendPQExpBufferStr(qry, "SET default_tablespace = ''"); |
| 3697 |
} |
| 3698 |
else |
| 3699 |
{ |
| 3700 |
/* We want an explicit tablespace */ |
| 3701 |
appendPQExpBuffer(qry, "SET default_tablespace = %s", fmtId(want)); |
| 3702 |
} |
| 3703 |
|
| 3704 |
if (RestoringToDB(AH)) |
| 3705 |
{ |
| 3706 |
PGresult *res; |
| 3707 |
|
| 3708 |
res = PQexec(AH->connection, qry->data); |
| 3709 |
|
| 3710 |
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) |
| 3711 |
warn_or_exit_horribly(AH, |
| 3712 |
"could not set \"default_tablespace\" to %s: %s", |
| 3713 |
fmtId(want), PQerrorMessage(AH->connection)); |
| 3714 |
|
| 3715 |
PQclear(res); |
| 3716 |
} |
| 3717 |
else |
| 3718 |
ahprintf(AH, "%s;\n\n", qry->data); |
| 3719 |
|
| 3720 |
free(AH->currTablespace); |
| 3721 |
AH->currTablespace = pg_strdup(want); |
| 3722 |
|
| 3723 |
destroyPQExpBuffer(qry); |
| 3724 |
} |
| 3725 |
|
| 3726 |
/* |
| 3727 |
* Set the proper default_table_access_method value for the table. |
| 3728 |
*/ |
| 3729 |
static void |
| 3730 |
_selectTableAccessMethod(ArchiveHandle *AH, const char *tableam) |
| 3731 |
{ |
| 3732 |
RestoreOptions *ropt = AH->public.ropt; |
| 3733 |
PQExpBuffer cmd; |
| 3734 |
const char *want, |
| 3735 |
*have; |
| 3736 |
|
| 3737 |
/* do nothing in --no-table-access-method mode */ |
| 3738 |
if (ropt->noTableAm) |
| 3739 |
return; |
| 3740 |
|
| 3741 |
have = AH->currTableAm; |
| 3742 |
want = tableam; |
| 3743 |
|
| 3744 |
if (!want) |
| 3745 |
return; |
| 3746 |
|
| 3747 |
if (have && strcmp(want, have) == 0) |
| 3748 |
return; |
| 3749 |
|
| 3750 |
cmd = createPQExpBuffer(); |
| 3751 |
appendPQExpBuffer(cmd, "SET default_table_access_method = %s;", fmtId(want)); |
| 3752 |
|
| 3753 |
if (RestoringToDB(AH)) |
| 3754 |
{ |
| 3755 |
PGresult *res; |
| 3756 |
|
| 3757 |
res = PQexec(AH->connection, cmd->data); |
| 3758 |
|
| 3759 |
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) |
| 3760 |
warn_or_exit_horribly(AH, |
| 3761 |
"could not set \"default_table_access_method\": %s", |
| 3762 |
PQerrorMessage(AH->connection)); |
| 3763 |
|
| 3764 |
PQclear(res); |
| 3765 |
} |
| 3766 |
else |
| 3767 |
ahprintf(AH, "%s\n\n", cmd->data); |
| 3768 |
|
| 3769 |
destroyPQExpBuffer(cmd); |
| 3770 |
|
| 3771 |
free(AH->currTableAm); |
| 3772 |
AH->currTableAm = pg_strdup(want); |
| 3773 |
} |
| 3774 |
|
| 3775 |
/* |
| 3776 |
* Set the proper default table access method for a table without storage. |
| 3777 |
* Currently, this is required only for partitioned tables with a table AM. |
| 3778 |
*/ |
| 3779 |
static void |
| 3780 |
_printTableAccessMethodNoStorage(ArchiveHandle *AH, TocEntry *te) |
| 3781 |
{ |
| 3782 |
RestoreOptions *ropt = AH->public.ropt; |
| 3783 |
const char *tableam = te->tableam; |
| 3784 |
PQExpBuffer cmd; |
| 3785 |
|
| 3786 |
/* do nothing in --no-table-access-method mode */ |
| 3787 |
if (ropt->noTableAm) |
| 3788 |
return; |
| 3789 |
|
| 3790 |
if (!tableam) |
| 3791 |
return; |
| 3792 |
|
| 3793 |
Assert(te->relkind == RELKIND_PARTITIONED_TABLE); |
| 3794 |
|
| 3795 |
cmd = createPQExpBuffer(); |
| 3796 |
|
| 3797 |
appendPQExpBufferStr(cmd, "ALTER TABLE "); |
| 3798 |
appendPQExpBuffer(cmd, "%s ", fmtQualifiedId(te->namespace, te->tag)); |
| 3799 |
appendPQExpBuffer(cmd, "SET ACCESS METHOD %s;", |
| 3800 |
fmtId(tableam)); |
| 3801 |
|
| 3802 |
if (RestoringToDB(AH)) |
| 3803 |
{ |
| 3804 |
PGresult *res; |
| 3805 |
|
| 3806 |
res = PQexec(AH->connection, cmd->data); |
| 3807 |
|
| 3808 |
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) |
| 3809 |
warn_or_exit_horribly(AH, |
| 3810 |
"could not alter table access method: %s", |
| 3811 |
PQerrorMessage(AH->connection)); |
| 3812 |
PQclear(res); |
| 3813 |
} |
| 3814 |
else |
| 3815 |
ahprintf(AH, "%s\n\n", cmd->data); |
| 3816 |
|
| 3817 |
destroyPQExpBuffer(cmd); |
| 3818 |
} |
| 3819 |
|
| 3820 |
/* |
| 3821 |
* Extract an object description for a TOC entry, and append it to buf. |
| 3822 |
* |
| 3823 |
* This is used for ALTER ... OWNER TO. |
| 3824 |
* |
| 3825 |
* If the object type has no owner, do nothing. |
| 3826 |
*/ |
| 3827 |
static void |
| 3828 |
_getObjectDescription(PQExpBuffer buf, const TocEntry *te) |
| 3829 |
{ |
| 3830 |
const char *type = te->desc; |
| 3831 |
|
| 3832 |
/* objects that don't require special decoration */ |
| 3833 |
if (strcmp(type, "COLLATION") == 0 || |
| 3834 |
strcmp(type, "CONVERSION") == 0 || |
| 3835 |
strcmp(type, "DOMAIN") == 0 || |
| 3836 |
strcmp(type, "FOREIGN TABLE") == 0 || |
| 3837 |
strcmp(type, "MATERIALIZED VIEW") == 0 || |
| 3838 |
strcmp(type, "SEQUENCE") == 0 || |
| 3839 |
strcmp(type, "STATISTICS") == 0 || |
| 3840 |
strcmp(type, "TABLE") == 0 || |
| 3841 |
strcmp(type, "TEXT SEARCH DICTIONARY") == 0 || |
| 3842 |
strcmp(type, "TEXT SEARCH CONFIGURATION") == 0 || |
| 3843 |
strcmp(type, "TYPE") == 0 || |
| 3844 |
strcmp(type, "VIEW") == 0 || |
| 3845 |
/* non-schema-specified objects */ |
| 3846 |
strcmp(type, "DATABASE") == 0 || |
| 3847 |
strcmp(type, "PROCEDURAL LANGUAGE") == 0 || |
| 3848 |
strcmp(type, "SCHEMA") == 0 || |
| 3849 |
strcmp(type, "EVENT TRIGGER") == 0 || |
| 3850 |
strcmp(type, "FOREIGN DATA WRAPPER") == 0 || |
| 3851 |
strcmp(type, "SERVER") == 0 || |
| 3852 |
strcmp(type, "PUBLICATION") == 0 || |
| 3853 |
strcmp(type, "SUBSCRIPTION") == 0) |
| 3854 |
{ |
| 3855 |
appendPQExpBuffer(buf, "%s ", type); |
| 3856 |
if (te->namespace && *te->namespace) |
| 3857 |
appendPQExpBuffer(buf, "%s.", fmtId(te->namespace)); |
| 3858 |
appendPQExpBufferStr(buf, fmtId(te->tag)); |
| 3859 |
} |
| 3860 |
/* LOs just have a name, but it's numeric so must not use fmtId */ |
| 3861 |
else if (strcmp(type, "BLOB") == 0) |
| 3862 |
{ |
| 3863 |
appendPQExpBuffer(buf, "LARGE OBJECT %s", te->tag); |
| 3864 |
} |
| 3865 |
|
| 3866 |
/* |
| 3867 |
* These object types require additional decoration. Fortunately, the |
| 3868 |
* information needed is exactly what's in the DROP command. |
| 3869 |
*/ |
| 3870 |
else if (strcmp(type, "AGGREGATE") == 0 || |
| 3871 |
strcmp(type, "FUNCTION") == 0 || |
| 3872 |
strcmp(type, "OPERATOR") == 0 || |
| 3873 |
strcmp(type, "OPERATOR CLASS") == 0 || |
| 3874 |
strcmp(type, "OPERATOR FAMILY") == 0 || |
| 3875 |
strcmp(type, "PROCEDURE") == 0) |
| 3876 |
{ |
| 3877 |
/* Chop "DROP " off the front and make a modifiable copy */ |
| 3878 |
char *first = pg_strdup(te->dropStmt + 5); |
| 3879 |
char *last; |
| 3880 |
|
| 3881 |
/* point to last character in string */ |
| 3882 |
last = first + strlen(first) - 1; |
| 3883 |
|
| 3884 |
/* Strip off any ';' or '\n' at the end */ |
| 3885 |
while (last >= first && (*last == '\n' || *last == ';')) |
| 3886 |
last--; |
| 3887 |
*(last + 1) = '\0'; |
| 3888 |
|
| 3889 |
appendPQExpBufferStr(buf, first); |
| 3890 |
|
| 3891 |
free(first); |
| 3892 |
return; |
| 3893 |
} |
| 3894 |
/* these object types don't have separate owners */ |
| 3895 |
else if (strcmp(type, "CAST") == 0 || |
| 3896 |
strcmp(type, "CHECK CONSTRAINT") == 0 || |
| 3897 |
strcmp(type, "CONSTRAINT") == 0 || |
| 3898 |
strcmp(type, "DATABASE PROPERTIES") == 0 || |
| 3899 |
strcmp(type, "DEFAULT") == 0 || |
| 3900 |
strcmp(type, "FK CONSTRAINT") == 0 || |
| 3901 |
strcmp(type, "INDEX") == 0 || |
| 3902 |
strcmp(type, "RULE") == 0 || |
| 3903 |
strcmp(type, "TRIGGER") == 0 || |
| 3904 |
strcmp(type, "ROW SECURITY") == 0 || |
| 3905 |
strcmp(type, "POLICY") == 0 || |
| 3906 |
strcmp(type, "USER MAPPING") == 0) |
| 3907 |
{ |
| 3908 |
/* do nothing */ |
| 3909 |
} |
| 3910 |
else |
| 3911 |
pg_fatal("don't know how to set owner for object type \"%s\"", type); |
| 3912 |
} |
| 3913 |
|
| 3914 |
/* |
| 3915 |
* Emit the SQL commands to create the object represented by a TOC entry |
| 3916 |
* |
| 3917 |
* This now also includes issuing an ALTER OWNER command to restore the |
| 3918 |
* object's ownership, if wanted. But note that the object's permissions |
| 3919 |
* will remain at default, until the matching ACL TOC entry is restored. |
| 3920 |
*/ |
| 3921 |
static void |
| 3922 |
_printTocEntry(ArchiveHandle *AH, TocEntry *te, const char *pfx) |
| 3923 |
{ |
| 3924 |
RestoreOptions *ropt = AH->public.ropt; |
| 3925 |
|
| 3926 |
/* |
| 3927 |
* Select owner, schema, tablespace and default AM as necessary. The |
| 3928 |
* default access method for partitioned tables is handled after |
| 3929 |
* generating the object definition, as it requires an ALTER command |
| 3930 |
* rather than SET. |
| 3931 |
*/ |
| 3932 |
_becomeOwner(AH, te); |
| 3933 |
_selectOutputSchema(AH, te->namespace); |
| 3934 |
_selectTablespace(AH, te->tablespace); |
| 3935 |
if (te->relkind != RELKIND_PARTITIONED_TABLE) |
| 3936 |
_selectTableAccessMethod(AH, te->tableam); |
| 3937 |
|
| 3938 |
/* Emit header comment for item */ |
| 3939 |
if (!AH->noTocComments) |
| 3940 |
{ |
| 3941 |
char *sanitized_name; |
| 3942 |
char *sanitized_schema; |
| 3943 |
char *sanitized_owner; |
| 3944 |
|
| 3945 |
ahprintf(AH, "--\n"); |
| 3946 |
if (AH->public.verbose) |
| 3947 |
{ |
| 3948 |
ahprintf(AH, "-- TOC entry %d (class %u OID %u)\n", |
| 3949 |
te->dumpId, te->catalogId.tableoid, te->catalogId.oid); |
| 3950 |
if (te->nDeps > 0) |
| 3951 |
{ |
| 3952 |
int i; |
| 3953 |
|
| 3954 |
ahprintf(AH, "-- Dependencies:"); |
| 3955 |
for (i = 0; i < te->nDeps; i++) |
| 3956 |
ahprintf(AH, " %d", te->dependencies[i]); |
| 3957 |
ahprintf(AH, "\n"); |
| 3958 |
} |
| 3959 |
} |
| 3960 |
|
| 3961 |
sanitized_name = sanitize_line(te->tag, false); |
| 3962 |
sanitized_schema = sanitize_line(te->namespace, true); |
| 3963 |
sanitized_owner = sanitize_line(ropt->noOwner ? NULL : te->owner, true); |
| 3964 |
|
| 3965 |
ahprintf(AH, "-- %sName: %s; Type: %s; Schema: %s; Owner: %s", |
| 3966 |
pfx, sanitized_name, te->desc, sanitized_schema, |
| 3967 |
sanitized_owner); |
| 3968 |
|
| 3969 |
free(sanitized_name); |
| 3970 |
free(sanitized_schema); |
| 3971 |
free(sanitized_owner); |
| 3972 |
|
| 3973 |
if (te->tablespace && strlen(te->tablespace) > 0 && !ropt->noTablespace) |
| 3974 |
{ |
| 3975 |
char *sanitized_tablespace; |
| 3976 |
|
| 3977 |
sanitized_tablespace = sanitize_line(te->tablespace, false); |
| 3978 |
ahprintf(AH, "; Tablespace: %s", sanitized_tablespace); |
| 3979 |
free(sanitized_tablespace); |
| 3980 |
} |
| 3981 |
ahprintf(AH, "\n"); |
| 3982 |
|
| 3983 |
if (AH->PrintExtraTocPtr != NULL) |
| 3984 |
AH->PrintExtraTocPtr(AH, te); |
| 3985 |
ahprintf(AH, "--\n\n"); |
| 3986 |
} |
| 3987 |
|
| 3988 |
/* |
| 3989 |
* Actually print the definition. Normally we can just print the defn |
| 3990 |
* string if any, but we have four special cases: |
| 3991 |
* |
| 3992 |
* 1. A crude hack for suppressing AUTHORIZATION clause that old pg_dump |
| 3993 |
* versions put into CREATE SCHEMA. Don't mutate the variant for schema |
| 3994 |
* "public" that is a comment. We have to do this when --no-owner mode is |
| 3995 |
* selected. This is ugly, but I see no other good way ... |
| 3996 |
* |
| 3997 |
* 2. BLOB METADATA entries need special processing since their defn |
| 3998 |
* strings are just lists of OIDs, not complete SQL commands. |
| 3999 |
* |
| 4000 |
* 3. ACL LARGE OBJECTS entries need special processing because they |
| 4001 |
* contain only one copy of the ACL GRANT/REVOKE commands, which we must |
| 4002 |
* apply to each large object listed in the associated BLOB METADATA. |
| 4003 |
* |
| 4004 |
* 4. Entries with a defnDumper need to call it to generate the |
| 4005 |
* definition. This is primarily intended to provide a way to save memory |
| 4006 |
* for objects that would otherwise need a lot of it (e.g., statistics |
| 4007 |
* data). |
| 4008 |
*/ |
| 4009 |
if (ropt->noOwner && |
| 4010 |
strcmp(te->desc, "SCHEMA") == 0 && strncmp(te->defn, "--", 2) != 0) |
| 4011 |
{ |
| 4012 |
ahprintf(AH, "CREATE SCHEMA %s;\n\n\n", fmtId(te->tag)); |
| 4013 |
} |
| 4014 |
else if (strcmp(te->desc, "BLOB METADATA") == 0) |
| 4015 |
{ |
| 4016 |
IssueCommandPerBlob(AH, te, "SELECT pg_catalog.lo_create('", "')"); |
| 4017 |
} |
| 4018 |
else if (strcmp(te->desc, "ACL") == 0 && |
| 4019 |
strncmp(te->tag, "LARGE OBJECTS", 13) == 0) |
| 4020 |
{ |
| 4021 |
IssueACLPerBlob(AH, te); |
| 4022 |
} |
| 4023 |
else if (te->defnLen && AH->format != archTar) |
| 4024 |
{ |
| 4025 |
/* |
| 4026 |
* If defnLen is set, the defnDumper has already been called for this |
| 4027 |
* TOC entry. We don't normally expect a defnDumper to be called for |
| 4028 |
* a TOC entry a second time in _printTocEntry(), but there's an |
| 4029 |
* exception. The tar format first calls WriteToc(), which scans the |
| 4030 |
* entire TOC, and then it later calls RestoreArchive() to generate |
| 4031 |
* restore.sql, which scans the TOC again. There doesn't appear to be |
| 4032 |
* a good way to prevent a second defnDumper call in this case without |
| 4033 |
* storing the definition in memory, which defeats the purpose. This |
| 4034 |
* second defnDumper invocation should generate the same output as the |
| 4035 |
* first, but even if it doesn't, the worst-case scenario is that |
| 4036 |
* restore.sql might have different statistics data than the archive. |
| 4037 |
* |
| 4038 |
* In all other cases, encountering a TOC entry a second time in |
| 4039 |
* _printTocEntry() is unexpected, so we fail because one of our |
| 4040 |
* assumptions must no longer hold true. |
| 4041 |
* |
| 4042 |
* XXX This is a layering violation, but the alternative is an awkward |
| 4043 |
* and complicated callback infrastructure for this special case. This |
| 4044 |
* might be worth revisiting in the future. |
| 4045 |
*/ |
| 4046 |
pg_fatal("unexpected TOC entry in _printTocEntry(): %d %s %s", |
| 4047 |
te->dumpId, te->desc, te->tag); |
| 4048 |
} |
| 4049 |
else if (te->defnDumper) |
| 4050 |
{ |
| 4051 |
char *defn = te->defnDumper((Archive *) AH, te->defnDumperArg, te); |
| 4052 |
|
| 4053 |
te->defnLen = ahprintf(AH, "%s\n\n", defn); |
| 4054 |
pg_free(defn); |
| 4055 |
} |
| 4056 |
else if (te->defn && strlen(te->defn) > 0) |
| 4057 |
{ |
| 4058 |
ahprintf(AH, "%s\n\n", te->defn); |
| 4059 |
|
| 4060 |
/* |
| 4061 |
* If the defn string contains multiple SQL commands, txn_size mode |
| 4062 |
* should count it as N actions not one. But rather than build a full |
| 4063 |
* SQL parser, approximate this by counting semicolons. One case |
| 4064 |
* where that tends to be badly fooled is function definitions, so |
| 4065 |
* ignore them. (restore_toc_entry will count one action anyway.) |
| 4066 |
*/ |
| 4067 |
if (ropt->txn_size > 0 && |
| 4068 |
strcmp(te->desc, "FUNCTION") != 0 && |
| 4069 |
strcmp(te->desc, "PROCEDURE") != 0) |
| 4070 |
{ |
| 4071 |
const char *p = te->defn; |
| 4072 |
int nsemis = 0; |
| 4073 |
|
| 4074 |
while ((p = strchr(p, ';')) != NULL) |
| 4075 |
{ |
| 4076 |
nsemis++; |
| 4077 |
p++; |
| 4078 |
} |
| 4079 |
if (nsemis > 1) |
| 4080 |
AH->txnCount += nsemis - 1; |
| 4081 |
} |
| 4082 |
} |
| 4083 |
|
| 4084 |
/* |
| 4085 |
* If we aren't using SET SESSION AUTH to determine ownership, we must |
| 4086 |
* instead issue an ALTER OWNER command. Schema "public" is special; when |
| 4087 |
* a dump emits a comment in lieu of creating it, we use ALTER OWNER even |
| 4088 |
* when using SET SESSION for all other objects. We assume that anything |
| 4089 |
* without a DROP command is not a separately ownable object. |
| 4090 |
*/ |
| 4091 |
if (!ropt->noOwner && |
| 4092 |
(!ropt->use_setsessauth || |
| 4093 |
(strcmp(te->desc, "SCHEMA") == 0 && |
| 4094 |
strncmp(te->defn, "--", 2) == 0)) && |
| 4095 |
te->owner && strlen(te->owner) > 0 && |
| 4096 |
te->dropStmt && strlen(te->dropStmt) > 0) |
| 4097 |
{ |
| 4098 |
if (strcmp(te->desc, "BLOB METADATA") == 0) |
| 4099 |
{ |
| 4100 |
/* BLOB METADATA needs special code to handle multiple LOs */ |
| 4101 |
char *cmdEnd = psprintf(" OWNER TO %s", fmtId(te->owner)); |
| 4102 |
|
| 4103 |
IssueCommandPerBlob(AH, te, "ALTER LARGE OBJECT ", cmdEnd); |
| 4104 |
pg_free(cmdEnd); |
| 4105 |
} |
| 4106 |
else |
| 4107 |
{ |
| 4108 |
/* For all other cases, we can use _getObjectDescription */ |
| 4109 |
PQExpBufferData temp; |
| 4110 |
|
| 4111 |
initPQExpBuffer(&temp); |
| 4112 |
_getObjectDescription(&temp, te); |
| 4113 |
|
| 4114 |
/* |
| 4115 |
* If _getObjectDescription() didn't fill the buffer, then there |
| 4116 |
* is no owner. |
| 4117 |
*/ |
| 4118 |
if (temp.data[0]) |
| 4119 |
ahprintf(AH, "ALTER %s OWNER TO %s;\n\n", |
| 4120 |
temp.data, fmtId(te->owner)); |
| 4121 |
termPQExpBuffer(&temp); |
| 4122 |
} |
| 4123 |
} |
| 4124 |
|
| 4125 |
/* |
| 4126 |
* Select a partitioned table's default AM, once the table definition has |
| 4127 |
* been generated. |
| 4128 |
*/ |
| 4129 |
if (te->relkind == RELKIND_PARTITIONED_TABLE) |
| 4130 |
_printTableAccessMethodNoStorage(AH, te); |
| 4131 |
|
| 4132 |
/* |
| 4133 |
* If it's an ACL entry, it might contain SET SESSION AUTHORIZATION |
| 4134 |
* commands, so we can no longer assume we know the current auth setting. |
| 4135 |
*/ |
| 4136 |
if (_tocEntryIsACL(te)) |
| 4137 |
{ |
| 4138 |
free(AH->currUser); |
| 4139 |
AH->currUser = NULL; |
| 4140 |
} |
| 4141 |
} |
| 4142 |
|
| 4143 |
/* |
| 4144 |
* Write the file header for a custom-format archive |
| 4145 |
*/ |
| 4146 |
void |
| 4147 |
WriteHead(ArchiveHandle *AH) |
| 4148 |
{ |
| 4149 |
struct tm crtm; |
| 4150 |
|
| 4151 |
AH->WriteBufPtr(AH, "PGDMP", 5); /* Magic code */ |
| 4152 |
AH->WriteBytePtr(AH, ARCHIVE_MAJOR(AH->version)); |
| 4153 |
AH->WriteBytePtr(AH, ARCHIVE_MINOR(AH->version)); |
| 4154 |
AH->WriteBytePtr(AH, ARCHIVE_REV(AH->version)); |
| 4155 |
AH->WriteBytePtr(AH, AH->intSize); |
| 4156 |
AH->WriteBytePtr(AH, AH->offSize); |
| 4157 |
AH->WriteBytePtr(AH, AH->format); |
| 4158 |
AH->WriteBytePtr(AH, AH->compression_spec.algorithm); |
| 4159 |
crtm = *localtime(&AH->createDate); |
| 4160 |
WriteInt(AH, crtm.tm_sec); |
| 4161 |
WriteInt(AH, crtm.tm_min); |
| 4162 |
WriteInt(AH, crtm.tm_hour); |
| 4163 |
WriteInt(AH, crtm.tm_mday); |
| 4164 |
WriteInt(AH, crtm.tm_mon); |
| 4165 |
WriteInt(AH, crtm.tm_year); |
| 4166 |
WriteInt(AH, crtm.tm_isdst); |
| 4167 |
WriteStr(AH, PQdb(AH->connection)); |
| 4168 |
WriteStr(AH, AH->public.remoteVersionStr); |
| 4169 |
WriteStr(AH, PG_VERSION); |
| 4170 |
} |
| 4171 |
|
| 4172 |
void |
| 4173 |
ReadHead(ArchiveHandle *AH) |
| 4174 |
{ |
| 4175 |
char *errmsg; |
| 4176 |
char vmaj, |
| 4177 |
vmin, |
| 4178 |
vrev; |
| 4179 |
int fmt; |
| 4180 |
|
| 4181 |
/* |
| 4182 |
* If we haven't already read the header, do so. |
| 4183 |
* |
| 4184 |
* NB: this code must agree with _discoverArchiveFormat(). Maybe find a |
| 4185 |
* way to unify the cases? |
| 4186 |
*/ |
| 4187 |
if (!AH->readHeader) |
| 4188 |
{ |
| 4189 |
char tmpMag[7]; |
| 4190 |
|
| 4191 |
AH->ReadBufPtr(AH, tmpMag, 5); |
| 4192 |
|
| 4193 |
if (strncmp(tmpMag, "PGDMP", 5) != 0) |
| 4194 |
pg_fatal("did not find magic string in file header"); |
| 4195 |
} |
| 4196 |
|
| 4197 |
vmaj = AH->ReadBytePtr(AH); |
| 4198 |
vmin = AH->ReadBytePtr(AH); |
| 4199 |
|
| 4200 |
if (vmaj > 1 || (vmaj == 1 && vmin > 0)) /* Version > 1.0 */ |
| 4201 |
vrev = AH->ReadBytePtr(AH); |
| 4202 |
else |
| 4203 |
vrev = 0; |
| 4204 |
|
| 4205 |
AH->version = MAKE_ARCHIVE_VERSION(vmaj, vmin, vrev); |
| 4206 |
|
| 4207 |
if (AH->version < K_VERS_1_0 || AH->version > K_VERS_MAX) |
| 4208 |
pg_fatal("unsupported version (%d.%d) in file header", |
| 4209 |
vmaj, vmin); |
| 4210 |
|
| 4211 |
AH->intSize = AH->ReadBytePtr(AH); |
| 4212 |
if (AH->intSize > 32) |
| 4213 |
pg_fatal("sanity check on integer size (%lu) failed", |
| 4214 |
(unsigned long) AH->intSize); |
| 4215 |
|
| 4216 |
if (AH->intSize > sizeof(int)) |
| 4217 |
pg_log_warning("archive was made on a machine with larger integers, some operations might fail"); |
| 4218 |
|
| 4219 |
if (AH->version >= K_VERS_1_7) |
| 4220 |
AH->offSize = AH->ReadBytePtr(AH); |
| 4221 |
else |
| 4222 |
AH->offSize = AH->intSize; |
| 4223 |
|
| 4224 |
fmt = AH->ReadBytePtr(AH); |
| 4225 |
|
| 4226 |
if (AH->format != fmt) |
| 4227 |
pg_fatal("expected format (%d) differs from format found in file (%d)", |
| 4228 |
AH->format, fmt); |
| 4229 |
|
| 4230 |
if (AH->version >= K_VERS_1_15) |
| 4231 |
AH->compression_spec.algorithm = AH->ReadBytePtr(AH); |
| 4232 |
else if (AH->version >= K_VERS_1_2) |
| 4233 |
{ |
| 4234 |
/* Guess the compression method based on the level */ |
| 4235 |
if (AH->version < K_VERS_1_4) |
| 4236 |
AH->compression_spec.level = AH->ReadBytePtr(AH); |
| 4237 |
else |
| 4238 |
AH->compression_spec.level = ReadInt(AH); |
| 4239 |
|
| 4240 |
if (AH->compression_spec.level != 0) |
| 4241 |
AH->compression_spec.algorithm = PG_COMPRESSION_GZIP; |
| 4242 |
} |
| 4243 |
else |
| 4244 |
AH->compression_spec.algorithm = PG_COMPRESSION_GZIP; |
| 4245 |
|
| 4246 |
errmsg = supports_compression(AH->compression_spec); |
| 4247 |
if (errmsg) |
| 4248 |
{ |
| 4249 |
pg_log_warning("archive is compressed, but this installation does not support compression (%s) -- no data will be available", |
| 4250 |
errmsg); |
| 4251 |
pg_free(errmsg); |
| 4252 |
} |
| 4253 |
|
| 4254 |
if (AH->version >= K_VERS_1_4) |
| 4255 |
{ |
| 4256 |
struct tm crtm; |
| 4257 |
|
| 4258 |
crtm.tm_sec = ReadInt(AH); |
| 4259 |
crtm.tm_min = ReadInt(AH); |
| 4260 |
crtm.tm_hour = ReadInt(AH); |
| 4261 |
crtm.tm_mday = ReadInt(AH); |
| 4262 |
crtm.tm_mon = ReadInt(AH); |
| 4263 |
crtm.tm_year = ReadInt(AH); |
| 4264 |
crtm.tm_isdst = ReadInt(AH); |
| 4265 |
|
| 4266 |
/* |
| 4267 |
* Newer versions of glibc have mktime() report failure if tm_isdst is |
| 4268 |
* inconsistent with the prevailing timezone, e.g. tm_isdst = 1 when |
| 4269 |
* TZ=UTC. This is problematic when restoring an archive under a |
| 4270 |
* different timezone setting. If we get a failure, try again with |
| 4271 |
* tm_isdst set to -1 ("don't know"). |
| 4272 |
* |
| 4273 |
* XXX with or without this hack, we reconstruct createDate |
| 4274 |
* incorrectly when the prevailing timezone is different from |
| 4275 |
* pg_dump's. Next time we bump the archive version, we should flush |
| 4276 |
* this representation and store a plain seconds-since-the-Epoch |
| 4277 |
* timestamp instead. |
| 4278 |
*/ |
| 4279 |
AH->createDate = mktime(&crtm); |
| 4280 |
if (AH->createDate == (time_t) -1) |
| 4281 |
{ |
| 4282 |
crtm.tm_isdst = -1; |
| 4283 |
AH->createDate = mktime(&crtm); |
| 4284 |
if (AH->createDate == (time_t) -1) |
| 4285 |
pg_log_warning("invalid creation date in header"); |
| 4286 |
} |
| 4287 |
} |
| 4288 |
|
| 4289 |
if (AH->version >= K_VERS_1_4) |
| 4290 |
{ |
| 4291 |
AH->archdbname = ReadStr(AH); |
| 4292 |
} |
| 4293 |
|
| 4294 |
if (AH->version >= K_VERS_1_10) |
| 4295 |
{ |
| 4296 |
AH->archiveRemoteVersion = ReadStr(AH); |
| 4297 |
AH->archiveDumpVersion = ReadStr(AH); |
| 4298 |
} |
| 4299 |
} |
| 4300 |
|
| 4301 |
|
| 4302 |
/* |
| 4303 |
* checkSeek |
| 4304 |
* check to see if ftell/fseek can be performed. |
| 4305 |
*/ |
| 4306 |
bool |
| 4307 |
checkSeek(FILE *fp) |
| 4308 |
{ |
| 4309 |
pgoff_t tpos; |
| 4310 |
|
| 4311 |
/* Check that ftello works on this file */ |
| 4312 |
tpos = ftello(fp); |
| 4313 |
if (tpos < 0) |
| 4314 |
return false; |
| 4315 |
|
| 4316 |
/* |
| 4317 |
* Check that fseeko(SEEK_SET) works, too. NB: we used to try to test |
| 4318 |
* this with fseeko(fp, 0, SEEK_CUR). But some platforms treat that as a |
| 4319 |
* successful no-op even on files that are otherwise unseekable. |
| 4320 |
*/ |
| 4321 |
if (fseeko(fp, tpos, SEEK_SET) != 0) |
| 4322 |
return false; |
| 4323 |
|
| 4324 |
return true; |
| 4325 |
} |
| 4326 |
|
| 4327 |
|
| 4328 |
/* |
| 4329 |
* dumpTimestamp |
| 4330 |
*/ |
| 4331 |
static void |
| 4332 |
dumpTimestamp(ArchiveHandle *AH, const char *msg, time_t tim) |
| 4333 |
{ |
| 4334 |
char buf[64]; |
| 4335 |
|
| 4336 |
if (strftime(buf, sizeof(buf), PGDUMP_STRFTIME_FMT, localtime(&tim)) != 0) |
| 4337 |
ahprintf(AH, "-- %s %s\n\n", msg, buf); |
| 4338 |
} |
| 4339 |
|
| 4340 |
/* |
| 4341 |
* Main engine for parallel restore. |
| 4342 |
* |
| 4343 |
* Parallel restore is done in three phases. In this first phase, |
| 4344 |
* we'll process all SECTION_PRE_DATA TOC entries that are allowed to be |
| 4345 |
* processed in the RESTORE_PASS_MAIN pass. (In practice, that's all |
| 4346 |
* PRE_DATA items other than ACLs.) Entries we can't process now are |
| 4347 |
* added to the pending_list for later phases to deal with. |
| 4348 |
*/ |
| 4349 |
static void |
| 4350 |
restore_toc_entries_prefork(ArchiveHandle *AH, TocEntry *pending_list) |
| 4351 |
{ |
| 4352 |
bool skipped_some; |
| 4353 |
TocEntry *next_work_item; |
| 4354 |
|
| 4355 |
pg_log_debug("entering restore_toc_entries_prefork"); |
| 4356 |
|
| 4357 |
/* Adjust dependency information */ |
| 4358 |
fix_dependencies(AH); |
| 4359 |
|
| 4360 |
/* |
| 4361 |
* Do all the early stuff in a single connection in the parent. There's no |
| 4362 |
* great point in running it in parallel, in fact it will actually run |
| 4363 |
* faster in a single connection because we avoid all the connection and |
| 4364 |
* setup overhead. Also, pre-9.2 pg_dump versions were not very good |
| 4365 |
* about showing all the dependencies of SECTION_PRE_DATA items, so we do |
| 4366 |
* not risk trying to process them out-of-order. |
| 4367 |
* |
| 4368 |
* Stuff that we can't do immediately gets added to the pending_list. |
| 4369 |
* Note: we don't yet filter out entries that aren't going to be restored. |
| 4370 |
* They might participate in dependency chains connecting entries that |
| 4371 |
* should be restored, so we treat them as live until we actually process |
| 4372 |
* them. |
| 4373 |
* |
| 4374 |
* Note: as of 9.2, it should be guaranteed that all PRE_DATA items appear |
| 4375 |
* before DATA items, and all DATA items before POST_DATA items. That is |
| 4376 |
* not certain to be true in older archives, though, and in any case use |
| 4377 |
* of a list file would destroy that ordering (cf. SortTocFromFile). So |
| 4378 |
* this loop cannot assume that it holds. |
| 4379 |
*/ |
| 4380 |
AH->restorePass = RESTORE_PASS_MAIN; |
| 4381 |
skipped_some = false; |
| 4382 |
for (next_work_item = AH->toc->next; next_work_item != AH->toc; next_work_item = next_work_item->next) |
| 4383 |
{ |
| 4384 |
bool do_now = true; |
| 4385 |
|
| 4386 |
if (next_work_item->section != SECTION_PRE_DATA) |
| 4387 |
{ |
| 4388 |
/* DATA and POST_DATA items are just ignored for now */ |
| 4389 |
if (next_work_item->section == SECTION_DATA || |
| 4390 |
next_work_item->section == SECTION_POST_DATA) |
| 4391 |
{ |
| 4392 |
do_now = false; |
| 4393 |
skipped_some = true; |
| 4394 |
} |
| 4395 |
else |
| 4396 |
{ |
| 4397 |
/* |
| 4398 |
* SECTION_NONE items, such as comments, can be processed now |
| 4399 |
* if we are still in the PRE_DATA part of the archive. Once |
| 4400 |
* we've skipped any items, we have to consider whether the |
| 4401 |
* comment's dependencies are satisfied, so skip it for now. |
| 4402 |
*/ |
| 4403 |
if (skipped_some) |
| 4404 |
do_now = false; |
| 4405 |
} |
| 4406 |
} |
| 4407 |
|
| 4408 |
/* |
| 4409 |
* Also skip items that need to be forced into later passes. We need |
| 4410 |
* not set skipped_some in this case, since by assumption no main-pass |
| 4411 |
* items could depend on these. |
| 4412 |
*/ |
| 4413 |
if (_tocEntryRestorePass(next_work_item) != RESTORE_PASS_MAIN) |
| 4414 |
do_now = false; |
| 4415 |
|
| 4416 |
if (do_now) |
| 4417 |
{ |
| 4418 |
/* OK, restore the item and update its dependencies */ |
| 4419 |
pg_log_info("processing item %d %s %s", |
| 4420 |
next_work_item->dumpId, |
| 4421 |
next_work_item->desc, next_work_item->tag); |
| 4422 |
|
| 4423 |
(void) restore_toc_entry(AH, next_work_item, false); |
| 4424 |
|
| 4425 |
/* Reduce dependencies, but don't move anything to ready_heap */ |
| 4426 |
reduce_dependencies(AH, next_work_item, NULL); |
| 4427 |
} |
| 4428 |
else |
| 4429 |
{ |
| 4430 |
/* Nope, so add it to pending_list */ |
| 4431 |
pending_list_append(pending_list, next_work_item); |
| 4432 |
} |
| 4433 |
} |
| 4434 |
|
| 4435 |
/* |
| 4436 |
* In --transaction-size mode, we must commit the open transaction before |
| 4437 |
* dropping the database connection. This also ensures that child workers |
| 4438 |
* can see the objects we've created so far. |
| 4439 |
*/ |
| 4440 |
if (AH->public.ropt->txn_size > 0) |
| 4441 |
CommitTransaction(&AH->public); |
| 4442 |
|
| 4443 |
/* |
| 4444 |
* Now close parent connection in prep for parallel steps. We do this |
| 4445 |
* mainly to ensure that we don't exceed the specified number of parallel |
| 4446 |
* connections. |
| 4447 |
*/ |
| 4448 |
DisconnectDatabase(&AH->public); |
| 4449 |
|
| 4450 |
/* blow away any transient state from the old connection */ |
| 4451 |
free(AH->currUser); |
| 4452 |
AH->currUser = NULL; |
| 4453 |
free(AH->currSchema); |
| 4454 |
AH->currSchema = NULL; |
| 4455 |
free(AH->currTablespace); |
| 4456 |
AH->currTablespace = NULL; |
| 4457 |
free(AH->currTableAm); |
| 4458 |
AH->currTableAm = NULL; |
| 4459 |
} |
| 4460 |
|
| 4461 |
/* |
| 4462 |
* Main engine for parallel restore. |
| 4463 |
* |
| 4464 |
* Parallel restore is done in three phases. In this second phase, |
| 4465 |
* we process entries by dispatching them to parallel worker children |
| 4466 |
* (processes on Unix, threads on Windows), each of which connects |
| 4467 |
* separately to the database. Inter-entry dependencies are respected, |
| 4468 |
* and so is the RestorePass multi-pass structure. When we can no longer |
| 4469 |
* make any entries ready to process, we exit. Normally, there will be |
| 4470 |
* nothing left to do; but if there is, the third phase will mop up. |
| 4471 |
*/ |
| 4472 |
static void |
| 4473 |
restore_toc_entries_parallel(ArchiveHandle *AH, ParallelState *pstate, |
| 4474 |
TocEntry *pending_list) |
| 4475 |
{ |
| 4476 |
binaryheap *ready_heap; |
| 4477 |
TocEntry *next_work_item; |
| 4478 |
|
| 4479 |
pg_log_debug("entering restore_toc_entries_parallel"); |
| 4480 |
|
| 4481 |
/* Set up ready_heap with enough room for all known TocEntrys */ |
| 4482 |
ready_heap = binaryheap_allocate(AH->tocCount, |
| 4483 |
TocEntrySizeCompareBinaryheap, |
| 4484 |
NULL); |
| 4485 |
|
| 4486 |
/* |
| 4487 |
* The pending_list contains all items that we need to restore. Move all |
| 4488 |
* items that are available to process immediately into the ready_heap. |
| 4489 |
* After this setup, the pending list is everything that needs to be done |
| 4490 |
* but is blocked by one or more dependencies, while the ready heap |
| 4491 |
* contains items that have no remaining dependencies and are OK to |
| 4492 |
* process in the current restore pass. |
| 4493 |
*/ |
| 4494 |
AH->restorePass = RESTORE_PASS_MAIN; |
| 4495 |
move_to_ready_heap(pending_list, ready_heap, AH->restorePass); |
| 4496 |
|
| 4497 |
/* |
| 4498 |
* main parent loop |
| 4499 |
* |
| 4500 |
* Keep going until there is no worker still running AND there is no work |
| 4501 |
* left to be done. Note invariant: at top of loop, there should always |
| 4502 |
* be at least one worker available to dispatch a job to. |
| 4503 |
*/ |
| 4504 |
pg_log_info("entering main parallel loop"); |
| 4505 |
|
| 4506 |
for (;;) |
| 4507 |
{ |
| 4508 |
/* Look for an item ready to be dispatched to a worker */ |
| 4509 |
next_work_item = pop_next_work_item(ready_heap, pstate); |
| 4510 |
if (next_work_item != NULL) |
| 4511 |
{ |
| 4512 |
/* If not to be restored, don't waste time launching a worker */ |
| 4513 |
if ((next_work_item->reqs & (REQ_SCHEMA | REQ_DATA | REQ_STATS)) == 0) |
| 4514 |
{ |
| 4515 |
pg_log_info("skipping item %d %s %s", |
| 4516 |
next_work_item->dumpId, |
| 4517 |
next_work_item->desc, next_work_item->tag); |
| 4518 |
/* Update its dependencies as though we'd completed it */ |
| 4519 |
reduce_dependencies(AH, next_work_item, ready_heap); |
| 4520 |
/* Loop around to see if anything else can be dispatched */ |
| 4521 |
continue; |
| 4522 |
} |
| 4523 |
|
| 4524 |
pg_log_info("launching item %d %s %s", |
| 4525 |
next_work_item->dumpId, |
| 4526 |
next_work_item->desc, next_work_item->tag); |
| 4527 |
|
| 4528 |
/* Dispatch to some worker */ |
| 4529 |
DispatchJobForTocEntry(AH, pstate, next_work_item, ACT_RESTORE, |
| 4530 |
mark_restore_job_done, ready_heap); |
| 4531 |
} |
| 4532 |
else if (IsEveryWorkerIdle(pstate)) |
| 4533 |
{ |
| 4534 |
/* |
| 4535 |
* Nothing is ready and no worker is running, so we're done with |
| 4536 |
* the current pass or maybe with the whole process. |
| 4537 |
*/ |
| 4538 |
if (AH->restorePass == RESTORE_PASS_LAST) |
| 4539 |
break; /* No more parallel processing is possible */ |
| 4540 |
|
| 4541 |
/* Advance to next restore pass */ |
| 4542 |
AH->restorePass++; |
| 4543 |
/* That probably allows some stuff to be made ready */ |
| 4544 |
move_to_ready_heap(pending_list, ready_heap, AH->restorePass); |
| 4545 |
/* Loop around to see if anything's now ready */ |
| 4546 |
continue; |
| 4547 |
} |
| 4548 |
else |
| 4549 |
{ |
| 4550 |
/* |
| 4551 |
* We have nothing ready, but at least one child is working, so |
| 4552 |
* wait for some subjob to finish. |
| 4553 |
*/ |
| 4554 |
} |
| 4555 |
|
| 4556 |
/* |
| 4557 |
* Before dispatching another job, check to see if anything has |
| 4558 |
* finished. We should check every time through the loop so as to |
| 4559 |
* reduce dependencies as soon as possible. If we were unable to |
| 4560 |
* dispatch any job this time through, wait until some worker finishes |
| 4561 |
* (and, hopefully, unblocks some pending item). If we did dispatch |
| 4562 |
* something, continue as soon as there's at least one idle worker. |
| 4563 |
* Note that in either case, there's guaranteed to be at least one |
| 4564 |
* idle worker when we return to the top of the loop. This ensures we |
| 4565 |
* won't block inside DispatchJobForTocEntry, which would be |
| 4566 |
* undesirable: we'd rather postpone dispatching until we see what's |
| 4567 |
* been unblocked by finished jobs. |
| 4568 |
*/ |
| 4569 |
WaitForWorkers(AH, pstate, |
| 4570 |
next_work_item ? WFW_ONE_IDLE : WFW_GOT_STATUS); |
| 4571 |
} |
| 4572 |
|
| 4573 |
/* There should now be nothing in ready_heap. */ |
| 4574 |
Assert(binaryheap_empty(ready_heap)); |
| 4575 |
|
| 4576 |
binaryheap_free(ready_heap); |
| 4577 |
|
| 4578 |
pg_log_info("finished main parallel loop"); |
| 4579 |
} |
| 4580 |
|
| 4581 |
/* |
| 4582 |
* Main engine for parallel restore. |
| 4583 |
* |
| 4584 |
* Parallel restore is done in three phases. In this third phase, |
| 4585 |
* we mop up any remaining TOC entries by processing them serially. |
| 4586 |
* This phase normally should have nothing to do, but if we've somehow |
| 4587 |
* gotten stuck due to circular dependencies or some such, this provides |
| 4588 |
* at least some chance of completing the restore successfully. |
| 4589 |
*/ |
| 4590 |
static void |
| 4591 |
restore_toc_entries_postfork(ArchiveHandle *AH, TocEntry *pending_list) |
| 4592 |
{ |
| 4593 |
RestoreOptions *ropt = AH->public.ropt; |
| 4594 |
TocEntry *te; |
| 4595 |
|
| 4596 |
pg_log_debug("entering restore_toc_entries_postfork"); |
| 4597 |
|
| 4598 |
/* |
| 4599 |
* Now reconnect the single parent connection. |
| 4600 |
*/ |
| 4601 |
ConnectDatabaseAhx((Archive *) AH, &ropt->cparams, true); |
| 4602 |
|
| 4603 |
/* re-establish fixed state */ |
| 4604 |
_doSetFixedOutputState(AH); |
| 4605 |
|
| 4606 |
/* |
| 4607 |
* Make sure there is no work left due to, say, circular dependencies, or |
| 4608 |
* some other pathological condition. If so, do it in the single parent |
| 4609 |
* connection. We don't sweat about RestorePass ordering; it's likely we |
| 4610 |
* already violated that. |
| 4611 |
*/ |
| 4612 |
for (te = pending_list->pending_next; te != pending_list; te = te->pending_next) |
| 4613 |
{ |
| 4614 |
pg_log_info("processing missed item %d %s %s", |
| 4615 |
te->dumpId, te->desc, te->tag); |
| 4616 |
(void) restore_toc_entry(AH, te, false); |
| 4617 |
} |
| 4618 |
} |
| 4619 |
|
| 4620 |
/* |
| 4621 |
* Check if te1 has an exclusive lock requirement for an item that te2 also |
| 4622 |
* requires, whether or not te2's requirement is for an exclusive lock. |
| 4623 |
*/ |
| 4624 |
static bool |
| 4625 |
has_lock_conflicts(TocEntry *te1, TocEntry *te2) |
| 4626 |
{ |
| 4627 |
int j, |
| 4628 |
k; |
| 4629 |
|
| 4630 |
for (j = 0; j < te1->nLockDeps; j++) |
| 4631 |
{ |
| 4632 |
for (k = 0; k < te2->nDeps; k++) |
| 4633 |
{ |
| 4634 |
if (te1->lockDeps[j] == te2->dependencies[k]) |
| 4635 |
return true; |
| 4636 |
} |
| 4637 |
} |
| 4638 |
return false; |
| 4639 |
} |
| 4640 |
|
| 4641 |
|
| 4642 |
/* |
| 4643 |
* Initialize the header of the pending-items list. |
| 4644 |
* |
| 4645 |
* This is a circular list with a dummy TocEntry as header, just like the |
| 4646 |
* main TOC list; but we use separate list links so that an entry can be in |
| 4647 |
* the main TOC list as well as in the pending list. |
| 4648 |
*/ |
| 4649 |
static void |
| 4650 |
pending_list_header_init(TocEntry *l) |
| 4651 |
{ |
| 4652 |
l->pending_prev = l->pending_next = l; |
| 4653 |
} |
| 4654 |
|
| 4655 |
/* Append te to the end of the pending-list headed by l */ |
| 4656 |
static void |
| 4657 |
pending_list_append(TocEntry *l, TocEntry *te) |
| 4658 |
{ |
| 4659 |
te->pending_prev = l->pending_prev; |
| 4660 |
l->pending_prev->pending_next = te; |
| 4661 |
l->pending_prev = te; |
| 4662 |
te->pending_next = l; |
| 4663 |
} |
| 4664 |
|
| 4665 |
/* Remove te from the pending-list */ |
| 4666 |
static void |
| 4667 |
pending_list_remove(TocEntry *te) |
| 4668 |
{ |
| 4669 |
te->pending_prev->pending_next = te->pending_next; |
| 4670 |
te->pending_next->pending_prev = te->pending_prev; |
| 4671 |
te->pending_prev = NULL; |
| 4672 |
te->pending_next = NULL; |
| 4673 |
} |
| 4674 |
|
| 4675 |
|
| 4676 |
/* qsort comparator for sorting TocEntries by dataLength */ |
| 4677 |
static int |
| 4678 |
TocEntrySizeCompareQsort(const void *p1, const void *p2) |
| 4679 |
{ |
| 4680 |
const TocEntry *te1 = *(const TocEntry *const *) p1; |
| 4681 |
const TocEntry *te2 = *(const TocEntry *const *) p2; |
| 4682 |
|
| 4683 |
/* Sort by decreasing dataLength */ |
| 4684 |
if (te1->dataLength > te2->dataLength) |
| 4685 |
return -1; |
| 4686 |
if (te1->dataLength < te2->dataLength) |
| 4687 |
return 1; |
| 4688 |
|
| 4689 |
/* For equal dataLengths, sort by dumpId, just to be stable */ |
| 4690 |
if (te1->dumpId < te2->dumpId) |
| 4691 |
return -1; |
| 4692 |
if (te1->dumpId > te2->dumpId) |
| 4693 |
return 1; |
| 4694 |
|
| 4695 |
return 0; |
| 4696 |
} |
| 4697 |
|
| 4698 |
/* binaryheap comparator for sorting TocEntries by dataLength */ |
| 4699 |
static int |
| 4700 |
TocEntrySizeCompareBinaryheap(void *p1, void *p2, void *arg) |
| 4701 |
{ |
| 4702 |
/* return opposite of qsort comparator for max-heap */ |
| 4703 |
return -TocEntrySizeCompareQsort(&p1, &p2); |
| 4704 |
} |
| 4705 |
|
| 4706 |
|
| 4707 |
/* |
| 4708 |
* Move all immediately-ready items from pending_list to ready_heap. |
| 4709 |
* |
| 4710 |
* Items are considered ready if they have no remaining dependencies and |
| 4711 |
* they belong in the current restore pass. (See also reduce_dependencies, |
| 4712 |
* which applies the same logic one-at-a-time.) |
| 4713 |
*/ |
| 4714 |
static void |
| 4715 |
move_to_ready_heap(TocEntry *pending_list, |
| 4716 |
binaryheap *ready_heap, |
| 4717 |
RestorePass pass) |
| 4718 |
{ |
| 4719 |
TocEntry *te; |
| 4720 |
TocEntry *next_te; |
| 4721 |
|
| 4722 |
for (te = pending_list->pending_next; te != pending_list; te = next_te) |
| 4723 |
{ |
| 4724 |
/* must save list link before possibly removing te from list */ |
| 4725 |
next_te = te->pending_next; |
| 4726 |
|
| 4727 |
if (te->depCount == 0 && |
| 4728 |
_tocEntryRestorePass(te) == pass) |
| 4729 |
{ |
| 4730 |
/* Remove it from pending_list ... */ |
| 4731 |
pending_list_remove(te); |
| 4732 |
/* ... and add to ready_heap */ |
| 4733 |
binaryheap_add(ready_heap, te); |
| 4734 |
} |
| 4735 |
} |
| 4736 |
} |
| 4737 |
|
| 4738 |
/* |
| 4739 |
* Find the next work item (if any) that is capable of being run now, |
| 4740 |
* and remove it from the ready_heap. |
| 4741 |
* |
| 4742 |
* Returns the item, or NULL if nothing is runnable. |
| 4743 |
* |
| 4744 |
* To qualify, the item must have no remaining dependencies |
| 4745 |
* and no requirements for locks that are incompatible with |
| 4746 |
* items currently running. Items in the ready_heap are known to have |
| 4747 |
* no remaining dependencies, but we have to check for lock conflicts. |
| 4748 |
*/ |
| 4749 |
static TocEntry * |
| 4750 |
pop_next_work_item(binaryheap *ready_heap, |
| 4751 |
ParallelState *pstate) |
| 4752 |
{ |
| 4753 |
/* |
| 4754 |
* Search the ready_heap until we find a suitable item. Note that we do a |
| 4755 |
* sequential scan through the heap nodes, so even though we will first |
| 4756 |
* try to choose the highest-priority item, we might end up picking |
| 4757 |
* something with a much lower priority. However, we expect that we will |
| 4758 |
* typically be able to pick one of the first few items, which should |
| 4759 |
* usually have a relatively high priority. |
| 4760 |
*/ |
| 4761 |
for (int i = 0; i < binaryheap_size(ready_heap); i++) |
| 4762 |
{ |
| 4763 |
TocEntry *te = (TocEntry *) binaryheap_get_node(ready_heap, i); |
| 4764 |
bool conflicts = false; |
| 4765 |
|
| 4766 |
/* |
| 4767 |
* Check to see if the item would need exclusive lock on something |
| 4768 |
* that a currently running item also needs lock on, or vice versa. If |
| 4769 |
* so, we don't want to schedule them together. |
| 4770 |
*/ |
| 4771 |
for (int k = 0; k < pstate->numWorkers; k++) |
| 4772 |
{ |
| 4773 |
TocEntry *running_te = pstate->te[k]; |
| 4774 |
|
| 4775 |
if (running_te == NULL) |
| 4776 |
continue; |
| 4777 |
if (has_lock_conflicts(te, running_te) || |
| 4778 |
has_lock_conflicts(running_te, te)) |
| 4779 |
{ |
| 4780 |
conflicts = true; |
| 4781 |
break; |
| 4782 |
} |
| 4783 |
} |
| 4784 |
|
| 4785 |
if (conflicts) |
| 4786 |
continue; |
| 4787 |
|
| 4788 |
/* passed all tests, so this item can run */ |
| 4789 |
binaryheap_remove_node(ready_heap, i); |
| 4790 |
return te; |
| 4791 |
} |
| 4792 |
|
| 4793 |
pg_log_debug("no item ready"); |
| 4794 |
return NULL; |
| 4795 |
} |
| 4796 |
|
| 4797 |
|
| 4798 |
/* |
| 4799 |
* Restore a single TOC item in parallel with others |
| 4800 |
* |
| 4801 |
* this is run in the worker, i.e. in a thread (Windows) or a separate process |
| 4802 |
* (everything else). A worker process executes several such work items during |
| 4803 |
* a parallel backup or restore. Once we terminate here and report back that |
| 4804 |
* our work is finished, the leader process will assign us a new work item. |
| 4805 |
*/ |
| 4806 |
int |
| 4807 |
parallel_restore(ArchiveHandle *AH, TocEntry *te) |
| 4808 |
{ |
| 4809 |
int status; |
| 4810 |
|
| 4811 |
Assert(AH->connection != NULL); |
| 4812 |
|
| 4813 |
/* Count only errors associated with this TOC entry */ |
| 4814 |
AH->public.n_errors = 0; |
| 4815 |
|
| 4816 |
/* Restore the TOC item */ |
| 4817 |
status = restore_toc_entry(AH, te, true); |
| 4818 |
|
| 4819 |
return status; |
| 4820 |
} |
| 4821 |
|
| 4822 |
|
| 4823 |
/* |
| 4824 |
* Callback function that's invoked in the leader process after a step has |
| 4825 |
* been parallel restored. |
| 4826 |
* |
| 4827 |
* Update status and reduce the dependency count of any dependent items. |
| 4828 |
*/ |
| 4829 |
static void |
| 4830 |
mark_restore_job_done(ArchiveHandle *AH, |
| 4831 |
TocEntry *te, |
| 4832 |
int status, |
| 4833 |
void *callback_data) |
| 4834 |
{ |
| 4835 |
binaryheap *ready_heap = (binaryheap *) callback_data; |
| 4836 |
|
| 4837 |
pg_log_info("finished item %d %s %s", |
| 4838 |
te->dumpId, te->desc, te->tag); |
| 4839 |
|
| 4840 |
if (status == WORKER_CREATE_DONE) |
| 4841 |
mark_create_done(AH, te); |
| 4842 |
else if (status == WORKER_INHIBIT_DATA) |
| 4843 |
{ |
| 4844 |
inhibit_data_for_failed_table(AH, te); |
| 4845 |
AH->public.n_errors++; |
| 4846 |
} |
| 4847 |
else if (status == WORKER_IGNORED_ERRORS) |
| 4848 |
AH->public.n_errors++; |
| 4849 |
else if (status != 0) |
| 4850 |
pg_fatal("worker process failed: exit code %d", |
| 4851 |
status); |
| 4852 |
|
| 4853 |
reduce_dependencies(AH, te, ready_heap); |
| 4854 |
} |
| 4855 |
|
| 4856 |
|
| 4857 |
/* |
| 4858 |
* Process the dependency information into a form useful for parallel restore. |
| 4859 |
* |
| 4860 |
* This function takes care of fixing up some missing or badly designed |
| 4861 |
* dependencies, and then prepares subsidiary data structures that will be |
| 4862 |
* used in the main parallel-restore logic, including: |
| 4863 |
* 1. We build the revDeps[] arrays of incoming dependency dumpIds. |
| 4864 |
* 2. We set up depCount fields that are the number of as-yet-unprocessed |
| 4865 |
* dependencies for each TOC entry. |
| 4866 |
* |
| 4867 |
* We also identify locking dependencies so that we can avoid trying to |
| 4868 |
* schedule conflicting items at the same time. |
| 4869 |
*/ |
| 4870 |
static void |
| 4871 |
fix_dependencies(ArchiveHandle *AH) |
| 4872 |
{ |
| 4873 |
TocEntry *te; |
| 4874 |
int i; |
| 4875 |
|
| 4876 |
/* |
| 4877 |
* Initialize the depCount/revDeps/nRevDeps fields, and make sure the TOC |
| 4878 |
* items are marked as not being in any parallel-processing list. |
| 4879 |
*/ |
| 4880 |
for (te = AH->toc->next; te != AH->toc; te = te->next) |
| 4881 |
{ |
| 4882 |
te->depCount = te->nDeps; |
| 4883 |
te->revDeps = NULL; |
| 4884 |
te->nRevDeps = 0; |
| 4885 |
te->pending_prev = NULL; |
| 4886 |
te->pending_next = NULL; |
| 4887 |
} |
| 4888 |
|
| 4889 |
/* |
| 4890 |
* POST_DATA items that are shown as depending on a table need to be |
| 4891 |
* re-pointed to depend on that table's data, instead. This ensures they |
| 4892 |
* won't get scheduled until the data has been loaded. |
| 4893 |
*/ |
| 4894 |
repoint_table_dependencies(AH); |
| 4895 |
|
| 4896 |
/* |
| 4897 |
* Pre-8.4 versions of pg_dump neglected to set up a dependency from BLOB |
| 4898 |
* COMMENTS to BLOBS. Cope. (We assume there's only one BLOBS and only |
| 4899 |
* one BLOB COMMENTS in such files.) |
| 4900 |
*/ |
| 4901 |
if (AH->version < K_VERS_1_11) |
| 4902 |
{ |
| 4903 |
for (te = AH->toc->next; te != AH->toc; te = te->next) |
| 4904 |
{ |
| 4905 |
if (strcmp(te->desc, "BLOB COMMENTS") == 0 && te->nDeps == 0) |
| 4906 |
{ |
| 4907 |
TocEntry *te2; |
| 4908 |
|
| 4909 |
for (te2 = AH->toc->next; te2 != AH->toc; te2 = te2->next) |
| 4910 |
{ |
| 4911 |
if (strcmp(te2->desc, "BLOBS") == 0) |
| 4912 |
{ |
| 4913 |
te->dependencies = (DumpId *) pg_malloc(sizeof(DumpId)); |
| 4914 |
te->dependencies[0] = te2->dumpId; |
| 4915 |
te->nDeps++; |
| 4916 |
te->depCount++; |
| 4917 |
break; |
| 4918 |
} |
| 4919 |
} |
| 4920 |
break; |
| 4921 |
} |
| 4922 |
} |
| 4923 |
} |
| 4924 |
|
| 4925 |
/* |
| 4926 |
* At this point we start to build the revDeps reverse-dependency arrays, |
| 4927 |
* so all changes of dependencies must be complete. |
| 4928 |
*/ |
| 4929 |
|
| 4930 |
/* |
| 4931 |
* Count the incoming dependencies for each item. Also, it is possible |
| 4932 |
* that the dependencies list items that are not in the archive at all |
| 4933 |
* (that should not happen in 9.2 and later, but is highly likely in older |
| 4934 |
* archives). Subtract such items from the depCounts. |
| 4935 |
*/ |
| 4936 |
for (te = AH->toc->next; te != AH->toc; te = te->next) |
| 4937 |
{ |
| 4938 |
for (i = 0; i < te->nDeps; i++) |
| 4939 |
{ |
| 4940 |
DumpId depid = te->dependencies[i]; |
| 4941 |
|
| 4942 |
if (depid <= AH->maxDumpId && AH->tocsByDumpId[depid] != NULL) |
| 4943 |
AH->tocsByDumpId[depid]->nRevDeps++; |
| 4944 |
else |
| 4945 |
te->depCount--; |
| 4946 |
} |
| 4947 |
} |
| 4948 |
|
| 4949 |
/* |
| 4950 |
* Allocate space for revDeps[] arrays, and reset nRevDeps so we can use |
| 4951 |
* it as a counter below. |
| 4952 |
*/ |
| 4953 |
for (te = AH->toc->next; te != AH->toc; te = te->next) |
| 4954 |
{ |
| 4955 |
if (te->nRevDeps > 0) |
| 4956 |
te->revDeps = (DumpId *) pg_malloc(te->nRevDeps * sizeof(DumpId)); |
| 4957 |
te->nRevDeps = 0; |
| 4958 |
} |
| 4959 |
|
| 4960 |
/* |
| 4961 |
* Build the revDeps[] arrays of incoming-dependency dumpIds. This had |
| 4962 |
* better agree with the loops above. |
| 4963 |
*/ |
| 4964 |
for (te = AH->toc->next; te != AH->toc; te = te->next) |
| 4965 |
{ |
| 4966 |
for (i = 0; i < te->nDeps; i++) |
| 4967 |
{ |
| 4968 |
DumpId depid = te->dependencies[i]; |
| 4969 |
|
| 4970 |
if (depid <= AH->maxDumpId && AH->tocsByDumpId[depid] != NULL) |
| 4971 |
{ |
| 4972 |
TocEntry *otherte = AH->tocsByDumpId[depid]; |
| 4973 |
|
| 4974 |
otherte->revDeps[otherte->nRevDeps++] = te->dumpId; |
| 4975 |
} |
| 4976 |
} |
| 4977 |
} |
| 4978 |
|
| 4979 |
/* |
| 4980 |
* Lastly, work out the locking dependencies. |
| 4981 |
*/ |
| 4982 |
for (te = AH->toc->next; te != AH->toc; te = te->next) |
| 4983 |
{ |
| 4984 |
te->lockDeps = NULL; |
| 4985 |
te->nLockDeps = 0; |
| 4986 |
identify_locking_dependencies(AH, te); |
| 4987 |
} |
| 4988 |
} |
| 4989 |
|
| 4990 |
/* |
| 4991 |
* Change dependencies on table items to depend on table data items instead, |
| 4992 |
* but only in POST_DATA items. |
| 4993 |
* |
| 4994 |
* Also, for any item having such dependency(s), set its dataLength to the |
| 4995 |
* largest dataLength of the table data items it depends on. This ensures |
| 4996 |
* that parallel restore will prioritize larger jobs (index builds, FK |
| 4997 |
* constraint checks, etc) over smaller ones, avoiding situations where we |
| 4998 |
* end a restore with only one active job working on a large table. |
| 4999 |
*/ |
| 5000 |
static void |
| 5001 |
repoint_table_dependencies(ArchiveHandle *AH) |
| 5002 |
{ |
| 5003 |
TocEntry *te; |
| 5004 |
int i; |
| 5005 |
DumpId olddep; |
| 5006 |
|
| 5007 |
for (te = AH->toc->next; te != AH->toc; te = te->next) |
| 5008 |
{ |
| 5009 |
if (te->section != SECTION_POST_DATA) |
| 5010 |
continue; |
| 5011 |
for (i = 0; i < te->nDeps; i++) |
| 5012 |
{ |
| 5013 |
olddep = te->dependencies[i]; |
| 5014 |
if (olddep <= AH->maxDumpId && |
| 5015 |
AH->tableDataId[olddep] != 0) |
| 5016 |
{ |
| 5017 |
DumpId tabledataid = AH->tableDataId[olddep]; |
| 5018 |
TocEntry *tabledatate = AH->tocsByDumpId[tabledataid]; |
| 5019 |
|
| 5020 |
te->dependencies[i] = tabledataid; |
| 5021 |
te->dataLength = Max(te->dataLength, tabledatate->dataLength); |
| 5022 |
pg_log_debug("transferring dependency %d -> %d to %d", |
| 5023 |
te->dumpId, olddep, tabledataid); |
| 5024 |
} |
| 5025 |
} |
| 5026 |
} |
| 5027 |
} |
| 5028 |
|
| 5029 |
/* |
| 5030 |
* Identify which objects we'll need exclusive lock on in order to restore |
| 5031 |
* the given TOC entry (*other* than the one identified by the TOC entry |
| 5032 |
* itself). Record their dump IDs in the entry's lockDeps[] array. |
| 5033 |
*/ |
| 5034 |
static void |
| 5035 |
identify_locking_dependencies(ArchiveHandle *AH, TocEntry *te) |
| 5036 |
{ |
| 5037 |
DumpId *lockids; |
| 5038 |
int nlockids; |
| 5039 |
int i; |
| 5040 |
|
| 5041 |
/* |
| 5042 |
* We only care about this for POST_DATA items. PRE_DATA items are not |
| 5043 |
* run in parallel, and DATA items are all independent by assumption. |
| 5044 |
*/ |
| 5045 |
if (te->section != SECTION_POST_DATA) |
| 5046 |
return; |
| 5047 |
|
| 5048 |
/* Quick exit if no dependencies at all */ |
| 5049 |
if (te->nDeps == 0) |
| 5050 |
return; |
| 5051 |
|
| 5052 |
/* |
| 5053 |
* Most POST_DATA items are ALTER TABLEs or some moral equivalent of that, |
| 5054 |
* and hence require exclusive lock. However, we know that CREATE INDEX |
| 5055 |
* does not. (Maybe someday index-creating CONSTRAINTs will fall in that |
| 5056 |
* category too ... but today is not that day.) |
| 5057 |
*/ |
| 5058 |
if (strcmp(te->desc, "INDEX") == 0) |
| 5059 |
return; |
| 5060 |
|
| 5061 |
/* |
| 5062 |
* We assume the entry requires exclusive lock on each TABLE or TABLE DATA |
| 5063 |
* item listed among its dependencies. Originally all of these would have |
| 5064 |
* been TABLE items, but repoint_table_dependencies would have repointed |
| 5065 |
* them to the TABLE DATA items if those are present (which they might not |
| 5066 |
* be, eg in a schema-only dump). Note that all of the entries we are |
| 5067 |
* processing here are POST_DATA; otherwise there might be a significant |
| 5068 |
* difference between a dependency on a table and a dependency on its |
| 5069 |
* data, so that closer analysis would be needed here. |
| 5070 |
*/ |
| 5071 |
lockids = (DumpId *) pg_malloc(te->nDeps * sizeof(DumpId)); |
| 5072 |
nlockids = 0; |
| 5073 |
for (i = 0; i < te->nDeps; i++) |
| 5074 |
{ |
| 5075 |
DumpId depid = te->dependencies[i]; |
| 5076 |
|
| 5077 |
if (depid <= AH->maxDumpId && AH->tocsByDumpId[depid] != NULL && |
| 5078 |
((strcmp(AH->tocsByDumpId[depid]->desc, "TABLE DATA") == 0) || |
| 5079 |
strcmp(AH->tocsByDumpId[depid]->desc, "TABLE") == 0)) |
| 5080 |
lockids[nlockids++] = depid; |
| 5081 |
} |
| 5082 |
|
| 5083 |
if (nlockids == 0) |
| 5084 |
{ |
| 5085 |
free(lockids); |
| 5086 |
return; |
| 5087 |
} |
| 5088 |
|
| 5089 |
te->lockDeps = pg_realloc(lockids, nlockids * sizeof(DumpId)); |
| 5090 |
te->nLockDeps = nlockids; |
| 5091 |
} |
| 5092 |
|
| 5093 |
/* |
| 5094 |
* Remove the specified TOC entry from the depCounts of items that depend on |
| 5095 |
* it, thereby possibly making them ready-to-run. Any pending item that |
| 5096 |
* becomes ready should be moved to the ready_heap, if that's provided. |
| 5097 |
*/ |
| 5098 |
static void |
| 5099 |
reduce_dependencies(ArchiveHandle *AH, TocEntry *te, |
| 5100 |
binaryheap *ready_heap) |
| 5101 |
{ |
| 5102 |
int i; |
| 5103 |
|
| 5104 |
pg_log_debug("reducing dependencies for %d", te->dumpId); |
| 5105 |
|
| 5106 |
for (i = 0; i < te->nRevDeps; i++) |
| 5107 |
{ |
| 5108 |
TocEntry *otherte = AH->tocsByDumpId[te->revDeps[i]]; |
| 5109 |
|
| 5110 |
Assert(otherte->depCount > 0); |
| 5111 |
otherte->depCount--; |
| 5112 |
|
| 5113 |
/* |
| 5114 |
* It's ready if it has no remaining dependencies, and it belongs in |
| 5115 |
* the current restore pass, and it is currently a member of the |
| 5116 |
* pending list (that check is needed to prevent double restore in |
| 5117 |
* some cases where a list-file forces out-of-order restoring). |
| 5118 |
* However, if ready_heap == NULL then caller doesn't want any list |
| 5119 |
* memberships changed. |
| 5120 |
*/ |
| 5121 |
if (otherte->depCount == 0 && |
| 5122 |
_tocEntryRestorePass(otherte) == AH->restorePass && |
| 5123 |
otherte->pending_prev != NULL && |
| 5124 |
ready_heap != NULL) |
| 5125 |
{ |
| 5126 |
/* Remove it from pending list ... */ |
| 5127 |
pending_list_remove(otherte); |
| 5128 |
/* ... and add to ready_heap */ |
| 5129 |
binaryheap_add(ready_heap, otherte); |
| 5130 |
} |
| 5131 |
} |
| 5132 |
} |
| 5133 |
|
| 5134 |
/* |
| 5135 |
* Set the created flag on the DATA member corresponding to the given |
| 5136 |
* TABLE member |
| 5137 |
*/ |
| 5138 |
static void |
| 5139 |
mark_create_done(ArchiveHandle *AH, TocEntry *te) |
| 5140 |
{ |
| 5141 |
if (AH->tableDataId[te->dumpId] != 0) |
| 5142 |
{ |
| 5143 |
TocEntry *ted = AH->tocsByDumpId[AH->tableDataId[te->dumpId]]; |
| 5144 |
|
| 5145 |
ted->created = true; |
| 5146 |
} |
| 5147 |
} |
| 5148 |
|
| 5149 |
/* |
| 5150 |
* Mark the DATA member corresponding to the given TABLE member |
| 5151 |
* as not wanted |
| 5152 |
*/ |
| 5153 |
static void |
| 5154 |
inhibit_data_for_failed_table(ArchiveHandle *AH, TocEntry *te) |
| 5155 |
{ |
| 5156 |
pg_log_info("table \"%s\" could not be created, will not restore its data", |
| 5157 |
te->tag); |
| 5158 |
|
| 5159 |
if (AH->tableDataId[te->dumpId] != 0) |
| 5160 |
{ |
| 5161 |
TocEntry *ted = AH->tocsByDumpId[AH->tableDataId[te->dumpId]]; |
| 5162 |
|
| 5163 |
ted->reqs = 0; |
| 5164 |
} |
| 5165 |
} |
| 5166 |
|
| 5167 |
/* |
| 5168 |
* Clone and de-clone routines used in parallel restoration. |
| 5169 |
* |
| 5170 |
* Enough of the structure is cloned to ensure that there is no |
| 5171 |
* conflict between different threads each with their own clone. |
| 5172 |
*/ |
| 5173 |
ArchiveHandle * |
| 5174 |
CloneArchive(ArchiveHandle *AH) |
| 5175 |
{ |
| 5176 |
ArchiveHandle *clone; |
| 5177 |
|
| 5178 |
/* Make a "flat" copy */ |
| 5179 |
clone = (ArchiveHandle *) pg_malloc(sizeof(ArchiveHandle)); |
| 5180 |
memcpy(clone, AH, sizeof(ArchiveHandle)); |
| 5181 |
|
| 5182 |
/* Likewise flat-copy the RestoreOptions, so we can alter them locally */ |
| 5183 |
clone->public.ropt = (RestoreOptions *) pg_malloc(sizeof(RestoreOptions)); |
| 5184 |
memcpy(clone->public.ropt, AH->public.ropt, sizeof(RestoreOptions)); |
| 5185 |
|
| 5186 |
/* Handle format-independent fields */ |
| 5187 |
memset(&(clone->sqlparse), 0, sizeof(clone->sqlparse)); |
| 5188 |
|
| 5189 |
/* The clone will have its own connection, so disregard connection state */ |
| 5190 |
clone->connection = NULL; |
| 5191 |
clone->connCancel = NULL; |
| 5192 |
clone->currUser = NULL; |
| 5193 |
clone->currSchema = NULL; |
| 5194 |
clone->currTableAm = NULL; |
| 5195 |
clone->currTablespace = NULL; |
| 5196 |
|
| 5197 |
/* savedPassword must be local in case we change it while connecting */ |
| 5198 |
if (clone->savedPassword) |
| 5199 |
clone->savedPassword = pg_strdup(clone->savedPassword); |
| 5200 |
|
| 5201 |
/* clone has its own error count, too */ |
| 5202 |
clone->public.n_errors = 0; |
| 5203 |
|
| 5204 |
/* clones should not share lo_buf */ |
| 5205 |
clone->lo_buf = NULL; |
| 5206 |
|
| 5207 |
/* |
| 5208 |
* Clone connections disregard --transaction-size; they must commit after |
| 5209 |
* each command so that the results are immediately visible to other |
| 5210 |
* workers. |
| 5211 |
*/ |
| 5212 |
clone->public.ropt->txn_size = 0; |
| 5213 |
|
| 5214 |
/* |
| 5215 |
* Connect our new clone object to the database, using the same connection |
| 5216 |
* parameters used for the original connection. |
| 5217 |
*/ |
| 5218 |
ConnectDatabaseAhx((Archive *) clone, &clone->public.ropt->cparams, true); |
| 5219 |
|
| 5220 |
/* re-establish fixed state */ |
| 5221 |
if (AH->mode == archModeRead) |
| 5222 |
_doSetFixedOutputState(clone); |
| 5223 |
/* in write case, setupDumpWorker will fix up connection state */ |
| 5224 |
|
| 5225 |
/* Let the format-specific code have a chance too */ |
| 5226 |
clone->ClonePtr(clone); |
| 5227 |
|
| 5228 |
Assert(clone->connection != NULL); |
| 5229 |
return clone; |
| 5230 |
} |
| 5231 |
|
| 5232 |
/* |
| 5233 |
* Release clone-local storage. |
| 5234 |
* |
| 5235 |
* Note: we assume any clone-local connection was already closed. |
| 5236 |
*/ |
| 5237 |
void |
| 5238 |
DeCloneArchive(ArchiveHandle *AH) |
| 5239 |
{ |
| 5240 |
/* Should not have an open database connection */ |
| 5241 |
Assert(AH->connection == NULL); |
| 5242 |
|
| 5243 |
/* Clear format-specific state */ |
| 5244 |
AH->DeClonePtr(AH); |
| 5245 |
|
| 5246 |
/* Clear state allocated by CloneArchive */ |
| 5247 |
if (AH->sqlparse.curCmd) |
| 5248 |
destroyPQExpBuffer(AH->sqlparse.curCmd); |
| 5249 |
|
| 5250 |
/* Clear any connection-local state */ |
| 5251 |
free(AH->currUser); |
| 5252 |
free(AH->currSchema); |
| 5253 |
free(AH->currTablespace); |
| 5254 |
free(AH->currTableAm); |
| 5255 |
free(AH->savedPassword); |
| 5256 |
|
| 5257 |
free(AH); |
| 5258 |
} |