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 237 of file pg_backup.h.

◆ DumpId

Definition at line 235 of file pg_backup.h.

◆ DumpOptions

◆ RestoreOptions

◆ SetupWorkerPtrType

typedef void(* SetupWorkerPtrType) (Archive *AH)

Definition at line 239 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 1052 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(), dumpIndexAttach(), dumpNamespace(), dumpOpclass(), dumpOpfamily(), dumpOpr(), dumpPolicy(), dumpProcLang(), dumpPublication(), dumpPublicationTable(), dumpRangeType(), dumpRule(), dumpSearchPath(), dumpSecLabel(), dumpSequence(), dumpSequenceData(), dumpShellType(), dumpStatisticsExt(), dumpStdStrings(), dumpSubscription(), dumpTableComment(), dumpTableData(), dumpTableSchema(), dumpTableSecLabel(), dumpTransform(), dumpTrigger(), dumpTSConfig(), dumpTSDictionary(), dumpTSParser(), dumpTSTemplate(), dumpUndefinedType(), dumpUserMappings(), and refreshMatViewData().

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

References WriteData().

Referenced by dumpTableData_insert().

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

◆ CloseArchive()

void CloseArchive ( Archive AH)

Definition at line 236 of file pg_backup_archiver.c.

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

Referenced by main().

237 {
238  int res = 0;
239  ArchiveHandle *AH = (ArchiveHandle *) AHX;
240 
241  AH->ClosePtr(AH);
242 
243  /* Close the output */
244  if (AH->gzOut)
245  res = GZCLOSE(AH->OF);
246  else if (AH->OF != stdout)
247  res = fclose(AH->OF);
248 
249  if (res != 0)
250  exit_horribly(modulename, "could not close output file: %s\n",
251  strerror(errno));
252 }
#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 240 of file pg_backup_db.c.

References _check_database_version(), ALWAYS_SECURE_SEARCH_PATH_SQL, _archiveHandle::connection, CONNECTION_BAD, dbname, ExecuteSqlQueryForSingleRow(), exit_horribly(), free, modulename, 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().

246 {
247  ArchiveHandle *AH = (ArchiveHandle *) AHX;
248  char *password;
249  char passbuf[100];
250  bool new_pass;
251 
252  if (AH->connection)
253  exit_horribly(modulename, "already connected to a database\n");
254 
255  password = AH->savedPassword;
256 
257  if (prompt_password == TRI_YES && password == NULL)
258  {
259  simple_prompt("Password: ", passbuf, sizeof(passbuf), false);
260  password = passbuf;
261  }
262  AH->promptPassword = prompt_password;
263 
264  /*
265  * Start the connection. Loop until we have a password if requested by
266  * backend.
267  */
268  do
269  {
270  const char *keywords[7];
271  const char *values[7];
272 
273  keywords[0] = "host";
274  values[0] = pghost;
275  keywords[1] = "port";
276  values[1] = pgport;
277  keywords[2] = "user";
278  values[2] = username;
279  keywords[3] = "password";
280  values[3] = password;
281  keywords[4] = "dbname";
282  values[4] = dbname;
283  keywords[5] = "fallback_application_name";
284  values[5] = progname;
285  keywords[6] = NULL;
286  values[6] = NULL;
287 
288  new_pass = false;
289  AH->connection = PQconnectdbParams(keywords, values, true);
290 
291  if (!AH->connection)
292  exit_horribly(modulename, "failed to connect to database\n");
293 
294  if (PQstatus(AH->connection) == CONNECTION_BAD &&
296  password == NULL &&
297  prompt_password != TRI_NO)
298  {
299  PQfinish(AH->connection);
300  simple_prompt("Password: ", passbuf, sizeof(passbuf), false);
301  password = passbuf;
302  new_pass = true;
303  }
304  } while (new_pass);
305 
306  /* check to see that the backend connection was successfully made */
307  if (PQstatus(AH->connection) == CONNECTION_BAD)
308  exit_horribly(modulename, "connection to database \"%s\" failed: %s",
309  PQdb(AH->connection) ? PQdb(AH->connection) : "",
311 
312  /* Start strict; later phases may override this. */
315 
316  /*
317  * We want to remember connection's actual password, whether or not we got
318  * it by prompting. So we don't just store the password variable.
319  */
321  {
322  if (AH->savedPassword)
323  free(AH->savedPassword);
325  }
326 
327  /* check for version mismatch */
329 
331 
332  /* arrange for SIGINT to issue a query cancel on this connection */
334 }
static char password[100]
Definition: streamutil.c:54
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6116
PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg)
Definition: fe-connect.c:6292
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3638
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:536
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:420
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:5999
static void notice_processor(void *arg, const char *message)
Definition: pg_backup_db.c:378
char * pghost
Definition: pgbench.c:193
static const char * modulename
Definition: pg_backup_db.c:30
static char * username
Definition: initdb.c:132
void PQclear(PGresult *res)
Definition: fe-exec.c:671
#define free(a)
Definition: header.h:65
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:5983
char * dbname
Definition: streamutil.c:51
#define ALWAYS_SECURE_SEARCH_PATH_SQL
Definition: connect.h:25
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:37
int PQconnectionUsedPassword(const PGconn *conn)
Definition: fe-connect.c:6165
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:6150
void set_archive_cancel_info(ArchiveHandle *AH, PGconn *conn)
Definition: parallel.c:744
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6063
char * pgport
Definition: pgbench.c:194

◆ CreateArchive()

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

Definition at line 213 of file pg_backup_archiver.c.

References _allocAH().

Referenced by main().

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

◆ DisconnectDatabase()

void DisconnectDatabase ( Archive AHX)

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

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

◆ dumpOptionsFromRestoreOptions()

DumpOptions* dumpOptionsFromRestoreOptions ( RestoreOptions ropt)

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

156 {
157  DumpOptions *dopt = NewDumpOptions();
158 
159  /* this is the inverse of what's at the end of pg_dump.c's main() */
160  dopt->outputClean = ropt->dropSchema;
161  dopt->dataOnly = ropt->dataOnly;
162  dopt->schemaOnly = ropt->schemaOnly;
163  dopt->if_exists = ropt->if_exists;
164  dopt->column_inserts = ropt->column_inserts;
165  dopt->dumpSections = ropt->dumpSections;
166  dopt->aclsSkip = ropt->aclsSkip;
167  dopt->outputSuperuser = ropt->superuser;
168  dopt->outputCreateDB = ropt->createDB;
169  dopt->outputNoOwner = ropt->noOwner;
170  dopt->outputNoTablespaces = ropt->noTablespace;
171  dopt->disable_triggers = ropt->disable_triggers;
172  dopt->use_setsessauth = ropt->use_setsessauth;
174  dopt->dump_inserts = ropt->dump_inserts;
175  dopt->no_comments = ropt->no_comments;
176  dopt->no_publications = ropt->no_publications;
178  dopt->no_subscriptions = ropt->no_subscriptions;
179  dopt->lockWaitTimeout = ropt->lockWaitTimeout;
182  dopt->sequence_data = ropt->sequence_data;
183 
184  return dopt;
185 }
int column_inserts
Definition: pg_backup.h:148
int disable_triggers
Definition: pg_backup.h:158
int disable_dollar_quoting
Definition: pg_backup.h:73
bool schemaOnly
Definition: pg_backup.h:139
int no_subscriptions
Definition: pg_backup.h:153
int no_publications
Definition: pg_backup.h:152
DumpOptions * NewDumpOptions(void)
int sequence_data
Definition: pg_backup.h:174
const char * lockWaitTimeout
Definition: pg_backup.h:89
int use_setsessauth
Definition: pg_backup.h:160
int disable_dollar_quoting
Definition: pg_backup.h:146
int column_inserts
Definition: pg_backup.h:75
const char * lockWaitTimeout
Definition: pg_backup.h:143
bool dataOnly
Definition: pg_backup.h:140
bool include_everything
Definition: pg_backup.h:165
char * outputSuperuser
Definition: pg_backup.h:172
int include_everything
Definition: pg_backup.h:90
int no_security_labels
Definition: pg_backup.h:79
int dumpSections
Definition: pg_backup.h:141
int enable_row_security
Definition: pg_backup.h:123
int dump_inserts
Definition: pg_backup.h:147
int outputNoOwner
Definition: pg_backup.h:171
int no_security_labels
Definition: pg_backup.h:151
int no_subscriptions
Definition: pg_backup.h:80
char * superuser
Definition: pg_backup.h:70
bool aclsSkip
Definition: pg_backup.h:142
int use_setsessauth
Definition: pg_backup.h:68
int enable_row_security
Definition: pg_backup.h:161
int outputCreateDB
Definition: pg_backup.h:168
int outputClean
Definition: pg_backup.h:167
int no_comments
Definition: pg_backup.h:150
int outputNoTablespaces
Definition: pg_backup.h:159
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 1245 of file pg_backup_archiver.c.

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

Referenced by dumpBlobs().

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

◆ GetConnection()

PGconn* GetConnection ( Archive AHX)

Definition at line 370 of file pg_backup_db.c.

References _archiveHandle::connection.

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

◆ InitDumpOptions()

void InitDumpOptions ( DumpOptions opts)

Definition at line 142 of file pg_backup_archiver.c.

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

Referenced by main(), and NewDumpOptions().

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

◆ NewDumpOptions()

DumpOptions* NewDumpOptions ( void  )

Definition at line 130 of file pg_backup_archiver.c.

References InitDumpOptions(), and pg_malloc().

Referenced by dumpOptionsFromRestoreOptions().

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

◆ NewRestoreOptions()

RestoreOptions* NewRestoreOptions ( void  )

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

958 {
959  RestoreOptions *opts;
960 
961  opts = (RestoreOptions *) pg_malloc0(sizeof(RestoreOptions));
962 
963  /* set any fields that shouldn't default to zeroes */
964  opts->format = archUnknown;
965  opts->promptPassword = TRI_DEFAULT;
967 
968  return opts;
969 }
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 227 of file pg_backup_archiver.c.

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

Referenced by main().

228 {
229  ArchiveHandle *AH = _allocAH(FileSpec, fmt, 0, true, archModeRead, setupRestoreWorker);
230 
231  return (Archive *) AH;
232 }
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 1116 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().

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

◆ ProcessArchiveRestoreOptions()

void ProcessArchiveRestoreOptions ( Archive AH)

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

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

◆ RestoreArchive()

void RestoreArchive ( Archive AH)

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

327 {
328  ArchiveHandle *AH = (ArchiveHandle *) AHX;
329  RestoreOptions *ropt = AH->public.ropt;
330  bool parallel_mode;
331  TocEntry *te;
332  OutputContext sav;
333 
335 
336  /*
337  * Check for nonsensical option combinations.
338  *
339  * -C is not compatible with -1, because we can't create a database inside
340  * a transaction block.
341  */
342  if (ropt->createDB && ropt->single_txn)
343  exit_horribly(modulename, "-C and -1 are incompatible options\n");
344 
345  /*
346  * If we're going to do parallel restore, there are some restrictions.
347  */
348  parallel_mode = (AH->public.numWorkers > 1 && ropt->useDB);
349  if (parallel_mode)
350  {
351  /* We haven't got round to making this work for all archive formats */
352  if (AH->ClonePtr == NULL || AH->ReopenPtr == NULL)
353  exit_horribly(modulename, "parallel restore is not supported with this archive file format\n");
354 
355  /* Doesn't work if the archive represents dependencies as OIDs */
356  if (AH->version < K_VERS_1_8)
357  exit_horribly(modulename, "parallel restore is not supported with archives made by pre-8.0 pg_dump\n");
358 
359  /*
360  * It's also not gonna work if we can't reopen the input file, so
361  * let's try that immediately.
362  */
363  AH->ReopenPtr(AH);
364  }
365 
366  /*
367  * Make sure we won't need (de)compression we haven't got
368  */
369 #ifndef HAVE_LIBZ
370  if (AH->compression != 0 && AH->PrintTocDataPtr != NULL)
371  {
372  for (te = AH->toc->next; te != AH->toc; te = te->next)
373  {
374  if (te->hadDumper && (te->reqs & REQ_DATA) != 0)
375  exit_horribly(modulename, "cannot restore from compressed archive (compression not supported in this installation)\n");
376  }
377  }
378 #endif
379 
380  /*
381  * Prepare index arrays, so we can assume we have them throughout restore.
382  * It's possible we already did this, though.
383  */
384  if (AH->tocsByDumpId == NULL)
386 
387  /*
388  * If we're using a DB connection, then connect it.
389  */
390  if (ropt->useDB)
391  {
392  ahlog(AH, 1, "connecting to database for restore\n");
393  if (AH->version < K_VERS_1_3)
394  exit_horribly(modulename, "direct database connections are not supported in pre-1.3 archives\n");
395 
396  /*
397  * We don't want to guess at whether the dump will successfully
398  * restore; allow the attempt regardless of the version of the restore
399  * target.
400  */
401  AHX->minRemoteVersion = 0;
402  AHX->maxRemoteVersion = 9999999;
403 
404  ConnectDatabase(AHX, ropt->dbname,
405  ropt->pghost, ropt->pgport, ropt->username,
406  ropt->promptPassword);
407 
408  /*
409  * If we're talking to the DB directly, don't send comments since they
410  * obscure SQL when displaying errors
411  */
412  AH->noTocComments = 1;
413  }
414 
415  /*
416  * Work out if we have an implied data-only restore. This can happen if
417  * the dump was data only or if the user has used a toc list to exclude
418  * all of the schema data. All we do is look for schema entries - if none
419  * are found then we set the dataOnly flag.
420  *
421  * We could scan for wanted TABLE entries, but that is not the same as
422  * dataOnly. At this stage, it seems unnecessary (6-Mar-2001).
423  */
424  if (!ropt->dataOnly)
425  {
426  int impliedDataOnly = 1;
427 
428  for (te = AH->toc->next; te != AH->toc; te = te->next)
429  {
430  if ((te->reqs & REQ_SCHEMA) != 0)
431  { /* It's schema, and it's wanted */
432  impliedDataOnly = 0;
433  break;
434  }
435  }
436  if (impliedDataOnly)
437  {
438  ropt->dataOnly = impliedDataOnly;
439  ahlog(AH, 1, "implied data-only restore\n");
440  }
441  }
442 
443  /*
444  * Setup the output file if necessary.
445  */
446  sav = SaveOutput(AH);
447  if (ropt->filename || ropt->compression)
448  SetOutput(AH, ropt->filename, ropt->compression);
449 
450  ahprintf(AH, "--\n-- PostgreSQL database dump\n--\n\n");
451 
452  if (AH->archiveRemoteVersion)
453  ahprintf(AH, "-- Dumped from database version %s\n",
455  if (AH->archiveDumpVersion)
456  ahprintf(AH, "-- Dumped by pg_dump version %s\n",
457  AH->archiveDumpVersion);
458 
459  ahprintf(AH, "\n");
460 
461  if (AH->public.verbose)
462  dumpTimestamp(AH, "Started on", AH->createDate);
463 
464  if (ropt->single_txn)
465  {
466  if (AH->connection)
467  StartTransaction(AHX);
468  else
469  ahprintf(AH, "BEGIN;\n\n");
470  }
471 
472  /*
473  * Establish important parameter values right away.
474  */
476 
477  AH->stage = STAGE_PROCESSING;
478 
479  /*
480  * Drop the items at the start, in reverse order
481  */
482  if (ropt->dropSchema)
483  {
484  for (te = AH->toc->prev; te != AH->toc; te = te->prev)
485  {
486  AH->currentTE = te;
487 
488  /*
489  * In createDB mode, issue a DROP *only* for the database as a
490  * whole. Issuing drops against anything else would be wrong,
491  * because at this point we're connected to the wrong database.
492  * (The DATABASE PROPERTIES entry, if any, should be treated like
493  * the DATABASE entry.)
494  */
495  if (ropt->createDB)
496  {
497  if (strcmp(te->desc, "DATABASE") != 0 &&
498  strcmp(te->desc, "DATABASE PROPERTIES") != 0)
499  continue;
500  }
501 
502  /* Otherwise, drop anything that's selected and has a dropStmt */
503  if (((te->reqs & (REQ_SCHEMA | REQ_DATA)) != 0) && te->dropStmt)
504  {
505  ahlog(AH, 1, "dropping %s %s\n", te->desc, te->tag);
506  /* Select owner and schema as necessary */
507  _becomeOwner(AH, te);
508  _selectOutputSchema(AH, te->namespace);
509 
510  /*
511  * Now emit the DROP command, if the object has one. Note we
512  * don't necessarily emit it verbatim; at this point we add an
513  * appropriate IF EXISTS clause, if the user requested it.
514  */
515  if (*te->dropStmt != '\0')
516  {
517  if (!ropt->if_exists)
518  {
519  /* No --if-exists? Then just use the original */
520  ahprintf(AH, "%s", te->dropStmt);
521  }
522  else
523  {
524  /*
525  * Inject an appropriate spelling of "if exists". For
526  * large objects, we have a separate routine that
527  * knows how to do it, without depending on
528  * te->dropStmt; use that. For other objects we need
529  * to parse the command.
530  */
531  if (strncmp(te->desc, "BLOB", 4) == 0)
532  {
533  DropBlobIfExists(AH, te->catalogId.oid);
534  }
535  else
536  {
537  char *dropStmt = pg_strdup(te->dropStmt);
538  char *dropStmtOrig = dropStmt;
539  PQExpBuffer ftStmt = createPQExpBuffer();
540 
541  /*
542  * Need to inject IF EXISTS clause after ALTER
543  * TABLE part in ALTER TABLE .. DROP statement
544  */
545  if (strncmp(dropStmt, "ALTER TABLE", 11) == 0)
546  {
547  appendPQExpBuffer(ftStmt,
548  "ALTER TABLE IF EXISTS");
549  dropStmt = dropStmt + 11;
550  }
551 
552  /*
553  * ALTER TABLE..ALTER COLUMN..DROP DEFAULT does
554  * not support the IF EXISTS clause, and therefore
555  * we simply emit the original command for DEFAULT
556  * objects (modulo the adjustment made above).
557  *
558  * Likewise, don't mess with DATABASE PROPERTIES.
559  *
560  * If we used CREATE OR REPLACE VIEW as a means of
561  * quasi-dropping an ON SELECT rule, that should
562  * be emitted unchanged as well.
563  *
564  * For other object types, we need to extract the
565  * first part of the DROP which includes the
566  * object type. Most of the time this matches
567  * te->desc, so search for that; however for the
568  * different kinds of CONSTRAINTs, we know to
569  * search for hardcoded "DROP CONSTRAINT" instead.
570  */
571  if (strcmp(te->desc, "DEFAULT") == 0 ||
572  strcmp(te->desc, "DATABASE PROPERTIES") == 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:661
RestoreOptions * ropt
Definition: pg_backup.h:184
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:113
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:83
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:341
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:186
PrintTocDataPtrType PrintTocDataPtr
static OutputContext SaveOutput(ArchiveHandle *AH)
static void StartTransaction(void)
Definition: xact.c:1795
#define free(a)
Definition: header.h:65
void write_msg(const char *modulename, const char *fmt,...)
#define Assert(condition)
Definition: c.h:699
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:215
static void _selectOutputSchema(ArchiveHandle *AH, const char *schemaName)
int numWorkers
Definition: pg_backup.h:194
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:1933
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:240
trivalue promptPassword
Definition: pg_backup.h:115

◆ SetArchiveOptions()

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

Definition at line 256 of file pg_backup_archiver.c.

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

Referenced by _CloseArchive(), and main().

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

◆ SortTocFromFile()

void SortTocFromFile ( Archive AHX)

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

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

◆ StartBlob()

int StartBlob ( Archive AH,
Oid  oid 
)

Definition at line 1231 of file pg_backup_archiver.c.

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

Referenced by dumpBlobs().

1232 {
1233  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1234 
1235  if (!AH->StartBlobPtr)
1236  exit_horribly(modulename, "large-object output not supported in chosen format\n");
1237 
1238  AH->StartBlobPtr(AH, AH->currToc, oid);
1239 
1240  return 1;
1241 }
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 1033 of file pg_backup_archiver.c.

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

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

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