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 ArchiveEntry (Archive *AHX, CatalogId catalogId, DumpId dumpId, const char *tag, const char *namespace, const char *tablespace, const char *owner, bool withOids, const char *desc, teSection section, const char *defn, const char *dropStmt, const char *copyStmt, const DumpId *deps, int nDeps, DataDumperPtr dumpFn, void *dumpArg)
 
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

◆ ArchiveFormat

◆ ArchiveMode

◆ DataDumperPtr

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

Definition at line 232 of file pg_backup.h.

◆ DumpId

Definition at line 230 of file pg_backup.h.

◆ DumpOptions

◆ RestoreOptions

◆ SetupWorkerPtrType

typedef void(* SetupWorkerPtrType) (Archive *AH)

Definition at line 234 of file pg_backup.h.

◆ teSection

◆ 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

◆ ArchiveEntry()

void ArchiveEntry ( Archive AHX,
CatalogId  catalogId,
DumpId  dumpId,
const char *  tag,
const char *  namespace,
const char *  tablespace,
const char *  owner,
bool  withOids,
const char *  desc,
teSection  section,
const char *  defn,
const char *  dropStmt,
const char *  copyStmt,
const DumpId deps,
int  nDeps,
DataDumperPtr  dumpFn,
void *  dumpArg 
)

Definition at line 1053 of file pg_backup_archiver.c.

References _archiveHandle::ArchiveEntryPtr, _tocEntry::catalogId, _tocEntry::dataDumper, _tocEntry::dataDumperArg, _tocEntry::dependencies, _tocEntry::dumpId, _tocEntry::formatData, _tocEntry::hadDumper, _archiveHandle::maxDumpId, _tocEntry::nDeps, _tocEntry::next, pg_malloc(), pg_malloc0(), pg_strdup(), _tocEntry::prev, _tocEntry::section, _tocEntry::tag, _archiveHandle::toc, _archiveHandle::tocCount, and true.

Referenced by dumpAccessMethod(), dumpACL(), dumpAgg(), dumpAttrDef(), dumpBaseType(), dumpBlob(), dumpCast(), dumpCollation(), dumpComment(), dumpCompositeType(), dumpCompositeTypeColComments(), dumpConstraint(), dumpConversion(), dumpDatabase(), dumpDefaultACL(), dumpDomain(), dumpDumpableObject(), dumpEncoding(), dumpEnumType(), dumpEventTrigger(), dumpExtension(), dumpForeignDataWrapper(), dumpForeignServer(), dumpFunc(), dumpIndex(), dumpNamespace(), dumpOpclass(), dumpOpfamily(), dumpOpr(), dumpPolicy(), dumpProcLang(), dumpPublication(), dumpPublicationTable(), dumpRangeType(), dumpRule(), dumpSecLabel(), dumpSequence(), dumpSequenceData(), dumpShellType(), dumpStatisticsExt(), dumpStdStrings(), dumpSubscription(), dumpTableComment(), dumpTableData(), dumpTableSchema(), dumpTableSecLabel(), dumpTransform(), dumpTrigger(), dumpTSConfig(), dumpTSDictionary(), dumpTSParser(), dumpTSTemplate(), dumpUndefinedType(), dumpUserMappings(), and refreshMatViewData().

1064 {
1065  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1066  TocEntry *newToc;
1067 
1068  newToc = (TocEntry *) pg_malloc0(sizeof(TocEntry));
1069 
1070  AH->tocCount++;
1071  if (dumpId > AH->maxDumpId)
1072  AH->maxDumpId = dumpId;
1073 
1074  newToc->prev = AH->toc->prev;
1075  newToc->next = AH->toc;
1076  AH->toc->prev->next = newToc;
1077  AH->toc->prev = newToc;
1078 
1079  newToc->catalogId = catalogId;
1080  newToc->dumpId = dumpId;
1081  newToc->section = section;
1082 
1083  newToc->tag = pg_strdup(tag);
1084  newToc->namespace = namespace ? pg_strdup(namespace) : NULL;
1085  newToc->tablespace = tablespace ? pg_strdup(tablespace) : NULL;
1086  newToc->owner = pg_strdup(owner);
1087  newToc->withOids = withOids;
1088  newToc->desc = pg_strdup(desc);
1089  newToc->defn = pg_strdup(defn);
1090  newToc->dropStmt = pg_strdup(dropStmt);
1091  newToc->copyStmt = copyStmt ? pg_strdup(copyStmt) : NULL;
1092 
1093  if (nDeps > 0)
1094  {
1095  newToc->dependencies = (DumpId *) pg_malloc(nDeps * sizeof(DumpId));
1096  memcpy(newToc->dependencies, deps, nDeps * sizeof(DumpId));
1097  newToc->nDeps = nDeps;
1098  }
1099  else
1100  {
1101  newToc->dependencies = NULL;
1102  newToc->nDeps = 0;
1103  }
1104 
1105  newToc->dataDumper = dumpFn;
1106  newToc->dataDumperArg = dumpArg;
1107  newToc->hadDumper = dumpFn ? true : false;
1108 
1109  newToc->formatData = NULL;
1110 
1111  if (AH->ArchiveEntryPtr != NULL)
1112  AH->ArchiveEntryPtr(AH, newToc);
1113 }
struct _tocEntry * next
int DumpId
Definition: pg_backup.h:230
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
CatalogId catalogId
void * dataDumperArg
DataDumperPtr dataDumper
teSection section
#define true
Definition: c.h:261
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
struct _tocEntry * toc
DumpId * dependencies
char * tablespace
Definition: pgbench.c:146
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
struct _tocEntry * prev
ArchiveEntryPtrType ArchiveEntryPtr

◆ archprintf()

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

◆ archputs()

void archputs ( const char *  s,
Archive AH 
)

Definition at line 1471 of file pg_backup_archiver.c.

References WriteData().

Referenced by dumpTableData_insert().

1472 {
1473  WriteData(AH, s, strlen(s));
1474  return;
1475 }
void WriteData(Archive *AHX, const void *data, size_t dLen)

◆ CloseArchive()

void CloseArchive ( Archive AH)

Definition at line 235 of file pg_backup_archiver.c.

References _archiveHandle::ClosePtr, exit_horribly(), GZCLOSE, _archiveHandle::gzOut, modulename, _archiveHandle::OF, and strerror().

Referenced by main().

236 {
237  int res = 0;
238  ArchiveHandle *AH = (ArchiveHandle *) AHX;
239 
240  AH->ClosePtr(AH);
241 
242  /* Close the output */
243  if (AH->gzOut)
244  res = GZCLOSE(AH->OF);
245  else if (AH->OF != stdout)
246  res = fclose(AH->OF);
247 
248  if (res != 0)
249  exit_horribly(modulename, "could not close output file: %s\n",
250  strerror(errno));
251 }
#define GZCLOSE(fh)
void exit_horribly(const char *modulename, const char *fmt,...)
const char * strerror(int errnum)
Definition: strerror.c:19
ClosePtrType ClosePtr
static const char * modulename

◆ ConnectDatabase()

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

Definition at line 246 of file pg_backup_db.c.

References _check_database_version(), _archiveHandle::connection, CONNECTION_BAD, dbname, exit_horribly(), free, modulename, notice_processor(), password, pg_strdup(), pghost, pgport, 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().

252 {
253  ArchiveHandle *AH = (ArchiveHandle *) AHX;
254  char *password;
255  char passbuf[100];
256  bool new_pass;
257 
258  if (AH->connection)
259  exit_horribly(modulename, "already connected to a database\n");
260 
261  password = AH->savedPassword;
262 
263  if (prompt_password == TRI_YES && password == NULL)
264  {
265  simple_prompt("Password: ", passbuf, sizeof(passbuf), false);
266  password = passbuf;
267  }
268  AH->promptPassword = prompt_password;
269 
270  /*
271  * Start the connection. Loop until we have a password if requested by
272  * backend.
273  */
274  do
275  {
276  const char *keywords[7];
277  const char *values[7];
278 
279  keywords[0] = "host";
280  values[0] = pghost;
281  keywords[1] = "port";
282  values[1] = pgport;
283  keywords[2] = "user";
284  values[2] = username;
285  keywords[3] = "password";
286  values[3] = password;
287  keywords[4] = "dbname";
288  values[4] = dbname;
289  keywords[5] = "fallback_application_name";
290  values[5] = progname;
291  keywords[6] = NULL;
292  values[6] = NULL;
293 
294  new_pass = false;
295  AH->connection = PQconnectdbParams(keywords, values, true);
296 
297  if (!AH->connection)
298  exit_horribly(modulename, "failed to connect to database\n");
299 
300  if (PQstatus(AH->connection) == CONNECTION_BAD &&
302  password == NULL &&
303  prompt_password != TRI_NO)
304  {
305  PQfinish(AH->connection);
306  simple_prompt("Password: ", passbuf, sizeof(passbuf), false);
307  password = passbuf;
308  new_pass = true;
309  }
310  } while (new_pass);
311 
312  /* check to see that the backend connection was successfully made */
313  if (PQstatus(AH->connection) == CONNECTION_BAD)
314  exit_horribly(modulename, "connection to database \"%s\" failed: %s",
315  PQdb(AH->connection) ? PQdb(AH->connection) : "",
317 
318  /*
319  * We want to remember connection's actual password, whether or not we got
320  * it by prompting. So we don't just store the password variable.
321  */
323  {
324  if (AH->savedPassword)
325  free(AH->savedPassword);
327  }
328 
329  /* check for version mismatch */
331 
333 
334  /* arrange for SIGINT to issue a query cancel on this connection */
336 }
static char password[100]
Definition: streamutil.c:45
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6106
PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg)
Definition: fe-connect.c:6282
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3629
const char * progname
Definition: pg_standby.c:37
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:529
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:5990
static void notice_processor(void *arg, const char *message)
Definition: pg_backup_db.c:380
char * pghost
Definition: pgbench.c:180
static const char * modulename
Definition: pg_backup_db.c:29
static char * username
Definition: initdb.c:132
#define free(a)
Definition: header.h:65
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:5974
char * dbname
Definition: streamutil.c:42
void exit_horribly(const char *modulename, const char *fmt,...)
static Datum values[MAXATTR]
Definition: bootstrap.c:164
static void _check_database_version(ArchiveHandle *AH)
Definition: pg_backup_db.c:36
int PQconnectionUsedPassword(const PGconn *conn)
Definition: fe-connect.c:6155
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:6140
void set_archive_cancel_info(ArchiveHandle *AH, PGconn *conn)
Definition: parallel.c:744
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6053
char * pgport
Definition: pgbench.c:181

◆ CreateArchive()

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

Definition at line 212 of file pg_backup_archiver.c.

References _allocAH().

Referenced by main().

216 {
217  ArchiveHandle *AH = _allocAH(FileSpec, fmt, compression, dosync,
218  mode, setupDumpWorker);
219 
220  return (Archive *) AH;
221 }
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:94
static void setupDumpWorker(Archive *AHX)
Definition: pg_dump.c:1153

◆ DisconnectDatabase()

void DisconnectDatabase ( Archive AHX)

Definition at line 343 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().

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

◆ dumpOptionsFromRestoreOptions()

DumpOptions* dumpOptionsFromRestoreOptions ( RestoreOptions ropt)

Definition at line 154 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_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().

155 {
156  DumpOptions *dopt = NewDumpOptions();
157 
158  /* this is the inverse of what's at the end of pg_dump.c's main() */
159  dopt->outputClean = ropt->dropSchema;
160  dopt->dataOnly = ropt->dataOnly;
161  dopt->schemaOnly = ropt->schemaOnly;
162  dopt->if_exists = ropt->if_exists;
163  dopt->column_inserts = ropt->column_inserts;
164  dopt->dumpSections = ropt->dumpSections;
165  dopt->aclsSkip = ropt->aclsSkip;
166  dopt->outputSuperuser = ropt->superuser;
167  dopt->outputCreateDB = ropt->createDB;
168  dopt->outputNoOwner = ropt->noOwner;
169  dopt->outputNoTablespaces = ropt->noTablespace;
170  dopt->disable_triggers = ropt->disable_triggers;
171  dopt->use_setsessauth = ropt->use_setsessauth;
172 
174  dopt->dump_inserts = ropt->dump_inserts;
175  dopt->no_publications = ropt->no_publications;
177  dopt->no_subscriptions = ropt->no_subscriptions;
178  dopt->lockWaitTimeout = ropt->lockWaitTimeout;
181  dopt->sequence_data = ropt->sequence_data;
182 
183  return dopt;
184 }
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:151
int no_publications
Definition: pg_backup.h:150
DumpOptions * NewDumpOptions(void)
int sequence_data
Definition: pg_backup.h:172
const char * lockWaitTimeout
Definition: pg_backup.h:88
int use_setsessauth
Definition: pg_backup.h:158
int disable_dollar_quoting
Definition: pg_backup.h:145
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:89
int no_security_labels
Definition: pg_backup.h:78
int dumpSections
Definition: pg_backup.h:140
int enable_row_security
Definition: pg_backup.h:122
int dump_inserts
Definition: pg_backup.h:146
int outputNoOwner
Definition: pg_backup.h:169
int no_security_labels
Definition: pg_backup.h:149
int no_subscriptions
Definition: pg_backup.h:79
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 outputNoTablespaces
Definition: pg_backup.h:157
int no_publications
Definition: pg_backup.h:77
int disable_triggers
Definition: pg_backup.h:66

◆ EndBlob()

int EndBlob ( Archive AH,
Oid  oid 
)

Definition at line 1246 of file pg_backup_archiver.c.

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

Referenced by dumpBlobs().

1247 {
1248  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1249 
1250  if (AH->EndBlobPtr)
1251  AH->EndBlobPtr(AH, AH->currToc, oid);
1252 
1253  return 1;
1254 }
struct _tocEntry * currToc
EndBlobPtrType EndBlobPtr

◆ GetConnection()

PGconn* GetConnection ( Archive AHX)

Definition at line 372 of file pg_backup_db.c.

References _archiveHandle::connection.

373 {
374  ArchiveHandle *AH = (ArchiveHandle *) AHX;
375 
376  return AH->connection;
377 }

◆ InitDumpOptions()

void InitDumpOptions ( DumpOptions opts)

Definition at line 141 of file pg_backup_archiver.c.

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

Referenced by main(), and NewDumpOptions().

142 {
143  memset(opts, 0, sizeof(DumpOptions));
144  /* set any fields that shouldn't default to zeroes */
145  opts->include_everything = true;
147 }
bool include_everything
Definition: pg_backup.h:163
int dumpSections
Definition: pg_backup.h:140

◆ NewDumpOptions()

DumpOptions* NewDumpOptions ( void  )

Definition at line 129 of file pg_backup_archiver.c.

References InitDumpOptions(), and pg_malloc().

Referenced by dumpOptionsFromRestoreOptions().

130 {
131  DumpOptions *opts = (DumpOptions *) pg_malloc(sizeof(DumpOptions));
132 
133  InitDumpOptions(opts);
134  return opts;
135 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
void InitDumpOptions(DumpOptions *opts)

◆ NewRestoreOptions()

RestoreOptions* NewRestoreOptions ( void  )

Definition at line 958 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().

959 {
960  RestoreOptions *opts;
961 
962  opts = (RestoreOptions *) pg_malloc0(sizeof(RestoreOptions));
963 
964  /* set any fields that shouldn't default to zeroes */
965  opts->format = archUnknown;
966  opts->promptPassword = TRI_DEFAULT;
968 
969  return opts;
970 }
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
trivalue promptPassword
Definition: pg_backup.h:114

◆ OpenArchive()

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

Definition at line 226 of file pg_backup_archiver.c.

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

Referenced by main().

227 {
228  ArchiveHandle *AH = _allocAH(FileSpec, fmt, 0, true, archModeRead, setupRestoreWorker);
229 
230  return (Archive *) AH;
231 }
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 1117 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, pg_strdup(), PGDUMP_STRFTIME_FMT, _archiveHandle::public, replace_line_endings(), REQ_DATA, REQ_SCHEMA, RestoreOutput(), Archive::ropt, 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().

1118 {
1119  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1120  RestoreOptions *ropt = AH->public.ropt;
1121  TocEntry *te;
1122  teSection curSection;
1123  OutputContext sav;
1124  const char *fmtName;
1125  char stamp_str[64];
1126 
1127  sav = SaveOutput(AH);
1128  if (ropt->filename)
1129  SetOutput(AH, ropt->filename, 0 /* no compression */ );
1130 
1131  if (strftime(stamp_str, sizeof(stamp_str), PGDUMP_STRFTIME_FMT,
1132  localtime(&AH->createDate)) == 0)
1133  strcpy(stamp_str, "[unknown]");
1134 
1135  ahprintf(AH, ";\n; Archive created at %s\n", stamp_str);
1136  ahprintf(AH, "; dbname: %s\n; TOC Entries: %d\n; Compression: %d\n",
1138  AH->tocCount, AH->compression);
1139 
1140  switch (AH->format)
1141  {
1142  case archCustom:
1143  fmtName = "CUSTOM";
1144  break;
1145  case archDirectory:
1146  fmtName = "DIRECTORY";
1147  break;
1148  case archTar:
1149  fmtName = "TAR";
1150  break;
1151  default:
1152  fmtName = "UNKNOWN";
1153  }
1154 
1155  ahprintf(AH, "; Dump Version: %d.%d-%d\n",
1157  ahprintf(AH, "; Format: %s\n", fmtName);
1158  ahprintf(AH, "; Integer: %d bytes\n", (int) AH->intSize);
1159  ahprintf(AH, "; Offset: %d bytes\n", (int) AH->offSize);
1160  if (AH->archiveRemoteVersion)
1161  ahprintf(AH, "; Dumped from database version: %s\n",
1162  AH->archiveRemoteVersion);
1163  if (AH->archiveDumpVersion)
1164  ahprintf(AH, "; Dumped by pg_dump version: %s\n",
1165  AH->archiveDumpVersion);
1166 
1167  ahprintf(AH, ";\n;\n; Selected TOC Entries:\n;\n");
1168 
1169  curSection = SECTION_PRE_DATA;
1170  for (te = AH->toc->next; te != AH->toc; te = te->next)
1171  {
1172  if (te->section != SECTION_NONE)
1173  curSection = te->section;
1174  if (ropt->verbose ||
1175  (_tocEntryRequired(te, curSection, ropt) & (REQ_SCHEMA | REQ_DATA)) != 0)
1176  {
1177  char *sanitized_name;
1178  char *sanitized_schema;
1179  char *sanitized_owner;
1180 
1181  /*
1182  * As in _printTocEntry(), sanitize strings that might contain
1183  * newlines, to ensure that each logical output line is in fact
1184  * one physical output line. This prevents confusion when the
1185  * file is read by "pg_restore -L". Note that we currently don't
1186  * bother to quote names, meaning that the name fields aren't
1187  * automatically parseable. "pg_restore -L" doesn't care because
1188  * it only examines the dumpId field, but someday we might want to
1189  * try harder.
1190  */
1191  sanitized_name = replace_line_endings(te->tag);
1192  if (te->namespace)
1193  sanitized_schema = replace_line_endings(te->namespace);
1194  else
1195  sanitized_schema = pg_strdup("-");
1196  sanitized_owner = replace_line_endings(te->owner);
1197 
1198  ahprintf(AH, "%d; %u %u %s %s %s %s\n", te->dumpId,
1199  te->catalogId.tableoid, te->catalogId.oid,
1200  te->desc, sanitized_schema, sanitized_name,
1201  sanitized_owner);
1202 
1203  free(sanitized_name);
1204  free(sanitized_schema);
1205  free(sanitized_owner);
1206  }
1207  if (ropt->verbose && te->nDeps > 0)
1208  {
1209  int i;
1210 
1211  ahprintf(AH, ";\tdepends on:");
1212  for (i = 0; i < te->nDeps; i++)
1213  ahprintf(AH, " %d", te->dependencies[i]);
1214  ahprintf(AH, "\n");
1215  }
1216  }
1217 
1218  /* Enforce strict names checking */
1219  if (ropt->strict_names)
1220  StrictNamesCheck(ropt);
1221 
1222  if (ropt->filename)
1223  RestoreOutput(AH, sav);
1224 }
struct _tocEntry * next
Oid tableoid
Definition: pg_backup.h:226
RestoreOptions * ropt
Definition: pg_backup.h:182
CatalogId catalogId
static void SetOutput(ArchiveHandle *AH, const char *filename, int compression)
teSection section
const char * filename
Definition: pg_backup.h:82
static char * replace_line_endings(const char *str)
struct _tocEntry * toc
DumpId * dependencies
#define ARCHIVE_REV(version)
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
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)
static teReqs _tocEntryRequired(TocEntry *te, teSection curSection, RestoreOptions *ropt)
int i

◆ ProcessArchiveRestoreOptions()

void ProcessArchiveRestoreOptions ( Archive AH)

Definition at line 268 of file pg_backup_archiver.c.

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

Referenced by main().

269 {
270  ArchiveHandle *AH = (ArchiveHandle *) AHX;
271  RestoreOptions *ropt = AH->public.ropt;
272  TocEntry *te;
273  teSection curSection;
274 
275  /* Decide which TOC entries will be dumped/restored, and mark them */
276  curSection = SECTION_PRE_DATA;
277  for (te = AH->toc->next; te != AH->toc; te = te->next)
278  {
279  /*
280  * When writing an archive, we also take this opportunity to check
281  * that we have generated the entries in a sane order that respects
282  * the section divisions. When reading, don't complain, since buggy
283  * old versions of pg_dump might generate out-of-order archives.
284  */
285  if (AH->mode != archModeRead)
286  {
287  switch (te->section)
288  {
289  case SECTION_NONE:
290  /* ok to be anywhere */
291  break;
292  case SECTION_PRE_DATA:
293  if (curSection != SECTION_PRE_DATA)
295  "WARNING: archive items not in correct section order\n");
296  break;
297  case SECTION_DATA:
298  if (curSection == SECTION_POST_DATA)
300  "WARNING: archive items not in correct section order\n");
301  break;
302  case SECTION_POST_DATA:
303  /* ok no matter which section we were in */
304  break;
305  default:
306  exit_horribly(modulename, "unexpected section code %d\n",
307  (int) te->section);
308  break;
309  }
310  }
311 
312  if (te->section != SECTION_NONE)
313  curSection = te->section;
314 
315  te->reqs = _tocEntryRequired(te, curSection, ropt);
316  }
317 
318  /* Enforce strict names checking */
319  if (ropt->strict_names)
320  StrictNamesCheck(ropt);
321 }
struct _tocEntry * next
RestoreOptions * ropt
Definition: pg_backup.h:182
teSection section
struct _tocEntry * toc
static void StrictNamesCheck(RestoreOptions *ropt)
enum _teSection teSection
void write_msg(const char *modulename, const char *fmt,...)
void exit_horribly(const char *modulename, const char *fmt,...)
static teReqs _tocEntryRequired(TocEntry *te, teSection curSection, RestoreOptions *ropt)
static const char * modulename

◆ RestoreArchive()

void RestoreArchive ( Archive AH)

Definition at line 325 of file pg_backup_archiver.c.

References _becomeOwner(), _doSetFixedOutputState(), _selectOutputSchema(), _tocEntryRestorePass(), ahlog(), ahprintf(), appendPQExpBuffer(), appendPQExpBufferStr(), _archiveHandle::archiveDumpVersion, _archiveHandle::archiveRemoteVersion, Assert, buffer, 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(), exit_horribly(), _restoreOptions::filename, free, _tocEntry::hadDumper, _restoreOptions::if_exists, K_VERS_1_3, K_VERS_1_8, Archive::maxRemoteVersion, Archive::minRemoteVersion, modulename, _tocEntry::next, _archiveHandle::noTocComments, Archive::numWorkers, CatalogId::oid, par_list_header_init(), ParallelBackupEnd(), ParallelBackupStart(), pg_free(), pg_strdup(), _restoreOptions::pghost, _restoreOptions::pgport, _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, _archiveHandle::version, and write_msg().

Referenced by _CloseArchive(), and main().

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

◆ SetArchiveOptions()

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

Definition at line 255 of file pg_backup_archiver.c.

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

Referenced by _CloseArchive(), and main().

256 {
257  /* Caller can omit dump options, in which case we synthesize them */
258  if (dopt == NULL && ropt != NULL)
259  dopt = dumpOptionsFromRestoreOptions(ropt);
260 
261  /* Save options for later access */
262  AH->dopt = dopt;
263  AH->ropt = ropt;
264 }
RestoreOptions * ropt
Definition: pg_backup.h:182
DumpOptions * dopt
Definition: pg_backup.h:181
DumpOptions * dumpOptionsFromRestoreOptions(RestoreOptions *ropt)

◆ SortTocFromFile()

void SortTocFromFile ( Archive AHX)

Definition at line 1376 of file pg_backup_archiver.c.

References _moveBefore(), buf, exit_horribly(), getTocEntryByDumpId(), _restoreOptions::idWanted, _archiveHandle::maxDumpId, modulename, PG_BINARY_R, pg_malloc(), _archiveHandle::public, Archive::ropt, strerror(), _archiveHandle::toc, _restoreOptions::tocFile, and write_msg().

Referenced by main().

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

◆ StartBlob()

int StartBlob ( Archive AH,
Oid  oid 
)

Definition at line 1232 of file pg_backup_archiver.c.

References _archiveHandle::currToc, exit_horribly(), modulename, and _archiveHandle::StartBlobPtr.

Referenced by dumpBlobs().

1233 {
1234  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1235 
1236  if (!AH->StartBlobPtr)
1237  exit_horribly(modulename, "large-object output not supported in chosen format\n");
1238 
1239  AH->StartBlobPtr(AH, AH->currToc, oid);
1240 
1241  return 1;
1242 }
StartBlobPtrType StartBlobPtr
struct _tocEntry * currToc
void exit_horribly(const char *modulename, const char *fmt,...)
static const char * modulename

◆ WriteData()

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

Definition at line 1034 of file pg_backup_archiver.c.

References _archiveHandle::currToc, exit_horribly(), modulename, and _archiveHandle::WriteDataPtr.

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

1035 {
1036  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1037 
1038  if (!AH->currToc)
1039  exit_horribly(modulename, "internal error -- WriteData cannot be called outside the context of a DataDumper routine\n");
1040 
1041  AH->WriteDataPtr(AH, data, dLen);
1042 
1043  return;
1044 }
struct _tocEntry * currToc
WriteDataPtrType WriteDataPtr
void exit_horribly(const char *modulename, const char *fmt,...)
static const char * modulename