PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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

Typedef Documentation

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

Definition at line 231 of file pg_backup.h.

Definition at line 229 of file pg_backup.h.

typedef void(* SetupWorkerPtrType)(Archive *AH)

Definition at line 233 of file pg_backup.h.

Enumeration Type Documentation

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
Enumerator
archModeAppend 
archModeWrite 
archModeRead 

Definition at line 46 of file pg_backup.h.

47 {
51 } ArchiveMode;
enum _archiveMode ArchiveMode
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
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

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 1027 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, NULL, pg_malloc(), pg_malloc0(), pg_strdup(), _tocEntry::prev, _tocEntry::section, _tocEntry::tag, _archiveHandle::toc, and _archiveHandle::tocCount.

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().

1038 {
1039  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1040  TocEntry *newToc;
1041 
1042  newToc = (TocEntry *) pg_malloc0(sizeof(TocEntry));
1043 
1044  AH->tocCount++;
1045  if (dumpId > AH->maxDumpId)
1046  AH->maxDumpId = dumpId;
1047 
1048  newToc->prev = AH->toc->prev;
1049  newToc->next = AH->toc;
1050  AH->toc->prev->next = newToc;
1051  AH->toc->prev = newToc;
1052 
1053  newToc->catalogId = catalogId;
1054  newToc->dumpId = dumpId;
1055  newToc->section = section;
1056 
1057  newToc->tag = pg_strdup(tag);
1058  newToc->namespace = namespace ? pg_strdup(namespace) : NULL;
1059  newToc->tablespace = tablespace ? pg_strdup(tablespace) : NULL;
1060  newToc->owner = pg_strdup(owner);
1061  newToc->withOids = withOids;
1062  newToc->desc = pg_strdup(desc);
1063  newToc->defn = pg_strdup(defn);
1064  newToc->dropStmt = pg_strdup(dropStmt);
1065  newToc->copyStmt = copyStmt ? pg_strdup(copyStmt) : NULL;
1066 
1067  if (nDeps > 0)
1068  {
1069  newToc->dependencies = (DumpId *) pg_malloc(nDeps * sizeof(DumpId));
1070  memcpy(newToc->dependencies, deps, nDeps * sizeof(DumpId));
1071  newToc->nDeps = nDeps;
1072  }
1073  else
1074  {
1075  newToc->dependencies = NULL;
1076  newToc->nDeps = 0;
1077  }
1078 
1079  newToc->dataDumper = dumpFn;
1080  newToc->dataDumperArg = dumpArg;
1081  newToc->hadDumper = dumpFn ? true : false;
1082 
1083  newToc->formatData = NULL;
1084 
1085  if (AH->ArchiveEntryPtr != NULL)
1086  (*AH->ArchiveEntryPtr) (AH, newToc);
1087 }
struct _tocEntry * next
int DumpId
Definition: pg_backup.h:229
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
CatalogId catalogId
void * dataDumperArg
DataDumperPtr dataDumper
teSection section
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
#define NULL
Definition: c.h:229
ArchiveEntryPtrType ArchiveEntryPtr
int archprintf ( Archive AH,
const char *  fmt,
  ... 
)
void archputs ( const char *  s,
Archive AH 
)

Definition at line 1445 of file pg_backup_archiver.c.

References WriteData().

Referenced by dumpTableData_insert().

1446 {
1447  WriteData(AH, s, strlen(s));
1448  return;
1449 }
void WriteData(Archive *AHX, const void *data, size_t dLen)
void CloseArchive ( Archive AH)

Definition at line 229 of file pg_backup_archiver.c.

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

Referenced by main().

230 {
231  int res = 0;
232  ArchiveHandle *AH = (ArchiveHandle *) AHX;
233 
234  (*AH->ClosePtr) (AH);
235 
236  /* Close the output */
237  if (AH->gzOut)
238  res = GZCLOSE(AH->OF);
239  else if (AH->OF != stdout)
240  res = fclose(AH->OF);
241 
242  if (res != 0)
243  exit_horribly(modulename, "could not close output file: %s\n",
244  strerror(errno));
245 }
#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
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(), NULL, 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:41
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6014
PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg)
Definition: fe-connect.c:6190
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3547
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:510
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:5898
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:131
#define free(a)
Definition: header.h:65
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:5882
#define NULL
Definition: c.h:229
char * dbname
Definition: streamutil.c:38
void exit_horribly(const char *modulename, const char *fmt,...)
static Datum values[MAXATTR]
Definition: bootstrap.c:163
static void _check_database_version(ArchiveHandle *AH)
Definition: pg_backup_db.c:36
int PQconnectionUsedPassword(const PGconn *conn)
Definition: fe-connect.c:6063
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:6048
void set_archive_cancel_info(ArchiveHandle *AH, PGconn *conn)
Definition: parallel.c:742
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:5961
char * pgport
Definition: pgbench.c:181
Archive* CreateArchive ( const char *  FileSpec,
const ArchiveFormat  fmt,
const int  compression,
bool  dosync,
ArchiveMode  mode,
SetupWorkerPtrType  setupDumpWorker 
)

Definition at line 206 of file pg_backup_archiver.c.

References _allocAH().

Referenced by main().

210 {
211  ArchiveHandle *AH = _allocAH(FileSpec, fmt, compression, dosync,
212  mode, setupDumpWorker);
213 
214  return (Archive *) AH;
215 }
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:93
static void setupDumpWorker(Archive *AHX)
Definition: pg_dump.c:1147
void DisconnectDatabase ( Archive AHX)

Definition at line 343 of file pg_backup_db.c.

References _archiveHandle::connCancel, _archiveHandle::connection, NULL, 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  */
365  }
366 
367  PQfinish(AH->connection);
368  AH->connection = NULL;
369 }
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3547
PGcancel *volatile connCancel
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:5969
#define NULL
Definition: c.h:229
int PQcancel(PGcancel *cancel, char *errbuf, int errbufsize)
Definition: fe-connect.c:3823
void set_archive_cancel_info(ArchiveHandle *AH, PGconn *conn)
Definition: parallel.c:742
DumpOptions* dumpOptionsFromRestoreOptions ( RestoreOptions ropt)

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

149 {
150  DumpOptions *dopt = NewDumpOptions();
151 
152  /* this is the inverse of what's at the end of pg_dump.c's main() */
153  dopt->outputClean = ropt->dropSchema;
154  dopt->dataOnly = ropt->dataOnly;
155  dopt->schemaOnly = ropt->schemaOnly;
156  dopt->if_exists = ropt->if_exists;
157  dopt->column_inserts = ropt->column_inserts;
158  dopt->dumpSections = ropt->dumpSections;
159  dopt->aclsSkip = ropt->aclsSkip;
160  dopt->outputSuperuser = ropt->superuser;
161  dopt->outputCreateDB = ropt->createDB;
162  dopt->outputNoOwner = ropt->noOwner;
163  dopt->outputNoTablespaces = ropt->noTablespace;
164  dopt->disable_triggers = ropt->disable_triggers;
165  dopt->use_setsessauth = ropt->use_setsessauth;
166 
168  dopt->dump_inserts = ropt->dump_inserts;
169  dopt->no_publications = ropt->no_publications;
171  dopt->no_subscriptions = ropt->no_subscriptions;
172  dopt->lockWaitTimeout = ropt->lockWaitTimeout;
175  dopt->sequence_data = ropt->sequence_data;
176 
177  return dopt;
178 }
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:171
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:162
char * outputSuperuser
Definition: pg_backup.h:169
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:168
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:165
int outputClean
Definition: pg_backup.h:164
int outputNoTablespaces
Definition: pg_backup.h:157
int no_publications
Definition: pg_backup.h:77
int disable_triggers
Definition: pg_backup.h:66
int EndBlob ( Archive AH,
Oid  oid 
)

Definition at line 1220 of file pg_backup_archiver.c.

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

Referenced by dumpBlobs().

1221 {
1222  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1223 
1224  if (AH->EndBlobPtr)
1225  (*AH->EndBlobPtr) (AH, AH->currToc, oid);
1226 
1227  return 1;
1228 }
struct _tocEntry * currToc
EndBlobPtrType EndBlobPtr
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 }
void InitDumpOptions ( DumpOptions opts)

Definition at line 135 of file pg_backup_archiver.c.

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

Referenced by main(), and NewDumpOptions().

136 {
137  memset(opts, 0, sizeof(DumpOptions));
138  /* set any fields that shouldn't default to zeroes */
139  opts->include_everything = true;
141 }
bool include_everything
Definition: pg_backup.h:162
int dumpSections
Definition: pg_backup.h:140
DumpOptions* NewDumpOptions ( void  )

Definition at line 123 of file pg_backup_archiver.c.

References InitDumpOptions(), and pg_malloc().

Referenced by dumpOptionsFromRestoreOptions().

124 {
125  DumpOptions *opts = (DumpOptions *) pg_malloc(sizeof(DumpOptions));
126 
127  InitDumpOptions(opts);
128  return opts;
129 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
void InitDumpOptions(DumpOptions *opts)
RestoreOptions* NewRestoreOptions ( void  )

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

933 {
934  RestoreOptions *opts;
935 
936  opts = (RestoreOptions *) pg_malloc0(sizeof(RestoreOptions));
937 
938  /* set any fields that shouldn't default to zeroes */
939  opts->format = archUnknown;
940  opts->promptPassword = TRI_DEFAULT;
942 
943  return opts;
944 }
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
trivalue promptPassword
Definition: pg_backup.h:114
Archive* OpenArchive ( const char *  FileSpec,
const ArchiveFormat  fmt 
)

Definition at line 220 of file pg_backup_archiver.c.

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

Referenced by main().

221 {
222  ArchiveHandle *AH = _allocAH(FileSpec, fmt, 0, true, archModeRead, setupRestoreWorker);
223 
224  return (Archive *) AH;
225 }
static ArchiveHandle * _allocAH(const char *FileSpec, const ArchiveFormat fmt, const int compression, bool dosync, ArchiveMode mode, SetupWorkerPtrType setupWorkerPtr)
static void setupRestoreWorker(Archive *AHX)
void PrintTOCSummary ( Archive AH)

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

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

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

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

Definition at line 319 of file pg_backup_archiver.c.

References _becomeOwner(), _doSetFixedOutputState(), _printTocEntry(), _selectOutputSchema(), 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, NULL, 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_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().

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

Definition at line 249 of file pg_backup_archiver.c.

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

Referenced by _CloseArchive(), and main().

250 {
251  /* Caller can omit dump options, in which case we synthesize them */
252  if (dopt == NULL && ropt != NULL)
253  dopt = dumpOptionsFromRestoreOptions(ropt);
254 
255  /* Save options for later access */
256  AH->dopt = dopt;
257  AH->ropt = ropt;
258 }
RestoreOptions * ropt
Definition: pg_backup.h:181
DumpOptions * dopt
Definition: pg_backup.h:180
DumpOptions * dumpOptionsFromRestoreOptions(RestoreOptions *ropt)
#define NULL
Definition: c.h:229
void SortTocFromFile ( Archive AHX)

Definition at line 1350 of file pg_backup_archiver.c.

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

Referenced by main().

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

Definition at line 1206 of file pg_backup_archiver.c.

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

Referenced by dumpBlobs().

1207 {
1208  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1209 
1210  if (!AH->StartBlobPtr)
1211  exit_horribly(modulename, "large-object output not supported in chosen format\n");
1212 
1213  (*AH->StartBlobPtr) (AH, AH->currToc, oid);
1214 
1215  return 1;
1216 }
StartBlobPtrType StartBlobPtr
struct _tocEntry * currToc
void exit_horribly(const char *modulename, const char *fmt,...)
static const char * modulename
void WriteData ( Archive AH,
const void *  data,
size_t  dLen 
)

Definition at line 1008 of file pg_backup_archiver.c.

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

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

1009 {
1010  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1011 
1012  if (!AH->currToc)
1013  exit_horribly(modulename, "internal error -- WriteData cannot be called outside the context of a DataDumper routine\n");
1014 
1015  (*AH->WriteDataPtr) (AH, data, dLen);
1016 
1017  return;
1018 }
struct _tocEntry * currToc
WriteDataPtrType WriteDataPtr
void exit_horribly(const char *modulename, const char *fmt,...)
static const char * modulename