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, const 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, const void *userArg)

Definition at line 253 of file pg_backup.h.

◆ DumpId

typedef int DumpId

Definition at line 245 of file pg_backup.h.

◆ DumpOptions

typedef struct _dumpOptions DumpOptions

◆ RestoreOptions

◆ SetupWorkerPtrType

typedef void(* SetupWorkerPtrType) (Archive *AH)

Definition at line 255 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 1445 of file pg_backup_archiver.c.

References WriteData().

Referenced by dumpTableData_insert().

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

◆ CloseArchive()

void CloseArchive ( Archive AH)

Definition at line 264 of file pg_backup_archiver.c.

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

Referenced by main().

265 {
266  int res = 0;
267  ArchiveHandle *AH = (ArchiveHandle *) AHX;
268 
269  AH->ClosePtr(AH);
270 
271  /* Close the output */
272  if (AH->gzOut)
273  res = GZCLOSE(AH->OF);
274  else if (AH->OF != stdout)
275  res = fclose(AH->OF);
276 
277  if (res != 0)
278  fatal("could not close output file: %m");
279 }
#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(), 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 failed: %s",
193  else
194  fatal("%s",
196  }
197 
198  /* Start strict; later phases may override this. */
201 
202  if (password && password != AH->savedPassword)
203  free(password);
204 
205  /*
206  * We want to remember connection's actual password, whether or not we got
207  * it by prompting. So we don't just store the password variable.
208  */
210  {
211  if (AH->savedPassword)
212  free(AH->savedPassword);
214  }
215 
216  /* check for version mismatch */
218 
220 
221  /* arrange for SIGINT to issue a query cancel on this connection */
223 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6735
const char * progname
Definition: main.c:46
PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg)
Definition: fe-connect.c:6895
char * pgport
Definition: pg_backup.h:66
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4229
char * simple_prompt(const char *prompt, bool echo)
Definition: sprompt.c:38
#define lengthof(array)
Definition: c.h:734
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:662
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:309
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
char * PQpass(const PGconn *conn)
Definition: fe-connect.c:6597
static char * password
Definition: streamutil.c:53
static void notice_processor(void *arg, const char *message)
Definition: pg_backup_db.c:267
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:680
#define free(a)
Definition: header.h:65
#define Assert(condition)
Definition: c.h:804
#define fatal(...)
#define ALWAYS_SECURE_SEARCH_PATH_SQL
Definition: connect.h:25
static Datum values[MAXATTR]
Definition: bootstrap.c:166
static void _check_database_version(ArchiveHandle *AH)
Definition: pg_backup_db.c:33
int PQconnectionUsedPassword(const PGconn *conn)
Definition: fe-connect.c:6793
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:6778
void set_archive_cancel_info(ArchiveHandle *AH, PGconn *conn)
Definition: parallel.c:735
int i
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6682

◆ CreateArchive()

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

Definition at line 241 of file pg_backup_archiver.c.

References _allocAH().

Referenced by main().

245 {
246  ArchiveHandle *AH = _allocAH(FileSpec, fmt, compression, dosync,
248 
249  return (Archive *) AH;
250 }
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:1275

◆ DisconnectDatabase()

void DisconnectDatabase ( Archive AHX)

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

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

◆ dumpOptionsFromRestoreOptions()

DumpOptions* dumpOptionsFromRestoreOptions ( RestoreOptions ropt)

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

179 {
180  DumpOptions *dopt = NewDumpOptions();
181 
182  /* this is the inverse of what's at the end of pg_dump.c's main() */
183  dopt->cparams.dbname = ropt->cparams.dbname ? pg_strdup(ropt->cparams.dbname) : NULL;
184  dopt->cparams.pgport = ropt->cparams.pgport ? pg_strdup(ropt->cparams.pgport) : NULL;
185  dopt->cparams.pghost = ropt->cparams.pghost ? pg_strdup(ropt->cparams.pghost) : NULL;
186  dopt->cparams.username = ropt->cparams.username ? pg_strdup(ropt->cparams.username) : NULL;
188  dopt->outputClean = ropt->dropSchema;
189  dopt->dataOnly = ropt->dataOnly;
190  dopt->schemaOnly = ropt->schemaOnly;
191  dopt->if_exists = ropt->if_exists;
192  dopt->column_inserts = ropt->column_inserts;
193  dopt->dumpSections = ropt->dumpSections;
194  dopt->aclsSkip = ropt->aclsSkip;
195  dopt->outputSuperuser = ropt->superuser;
196  dopt->outputCreateDB = ropt->createDB;
197  dopt->outputNoOwner = ropt->noOwner;
198  dopt->outputNoTablespaces = ropt->noTablespace;
199  dopt->disable_triggers = ropt->disable_triggers;
200  dopt->use_setsessauth = ropt->use_setsessauth;
202  dopt->dump_inserts = ropt->dump_inserts;
203  dopt->no_comments = ropt->no_comments;
204  dopt->no_publications = ropt->no_publications;
206  dopt->no_subscriptions = ropt->no_subscriptions;
207  dopt->lockWaitTimeout = ropt->lockWaitTimeout;
210  dopt->sequence_data = ropt->sequence_data;
211 
212  return dopt;
213 }
int column_inserts
Definition: pg_backup.h:155
int disable_triggers
Definition: pg_backup.h:165
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:181
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:167
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:172
char * outputSuperuser
Definition: pg_backup.h:179
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:178
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:168
int outputCreateDB
Definition: pg_backup.h:175
int outputClean
Definition: pg_backup.h:174
int no_comments
Definition: pg_backup.h:157
int outputNoTablespaces
Definition: pg_backup.h:166
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 1234 of file pg_backup_archiver.c.

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

Referenced by dumpBlobs().

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

◆ GetConnection()

PGconn* GetConnection ( Archive AHX)

Definition at line 259 of file pg_backup_db.c.

References _archiveHandle::connection.

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

◆ InitDumpOptions()

void InitDumpOptions ( DumpOptions opts)

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

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

◆ NewDumpOptions()

DumpOptions* NewDumpOptions ( void  )

Definition at line 152 of file pg_backup_archiver.c.

References InitDumpOptions(), opts, and pg_malloc().

Referenced by dumpOptionsFromRestoreOptions().

153 {
155 
156  InitDumpOptions(opts);
157  return opts;
158 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
static AmcheckOptions opts
Definition: pg_amcheck.c:110
void InitDumpOptions(DumpOptions *opts)

◆ NewRestoreOptions()

RestoreOptions* NewRestoreOptions ( void  )

Definition at line 965 of file pg_backup_archiver.c.

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

Referenced by _CloseArchive(), and main().

966 {
968 
969  opts = (RestoreOptions *) pg_malloc0(sizeof(RestoreOptions));
970 
971  /* set any fields that shouldn't default to zeroes */
972  opts->format = archUnknown;
975 
976  return opts;
977 }
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
static AmcheckOptions opts
Definition: pg_amcheck.c:110
#define DUMP_UNSECTIONED

◆ OpenArchive()

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

Definition at line 255 of file pg_backup_archiver.c.

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

Referenced by main().

256 {
257  ArchiveHandle *AH = _allocAH(FileSpec, fmt, 0, true, archModeRead, setupRestoreWorker);
258 
259  return (Archive *) AH;
260 }
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 1116 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().

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

◆ ProcessArchiveRestoreOptions()

void ProcessArchiveRestoreOptions ( Archive AH)

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

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

◆ RestoreArchive()

void RestoreArchive ( Archive AH)

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

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

◆ SetArchiveOptions()

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

Definition at line 283 of file pg_backup_archiver.c.

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

Referenced by _CloseArchive(), and main().

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

◆ SortTocFromFile()

void SortTocFromFile ( Archive AHX)

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

1365 {
1366  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1367  RestoreOptions *ropt = AH->public.ropt;
1368  FILE *fh;
1369  StringInfoData linebuf;
1370 
1371  /* Allocate space for the 'wanted' array, and init it */
1372  ropt->idWanted = (bool *) pg_malloc0(sizeof(bool) * AH->maxDumpId);
1373 
1374  /* Setup the file */
1375  fh = fopen(ropt->tocFile, PG_BINARY_R);
1376  if (!fh)
1377  fatal("could not open TOC file \"%s\": %m", ropt->tocFile);
1378 
1379  initStringInfo(&linebuf);
1380 
1381  while (pg_get_line_buf(fh, &linebuf))
1382  {
1383  char *cmnt;
1384  char *endptr;
1385  DumpId id;
1386  TocEntry *te;
1387 
1388  /* Truncate line at comment, if any */
1389  cmnt = strchr(linebuf.data, ';');
1390  if (cmnt != NULL)
1391  {
1392  cmnt[0] = '\0';
1393  linebuf.len = cmnt - linebuf.data;
1394  }
1395 
1396  /* Ignore if all blank */
1397  if (strspn(linebuf.data, " \t\r\n") == linebuf.len)
1398  continue;
1399 
1400  /* Get an ID, check it's valid and not already seen */
1401  id = strtol(linebuf.data, &endptr, 10);
1402  if (endptr == linebuf.data || id <= 0 || id > AH->maxDumpId ||
1403  ropt->idWanted[id - 1])
1404  {
1405  pg_log_warning("line ignored: %s", linebuf.data);
1406  continue;
1407  }
1408 
1409  /* Find TOC entry */
1410  te = getTocEntryByDumpId(AH, id);
1411  if (!te)
1412  fatal("could not find entry for ID %d",
1413  id);
1414 
1415  /* Mark it wanted */
1416  ropt->idWanted[id - 1] = true;
1417 
1418  /*
1419  * Move each item to the end of the list as it is selected, so that
1420  * they are placed in the desired order. Any unwanted items will end
1421  * up at the front of the list, which may seem unintuitive but it's
1422  * what we need. In an ordinary serial restore that makes no
1423  * difference, but in a parallel restore we need to mark unrestored
1424  * items' dependencies as satisfied before we start examining
1425  * restorable items. Otherwise they could have surprising
1426  * side-effects on the order in which restorable items actually get
1427  * restored.
1428  */
1429  _moveBefore(AH->toc, te);
1430  }
1431 
1432  pg_free(linebuf.data);
1433 
1434  if (fclose(fh) != 0)
1435  fatal("could not close TOC file: %m");
1436 }
bool pg_get_line_buf(FILE *stream, StringInfo buf)
Definition: pg_get_line.c:88
int DumpId
Definition: pg_backup.h:245
RestoreOptions * ropt
Definition: pg_backup.h:192
#define PG_BINARY_R
Definition: c.h:1273
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 1220 of file pg_backup_archiver.c.

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

Referenced by dumpBlobs().

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

◆ WriteData()

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

Definition at line 1037 of file pg_backup_archiver.c.

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

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

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