PostgreSQL Source Code  git master
pg_backup.h File Reference
#include "fe_utils/simple_list.h"
#include "libpq-fe.h"
Include dependency graph for pg_backup.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  _restoreOptions
 
struct  _dumpOptions
 
struct  Archive
 
struct  CatalogId
 

Macros

#define appendStringLiteralAH(buf, str, AH)   appendStringLiteral(buf, str, (AH)->encoding, (AH)->std_strings)
 

Typedefs

typedef enum trivalue trivalue
 
typedef enum _archiveFormat ArchiveFormat
 
typedef enum _archiveMode ArchiveMode
 
typedef enum _teSection teSection
 
typedef struct _restoreOptions RestoreOptions
 
typedef struct _dumpOptions DumpOptions
 
typedef struct Archive Archive
 
typedef int DumpId
 
typedef int(* DataDumperPtr) (Archive *AH, void *userArg)
 
typedef void(* SetupWorkerPtrType) (Archive *AH)
 

Enumerations

enum  trivalue {
  TRI_DEFAULT, TRI_NO, TRI_YES, TRI_DEFAULT,
  TRI_NO, TRI_YES, TRI_DEFAULT, TRI_NO,
  TRI_YES, TRI_DEFAULT, TRI_NO, TRI_YES
}
 
enum  _archiveFormat {
  archUnknown = 0, archCustom = 1, archTar = 3, archNull = 4,
  archDirectory = 5
}
 
enum  _archiveMode { archModeAppend, archModeWrite, archModeRead }
 
enum  _teSection { SECTION_NONE = 1, SECTION_PRE_DATA, SECTION_DATA, SECTION_POST_DATA }
 

Functions

void ConnectDatabase (Archive *AH, const char *dbname, const char *pghost, const char *pgport, const char *username, trivalue prompt_password)
 
void DisconnectDatabase (Archive *AHX)
 
PGconnGetConnection (Archive *AHX)
 
void WriteData (Archive *AH, const void *data, size_t dLen)
 
int StartBlob (Archive *AH, Oid oid)
 
int EndBlob (Archive *AH, Oid oid)
 
void CloseArchive (Archive *AH)
 
void SetArchiveOptions (Archive *AH, DumpOptions *dopt, RestoreOptions *ropt)
 
void ProcessArchiveRestoreOptions (Archive *AH)
 
void RestoreArchive (Archive *AH)
 
ArchiveOpenArchive (const char *FileSpec, const ArchiveFormat fmt)
 
ArchiveCreateArchive (const char *FileSpec, const ArchiveFormat fmt, const int compression, bool dosync, ArchiveMode mode, SetupWorkerPtrType setupDumpWorker)
 
void PrintTOCSummary (Archive *AH)
 
RestoreOptionsNewRestoreOptions (void)
 
DumpOptionsNewDumpOptions (void)
 
void InitDumpOptions (DumpOptions *opts)
 
DumpOptionsdumpOptionsFromRestoreOptions (RestoreOptions *ropt)
 
void SortTocFromFile (Archive *AHX)
 
void archputs (const char *s, Archive *AH)
 
int archprintf (Archive *AH, const char *fmt,...) pg_attribute_printf(2
 

Macro Definition Documentation

◆ appendStringLiteralAH

Typedef Documentation

◆ Archive

typedef struct Archive Archive

◆ ArchiveFormat

◆ ArchiveMode

typedef enum _archiveMode ArchiveMode

◆ DataDumperPtr

typedef int(* DataDumperPtr) (Archive *AH, void *userArg)

Definition at line 236 of file pg_backup.h.

◆ DumpId

typedef int DumpId

Definition at line 234 of file pg_backup.h.

◆ DumpOptions

typedef struct _dumpOptions DumpOptions

◆ RestoreOptions

◆ SetupWorkerPtrType

typedef void(* SetupWorkerPtrType) (Archive *AH)

Definition at line 238 of file pg_backup.h.

◆ teSection

typedef enum _teSection teSection

◆ trivalue

typedef enum trivalue trivalue

Enumeration Type Documentation

◆ _archiveFormat

Enumerator
archUnknown 
archCustom 
archTar 
archNull 
archDirectory 

Definition at line 37 of file pg_backup.h.

38 {
39  archUnknown = 0,
40  archCustom = 1,
41  archTar = 3,
42  archNull = 4,
43  archDirectory = 5
enum _archiveFormat ArchiveFormat

◆ _archiveMode

Enumerator
archModeAppend 
archModeWrite 
archModeRead 

Definition at line 46 of file pg_backup.h.

47 {
51 } ArchiveMode;
enum _archiveMode ArchiveMode

◆ _teSection

enum _teSection
Enumerator
SECTION_NONE 
SECTION_PRE_DATA 
SECTION_DATA 
SECTION_POST_DATA 

Definition at line 53 of file pg_backup.h.

54 {
55  SECTION_NONE = 1, /* COMMENTs, ACLs, etc; can be anywhere */
56  SECTION_PRE_DATA, /* stuff to be processed before data */
57  SECTION_DATA, /* TABLE DATA, BLOBS, BLOB COMMENTS */
58  SECTION_POST_DATA /* stuff to be processed after data */
59 } teSection;
enum _teSection teSection

◆ trivalue

enum trivalue
Enumerator
TRI_DEFAULT 
TRI_NO 
TRI_YES 
TRI_DEFAULT 
TRI_NO 
TRI_YES 
TRI_DEFAULT 
TRI_NO 
TRI_YES 
TRI_DEFAULT 
TRI_NO 
TRI_YES 

Definition at line 30 of file pg_backup.h.

31 {
33  TRI_NO,
34  TRI_YES
35 } trivalue;
trivalue
Definition: pg_backup.h:30

Function Documentation

◆ archprintf()

int archprintf ( Archive AH,
const char *  fmt,
  ... 
)

◆ archputs()

void archputs ( const char *  s,
Archive AH 
)

Definition at line 1459 of file pg_backup_archiver.c.

References WriteData().

Referenced by dumpTableData_insert().

1460 {
1461  WriteData(AH, s, strlen(s));
1462 }
void WriteData(Archive *AHX, const void *data, size_t dLen)

◆ CloseArchive()

void CloseArchive ( Archive AH)

Definition at line 257 of file pg_backup_archiver.c.

References _archiveHandle::ClosePtr, fatal, GZCLOSE, _archiveHandle::gzOut, _archiveHandle::OF, and generate_unaccent_rules::stdout.

Referenced by main().

258 {
259  int res = 0;
260  ArchiveHandle *AH = (ArchiveHandle *) AHX;
261 
262  AH->ClosePtr(AH);
263 
264  /* Close the output */
265  if (AH->gzOut)
266  res = GZCLOSE(AH->OF);
267  else if (AH->OF != stdout)
268  res = fclose(AH->OF);
269 
270  if (res != 0)
271  fatal("could not close output file: %m");
272 }
#define fatal(...)
#define GZCLOSE(fh)
ClosePtrType ClosePtr

◆ ConnectDatabase()

void ConnectDatabase ( Archive AH,
const char *  dbname,
const char *  pghost,
const char *  pgport,
const char *  username,
trivalue  prompt_password 
)

Definition at line 236 of file pg_backup_db.c.

References _check_database_version(), ALWAYS_SECURE_SEARCH_PATH_SQL, _archiveHandle::connection, CONNECTION_BAD, dbname, ExecuteSqlQueryForSingleRow(), fatal, free, notice_processor(), password, pg_strdup(), pghost, pgport, PQclear(), PQconnectdbParams(), PQconnectionNeedsPassword(), PQconnectionUsedPassword(), PQdb(), PQerrorMessage(), PQfinish(), PQpass(), PQsetNoticeProcessor(), PQstatus(), progname, _archiveHandle::promptPassword, _archiveHandle::savedPassword, set_archive_cancel_info(), simple_prompt(), TRI_NO, TRI_YES, username, and values.

Referenced by CloneArchive(), main(), restore_toc_entries_postfork(), and RestoreArchive().

242 {
243  ArchiveHandle *AH = (ArchiveHandle *) AHX;
244  char *password;
245  char passbuf[100];
246  bool new_pass;
247 
248  if (AH->connection)
249  fatal("already connected to a database");
250 
251  password = AH->savedPassword;
252 
253  if (prompt_password == TRI_YES && password == NULL)
254  {
255  simple_prompt("Password: ", passbuf, sizeof(passbuf), false);
256  password = passbuf;
257  }
258  AH->promptPassword = prompt_password;
259 
260  /*
261  * Start the connection. Loop until we have a password if requested by
262  * backend.
263  */
264  do
265  {
266  const char *keywords[7];
267  const char *values[7];
268 
269  keywords[0] = "host";
270  values[0] = pghost;
271  keywords[1] = "port";
272  values[1] = pgport;
273  keywords[2] = "user";
274  values[2] = username;
275  keywords[3] = "password";
276  values[3] = password;
277  keywords[4] = "dbname";
278  values[4] = dbname;
279  keywords[5] = "fallback_application_name";
280  values[5] = progname;
281  keywords[6] = NULL;
282  values[6] = NULL;
283 
284  new_pass = false;
285  AH->connection = PQconnectdbParams(keywords, values, true);
286 
287  if (!AH->connection)
288  fatal("could not connect to database");
289 
290  if (PQstatus(AH->connection) == CONNECTION_BAD &&
292  password == NULL &&
293  prompt_password != TRI_NO)
294  {
295  PQfinish(AH->connection);
296  simple_prompt("Password: ", passbuf, sizeof(passbuf), false);
297  password = passbuf;
298  new_pass = true;
299  }
300  } while (new_pass);
301 
302  /* check to see that the backend connection was successfully made */
303  if (PQstatus(AH->connection) == CONNECTION_BAD)
304  fatal("connection to database \"%s\" failed: %s",
305  PQdb(AH->connection) ? PQdb(AH->connection) : "",
307 
308  /* Start strict; later phases may override this. */
311 
312  /*
313  * We want to remember connection's actual password, whether or not we got
314  * it by prompting. So we don't just store the password variable.
315  */
317  {
318  if (AH->savedPassword)
319  free(AH->savedPassword);
321  }
322 
323  /* check for version mismatch */
325 
327 
328  /* arrange for SIGINT to issue a query cancel on this connection */
330 }
static char password[100]
Definition: streamutil.c:53
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6623
PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg)
Definition: fe-connect.c:6799
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4125
const char * progname
Definition: pg_standby.c:36
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:624
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:416
void simple_prompt(const char *prompt, char *destination, size_t destlen, bool echo)
Definition: sprompt.c:37
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
char * PQpass(const PGconn *conn)
Definition: fe-connect.c:6489
static void notice_processor(void *arg, const char *message)
Definition: pg_backup_db.c:374
char * pghost
Definition: pgbench.c:243
static char * username
Definition: initdb.c:133
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define free(a)
Definition: header.h:65
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:6473
char * dbname
Definition: streamutil.c:50
#define fatal(...)
#define ALWAYS_SECURE_SEARCH_PATH_SQL
Definition: connect.h:25
static Datum values[MAXATTR]
Definition: bootstrap.c:167
static void _check_database_version(ArchiveHandle *AH)
Definition: pg_backup_db.c:33
int PQconnectionUsedPassword(const PGconn *conn)
Definition: fe-connect.c:6672
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:6657
void set_archive_cancel_info(ArchiveHandle *AH, PGconn *conn)
Definition: parallel.c:739
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6570
char * pgport
Definition: pgbench.c:244

◆ CreateArchive()

Archive* CreateArchive ( const char *  FileSpec,
const ArchiveFormat  fmt,
const int  compression,
bool  dosync,
ArchiveMode  mode,
SetupWorkerPtrType  setupDumpWorker 
)

Definition at line 234 of file pg_backup_archiver.c.

References _allocAH().

Referenced by main().

238 {
239  ArchiveHandle *AH = _allocAH(FileSpec, fmt, compression, dosync,
241 
242  return (Archive *) AH;
243 }
static PgChecksumMode mode
Definition: pg_checksums.c:61
static ArchiveHandle * _allocAH(const char *FileSpec, const ArchiveFormat fmt, const int compression, bool dosync, ArchiveMode mode, SetupWorkerPtrType setupWorkerPtr)
static bool dosync
Definition: pg_dump.c:92
static void setupDumpWorker(Archive *AHX)
Definition: pg_dump.c:1226

◆ DisconnectDatabase()

void DisconnectDatabase ( Archive AHX)

Definition at line 337 of file pg_backup_db.c.

References _archiveHandle::connCancel, _archiveHandle::connection, PQcancel(), PQfinish(), PQTRANS_ACTIVE, PQtransactionStatus(), and set_archive_cancel_info().

Referenced by archive_close_connection(), restore_toc_entries_prefork(), RestoreArchive(), and RunWorker().

338 {
339  ArchiveHandle *AH = (ArchiveHandle *) AHX;
340  char errbuf[1];
341 
342  if (!AH->connection)
343  return;
344 
345  if (AH->connCancel)
346  {
347  /*
348  * If we have an active query, send a cancel before closing, ignoring
349  * any errors. This is of no use for a normal exit, but might be
350  * helpful during fatal().
351  */
353  (void) PQcancel(AH->connCancel, errbuf, sizeof(errbuf));
354 
355  /*
356  * Prevent signal handler from sending a cancel after this.
357  */
358  set_archive_cancel_info(AH, NULL);
359  }
360 
361  PQfinish(AH->connection);
362  AH->connection = NULL;
363 }
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4125
PGcancel *volatile connCancel
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:6578
int PQcancel(PGcancel *cancel, char *errbuf, int errbufsize)
Definition: fe-connect.c:4401
void set_archive_cancel_info(ArchiveHandle *AH, PGconn *conn)
Definition: parallel.c:739

◆ dumpOptionsFromRestoreOptions()

DumpOptions* dumpOptionsFromRestoreOptions ( RestoreOptions ropt)

Definition at line 176 of file pg_backup_archiver.c.

References _restoreOptions::aclsSkip, _dumpOptions::aclsSkip, _restoreOptions::column_inserts, _dumpOptions::column_inserts, _restoreOptions::createDB, _restoreOptions::dataOnly, _dumpOptions::dataOnly, _restoreOptions::disable_dollar_quoting, _dumpOptions::disable_dollar_quoting, _restoreOptions::disable_triggers, _dumpOptions::disable_triggers, _restoreOptions::dropSchema, _restoreOptions::dump_inserts, _dumpOptions::dump_inserts, _restoreOptions::dumpSections, _dumpOptions::dumpSections, _restoreOptions::enable_row_security, _dumpOptions::enable_row_security, _restoreOptions::if_exists, _dumpOptions::if_exists, _restoreOptions::include_everything, _dumpOptions::include_everything, _restoreOptions::lockWaitTimeout, _dumpOptions::lockWaitTimeout, NewDumpOptions(), _restoreOptions::no_comments, _dumpOptions::no_comments, _restoreOptions::no_publications, _dumpOptions::no_publications, _restoreOptions::no_security_labels, _dumpOptions::no_security_labels, _restoreOptions::no_subscriptions, _dumpOptions::no_subscriptions, _restoreOptions::noOwner, _restoreOptions::noTablespace, _dumpOptions::outputClean, _dumpOptions::outputCreateDB, _dumpOptions::outputNoOwner, _dumpOptions::outputNoTablespaces, _dumpOptions::outputSuperuser, _restoreOptions::schemaOnly, _dumpOptions::schemaOnly, _restoreOptions::sequence_data, _dumpOptions::sequence_data, _restoreOptions::superuser, _restoreOptions::use_setsessauth, and _dumpOptions::use_setsessauth.

Referenced by SetArchiveOptions().

177 {
178  DumpOptions *dopt = NewDumpOptions();
179 
180  /* this is the inverse of what's at the end of pg_dump.c's main() */
181  dopt->outputClean = ropt->dropSchema;
182  dopt->dataOnly = ropt->dataOnly;
183  dopt->schemaOnly = ropt->schemaOnly;
184  dopt->if_exists = ropt->if_exists;
185  dopt->column_inserts = ropt->column_inserts;
186  dopt->dumpSections = ropt->dumpSections;
187  dopt->aclsSkip = ropt->aclsSkip;
188  dopt->outputSuperuser = ropt->superuser;
189  dopt->outputCreateDB = ropt->createDB;
190  dopt->outputNoOwner = ropt->noOwner;
191  dopt->outputNoTablespaces = ropt->noTablespace;
192  dopt->disable_triggers = ropt->disable_triggers;
193  dopt->use_setsessauth = ropt->use_setsessauth;
195  dopt->dump_inserts = ropt->dump_inserts;
196  dopt->no_comments = ropt->no_comments;
197  dopt->no_publications = ropt->no_publications;
199  dopt->no_subscriptions = ropt->no_subscriptions;
200  dopt->lockWaitTimeout = ropt->lockWaitTimeout;
203  dopt->sequence_data = ropt->sequence_data;
204 
205  return dopt;
206 }
int column_inserts
Definition: pg_backup.h:147
int disable_triggers
Definition: pg_backup.h:156
int disable_dollar_quoting
Definition: pg_backup.h:73
bool schemaOnly
Definition: pg_backup.h:138
int no_subscriptions
Definition: pg_backup.h:152
int no_publications
Definition: pg_backup.h:151
DumpOptions * NewDumpOptions(void)
int sequence_data
Definition: pg_backup.h:172
const char * lockWaitTimeout
Definition: pg_backup.h:89
int use_setsessauth
Definition: pg_backup.h:158
int disable_dollar_quoting
Definition: pg_backup.h:146
int column_inserts
Definition: pg_backup.h:75
const char * lockWaitTimeout
Definition: pg_backup.h:142
bool dataOnly
Definition: pg_backup.h:139
bool include_everything
Definition: pg_backup.h:163
char * outputSuperuser
Definition: pg_backup.h:170
int include_everything
Definition: pg_backup.h:90
int no_security_labels
Definition: pg_backup.h:79
int dumpSections
Definition: pg_backup.h:140
int enable_row_security
Definition: pg_backup.h:123
int dump_inserts
Definition: pg_backup.h:143
int outputNoOwner
Definition: pg_backup.h:169
int no_security_labels
Definition: pg_backup.h:150
int no_subscriptions
Definition: pg_backup.h:80
char * superuser
Definition: pg_backup.h:70
bool aclsSkip
Definition: pg_backup.h:141
int use_setsessauth
Definition: pg_backup.h:68
int enable_row_security
Definition: pg_backup.h:159
int outputCreateDB
Definition: pg_backup.h:166
int outputClean
Definition: pg_backup.h:165
int no_comments
Definition: pg_backup.h:149
int outputNoTablespaces
Definition: pg_backup.h:157
int no_publications
Definition: pg_backup.h:78
int disable_triggers
Definition: pg_backup.h:66

◆ EndBlob()

int EndBlob ( Archive AH,
Oid  oid 
)

Definition at line 1237 of file pg_backup_archiver.c.

References _archiveHandle::currToc, and _archiveHandle::EndBlobPtr.

Referenced by dumpBlobs().

1238 {
1239  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1240 
1241  if (AH->EndBlobPtr)
1242  AH->EndBlobPtr(AH, AH->currToc, oid);
1243 
1244  return 1;
1245 }
struct _tocEntry * currToc
EndBlobPtrType EndBlobPtr

◆ GetConnection()

PGconn* GetConnection ( Archive AHX)

Definition at line 366 of file pg_backup_db.c.

References _archiveHandle::connection.

367 {
368  ArchiveHandle *AH = (ArchiveHandle *) AHX;
369 
370  return AH->connection;
371 }

◆ InitDumpOptions()

void InitDumpOptions ( DumpOptions opts)

Definition at line 163 of file pg_backup_archiver.c.

References DUMP_UNSECTIONED, _dumpOptions::dumpSections, and _dumpOptions::include_everything.

Referenced by main(), and NewDumpOptions().

164 {
165  memset(opts, 0, sizeof(DumpOptions));
166  /* set any fields that shouldn't default to zeroes */
167  opts->include_everything = true;
169 }
bool include_everything
Definition: pg_backup.h:163
int dumpSections
Definition: pg_backup.h:140

◆ NewDumpOptions()

DumpOptions* NewDumpOptions ( void  )

Definition at line 151 of file pg_backup_archiver.c.

References InitDumpOptions(), and pg_malloc().

Referenced by dumpOptionsFromRestoreOptions().

152 {
153  DumpOptions *opts = (DumpOptions *) pg_malloc(sizeof(DumpOptions));
154 
155  InitDumpOptions(opts);
156  return opts;
157 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
void InitDumpOptions(DumpOptions *opts)

◆ NewRestoreOptions()

RestoreOptions* NewRestoreOptions ( void  )

Definition at line 968 of file pg_backup_archiver.c.

References archUnknown, DUMP_UNSECTIONED, _restoreOptions::dumpSections, _restoreOptions::format, pg_malloc0(), _restoreOptions::promptPassword, and TRI_DEFAULT.

Referenced by _CloseArchive(), and main().

969 {
970  RestoreOptions *opts;
971 
972  opts = (RestoreOptions *) pg_malloc0(sizeof(RestoreOptions));
973 
974  /* set any fields that shouldn't default to zeroes */
975  opts->format = archUnknown;
976  opts->promptPassword = TRI_DEFAULT;
978 
979  return opts;
980 }
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
trivalue promptPassword
Definition: pg_backup.h:115

◆ OpenArchive()

Archive* OpenArchive ( const char *  FileSpec,
const ArchiveFormat  fmt 
)

Definition at line 248 of file pg_backup_archiver.c.

References _allocAH(), archModeRead, and setupRestoreWorker().

Referenced by main().

249 {
250  ArchiveHandle *AH = _allocAH(FileSpec, fmt, 0, true, archModeRead, setupRestoreWorker);
251 
252  return (Archive *) AH;
253 }
static ArchiveHandle * _allocAH(const char *FileSpec, const ArchiveFormat fmt, const int compression, bool dosync, ArchiveMode mode, SetupWorkerPtrType setupWorkerPtr)
static void setupRestoreWorker(Archive *AHX)

◆ PrintTOCSummary()

void PrintTOCSummary ( Archive AH)

Definition at line 1119 of file pg_backup_archiver.c.

References _tocEntryRequired(), ahprintf(), archCustom, _archiveHandle::archdbname, archDirectory, ARCHIVE_MAJOR, ARCHIVE_MINOR, ARCHIVE_REV, _archiveHandle::archiveDumpVersion, _archiveHandle::archiveRemoteVersion, archTar, _tocEntry::catalogId, _archiveHandle::compression, _archiveHandle::createDate, _tocEntry::dependencies, _tocEntry::desc, _tocEntry::dumpId, _restoreOptions::filename, _archiveHandle::format, free, i, _archiveHandle::intSize, _tocEntry::nDeps, _tocEntry::next, _archiveHandle::offSize, CatalogId::oid, _tocEntry::owner, PGDUMP_STRFTIME_FMT, _archiveHandle::public, REQ_DATA, REQ_SCHEMA, RestoreOutput(), Archive::ropt, sanitize_line(), SaveOutput(), _tocEntry::section, SECTION_NONE, SECTION_PRE_DATA, SetOutput(), _restoreOptions::strict_names, StrictNamesCheck(), CatalogId::tableoid, _tocEntry::tag, _archiveHandle::toc, _archiveHandle::tocCount, _restoreOptions::verbose, and _archiveHandle::version.

Referenced by main().

1120 {
1121  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1122  RestoreOptions *ropt = AH->public.ropt;
1123  TocEntry *te;
1124  teSection curSection;
1125  OutputContext sav;
1126  const char *fmtName;
1127  char stamp_str[64];
1128 
1129  sav = SaveOutput(AH);
1130  if (ropt->filename)
1131  SetOutput(AH, ropt->filename, 0 /* no compression */ );
1132 
1133  if (strftime(stamp_str, sizeof(stamp_str), PGDUMP_STRFTIME_FMT,
1134  localtime(&AH->createDate)) == 0)
1135  strcpy(stamp_str, "[unknown]");
1136 
1137  ahprintf(AH, ";\n; Archive created at %s\n", stamp_str);
1138  ahprintf(AH, "; dbname: %s\n; TOC Entries: %d\n; Compression: %d\n",
1139  sanitize_line(AH->archdbname, false),
1140  AH->tocCount, AH->compression);
1141 
1142  switch (AH->format)
1143  {
1144  case archCustom:
1145  fmtName = "CUSTOM";
1146  break;
1147  case archDirectory:
1148  fmtName = "DIRECTORY";
1149  break;
1150  case archTar:
1151  fmtName = "TAR";
1152  break;
1153  default:
1154  fmtName = "UNKNOWN";
1155  }
1156 
1157  ahprintf(AH, "; Dump Version: %d.%d-%d\n",
1159  ahprintf(AH, "; Format: %s\n", fmtName);
1160  ahprintf(AH, "; Integer: %d bytes\n", (int) AH->intSize);
1161  ahprintf(AH, "; Offset: %d bytes\n", (int) AH->offSize);
1162  if (AH->archiveRemoteVersion)
1163  ahprintf(AH, "; Dumped from database version: %s\n",
1164  AH->archiveRemoteVersion);
1165  if (AH->archiveDumpVersion)
1166  ahprintf(AH, "; Dumped by pg_dump version: %s\n",
1167  AH->archiveDumpVersion);
1168 
1169  ahprintf(AH, ";\n;\n; Selected TOC Entries:\n;\n");
1170 
1171  curSection = SECTION_PRE_DATA;
1172  for (te = AH->toc->next; te != AH->toc; te = te->next)
1173  {
1174  if (te->section != SECTION_NONE)
1175  curSection = te->section;
1176  if (ropt->verbose ||
1177  (_tocEntryRequired(te, curSection, AH) & (REQ_SCHEMA | REQ_DATA)) != 0)
1178  {
1179  char *sanitized_name;
1180  char *sanitized_schema;
1181  char *sanitized_owner;
1182 
1183  /*
1184  */
1185  sanitized_name = sanitize_line(te->tag, false);
1186  sanitized_schema = sanitize_line(te->namespace, true);
1187  sanitized_owner = sanitize_line(te->owner, false);
1188 
1189  ahprintf(AH, "%d; %u %u %s %s %s %s\n", te->dumpId,
1190  te->catalogId.tableoid, te->catalogId.oid,
1191  te->desc, sanitized_schema, sanitized_name,
1192  sanitized_owner);
1193 
1194  free(sanitized_name);
1195  free(sanitized_schema);
1196  free(sanitized_owner);
1197  }
1198  if (ropt->verbose && te->nDeps > 0)
1199  {
1200  int i;
1201 
1202  ahprintf(AH, ";\tdepends on:");
1203  for (i = 0; i < te->nDeps; i++)
1204  ahprintf(AH, " %d", te->dependencies[i]);
1205  ahprintf(AH, "\n");
1206  }
1207  }
1208 
1209  /* Enforce strict names checking */
1210  if (ropt->strict_names)
1211  StrictNamesCheck(ropt);
1212 
1213  if (ropt->filename)
1214  RestoreOutput(AH, sav);
1215 }
struct _tocEntry * next
Oid tableoid
Definition: pg_backup.h:230
RestoreOptions * ropt
Definition: pg_backup.h:183
CatalogId catalogId
static void SetOutput(ArchiveHandle *AH, const char *filename, int compression)
teSection section
const char * filename
Definition: pg_backup.h:83
struct _tocEntry * toc
DumpId * dependencies
#define ARCHIVE_REV(version)
int ahprintf(ArchiveHandle *AH, const char *fmt,...)
#define ARCHIVE_MINOR(version)
ArchiveFormat format
static void StrictNamesCheck(RestoreOptions *ropt)
#define ARCHIVE_MAJOR(version)
static OutputContext SaveOutput(ArchiveHandle *AH)
#define free(a)
Definition: header.h:65
enum _teSection teSection
#define PGDUMP_STRFTIME_FMT
Definition: dumputils.h:33
static void RestoreOutput(ArchiveHandle *AH, OutputContext savedContext)
int i
static char * sanitize_line(const char *str, bool want_hyphen)
static teReqs _tocEntryRequired(TocEntry *te, teSection curSection, ArchiveHandle *AH)

◆ ProcessArchiveRestoreOptions()

void ProcessArchiveRestoreOptions ( Archive AH)

Definition at line 289 of file pg_backup_archiver.c.

References _tocEntryRequired(), archModeRead, fatal, _archiveHandle::mode, _tocEntry::next, pg_log_warning, _archiveHandle::public, _tocEntry::reqs, Archive::ropt, _tocEntry::section, SECTION_DATA, SECTION_NONE, SECTION_POST_DATA, SECTION_PRE_DATA, _restoreOptions::strict_names, StrictNamesCheck(), and _archiveHandle::toc.

Referenced by main().

290 {
291  ArchiveHandle *AH = (ArchiveHandle *) AHX;
292  RestoreOptions *ropt = AH->public.ropt;
293  TocEntry *te;
294  teSection curSection;
295 
296  /* Decide which TOC entries will be dumped/restored, and mark them */
297  curSection = SECTION_PRE_DATA;
298  for (te = AH->toc->next; te != AH->toc; te = te->next)
299  {
300  /*
301  * When writing an archive, we also take this opportunity to check
302  * that we have generated the entries in a sane order that respects
303  * the section divisions. When reading, don't complain, since buggy
304  * old versions of pg_dump might generate out-of-order archives.
305  */
306  if (AH->mode != archModeRead)
307  {
308  switch (te->section)
309  {
310  case SECTION_NONE:
311  /* ok to be anywhere */
312  break;
313  case SECTION_PRE_DATA:
314  if (curSection != SECTION_PRE_DATA)
315  pg_log_warning("archive items not in correct section order");
316  break;
317  case SECTION_DATA:
318  if (curSection == SECTION_POST_DATA)
319  pg_log_warning("archive items not in correct section order");
320  break;
321  case SECTION_POST_DATA:
322  /* ok no matter which section we were in */
323  break;
324  default:
325  fatal("unexpected section code %d",
326  (int) te->section);
327  break;
328  }
329  }
330 
331  if (te->section != SECTION_NONE)
332  curSection = te->section;
333 
334  te->reqs = _tocEntryRequired(te, curSection, AH);
335  }
336 
337  /* Enforce strict names checking */
338  if (ropt->strict_names)
339  StrictNamesCheck(ropt);
340 }
struct _tocEntry * next
RestoreOptions * ropt
Definition: pg_backup.h:183
teSection section
struct _tocEntry * toc
static void StrictNamesCheck(RestoreOptions *ropt)
enum _teSection teSection
#define fatal(...)
static teReqs _tocEntryRequired(TocEntry *te, teSection curSection, ArchiveHandle *AH)
#define pg_log_warning(...)
Definition: pgfnames.c:24

◆ RestoreArchive()

void RestoreArchive ( Archive AH)

Definition at line 344 of file pg_backup_archiver.c.

References _becomeOwner(), _doSetFixedOutputState(), _selectOutputSchema(), _tocEntryRestorePass(), ahprintf(), appendPQExpBuffer(), appendPQExpBufferStr(), _archiveHandle::archiveDumpVersion, _archiveHandle::archiveRemoteVersion, Assert, buildTocEntryArrays(), _tocEntry::catalogId, _archiveHandle::ClonePtr, CommitTransaction(), _restoreOptions::compression, _archiveHandle::compression, ConnectDatabase(), _archiveHandle::connection, _archiveHandle::createDate, _restoreOptions::createDB, createPQExpBuffer(), _archiveHandle::currentTE, _archiveHandle::currSchema, PQExpBufferData::data, _restoreOptions::dataOnly, _restoreOptions::dbname, _tocEntry::desc, destroyPQExpBuffer(), DisconnectDatabase(), DropBlobIfExists(), _restoreOptions::dropSchema, _tocEntry::dropStmt, dumpTimestamp(), fatal, _restoreOptions::filename, free, _tocEntry::hadDumper, _restoreOptions::if_exists, K_VERS_1_3, K_VERS_1_8, Archive::maxRemoteVersion, Archive::minRemoteVersion, _tocEntry::next, _archiveHandle::noTocComments, Archive::numWorkers, CatalogId::oid, ParallelBackupEnd(), ParallelBackupStart(), pending_list_header_init(), pg_free(), pg_log_info, pg_log_warning, pg_strdup(), _restoreOptions::pghost, _restoreOptions::pgport, _archiveHandle::PrepParallelRestorePtr, _tocEntry::prev, _archiveHandle::PrintTocDataPtr, _restoreOptions::promptPassword, _archiveHandle::public, _archiveHandle::ReopenPtr, REQ_DATA, REQ_SCHEMA, _tocEntry::reqs, RESTORE_PASS_ACL, RESTORE_PASS_MAIN, restore_toc_entries_parallel(), restore_toc_entries_postfork(), restore_toc_entries_prefork(), restore_toc_entry(), RestoreOutput(), Archive::ropt, SaveOutput(), SetOutput(), _restoreOptions::single_txn, snprintf, _archiveHandle::stage, STAGE_FINALIZING, STAGE_INITIALIZING, STAGE_PROCESSING, StartTransaction(), _tocEntry::tag, _archiveHandle::toc, _archiveHandle::tocsByDumpId, _restoreOptions::useDB, _restoreOptions::username, Archive::verbose, and _archiveHandle::version.

Referenced by _CloseArchive(), and main().

345 {
346  ArchiveHandle *AH = (ArchiveHandle *) AHX;
347  RestoreOptions *ropt = AH->public.ropt;
348  bool parallel_mode;
349  TocEntry *te;
350  OutputContext sav;
351 
353 
354  /*
355  * If we're going to do parallel restore, there are some restrictions.
356  */
357  parallel_mode = (AH->public.numWorkers > 1 && ropt->useDB);
358  if (parallel_mode)
359  {
360  /* We haven't got round to making this work for all archive formats */
361  if (AH->ClonePtr == NULL || AH->ReopenPtr == NULL)
362  fatal("parallel restore is not supported with this archive file format");
363 
364  /* Doesn't work if the archive represents dependencies as OIDs */
365  if (AH->version < K_VERS_1_8)
366  fatal("parallel restore is not supported with archives made by pre-8.0 pg_dump");
367 
368  /*
369  * It's also not gonna work if we can't reopen the input file, so
370  * let's try that immediately.
371  */
372  AH->ReopenPtr(AH);
373  }
374 
375  /*
376  * Make sure we won't need (de)compression we haven't got
377  */
378 #ifndef HAVE_LIBZ
379  if (AH->compression != 0 && AH->PrintTocDataPtr != NULL)
380  {
381  for (te = AH->toc->next; te != AH->toc; te = te->next)
382  {
383  if (te->hadDumper && (te->reqs & REQ_DATA) != 0)
384  fatal("cannot restore from compressed archive (compression not supported in this installation)");
385  }
386  }
387 #endif
388 
389  /*
390  * Prepare index arrays, so we can assume we have them throughout restore.
391  * It's possible we already did this, though.
392  */
393  if (AH->tocsByDumpId == NULL)
395 
396  /*
397  * If we're using a DB connection, then connect it.
398  */
399  if (ropt->useDB)
400  {
401  pg_log_info("connecting to database for restore");
402  if (AH->version < K_VERS_1_3)
403  fatal("direct database connections are not supported in pre-1.3 archives");
404 
405  /*
406  * We don't want to guess at whether the dump will successfully
407  * restore; allow the attempt regardless of the version of the restore
408  * target.
409  */
410  AHX->minRemoteVersion = 0;
411  AHX->maxRemoteVersion = 9999999;
412 
413  ConnectDatabase(AHX, ropt->dbname,
414  ropt->pghost, ropt->pgport, ropt->username,
415  ropt->promptPassword);
416 
417  /*
418  * If we're talking to the DB directly, don't send comments since they
419  * obscure SQL when displaying errors
420  */
421  AH->noTocComments = 1;
422  }
423 
424  /*
425  * Work out if we have an implied data-only restore. This can happen if
426  * the dump was data only or if the user has used a toc list to exclude
427  * all of the schema data. All we do is look for schema entries - if none
428  * are found then we set the dataOnly flag.
429  *
430  * We could scan for wanted TABLE entries, but that is not the same as
431  * dataOnly. At this stage, it seems unnecessary (6-Mar-2001).
432  */
433  if (!ropt->dataOnly)
434  {
435  int impliedDataOnly = 1;
436 
437  for (te = AH->toc->next; te != AH->toc; te = te->next)
438  {
439  if ((te->reqs & REQ_SCHEMA) != 0)
440  { /* It's schema, and it's wanted */
441  impliedDataOnly = 0;
442  break;
443  }
444  }
445  if (impliedDataOnly)
446  {
447  ropt->dataOnly = impliedDataOnly;
448  pg_log_info("implied data-only restore");
449  }
450  }
451 
452  /*
453  * Setup the output file if necessary.
454  */
455  sav = SaveOutput(AH);
456  if (ropt->filename || ropt->compression)
457  SetOutput(AH, ropt->filename, ropt->compression);
458 
459  ahprintf(AH, "--\n-- PostgreSQL database dump\n--\n\n");
460 
461  if (AH->archiveRemoteVersion)
462  ahprintf(AH, "-- Dumped from database version %s\n",
464  if (AH->archiveDumpVersion)
465  ahprintf(AH, "-- Dumped by pg_dump version %s\n",
466  AH->archiveDumpVersion);
467 
468  ahprintf(AH, "\n");
469 
470  if (AH->public.verbose)
471  dumpTimestamp(AH, "Started on", AH->createDate);
472 
473  if (ropt->single_txn)
474  {
475  if (AH->connection)
476  StartTransaction(AHX);
477  else
478  ahprintf(AH, "BEGIN;\n\n");
479  }
480 
481  /*
482  * Establish important parameter values right away.
483  */
485 
486  AH->stage = STAGE_PROCESSING;
487 
488  /*
489  * Drop the items at the start, in reverse order
490  */
491  if (ropt->dropSchema)
492  {
493  for (te = AH->toc->prev; te != AH->toc; te = te->prev)
494  {
495  AH->currentTE = te;
496 
497  /*
498  * In createDB mode, issue a DROP *only* for the database as a
499  * whole. Issuing drops against anything else would be wrong,
500  * because at this point we're connected to the wrong database.
501  * (The DATABASE PROPERTIES entry, if any, should be treated like
502  * the DATABASE entry.)
503  */
504  if (ropt->createDB)
505  {
506  if (strcmp(te->desc, "DATABASE") != 0 &&
507  strcmp(te->desc, "DATABASE PROPERTIES") != 0)
508  continue;
509  }
510 
511  /* Otherwise, drop anything that's selected and has a dropStmt */
512  if (((te->reqs & (REQ_SCHEMA | REQ_DATA)) != 0) && te->dropStmt)
513  {
514  pg_log_info("dropping %s %s", te->desc, te->tag);
515  /* Select owner and schema as necessary */
516  _becomeOwner(AH, te);
517  _selectOutputSchema(AH, te->namespace);
518 
519  /*
520  * Now emit the DROP command, if the object has one. Note we
521  * don't necessarily emit it verbatim; at this point we add an
522  * appropriate IF EXISTS clause, if the user requested it.
523  */
524  if (*te->dropStmt != '\0')
525  {
526  if (!ropt->if_exists)
527  {
528  /* No --if-exists? Then just use the original */
529  ahprintf(AH, "%s", te->dropStmt);
530  }
531  else
532  {
533  /*
534  * Inject an appropriate spelling of "if exists". For
535  * large objects, we have a separate routine that
536  * knows how to do it, without depending on
537  * te->dropStmt; use that. For other objects we need
538  * to parse the command.
539  */
540  if (strncmp(te->desc, "BLOB", 4) == 0)
541  {
542  DropBlobIfExists(AH, te->catalogId.oid);
543  }
544  else
545  {
546  char *dropStmt = pg_strdup(te->dropStmt);
547  char *dropStmtOrig = dropStmt;
548  PQExpBuffer ftStmt = createPQExpBuffer();
549 
550  /*
551  * Need to inject IF EXISTS clause after ALTER
552  * TABLE part in ALTER TABLE .. DROP statement
553  */
554  if (strncmp(dropStmt, "ALTER TABLE", 11) == 0)
555  {
556  appendPQExpBufferStr(ftStmt,
557  "ALTER TABLE IF EXISTS");
558  dropStmt = dropStmt + 11;
559  }
560 
561  /*
562  * ALTER TABLE..ALTER COLUMN..DROP DEFAULT does
563  * not support the IF EXISTS clause, and therefore
564  * we simply emit the original command for DEFAULT
565  * objects (modulo the adjustment made above).
566  *
567  * Likewise, don't mess with DATABASE PROPERTIES.
568  *
569  * If we used CREATE OR REPLACE VIEW as a means of
570  * quasi-dropping an ON SELECT rule, that should
571  * be emitted unchanged as well.
572  *
573  * For other object types, we need to extract the
574  * first part of the DROP which includes the
575  * object type. Most of the time this matches
576  * te->desc, so search for that; however for the
577  * different kinds of CONSTRAINTs, we know to
578  * search for hardcoded "DROP CONSTRAINT" instead.
579  */
580  if (strcmp(te->desc, "DEFAULT") == 0 ||
581  strcmp(te->desc, "DATABASE PROPERTIES") == 0 ||
582  strncmp(dropStmt, "CREATE OR REPLACE VIEW", 22) == 0)
583  appendPQExpBufferStr(ftStmt, dropStmt);
584  else
585  {
586  char buffer[40];
587  char *mark;
588 
589  if (strcmp(te->desc, "CONSTRAINT") == 0 ||
590  strcmp(te->desc, "CHECK CONSTRAINT") == 0 ||
591  strcmp(te->desc, "FK CONSTRAINT") == 0)
592  strcpy(buffer, "DROP CONSTRAINT");
593  else
594  snprintf(buffer, sizeof(buffer), "DROP %s",
595  te->desc);
596 
597  mark = strstr(dropStmt, buffer);
598 
599  if (mark)
600  {
601  *mark = '\0';
602  appendPQExpBuffer(ftStmt, "%s%s IF EXISTS%s",
603  dropStmt, buffer,
604  mark + strlen(buffer));
605  }
606  else
607  {
608  /* complain and emit unmodified command */
609  pg_log_warning("could not find where to insert IF EXISTS in statement \"%s\"",
610  dropStmtOrig);
611  appendPQExpBufferStr(ftStmt, dropStmt);
612  }
613  }
614 
615  ahprintf(AH, "%s", ftStmt->data);
616 
617  destroyPQExpBuffer(ftStmt);
618  pg_free(dropStmtOrig);
619  }
620  }
621  }
622  }
623  }
624 
625  /*
626  * _selectOutputSchema may have set currSchema to reflect the effect
627  * of a "SET search_path" command it emitted. However, by now we may
628  * have dropped that schema; or it might not have existed in the first
629  * place. In either case the effective value of search_path will not
630  * be what we think. Forcibly reset currSchema so that we will
631  * re-establish the search_path setting when needed (after creating
632  * the schema).
633  *
634  * If we treated users as pg_dump'able objects then we'd need to reset
635  * currUser here too.
636  */
637  if (AH->currSchema)
638  free(AH->currSchema);
639  AH->currSchema = NULL;
640  }
641 
642  if (parallel_mode)
643  {
644  /*
645  * In parallel mode, turn control over to the parallel-restore logic.
646  */
647  ParallelState *pstate;
648  TocEntry pending_list;
649 
650  /* The archive format module may need some setup for this */
651  if (AH->PrepParallelRestorePtr)
652  AH->PrepParallelRestorePtr(AH);
653 
654  pending_list_header_init(&pending_list);
655 
656  /* This runs PRE_DATA items and then disconnects from the database */
657  restore_toc_entries_prefork(AH, &pending_list);
658  Assert(AH->connection == NULL);
659 
660  /* ParallelBackupStart() will actually fork the processes */
661  pstate = ParallelBackupStart(AH);
662  restore_toc_entries_parallel(AH, pstate, &pending_list);
663  ParallelBackupEnd(AH, pstate);
664 
665  /* reconnect the master and see if we missed something */
666  restore_toc_entries_postfork(AH, &pending_list);
667  Assert(AH->connection != NULL);
668  }
669  else
670  {
671  /*
672  * In serial mode, process everything in three phases: normal items,
673  * then ACLs, then matview refresh items. We might be able to skip
674  * one or both extra phases in some cases, eg data-only restores.
675  */
676  bool haveACL = false;
677  bool haveRefresh = false;
678 
679  for (te = AH->toc->next; te != AH->toc; te = te->next)
680  {
681  if ((te->reqs & (REQ_SCHEMA | REQ_DATA)) == 0)
682  continue; /* ignore if not to be dumped at all */
683 
684  switch (_tocEntryRestorePass(te))
685  {
686  case RESTORE_PASS_MAIN:
687  (void) restore_toc_entry(AH, te, false);
688  break;
689  case RESTORE_PASS_ACL:
690  haveACL = true;
691  break;
692  case RESTORE_PASS_REFRESH:
693  haveRefresh = true;
694  break;
695  }
696  }
697 
698  if (haveACL)
699  {
700  for (te = AH->toc->next; te != AH->toc; te = te->next)
701  {
702  if ((te->reqs & (REQ_SCHEMA | REQ_DATA)) != 0 &&
704  (void) restore_toc_entry(AH, te, false);
705  }
706  }
707 
708  if (haveRefresh)
709  {
710  for (te = AH->toc->next; te != AH->toc; te = te->next)
711  {
712  if ((te->reqs & (REQ_SCHEMA | REQ_DATA)) != 0 &&
713  _tocEntryRestorePass(te) == RESTORE_PASS_REFRESH)
714  (void) restore_toc_entry(AH, te, false);
715  }
716  }
717  }
718 
719  if (ropt->single_txn)
720  {
721  if (AH->connection)
722  CommitTransaction(AHX);
723  else
724  ahprintf(AH, "COMMIT;\n\n");
725  }
726 
727  if (AH->public.verbose)
728  dumpTimestamp(AH, "Completed on", time(NULL));
729 
730  ahprintf(AH, "--\n-- PostgreSQL database dump complete\n--\n\n");
731 
732  /*
733  * Clean up & we're done.
734  */
735  AH->stage = STAGE_FINALIZING;
736 
737  if (ropt->filename || ropt->compression)
738  RestoreOutput(AH, sav);
739 
740  if (ropt->useDB)
742 }
struct _tocEntry * next
ReopenPtrType ReopenPtr
struct _tocEntry * currentTE
static void dumpTimestamp(ArchiveHandle *AH, const char *msg, time_t tim)
void DropBlobIfExists(ArchiveHandle *AH, Oid oid)
Definition: pg_backup_db.c:656
RestoreOptions * ropt
Definition: pg_backup.h:183
static void restore_toc_entries_postfork(ArchiveHandle *AH, TocEntry *pending_list)
CatalogId catalogId
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
char * username
Definition: pg_backup.h:113
static void SetOutput(ArchiveHandle *AH, const char *filename, int compression)
static void restore_toc_entries_parallel(ArchiveHandle *AH, ParallelState *pstate, TocEntry *pending_list)
const char * filename
Definition: pg_backup.h:83
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
static int restore_toc_entry(ArchiveHandle *AH, TocEntry *te, bool is_parallel)
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
struct _tocEntry * toc
#define K_VERS_1_8
static RestorePass _tocEntryRestorePass(TocEntry *te)
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static void _doSetFixedOutputState(ArchiveHandle *AH)
static void buildTocEntryArrays(ArchiveHandle *AH)
static void _becomeOwner(ArchiveHandle *AH, TocEntry *te)
#define K_VERS_1_3
void DisconnectDatabase(Archive *AHX)
Definition: pg_backup_db.c:337
static void pending_list_header_init(TocEntry *l)
int ahprintf(ArchiveHandle *AH, const char *fmt,...)
ParallelState * ParallelBackupStart(ArchiveHandle *AH)
Definition: parallel.c:906
void ParallelBackupEnd(ArchiveHandle *AH, ParallelState *pstate)
Definition: parallel.c:1072
struct _tocEntry * prev
ArchiverStage stage
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int verbose
Definition: pg_backup.h:185
PrintTocDataPtrType PrintTocDataPtr
static OutputContext SaveOutput(ArchiveHandle *AH)
static void StartTransaction(void)
Definition: xact.c:1886
#define free(a)
Definition: header.h:65
#define Assert(condition)
Definition: c.h:739
PrepParallelRestorePtrType PrepParallelRestorePtr
static void _selectOutputSchema(ArchiveHandle *AH, const char *schemaName)
#define fatal(...)
int numWorkers
Definition: pg_backup.h:193
void pg_free(void *ptr)
Definition: fe_memutils.c:105
static void RestoreOutput(ArchiveHandle *AH, OutputContext savedContext)
struct _tocEntry ** tocsByDumpId
ClonePtrType ClonePtr
static void CommitTransaction(void)
Definition: xact.c:2045
#define pg_log_warning(...)
Definition: pgfnames.c:24
static void restore_toc_entries_prefork(ArchiveHandle *AH, TocEntry *pending_list)
#define snprintf
Definition: port.h:192
#define pg_log_info(...)
Definition: logging.h:87
void ConnectDatabase(Archive *AH, const char *dbname, const char *pghost, const char *pgport, const char *username, trivalue prompt_password)
Definition: pg_backup_db.c:236
trivalue promptPassword
Definition: pg_backup.h:115

◆ SetArchiveOptions()

void SetArchiveOptions ( Archive AH,
DumpOptions dopt,
RestoreOptions ropt 
)

Definition at line 276 of file pg_backup_archiver.c.

References Archive::dopt, dumpOptionsFromRestoreOptions(), and Archive::ropt.

Referenced by _CloseArchive(), and main().

277 {
278  /* Caller can omit dump options, in which case we synthesize them */
279  if (dopt == NULL && ropt != NULL)
280  dopt = dumpOptionsFromRestoreOptions(ropt);
281 
282  /* Save options for later access */
283  AH->dopt = dopt;
284  AH->ropt = ropt;
285 }
RestoreOptions * ropt
Definition: pg_backup.h:183
DumpOptions * dopt
Definition: pg_backup.h:182
DumpOptions * dumpOptionsFromRestoreOptions(RestoreOptions *ropt)

◆ SortTocFromFile()

void SortTocFromFile ( Archive AHX)

Definition at line 1367 of file pg_backup_archiver.c.

References _moveBefore(), buf, fatal, getTocEntryByDumpId(), _restoreOptions::idWanted, _archiveHandle::maxDumpId, PG_BINARY_R, pg_log_warning, pg_malloc0(), _archiveHandle::public, Archive::ropt, _archiveHandle::toc, and _restoreOptions::tocFile.

Referenced by main().

1368 {
1369  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1370  RestoreOptions *ropt = AH->public.ropt;
1371  FILE *fh;
1372  char buf[100];
1373  bool incomplete_line;
1374 
1375  /* Allocate space for the 'wanted' array, and init it */
1376  ropt->idWanted = (bool *) pg_malloc0(sizeof(bool) * AH->maxDumpId);
1377 
1378  /* Setup the file */
1379  fh = fopen(ropt->tocFile, PG_BINARY_R);
1380  if (!fh)
1381  fatal("could not open TOC file \"%s\": %m", ropt->tocFile);
1382 
1383  incomplete_line = false;
1384  while (fgets(buf, sizeof(buf), fh) != NULL)
1385  {
1386  bool prev_incomplete_line = incomplete_line;
1387  int buflen;
1388  char *cmnt;
1389  char *endptr;
1390  DumpId id;
1391  TocEntry *te;
1392 
1393  /*
1394  * Some lines in the file might be longer than sizeof(buf). This is
1395  * no problem, since we only care about the leading numeric ID which
1396  * can be at most a few characters; but we have to skip continuation
1397  * bufferloads when processing a long line.
1398  */
1399  buflen = strlen(buf);
1400  if (buflen > 0 && buf[buflen - 1] == '\n')
1401  incomplete_line = false;
1402  else
1403  incomplete_line = true;
1404  if (prev_incomplete_line)
1405  continue;
1406 
1407  /* Truncate line at comment, if any */
1408  cmnt = strchr(buf, ';');
1409  if (cmnt != NULL)
1410  cmnt[0] = '\0';
1411 
1412  /* Ignore if all blank */
1413  if (strspn(buf, " \t\r\n") == strlen(buf))
1414  continue;
1415 
1416  /* Get an ID, check it's valid and not already seen */
1417  id = strtol(buf, &endptr, 10);
1418  if (endptr == buf || id <= 0 || id > AH->maxDumpId ||
1419  ropt->idWanted[id - 1])
1420  {
1421  pg_log_warning("line ignored: %s", buf);
1422  continue;
1423  }
1424 
1425  /* Find TOC entry */
1426  te = getTocEntryByDumpId(AH, id);
1427  if (!te)
1428  fatal("could not find entry for ID %d",
1429  id);
1430 
1431  /* Mark it wanted */
1432  ropt->idWanted[id - 1] = true;
1433 
1434  /*
1435  * Move each item to the end of the list as it is selected, so that
1436  * they are placed in the desired order. Any unwanted items will end
1437  * up at the front of the list, which may seem unintuitive but it's
1438  * what we need. In an ordinary serial restore that makes no
1439  * difference, but in a parallel restore we need to mark unrestored
1440  * items' dependencies as satisfied before we start examining
1441  * restorable items. Otherwise they could have surprising
1442  * side-effects on the order in which restorable items actually get
1443  * restored.
1444  */
1445  _moveBefore(AH, AH->toc, te);
1446  }
1447 
1448  if (fclose(fh) != 0)
1449  fatal("could not close TOC file: %m");
1450 }
int DumpId
Definition: pg_backup.h:234
RestoreOptions * ropt
Definition: pg_backup.h:183
#define PG_BINARY_R
Definition: c.h:1224
bool * idWanted
Definition: pg_backup.h:122
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
struct _tocEntry * toc
static char * buf
Definition: pg_test_fsync.c:67
TocEntry * getTocEntryByDumpId(ArchiveHandle *AH, DumpId id)
static void _moveBefore(ArchiveHandle *AH, TocEntry *pos, TocEntry *te)
#define fatal(...)
#define pg_log_warning(...)
Definition: pgfnames.c:24
char * tocFile
Definition: pg_backup.h:93

◆ StartBlob()

int StartBlob ( Archive AH,
Oid  oid 
)

Definition at line 1223 of file pg_backup_archiver.c.

References _archiveHandle::currToc, fatal, and _archiveHandle::StartBlobPtr.

Referenced by dumpBlobs().

1224 {
1225  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1226 
1227  if (!AH->StartBlobPtr)
1228  fatal("large-object output not supported in chosen format");
1229 
1230  AH->StartBlobPtr(AH, AH->currToc, oid);
1231 
1232  return 1;
1233 }
StartBlobPtrType StartBlobPtr
struct _tocEntry * currToc
#define fatal(...)

◆ WriteData()

void WriteData ( Archive AH,
const void *  data,
size_t  dLen 
)

Definition at line 1040 of file pg_backup_archiver.c.

References _archiveHandle::currToc, fatal, and _archiveHandle::WriteDataPtr.

Referenced by archprintf(), archputs(), dumpBlobs(), and dumpTableData_copy().

1041 {
1042  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1043 
1044  if (!AH->currToc)
1045  fatal("internal error -- WriteData cannot be called outside the context of a DataDumper routine");
1046 
1047  AH->WriteDataPtr(AH, data, dLen);
1048 }
struct _tocEntry * currToc
WriteDataPtrType WriteDataPtr
#define fatal(...)