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(* SetupWorkerPtr )(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, SetupWorkerPtr 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(* SetupWorkerPtr)(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 1026 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(), dumpStdStrings(), dumpSubscription(), dumpTableComment(), dumpTableData(), dumpTableSchema(), dumpTableSecLabel(), dumpTransform(), dumpTrigger(), dumpTSConfig(), dumpTSDictionary(), dumpTSParser(), dumpTSTemplate(), dumpUndefinedType(), dumpUserMappings(), and refreshMatViewData().

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

Definition at line 1444 of file pg_backup_archiver.c.

References WriteData().

Referenced by dumpTableData_insert().

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

Definition at line 228 of file pg_backup_archiver.c.

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

Referenced by main().

229 {
230  int res = 0;
231  ArchiveHandle *AH = (ArchiveHandle *) AHX;
232 
233  (*AH->ClosePtr) (AH);
234 
235  /* Close the output */
236  if (AH->gzOut)
237  res = GZCLOSE(AH->OF);
238  else if (AH->OF != stdout)
239  res = fclose(AH->OF);
240 
241  if (res != 0)
242  exit_horribly(modulename, "could not close output file: %s\n",
243  strerror(errno));
244 }
#define GZCLOSE(fh)
void exit_horribly(const char *modulename, const char *fmt,...)
const char * strerror(int errnum)
Definition: strerror.c:19
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 248 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().

254 {
255  ArchiveHandle *AH = (ArchiveHandle *) AHX;
256  char *password;
257  char passbuf[100];
258  bool new_pass;
259 
260  if (AH->connection)
261  exit_horribly(modulename, "already connected to a database\n");
262 
263  password = AH->savedPassword;
264 
265  if (prompt_password == TRI_YES && password == NULL)
266  {
267  simple_prompt("Password: ", passbuf, sizeof(passbuf), false);
268  password = passbuf;
269  }
270  AH->promptPassword = prompt_password;
271 
272  /*
273  * Start the connection. Loop until we have a password if requested by
274  * backend.
275  */
276  do
277  {
278  const char *keywords[7];
279  const char *values[7];
280 
281  keywords[0] = "host";
282  values[0] = pghost;
283  keywords[1] = "port";
284  values[1] = pgport;
285  keywords[2] = "user";
286  values[2] = username;
287  keywords[3] = "password";
288  values[3] = password;
289  keywords[4] = "dbname";
290  values[4] = dbname;
291  keywords[5] = "fallback_application_name";
292  values[5] = progname;
293  keywords[6] = NULL;
294  values[6] = NULL;
295 
296  new_pass = false;
297  AH->connection = PQconnectdbParams(keywords, values, true);
298 
299  if (!AH->connection)
300  exit_horribly(modulename, "failed to connect to database\n");
301 
302  if (PQstatus(AH->connection) == CONNECTION_BAD &&
304  password == NULL &&
305  prompt_password != TRI_NO)
306  {
307  PQfinish(AH->connection);
308  simple_prompt("Password: ", passbuf, sizeof(passbuf), false);
309  password = passbuf;
310  new_pass = true;
311  }
312  } while (new_pass);
313 
314  /* check to see that the backend connection was successfully made */
315  if (PQstatus(AH->connection) == CONNECTION_BAD)
316  exit_horribly(modulename, "connection to database \"%s\" failed: %s",
317  PQdb(AH->connection) ? PQdb(AH->connection) : "",
319 
320  /*
321  * We want to remember connection's actual password, whether or not we got
322  * it by prompting. So we don't just store the password variable.
323  */
325  {
326  if (AH->savedPassword)
327  free(AH->savedPassword);
329  }
330 
331  /* check for version mismatch */
333 
335 
336  /* arrange for SIGINT to issue a query cancel on this connection */
338 }
static char password[100]
Definition: streamutil.c:41
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6011
PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg)
Definition: fe-connect.c:6187
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3568
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:470
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:5895
static void notice_processor(void *arg, const char *message)
Definition: pg_backup_db.c:382
char * pghost
Definition: pgbench.c:180
static const char * modulename
Definition: pg_backup_db.c:31
static char * username
Definition: initdb.c:131
#define free(a)
Definition: header.h:65
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:5879
#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:162
static void _check_database_version(ArchiveHandle *AH)
Definition: pg_backup_db.c:38
int PQconnectionUsedPassword(const PGconn *conn)
Definition: fe-connect.c:6060
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:6045
void set_archive_cancel_info(ArchiveHandle *AH, PGconn *conn)
Definition: parallel.c:742
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:5958
char * pgport
Definition: pgbench.c:181
Archive* CreateArchive ( const char *  FileSpec,
const ArchiveFormat  fmt,
const int  compression,
bool  dosync,
ArchiveMode  mode,
SetupWorkerPtr  setupDumpWorker 
)

Definition at line 205 of file pg_backup_archiver.c.

References _allocAH().

Referenced by main().

209 {
210  ArchiveHandle *AH = _allocAH(FileSpec, fmt, compression, dosync,
211  mode, setupDumpWorker);
212 
213  return (Archive *) AH;
214 }
static ArchiveHandle * _allocAH(const char *FileSpec, const ArchiveFormat fmt, const int compression, bool dosync, ArchiveMode mode, SetupWorkerPtr setupWorkerPtr)
static bool dosync
Definition: pg_dump.c:92
static void setupDumpWorker(Archive *AHX)
Definition: pg_dump.c:1145
void DisconnectDatabase ( Archive AHX)

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

346 {
347  ArchiveHandle *AH = (ArchiveHandle *) AHX;
348  char errbuf[1];
349 
350  if (!AH->connection)
351  return;
352 
353  if (AH->connCancel)
354  {
355  /*
356  * If we have an active query, send a cancel before closing. This is
357  * of no use for a normal exit, but might be helpful during
358  * exit_horribly().
359  */
361  PQcancel(AH->connCancel, errbuf, sizeof(errbuf));
362 
363  /*
364  * Prevent signal handler from sending a cancel after this.
365  */
367  }
368 
369  PQfinish(AH->connection);
370  AH->connection = NULL;
371 }
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3568
PGcancel *volatile connCancel
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:5966
#define NULL
Definition: c.h:229
int PQcancel(PGcancel *cancel, char *errbuf, int errbufsize)
Definition: fe-connect.c:3844
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::include_subscriptions, _dumpOptions::include_subscriptions, _restoreOptions::lockWaitTimeout, _dumpOptions::lockWaitTimeout, NewDumpOptions(), _restoreOptions::no_security_labels, _dumpOptions::no_security_labels, _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;
170  dopt->lockWaitTimeout = ropt->lockWaitTimeout;
173  dopt->sequence_data = ropt->sequence_data;
175 
176  return dopt;
177 }
int column_inserts
Definition: pg_backup.h:146
int disable_triggers
Definition: pg_backup.h:153
int disable_dollar_quoting
Definition: pg_backup.h:73
bool schemaOnly
Definition: pg_backup.h:137
DumpOptions * NewDumpOptions(void)
int sequence_data
Definition: pg_backup.h:170
int include_subscriptions
Definition: pg_backup.h:157
const char * lockWaitTimeout
Definition: pg_backup.h:86
int use_setsessauth
Definition: pg_backup.h:155
int disable_dollar_quoting
Definition: pg_backup.h:144
int column_inserts
Definition: pg_backup.h:75
const char * lockWaitTimeout
Definition: pg_backup.h:141
bool dataOnly
Definition: pg_backup.h:138
bool include_everything
Definition: pg_backup.h:161
char * outputSuperuser
Definition: pg_backup.h:168
int include_everything
Definition: pg_backup.h:87
int no_security_labels
Definition: pg_backup.h:77
int dumpSections
Definition: pg_backup.h:139
int enable_row_security
Definition: pg_backup.h:120
int dump_inserts
Definition: pg_backup.h:145
int outputNoOwner
Definition: pg_backup.h:167
int no_security_labels
Definition: pg_backup.h:148
char * superuser
Definition: pg_backup.h:70
bool aclsSkip
Definition: pg_backup.h:140
int use_setsessauth
Definition: pg_backup.h:68
int enable_row_security
Definition: pg_backup.h:156
int outputCreateDB
Definition: pg_backup.h:164
int outputClean
Definition: pg_backup.h:163
int include_subscriptions
Definition: pg_backup.h:122
int outputNoTablespaces
Definition: pg_backup.h:154
int disable_triggers
Definition: pg_backup.h:66
int EndBlob ( Archive AH,
Oid  oid 
)

Definition at line 1219 of file pg_backup_archiver.c.

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

Referenced by dumpBlobs().

1220 {
1221  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1222 
1223  if (AH->EndBlobPtr)
1224  (*AH->EndBlobPtr) (AH, AH->currToc, oid);
1225 
1226  return 1;
1227 }
struct _tocEntry * currToc
PGconn* GetConnection ( Archive AHX)

Definition at line 374 of file pg_backup_db.c.

References _archiveHandle::connection.

375 {
376  ArchiveHandle *AH = (ArchiveHandle *) AHX;
377 
378  return AH->connection;
379 }
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:161
int dumpSections
Definition: pg_backup.h:139
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 931 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().

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

Definition at line 219 of file pg_backup_archiver.c.

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

Referenced by main().

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

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

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

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

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

Definition at line 248 of file pg_backup_archiver.c.

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

Referenced by _CloseArchive(), and main().

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

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

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

Definition at line 1205 of file pg_backup_archiver.c.

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

Referenced by dumpBlobs().

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

Definition at line 1007 of file pg_backup_archiver.c.

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

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

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