PostgreSQL Source Code  git master
pg_backup.h File Reference
#include "fe_utils/simple_list.h"
#include "libpq-fe.h"
Include dependency graph for pg_backup.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  _connParams
 
struct  _restoreOptions
 
struct  _dumpOptions
 
struct  Archive
 
struct  CatalogId
 

Macros

#define InvalidDumpId   0
 
#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 _connParams ConnParams
 
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 *AHX, const ConnParams *cparams, bool isReconnect)
 
void DisconnectDatabase (Archive *AHX)
 
PGconnGetConnection (Archive *AHX)
 
void WriteData (Archive *AH, const void *data, size_t dLen)
 
int StartBlob (Archive *AH, Oid oid)
 
int EndBlob (Archive *AH, Oid oid)
 
void CloseArchive (Archive *AH)
 
void SetArchiveOptions (Archive *AH, DumpOptions *dopt, RestoreOptions *ropt)
 
void ProcessArchiveRestoreOptions (Archive *AH)
 
void RestoreArchive (Archive *AH)
 
ArchiveOpenArchive (const char *FileSpec, const ArchiveFormat fmt)
 
ArchiveCreateArchive (const char *FileSpec, const ArchiveFormat fmt, const int compression, bool dosync, ArchiveMode mode, SetupWorkerPtrType setupDumpWorker)
 
void PrintTOCSummary (Archive *AH)
 
RestoreOptionsNewRestoreOptions (void)
 
DumpOptionsNewDumpOptions (void)
 
void InitDumpOptions (DumpOptions *opts)
 
DumpOptionsdumpOptionsFromRestoreOptions (RestoreOptions *ropt)
 
void SortTocFromFile (Archive *AHX)
 
void archputs (const char *s, Archive *AH)
 
int archprintf (Archive *AH, const char *fmt,...) pg_attribute_printf(2
 

Macro Definition Documentation

◆ appendStringLiteralAH

◆ InvalidDumpId

Typedef Documentation

◆ Archive

typedef struct Archive Archive

◆ ArchiveFormat

◆ ArchiveMode

typedef enum _archiveMode ArchiveMode

◆ ConnParams

typedef struct _connParams ConnParams

◆ DataDumperPtr

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

Definition at line 250 of file pg_backup.h.

◆ DumpId

typedef int DumpId

Definition at line 242 of file pg_backup.h.

◆ DumpOptions

typedef struct _dumpOptions DumpOptions

◆ RestoreOptions

◆ SetupWorkerPtrType

typedef void(* SetupWorkerPtrType) (Archive *AH)

Definition at line 252 of file pg_backup.h.

◆ teSection

typedef enum _teSection teSection

◆ trivalue

typedef enum trivalue trivalue

Enumeration Type Documentation

◆ _archiveFormat

Enumerator
archUnknown 
archCustom 
archTar 
archNull 
archDirectory 

Definition at line 37 of file pg_backup.h.

38 {
39  archUnknown = 0,
40  archCustom = 1,
41  archTar = 3,
42  archNull = 4,
43  archDirectory = 5
enum _archiveFormat ArchiveFormat

◆ _archiveMode

Enumerator
archModeAppend 
archModeWrite 
archModeRead 

Definition at line 46 of file pg_backup.h.

47 {
51 } ArchiveMode;
enum _archiveMode ArchiveMode

◆ _teSection

enum _teSection
Enumerator
SECTION_NONE 
SECTION_PRE_DATA 
SECTION_DATA 
SECTION_POST_DATA 

Definition at line 53 of file pg_backup.h.

54 {
55  SECTION_NONE = 1, /* COMMENTs, ACLs, etc; can be anywhere */
56  SECTION_PRE_DATA, /* stuff to be processed before data */
57  SECTION_DATA, /* TABLE DATA, BLOBS, BLOB COMMENTS */
58  SECTION_POST_DATA /* stuff to be processed after data */
59 } teSection;
enum _teSection teSection

◆ trivalue

enum trivalue
Enumerator
TRI_DEFAULT 
TRI_NO 
TRI_YES 
TRI_DEFAULT 
TRI_NO 
TRI_YES 
TRI_DEFAULT 
TRI_NO 
TRI_YES 
TRI_DEFAULT 
TRI_NO 
TRI_YES 

Definition at line 30 of file pg_backup.h.

31 {
33  TRI_NO,
34  TRI_YES
35 } trivalue;
trivalue
Definition: pg_backup.h:30

Function Documentation

◆ archprintf()

int archprintf ( Archive AH,
const char *  fmt,
  ... 
)

◆ archputs()

void archputs ( const char *  s,
Archive AH 
)

Definition at line 1444 of file pg_backup_archiver.c.

References WriteData().

Referenced by dumpTableData_insert().

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

◆ CloseArchive()

void CloseArchive ( Archive AH)

Definition at line 263 of file pg_backup_archiver.c.

References _archiveHandle::ClosePtr, fatal, GZCLOSE, _archiveHandle::gzOut, _archiveHandle::OF, and generate_unaccent_rules::stdout.

Referenced by main().

264 {
265  int res = 0;
266  ArchiveHandle *AH = (ArchiveHandle *) AHX;
267 
268  AH->ClosePtr(AH);
269 
270  /* Close the output */
271  if (AH->gzOut)
272  res = GZCLOSE(AH->OF);
273  else if (AH->OF != stdout)
274  res = fclose(AH->OF);
275 
276  if (res != 0)
277  fatal("could not close output file: %m");
278 }
#define fatal(...)
#define GZCLOSE(fh)
ClosePtrType ClosePtr

◆ ConnectDatabase()

void ConnectDatabase ( Archive AHX,
const ConnParams cparams,
bool  isReconnect 
)

Definition at line 114 of file pg_backup_db.c.

References _check_database_version(), ALWAYS_SECURE_SEARCH_PATH_SQL, Assert, _archiveHandle::connection, CONNECTION_BAD, _connParams::dbname, ExecuteSqlQueryForSingleRow(), fatal, free, i, lengthof, notice_processor(), _connParams::override_dbname, password, pg_strdup(), _connParams::pghost, _connParams::pgport, PQclear(), PQconnectdbParams(), PQconnectionNeedsPassword(), PQconnectionUsedPassword(), PQdb(), PQerrorMessage(), PQfinish(), PQpass(), PQsetNoticeProcessor(), PQstatus(), progname, _connParams::promptPassword, _archiveHandle::savedPassword, set_archive_cancel_info(), simple_prompt(), TRI_NO, TRI_YES, _connParams::username, and values.

Referenced by CloneArchive(), main(), ReconnectToServer(), restore_toc_entries_postfork(), and RestoreArchive().

117 {
118  ArchiveHandle *AH = (ArchiveHandle *) AHX;
119  trivalue prompt_password;
120  char *password;
121  bool new_pass;
122 
123  if (AH->connection)
124  fatal("already connected to a database");
125 
126  /* Never prompt for a password during a reconnection */
127  prompt_password = isReconnect ? TRI_NO : cparams->promptPassword;
128 
129  password = AH->savedPassword;
130 
131  if (prompt_password == TRI_YES && password == NULL)
132  password = simple_prompt("Password: ", false);
133 
134  /*
135  * Start the connection. Loop until we have a password if requested by
136  * backend.
137  */
138  do
139  {
140  const char *keywords[8];
141  const char *values[8];
142  int i = 0;
143 
144  /*
145  * If dbname is a connstring, its entries can override the other
146  * values obtained from cparams; but in turn, override_dbname can
147  * override the dbname component of it.
148  */
149  keywords[i] = "host";
150  values[i++] = cparams->pghost;
151  keywords[i] = "port";
152  values[i++] = cparams->pgport;
153  keywords[i] = "user";
154  values[i++] = cparams->username;
155  keywords[i] = "password";
156  values[i++] = password;
157  keywords[i] = "dbname";
158  values[i++] = cparams->dbname;
159  if (cparams->override_dbname)
160  {
161  keywords[i] = "dbname";
162  values[i++] = cparams->override_dbname;
163  }
164  keywords[i] = "fallback_application_name";
165  values[i++] = progname;
166  keywords[i] = NULL;
167  values[i++] = NULL;
168  Assert(i <= lengthof(keywords));
169 
170  new_pass = false;
171  AH->connection = PQconnectdbParams(keywords, values, true);
172 
173  if (!AH->connection)
174  fatal("could not connect to database");
175 
176  if (PQstatus(AH->connection) == CONNECTION_BAD &&
178  password == NULL &&
179  prompt_password != TRI_NO)
180  {
181  PQfinish(AH->connection);
182  password = simple_prompt("Password: ", false);
183  new_pass = true;
184  }
185  } while (new_pass);
186 
187  /* check to see that the backend connection was successfully made */
188  if (PQstatus(AH->connection) == CONNECTION_BAD)
189  {
190  if (isReconnect)
191  fatal("reconnection to database \"%s\" failed: %s",
192  PQdb(AH->connection) ? PQdb(AH->connection) : "",
194  else
195  fatal("connection to database \"%s\" failed: %s",
196  PQdb(AH->connection) ? PQdb(AH->connection) : "",
198  }
199 
200  /* Start strict; later phases may override this. */
203 
204  if (password && password != AH->savedPassword)
205  free(password);
206 
207  /*
208  * We want to remember connection's actual password, whether or not we got
209  * it by prompting. So we don't just store the password variable.
210  */
212  {
213  if (AH->savedPassword)
214  free(AH->savedPassword);
216  }
217 
218  /* check for version mismatch */
220 
222 
223  /* arrange for SIGINT to issue a query cancel on this connection */
225 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6666
PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg)
Definition: fe-connect.c:6842
char * pgport
Definition: pg_backup.h:66
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4171
char * simple_prompt(const char *prompt, bool echo)
Definition: sprompt.c:38
#define lengthof(array)
Definition: c.h:675
const char * progname
Definition: pg_standby.c:36
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:647
trivalue promptPassword
Definition: pg_backup.h:69
char * dbname
Definition: pg_backup.h:65
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:311
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
char * PQpass(const PGconn *conn)
Definition: fe-connect.c:6532
static char * password
Definition: streamutil.c:53
static void notice_processor(void *arg, const char *message)
Definition: pg_backup_db.c:269
char * override_dbname
Definition: pg_backup.h:72
char * pghost
Definition: pg_backup.h:67
char * username
Definition: pg_backup.h:68
trivalue
Definition: vacuumlo.c:34
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define free(a)
Definition: header.h:65
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:6516
#define Assert(condition)
Definition: c.h:745
#define fatal(...)
#define ALWAYS_SECURE_SEARCH_PATH_SQL
Definition: connect.h:25
static Datum values[MAXATTR]
Definition: bootstrap.c:165
static void _check_database_version(ArchiveHandle *AH)
Definition: pg_backup_db.c:33
int PQconnectionUsedPassword(const PGconn *conn)
Definition: fe-connect.c:6715
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:6700
void set_archive_cancel_info(ArchiveHandle *AH, PGconn *conn)
Definition: parallel.c:749
int i
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6613

◆ CreateArchive()

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

Definition at line 240 of file pg_backup_archiver.c.

References _allocAH().

Referenced by main().

244 {
245  ArchiveHandle *AH = _allocAH(FileSpec, fmt, compression, dosync,
247 
248  return (Archive *) AH;
249 }
static PgChecksumMode mode
Definition: pg_checksums.c:61
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:91
static void setupDumpWorker(Archive *AHX)
Definition: pg_dump.c:1242

◆ DisconnectDatabase()

void DisconnectDatabase ( Archive AHX)

Definition at line 232 of file pg_backup_db.c.

References _archiveHandle::connCancel, _archiveHandle::connection, PQcancel(), PQfinish(), PQTRANS_ACTIVE, PQtransactionStatus(), and set_archive_cancel_info().

Referenced by archive_close_connection(), restore_toc_entries_prefork(), RestoreArchive(), and RunWorker().

233 {
234  ArchiveHandle *AH = (ArchiveHandle *) AHX;
235  char errbuf[1];
236 
237  if (!AH->connection)
238  return;
239 
240  if (AH->connCancel)
241  {
242  /*
243  * If we have an active query, send a cancel before closing, ignoring
244  * any errors. This is of no use for a normal exit, but might be
245  * helpful during fatal().
246  */
248  (void) PQcancel(AH->connCancel, errbuf, sizeof(errbuf));
249 
250  /*
251  * Prevent signal handler from sending a cancel after this.
252  */
253  set_archive_cancel_info(AH, NULL);
254  }
255 
256  PQfinish(AH->connection);
257  AH->connection = NULL;
258 }
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4171
PGcancel *volatile connCancel
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:6621
int PQcancel(PGcancel *cancel, char *errbuf, int errbufsize)
Definition: fe-connect.c:4447
void set_archive_cancel_info(ArchiveHandle *AH, PGconn *conn)
Definition: parallel.c:749

◆ dumpOptionsFromRestoreOptions()

DumpOptions* dumpOptionsFromRestoreOptions ( RestoreOptions ropt)

Definition at line 177 of file pg_backup_archiver.c.

References _restoreOptions::aclsSkip, _dumpOptions::aclsSkip, _restoreOptions::column_inserts, _dumpOptions::column_inserts, _restoreOptions::cparams, _dumpOptions::cparams, _restoreOptions::createDB, _restoreOptions::dataOnly, _dumpOptions::dataOnly, _connParams::dbname, _restoreOptions::disable_dollar_quoting, _dumpOptions::disable_dollar_quoting, _restoreOptions::disable_triggers, _dumpOptions::disable_triggers, _restoreOptions::dropSchema, _restoreOptions::dump_inserts, _dumpOptions::dump_inserts, _restoreOptions::dumpSections, _dumpOptions::dumpSections, _restoreOptions::enable_row_security, _dumpOptions::enable_row_security, _restoreOptions::if_exists, _dumpOptions::if_exists, _restoreOptions::include_everything, _dumpOptions::include_everything, _restoreOptions::lockWaitTimeout, _dumpOptions::lockWaitTimeout, NewDumpOptions(), _restoreOptions::no_comments, _dumpOptions::no_comments, _restoreOptions::no_publications, _dumpOptions::no_publications, _restoreOptions::no_security_labels, _dumpOptions::no_security_labels, _restoreOptions::no_subscriptions, _dumpOptions::no_subscriptions, _restoreOptions::noOwner, _restoreOptions::noTablespace, _dumpOptions::outputClean, _dumpOptions::outputCreateDB, _dumpOptions::outputNoOwner, _dumpOptions::outputNoTablespaces, _dumpOptions::outputSuperuser, pg_strdup(), _connParams::pghost, _connParams::pgport, _connParams::promptPassword, _restoreOptions::schemaOnly, _dumpOptions::schemaOnly, _restoreOptions::sequence_data, _dumpOptions::sequence_data, _restoreOptions::superuser, _restoreOptions::use_setsessauth, _dumpOptions::use_setsessauth, and _connParams::username.

Referenced by SetArchiveOptions().

178 {
179  DumpOptions *dopt = NewDumpOptions();
180 
181  /* this is the inverse of what's at the end of pg_dump.c's main() */
182  dopt->cparams.dbname = ropt->cparams.dbname ? pg_strdup(ropt->cparams.dbname) : NULL;
183  dopt->cparams.pgport = ropt->cparams.pgport ? pg_strdup(ropt->cparams.pgport) : NULL;
184  dopt->cparams.pghost = ropt->cparams.pghost ? pg_strdup(ropt->cparams.pghost) : NULL;
185  dopt->cparams.username = ropt->cparams.username ? pg_strdup(ropt->cparams.username) : NULL;
187  dopt->outputClean = ropt->dropSchema;
188  dopt->dataOnly = ropt->dataOnly;
189  dopt->schemaOnly = ropt->schemaOnly;
190  dopt->if_exists = ropt->if_exists;
191  dopt->column_inserts = ropt->column_inserts;
192  dopt->dumpSections = ropt->dumpSections;
193  dopt->aclsSkip = ropt->aclsSkip;
194  dopt->outputSuperuser = ropt->superuser;
195  dopt->outputCreateDB = ropt->createDB;
196  dopt->outputNoOwner = ropt->noOwner;
197  dopt->outputNoTablespaces = ropt->noTablespace;
198  dopt->disable_triggers = ropt->disable_triggers;
199  dopt->use_setsessauth = ropt->use_setsessauth;
201  dopt->dump_inserts = ropt->dump_inserts;
202  dopt->no_comments = ropt->no_comments;
203  dopt->no_publications = ropt->no_publications;
205  dopt->no_subscriptions = ropt->no_subscriptions;
206  dopt->lockWaitTimeout = ropt->lockWaitTimeout;
209  dopt->sequence_data = ropt->sequence_data;
210 
211  return dopt;
212 }
int column_inserts
Definition: pg_backup.h:155
int disable_triggers
Definition: pg_backup.h:164
int disable_dollar_quoting
Definition: pg_backup.h:87
char * pgport
Definition: pg_backup.h:66
bool schemaOnly
Definition: pg_backup.h:146
int no_subscriptions
Definition: pg_backup.h:160
int no_publications
Definition: pg_backup.h:159
DumpOptions * NewDumpOptions(void)
int sequence_data
Definition: pg_backup.h:180
trivalue promptPassword
Definition: pg_backup.h:69
char * dbname
Definition: pg_backup.h:65
const char * lockWaitTimeout
Definition: pg_backup.h:103
int use_setsessauth
Definition: pg_backup.h:166
int disable_dollar_quoting
Definition: pg_backup.h:154
int column_inserts
Definition: pg_backup.h:89
const char * lockWaitTimeout
Definition: pg_backup.h:150
bool dataOnly
Definition: pg_backup.h:147
bool include_everything
Definition: pg_backup.h:171
char * outputSuperuser
Definition: pg_backup.h:178
ConnParams cparams
Definition: pg_backup.h:141
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
int include_everything
Definition: pg_backup.h:104
int no_security_labels
Definition: pg_backup.h:93
int dumpSections
Definition: pg_backup.h:148
int enable_row_security
Definition: pg_backup.h:134
int dump_inserts
Definition: pg_backup.h:151
int outputNoOwner
Definition: pg_backup.h:177
ConnParams cparams
Definition: pg_backup.h:124
int no_security_labels
Definition: pg_backup.h:158
char * pghost
Definition: pg_backup.h:67
char * username
Definition: pg_backup.h:68
int no_subscriptions
Definition: pg_backup.h:94
char * superuser
Definition: pg_backup.h:84
bool aclsSkip
Definition: pg_backup.h:149
int use_setsessauth
Definition: pg_backup.h:82
int enable_row_security
Definition: pg_backup.h:167
int outputCreateDB
Definition: pg_backup.h:174
int outputClean
Definition: pg_backup.h:173
int no_comments
Definition: pg_backup.h:157
int outputNoTablespaces
Definition: pg_backup.h:165
int no_publications
Definition: pg_backup.h:92
int disable_triggers
Definition: pg_backup.h:80

◆ EndBlob()

int EndBlob ( Archive AH,
Oid  oid 
)

Definition at line 1233 of file pg_backup_archiver.c.

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

Referenced by dumpBlobs().

1234 {
1235  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1236 
1237  if (AH->EndBlobPtr)
1238  AH->EndBlobPtr(AH, AH->currToc, oid);
1239 
1240  return 1;
1241 }
struct _tocEntry * currToc
EndBlobPtrType EndBlobPtr

◆ GetConnection()

PGconn* GetConnection ( Archive AHX)

Definition at line 261 of file pg_backup_db.c.

References _archiveHandle::connection.

262 {
263  ArchiveHandle *AH = (ArchiveHandle *) AHX;
264 
265  return AH->connection;
266 }

◆ InitDumpOptions()

void InitDumpOptions ( DumpOptions opts)

Definition at line 163 of file pg_backup_archiver.c.

References _dumpOptions::cparams, DUMP_UNSECTIONED, _dumpOptions::dumpSections, _dumpOptions::include_everything, _connParams::promptPassword, and TRI_DEFAULT.

Referenced by main(), and NewDumpOptions().

164 {
165  memset(opts, 0, sizeof(DumpOptions));
166  /* set any fields that shouldn't default to zeroes */
167  opts->include_everything = true;
170 }
trivalue promptPassword
Definition: pg_backup.h:69
bool include_everything
Definition: pg_backup.h:171
ConnParams cparams
Definition: pg_backup.h:141
int dumpSections
Definition: pg_backup.h:148

◆ NewDumpOptions()

DumpOptions* NewDumpOptions ( void  )

Definition at line 151 of file pg_backup_archiver.c.

References InitDumpOptions(), and pg_malloc().

Referenced by dumpOptionsFromRestoreOptions().

152 {
153  DumpOptions *opts = (DumpOptions *) pg_malloc(sizeof(DumpOptions));
154 
155  InitDumpOptions(opts);
156  return opts;
157 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
void InitDumpOptions(DumpOptions *opts)

◆ NewRestoreOptions()

RestoreOptions* NewRestoreOptions ( void  )

Definition at line 964 of file pg_backup_archiver.c.

References archUnknown, _restoreOptions::cparams, DUMP_UNSECTIONED, _restoreOptions::dumpSections, _restoreOptions::format, pg_malloc0(), _connParams::promptPassword, and TRI_DEFAULT.

Referenced by _CloseArchive(), and main().

965 {
966  RestoreOptions *opts;
967 
968  opts = (RestoreOptions *) pg_malloc0(sizeof(RestoreOptions));
969 
970  /* set any fields that shouldn't default to zeroes */
971  opts->format = archUnknown;
974 
975  return opts;
976 }
trivalue promptPassword
Definition: pg_backup.h:69
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
ConnParams cparams
Definition: pg_backup.h:124

◆ OpenArchive()

Archive* OpenArchive ( const char *  FileSpec,
const ArchiveFormat  fmt 
)

Definition at line 254 of file pg_backup_archiver.c.

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

Referenced by main().

255 {
256  ArchiveHandle *AH = _allocAH(FileSpec, fmt, 0, true, archModeRead, setupRestoreWorker);
257 
258  return (Archive *) AH;
259 }
static ArchiveHandle * _allocAH(const char *FileSpec, const ArchiveFormat fmt, const int compression, bool dosync, ArchiveMode mode, SetupWorkerPtrType setupWorkerPtr)
static void setupRestoreWorker(Archive *AHX)

◆ PrintTOCSummary()

void PrintTOCSummary ( Archive AH)

Definition at line 1115 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, PGDUMP_STRFTIME_FMT, _archiveHandle::public, REQ_DATA, REQ_SCHEMA, RestoreOutput(), Archive::ropt, sanitize_line(), 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().

1116 {
1117  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1118  RestoreOptions *ropt = AH->public.ropt;
1119  TocEntry *te;
1120  teSection curSection;
1121  OutputContext sav;
1122  const char *fmtName;
1123  char stamp_str[64];
1124 
1125  sav = SaveOutput(AH);
1126  if (ropt->filename)
1127  SetOutput(AH, ropt->filename, 0 /* no compression */ );
1128 
1129  if (strftime(stamp_str, sizeof(stamp_str), PGDUMP_STRFTIME_FMT,
1130  localtime(&AH->createDate)) == 0)
1131  strcpy(stamp_str, "[unknown]");
1132 
1133  ahprintf(AH, ";\n; Archive created at %s\n", stamp_str);
1134  ahprintf(AH, "; dbname: %s\n; TOC Entries: %d\n; Compression: %d\n",
1135  sanitize_line(AH->archdbname, false),
1136  AH->tocCount, AH->compression);
1137 
1138  switch (AH->format)
1139  {
1140  case archCustom:
1141  fmtName = "CUSTOM";
1142  break;
1143  case archDirectory:
1144  fmtName = "DIRECTORY";
1145  break;
1146  case archTar:
1147  fmtName = "TAR";
1148  break;
1149  default:
1150  fmtName = "UNKNOWN";
1151  }
1152 
1153  ahprintf(AH, "; Dump Version: %d.%d-%d\n",
1155  ahprintf(AH, "; Format: %s\n", fmtName);
1156  ahprintf(AH, "; Integer: %d bytes\n", (int) AH->intSize);
1157  ahprintf(AH, "; Offset: %d bytes\n", (int) AH->offSize);
1158  if (AH->archiveRemoteVersion)
1159  ahprintf(AH, "; Dumped from database version: %s\n",
1160  AH->archiveRemoteVersion);
1161  if (AH->archiveDumpVersion)
1162  ahprintf(AH, "; Dumped by pg_dump version: %s\n",
1163  AH->archiveDumpVersion);
1164 
1165  ahprintf(AH, ";\n;\n; Selected TOC Entries:\n;\n");
1166 
1167  curSection = SECTION_PRE_DATA;
1168  for (te = AH->toc->next; te != AH->toc; te = te->next)
1169  {
1170  if (te->section != SECTION_NONE)
1171  curSection = te->section;
1172  if (ropt->verbose ||
1173  (_tocEntryRequired(te, curSection, AH) & (REQ_SCHEMA | REQ_DATA)) != 0)
1174  {
1175  char *sanitized_name;
1176  char *sanitized_schema;
1177  char *sanitized_owner;
1178 
1179  /*
1180  */
1181  sanitized_name = sanitize_line(te->tag, false);
1182  sanitized_schema = sanitize_line(te->namespace, true);
1183  sanitized_owner = sanitize_line(te->owner, false);
1184 
1185  ahprintf(AH, "%d; %u %u %s %s %s %s\n", te->dumpId,
1186  te->catalogId.tableoid, te->catalogId.oid,
1187  te->desc, sanitized_schema, sanitized_name,
1188  sanitized_owner);
1189 
1190  free(sanitized_name);
1191  free(sanitized_schema);
1192  free(sanitized_owner);
1193  }
1194  if (ropt->verbose && te->nDeps > 0)
1195  {
1196  int i;
1197 
1198  ahprintf(AH, ";\tdepends on:");
1199  for (i = 0; i < te->nDeps; i++)
1200  ahprintf(AH, " %d", te->dependencies[i]);
1201  ahprintf(AH, "\n");
1202  }
1203  }
1204 
1205  /* Enforce strict names checking */
1206  if (ropt->strict_names)
1207  StrictNamesCheck(ropt);
1208 
1209  if (ropt->filename)
1210  RestoreOutput(AH, sav);
1211 }
struct _tocEntry * next
Oid tableoid
Definition: pg_backup.h:238
RestoreOptions * ropt
Definition: pg_backup.h:191
CatalogId catalogId
static void SetOutput(ArchiveHandle *AH, const char *filename, int compression)
teSection section
const char * filename
Definition: pg_backup.h:97
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)
#define free(a)
Definition: header.h:65
enum _teSection teSection
#define PGDUMP_STRFTIME_FMT
Definition: dumputils.h:33
static void RestoreOutput(ArchiveHandle *AH, OutputContext savedContext)
int i
static char * sanitize_line(const char *str, bool want_hyphen)
static teReqs _tocEntryRequired(TocEntry *te, teSection curSection, ArchiveHandle *AH)

◆ ProcessArchiveRestoreOptions()

void ProcessArchiveRestoreOptions ( Archive AH)

Definition at line 295 of file pg_backup_archiver.c.

References _tocEntryRequired(), archModeRead, fatal, _archiveHandle::mode, _tocEntry::next, pg_log_warning, _archiveHandle::public, _tocEntry::reqs, Archive::ropt, _tocEntry::section, SECTION_DATA, SECTION_NONE, SECTION_POST_DATA, SECTION_PRE_DATA, _restoreOptions::strict_names, StrictNamesCheck(), and _archiveHandle::toc.

Referenced by main().

296 {
297  ArchiveHandle *AH = (ArchiveHandle *) AHX;
298  RestoreOptions *ropt = AH->public.ropt;
299  TocEntry *te;
300  teSection curSection;
301 
302  /* Decide which TOC entries will be dumped/restored, and mark them */
303  curSection = SECTION_PRE_DATA;
304  for (te = AH->toc->next; te != AH->toc; te = te->next)
305  {
306  /*
307  * When writing an archive, we also take this opportunity to check
308  * that we have generated the entries in a sane order that respects
309  * the section divisions. When reading, don't complain, since buggy
310  * old versions of pg_dump might generate out-of-order archives.
311  */
312  if (AH->mode != archModeRead)
313  {
314  switch (te->section)
315  {
316  case SECTION_NONE:
317  /* ok to be anywhere */
318  break;
319  case SECTION_PRE_DATA:
320  if (curSection != SECTION_PRE_DATA)
321  pg_log_warning("archive items not in correct section order");
322  break;
323  case SECTION_DATA:
324  if (curSection == SECTION_POST_DATA)
325  pg_log_warning("archive items not in correct section order");
326  break;
327  case SECTION_POST_DATA:
328  /* ok no matter which section we were in */
329  break;
330  default:
331  fatal("unexpected section code %d",
332  (int) te->section);
333  break;
334  }
335  }
336 
337  if (te->section != SECTION_NONE)
338  curSection = te->section;
339 
340  te->reqs = _tocEntryRequired(te, curSection, AH);
341  }
342 
343  /* Enforce strict names checking */
344  if (ropt->strict_names)
345  StrictNamesCheck(ropt);
346 }
struct _tocEntry * next
RestoreOptions * ropt
Definition: pg_backup.h:191
teSection section
struct _tocEntry * toc
static void StrictNamesCheck(RestoreOptions *ropt)
enum _teSection teSection
#define fatal(...)
static teReqs _tocEntryRequired(TocEntry *te, teSection curSection, ArchiveHandle *AH)
#define pg_log_warning(...)
Definition: pgfnames.c:24

◆ RestoreArchive()

void RestoreArchive ( Archive AH)

Definition at line 350 of file pg_backup_archiver.c.

References _becomeOwner(), _doSetFixedOutputState(), _selectOutputSchema(), _tocEntryRestorePass(), ahprintf(), appendPQExpBuffer(), appendPQExpBufferStr(), _archiveHandle::archiveDumpVersion, _archiveHandle::archiveRemoteVersion, Assert, buildTocEntryArrays(), _tocEntry::catalogId, _archiveHandle::ClonePtr, CommitTransaction(), _restoreOptions::compression, _archiveHandle::compression, ConnectDatabase(), _archiveHandle::connection, _restoreOptions::cparams, _archiveHandle::createDate, _restoreOptions::createDB, createPQExpBuffer(), _archiveHandle::currentTE, _archiveHandle::currSchema, PQExpBufferData::data, _restoreOptions::dataOnly, _tocEntry::desc, destroyPQExpBuffer(), DisconnectDatabase(), DropBlobIfExists(), _restoreOptions::dropSchema, _tocEntry::dropStmt, dumpTimestamp(), fatal, _restoreOptions::filename, free, _tocEntry::hadDumper, _restoreOptions::if_exists, K_VERS_1_3, K_VERS_1_8, Archive::maxRemoteVersion, Archive::minRemoteVersion, _tocEntry::next, _archiveHandle::noTocComments, Archive::numWorkers, CatalogId::oid, ParallelBackupEnd(), ParallelBackupStart(), pending_list_header_init(), pg_free(), pg_log_info, pg_log_warning, pg_strdup(), _archiveHandle::PrepParallelRestorePtr, _tocEntry::prev, _archiveHandle::PrintTocDataPtr, _archiveHandle::public, _archiveHandle::ReopenPtr, REQ_DATA, REQ_SCHEMA, _tocEntry::reqs, RESTORE_PASS_ACL, RESTORE_PASS_MAIN, restore_toc_entries_parallel(), restore_toc_entries_postfork(), restore_toc_entries_prefork(), restore_toc_entry(), RestoreOutput(), Archive::ropt, SaveOutput(), SetOutput(), _restoreOptions::single_txn, snprintf, _archiveHandle::stage, STAGE_FINALIZING, STAGE_INITIALIZING, STAGE_PROCESSING, StartTransaction(), _tocEntry::tag, _archiveHandle::toc, _archiveHandle::tocsByDumpId, _restoreOptions::useDB, Archive::verbose, and _archiveHandle::version.

Referenced by _CloseArchive(), and main().

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

◆ SetArchiveOptions()

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

Definition at line 282 of file pg_backup_archiver.c.

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

Referenced by _CloseArchive(), and main().

283 {
284  /* Caller can omit dump options, in which case we synthesize them */
285  if (dopt == NULL && ropt != NULL)
286  dopt = dumpOptionsFromRestoreOptions(ropt);
287 
288  /* Save options for later access */
289  AH->dopt = dopt;
290  AH->ropt = ropt;
291 }
RestoreOptions * ropt
Definition: pg_backup.h:191
DumpOptions * dopt
Definition: pg_backup.h:190
DumpOptions * dumpOptionsFromRestoreOptions(RestoreOptions *ropt)

◆ SortTocFromFile()

void SortTocFromFile ( Archive AHX)

Definition at line 1363 of file pg_backup_archiver.c.

References _moveBefore(), StringInfoData::data, fatal, getTocEntryByDumpId(), _restoreOptions::idWanted, initStringInfo(), StringInfoData::len, _archiveHandle::maxDumpId, PG_BINARY_R, pg_free(), pg_get_line_buf(), pg_log_warning, pg_malloc0(), _archiveHandle::public, Archive::ropt, _archiveHandle::toc, and _restoreOptions::tocFile.

Referenced by main().

1364 {
1365  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1366  RestoreOptions *ropt = AH->public.ropt;
1367  FILE *fh;
1368  StringInfoData linebuf;
1369 
1370  /* Allocate space for the 'wanted' array, and init it */
1371  ropt->idWanted = (bool *) pg_malloc0(sizeof(bool) * AH->maxDumpId);
1372 
1373  /* Setup the file */
1374  fh = fopen(ropt->tocFile, PG_BINARY_R);
1375  if (!fh)
1376  fatal("could not open TOC file \"%s\": %m", ropt->tocFile);
1377 
1378  initStringInfo(&linebuf);
1379 
1380  while (pg_get_line_buf(fh, &linebuf))
1381  {
1382  char *cmnt;
1383  char *endptr;
1384  DumpId id;
1385  TocEntry *te;
1386 
1387  /* Truncate line at comment, if any */
1388  cmnt = strchr(linebuf.data, ';');
1389  if (cmnt != NULL)
1390  {
1391  cmnt[0] = '\0';
1392  linebuf.len = cmnt - linebuf.data;
1393  }
1394 
1395  /* Ignore if all blank */
1396  if (strspn(linebuf.data, " \t\r\n") == linebuf.len)
1397  continue;
1398 
1399  /* Get an ID, check it's valid and not already seen */
1400  id = strtol(linebuf.data, &endptr, 10);
1401  if (endptr == linebuf.data || id <= 0 || id > AH->maxDumpId ||
1402  ropt->idWanted[id - 1])
1403  {
1404  pg_log_warning("line ignored: %s", linebuf.data);
1405  continue;
1406  }
1407 
1408  /* Find TOC entry */
1409  te = getTocEntryByDumpId(AH, id);
1410  if (!te)
1411  fatal("could not find entry for ID %d",
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->toc, te);
1429  }
1430 
1431  pg_free(linebuf.data);
1432 
1433  if (fclose(fh) != 0)
1434  fatal("could not close TOC file: %m");
1435 }
bool pg_get_line_buf(FILE *stream, StringInfo buf)
Definition: pg_get_line.c:88
int DumpId
Definition: pg_backup.h:242
RestoreOptions * ropt
Definition: pg_backup.h:191
#define PG_BINARY_R
Definition: c.h:1213
static void _moveBefore(TocEntry *pos, TocEntry *te)
bool * idWanted
Definition: pg_backup.h:133
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
struct _tocEntry * toc
TocEntry * getTocEntryByDumpId(ArchiveHandle *AH, DumpId id)
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
#define fatal(...)
void pg_free(void *ptr)
Definition: fe_memutils.c:105
#define pg_log_warning(...)
Definition: pgfnames.c:24
char * tocFile
Definition: pg_backup.h:107

◆ StartBlob()

int StartBlob ( Archive AH,
Oid  oid 
)

Definition at line 1219 of file pg_backup_archiver.c.

References _archiveHandle::currToc, fatal, and _archiveHandle::StartBlobPtr.

Referenced by dumpBlobs().

1220 {
1221  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1222 
1223  if (!AH->StartBlobPtr)
1224  fatal("large-object output not supported in chosen format");
1225 
1226  AH->StartBlobPtr(AH, AH->currToc, oid);
1227 
1228  return 1;
1229 }
StartBlobPtrType StartBlobPtr
struct _tocEntry * currToc
#define fatal(...)

◆ WriteData()

void WriteData ( Archive AH,
const void *  data,
size_t  dLen 
)

Definition at line 1036 of file pg_backup_archiver.c.

References _archiveHandle::currToc, fatal, and _archiveHandle::WriteDataPtr.

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

1037 {
1038  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1039 
1040  if (!AH->currToc)
1041  fatal("internal error -- WriteData cannot be called outside the context of a DataDumper routine");
1042 
1043  AH->WriteDataPtr(AH, data, dLen);
1044 }
struct _tocEntry * currToc
WriteDataPtrType WriteDataPtr
#define fatal(...)