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

Definition at line 228 of file pg_backup.h.

typedef void(* SetupWorkerPtr)(Archive *AH)

Definition at line 232 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(), 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:228
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:226
int archprintf ( Archive AH,
const char *  fmt,
  ... 
)
void archputs ( const char *  s,
Archive AH 
)

Definition at line 1415 of file pg_backup_archiver.c.

References WriteData().

Referenced by dumpTableData_insert().

1416 {
1417  WriteData(AH, s, strlen(s));
1418  return;
1419 }
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
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:44
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:5960
PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg)
Definition: fe-connect.c:6136
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3517
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:471
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:5844
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:130
#define free(a)
Definition: header.h:60
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:5828
#define NULL
Definition: c.h:226
char * dbname
Definition: streamutil.c:41
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:6009
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:5994
void set_archive_cancel_info(ArchiveHandle *AH, PGconn *conn)
Definition: parallel.c:744
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:5907
char * pgport
Definition: pgbench.c:181
Archive* CreateArchive ( const char *  FileSpec,
const ArchiveFormat  fmt,
const int  compression,
ArchiveMode  mode,
SetupWorkerPtr  setupDumpWorker 
)

Definition at line 206 of file pg_backup_archiver.c.

References _allocAH().

Referenced by main().

209 {
210  ArchiveHandle *AH = _allocAH(FileSpec, fmt, compression, mode, setupDumpWorker);
211 
212  return (Archive *) AH;
213 }
static ArchiveHandle * _allocAH(const char *FileSpec, const ArchiveFormat fmt, const int compression, ArchiveMode mode, SetupWorkerPtr setupWorkerPtr)
static void setupDumpWorker(Archive *AHX)
Definition: pg_dump.c:1132
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:3517
PGcancel *volatile connCancel
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:5915
#define NULL
Definition: c.h:226
int PQcancel(PGcancel *cancel, char *errbuf, int errbufsize)
Definition: fe-connect.c:3793
void set_archive_cancel_info(ArchiveHandle *AH, PGconn *conn)
Definition: parallel.c:744
DumpOptions* dumpOptionsFromRestoreOptions ( RestoreOptions ropt)

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

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

Definition at line 1190 of file pg_backup_archiver.c.

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

Referenced by dumpBlobs().

1191 {
1192  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1193 
1194  if (AH->EndBlobPtr)
1195  (*AH->EndBlobPtr) (AH, AH->currToc, oid);
1196 
1197  return 1;
1198 }
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 136 of file pg_backup_archiver.c.

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

Referenced by main(), and NewDumpOptions().

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

Definition at line 124 of file pg_backup_archiver.c.

References InitDumpOptions(), and pg_malloc().

Referenced by dumpOptionsFromRestoreOptions().

125 {
126  DumpOptions *opts = (DumpOptions *) pg_malloc(sizeof(DumpOptions));
127 
128  InitDumpOptions(opts);
129  return opts;
130 }
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, archModeRead, setupRestoreWorker);
221 
222  return (Archive *) AH;
223 }
static void setupRestoreWorker(Archive *AHX)
static ArchiveHandle * _allocAH(const char *FileSpec, const ArchiveFormat fmt, const int compression, ArchiveMode mode, SetupWorkerPtr setupWorkerPtr)
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, i, _archiveHandle::intSize, _tocEntry::nDeps, _tocEntry::next, _archiveHandle::offSize, CatalogId::oid, _tocEntry::owner, PGDUMP_STRFTIME_FMT, _archiveHandle::public, 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",
1109  AH->archdbname, AH->tocCount, AH->compression);
1110 
1111  switch (AH->format)
1112  {
1113  case archCustom:
1114  fmtName = "CUSTOM";
1115  break;
1116  case archDirectory:
1117  fmtName = "DIRECTORY";
1118  break;
1119  case archTar:
1120  fmtName = "TAR";
1121  break;
1122  default:
1123  fmtName = "UNKNOWN";
1124  }
1125 
1126  ahprintf(AH, "; Dump Version: %d.%d-%d\n",
1128  ahprintf(AH, "; Format: %s\n", fmtName);
1129  ahprintf(AH, "; Integer: %d bytes\n", (int) AH->intSize);
1130  ahprintf(AH, "; Offset: %d bytes\n", (int) AH->offSize);
1131  if (AH->archiveRemoteVersion)
1132  ahprintf(AH, "; Dumped from database version: %s\n",
1133  AH->archiveRemoteVersion);
1134  if (AH->archiveDumpVersion)
1135  ahprintf(AH, "; Dumped by pg_dump version: %s\n",
1136  AH->archiveDumpVersion);
1137 
1138  ahprintf(AH, ";\n;\n; Selected TOC Entries:\n;\n");
1139 
1140  curSection = SECTION_PRE_DATA;
1141  for (te = AH->toc->next; te != AH->toc; te = te->next)
1142  {
1143  if (te->section != SECTION_NONE)
1144  curSection = te->section;
1145  if (ropt->verbose ||
1146  (_tocEntryRequired(te, curSection, ropt) & (REQ_SCHEMA | REQ_DATA)) != 0)
1147  ahprintf(AH, "%d; %u %u %s %s %s %s\n", te->dumpId,
1148  te->catalogId.tableoid, te->catalogId.oid,
1149  te->desc, te->namespace ? te->namespace : "-",
1150  te->tag, te->owner);
1151  if (ropt->verbose && te->nDeps > 0)
1152  {
1153  int i;
1154 
1155  ahprintf(AH, ";\tdepends on:");
1156  for (i = 0; i < te->nDeps; i++)
1157  ahprintf(AH, " %d", te->dependencies[i]);
1158  ahprintf(AH, "\n");
1159  }
1160  }
1161 
1162  /* Enforce strict names checking */
1163  if (ropt->strict_names)
1164  StrictNamesCheck(ropt);
1165 
1166  if (ropt->filename)
1167  RestoreOutput(AH, sav);
1168 }
struct _tocEntry * next
Oid tableoid
Definition: pg_backup.h:224
RestoreOptions * ropt
Definition: pg_backup.h:179
CatalogId catalogId
static void SetOutput(ArchiveHandle *AH, const char *filename, int compression)
teSection section
const char * filename
Definition: pg_backup.h:80
struct _tocEntry * toc
DumpId * dependencies
#define ARCHIVE_REV(version)
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)
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:179
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, 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
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:179
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:911
void ParallelBackupEnd(ArchiveHandle *AH, ParallelState *pstate)
Definition: parallel.c:1081
struct _tocEntry * prev
ArchiverStage stage
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
int verbose
Definition: pg_backup.h:181
static OutputContext SaveOutput(ArchiveHandle *AH)
static void StartTransaction(void)
Definition: xact.c:1800
#define free(a)
Definition: header.h:60
void write_msg(const char *modulename, const char *fmt,...)
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
static void _selectOutputSchema(ArchiveHandle *AH, const char *schemaName)
int numWorkers
Definition: pg_backup.h:189
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:1938
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 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:179
DumpOptions * dopt
Definition: pg_backup.h:178
DumpOptions * dumpOptionsFromRestoreOptions(RestoreOptions *ropt)
#define NULL
Definition: c.h:226
void SortTocFromFile ( Archive AHX)

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

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

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

Referenced by dumpBlobs().

1177 {
1178  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1179 
1180  if (!AH->StartBlobPtr)
1181  exit_horribly(modulename, "large-object output not supported in chosen format\n");
1182 
1183  (*AH->StartBlobPtr) (AH, AH->currToc, oid);
1184 
1185  return 1;
1186 }
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 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
WriteDataPtr WriteDataPtr
void exit_horribly(const char *modulename, const char *fmt,...)
static const char * modulename