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

Definition at line 226 of file pg_backup.h.

typedef void(* SetupWorkerPtrType)(Archive *AH)

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

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

References WriteData().

Referenced by dumpTableData_insert().

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

Definition at line 227 of file pg_backup_archiver.c.

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

Referenced by main().

228 {
229  int res = 0;
230  ArchiveHandle *AH = (ArchiveHandle *) AHX;
231 
232  (*AH->ClosePtr) (AH);
233 
234  /* Close the output */
235  if (AH->gzOut)
236  res = GZCLOSE(AH->OF);
237  else if (AH->OF != stdout)
238  res = fclose(AH->OF);
239 
240  if (res != 0)
241  exit_horribly(modulename, "could not close output file: %s\n",
242  strerror(errno));
243 }
#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:5934
PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg)
Definition: fe-connect.c:6110
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3491
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:466
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:5818
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:5802
#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:5983
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:5968
void set_archive_cancel_info(ArchiveHandle *AH, PGconn *conn)
Definition: parallel.c:742
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:5881
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 204 of file pg_backup_archiver.c.

References _allocAH().

Referenced by main().

208 {
209  ArchiveHandle *AH = _allocAH(FileSpec, fmt, compression, dosync,
210  mode, setupDumpWorker);
211 
212  return (Archive *) AH;
213 }
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:1142
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. This is
355  * of no use for a normal exit, but might be helpful during
356  * exit_horribly().
357  */
359  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:3491
PGcancel *volatile connCancel
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:5889
#define NULL
Definition: c.h:229
int PQcancel(PGcancel *cancel, char *errbuf, int errbufsize)
Definition: fe-connect.c:3767
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_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;
174 
175  return dopt;
176 }
int column_inserts
Definition: pg_backup.h:145
int disable_triggers
Definition: pg_backup.h:152
int disable_dollar_quoting
Definition: pg_backup.h:73
bool schemaOnly
Definition: pg_backup.h:136
DumpOptions * NewDumpOptions(void)
int sequence_data
Definition: pg_backup.h:167
const char * lockWaitTimeout
Definition: pg_backup.h:86
int use_setsessauth
Definition: pg_backup.h:154
int disable_dollar_quoting
Definition: pg_backup.h:143
int column_inserts
Definition: pg_backup.h:75
const char * lockWaitTimeout
Definition: pg_backup.h:140
bool dataOnly
Definition: pg_backup.h:137
bool include_everything
Definition: pg_backup.h:158
char * outputSuperuser
Definition: pg_backup.h:165
int include_everything
Definition: pg_backup.h:87
int no_security_labels
Definition: pg_backup.h:77
int dumpSections
Definition: pg_backup.h:138
int enable_row_security
Definition: pg_backup.h:120
int dump_inserts
Definition: pg_backup.h:144
int outputNoOwner
Definition: pg_backup.h:164
int no_security_labels
Definition: pg_backup.h:147
char * superuser
Definition: pg_backup.h:70
bool aclsSkip
Definition: pg_backup.h:139
int use_setsessauth
Definition: pg_backup.h:68
int enable_row_security
Definition: pg_backup.h:155
int outputCreateDB
Definition: pg_backup.h:161
int outputClean
Definition: pg_backup.h:160
int outputNoTablespaces
Definition: pg_backup.h:153
int disable_triggers
Definition: pg_backup.h:66
int EndBlob ( Archive AH,
Oid  oid 
)

Definition at line 1218 of file pg_backup_archiver.c.

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

Referenced by dumpBlobs().

1219 {
1220  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1221 
1222  if (AH->EndBlobPtr)
1223  (*AH->EndBlobPtr) (AH, AH->currToc, oid);
1224 
1225  return 1;
1226 }
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:158
int dumpSections
Definition: pg_backup.h:138
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 930 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().

931 {
932  RestoreOptions *opts;
933 
934  opts = (RestoreOptions *) pg_malloc0(sizeof(RestoreOptions));
935 
936  /* set any fields that shouldn't default to zeroes */
937  opts->format = archUnknown;
938  opts->promptPassword = TRI_DEFAULT;
940 
941  return opts;
942 }
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 218 of file pg_backup_archiver.c.

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

Referenced by main().

219 {
220  ArchiveHandle *AH = _allocAH(FileSpec, fmt, 0, true, archModeRead, setupRestoreWorker);
221 
222  return (Archive *) AH;
223 }
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 1089 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().

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

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

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

Definition at line 247 of file pg_backup_archiver.c.

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

Referenced by _CloseArchive(), and main().

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

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

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

Definition at line 1204 of file pg_backup_archiver.c.

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

Referenced by dumpBlobs().

1205 {
1206  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1207 
1208  if (!AH->StartBlobPtr)
1209  exit_horribly(modulename, "large-object output not supported in chosen format\n");
1210 
1211  (*AH->StartBlobPtr) (AH, AH->currToc, oid);
1212 
1213  return 1;
1214 }
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 1006 of file pg_backup_archiver.c.

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

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

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