PostgreSQL Source Code  git master
pg_backup.h File Reference
#include "common/compression.h"
#include "common/file_utils.h"
#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 }
 
enum  _dumpPreparedQueries {
  PREPQUERY_DUMPAGG , PREPQUERY_DUMPBASETYPE , PREPQUERY_DUMPCOMPOSITETYPE , PREPQUERY_DUMPDOMAIN ,
  PREPQUERY_DUMPENUMTYPE , PREPQUERY_DUMPFUNC , PREPQUERY_DUMPOPR , PREPQUERY_DUMPRANGETYPE ,
  PREPQUERY_DUMPTABLEATTACH , PREPQUERY_GETCOLUMNACLS , PREPQUERY_GETDOMAINCONSTRAINTS , NUM_PREP_QUERIES
}
 

Functions

void ConnectDatabase (Archive *AHX, const ConnParams *cparams, bool isReconnect)
 
void DisconnectDatabase (Archive *AHX)
 
PGconnGetConnection (Archive *AHX)
 
void WriteData (Archive *AHX, const void *data, size_t dLen)
 
int StartLO (Archive *AHX, Oid oid)
 
int EndLO (Archive *AHX, Oid oid)
 
void CloseArchive (Archive *AHX)
 
void SetArchiveOptions (Archive *AH, DumpOptions *dopt, RestoreOptions *ropt)
 
void ProcessArchiveRestoreOptions (Archive *AHX)
 
void RestoreArchive (Archive *AHX)
 
ArchiveOpenArchive (const char *FileSpec, const ArchiveFormat fmt)
 
ArchiveCreateArchive (const char *FileSpec, const ArchiveFormat fmt, const pg_compress_specification compression_spec, bool dosync, ArchiveMode mode, SetupWorkerPtrType setupDumpWorker, DataDirSyncMethod sync_method)
 
void PrintTOCSummary (Archive *AHX)
 
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

#define appendStringLiteralAH (   buf,
  str,
  AH 
)     appendStringLiteral(buf, str, (AH)->encoding, (AH)->std_strings)

Definition at line 332 of file pg_backup.h.

◆ InvalidDumpId

#define InvalidDumpId   0

Definition at line 272 of file pg_backup.h.

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

◆ DumpId

typedef int DumpId

Definition at line 270 of file pg_backup.h.

◆ DumpOptions

typedef struct _dumpOptions DumpOptions

◆ RestoreOptions

◆ SetupWorkerPtrType

typedef void(* SetupWorkerPtrType) (Archive *AH)

Definition at line 280 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 39 of file pg_backup.h.

40 {
41  archUnknown = 0,
42  archCustom = 1,
43  archTar = 3,
44  archNull = 4,
45  archDirectory = 5,
enum _archiveFormat ArchiveFormat
@ archUnknown
Definition: pg_backup.h:41
@ archTar
Definition: pg_backup.h:43
@ archCustom
Definition: pg_backup.h:42
@ archDirectory
Definition: pg_backup.h:45
@ archNull
Definition: pg_backup.h:44

◆ _archiveMode

Enumerator
archModeAppend 
archModeWrite 
archModeRead 

Definition at line 48 of file pg_backup.h.

49 {
53 } ArchiveMode;
@ archModeWrite
Definition: pg_backup.h:51
@ archModeAppend
Definition: pg_backup.h:50
@ archModeRead
Definition: pg_backup.h:52
enum _archiveMode ArchiveMode

◆ _dumpPreparedQueries

Enumerator
PREPQUERY_DUMPAGG 
PREPQUERY_DUMPBASETYPE 
PREPQUERY_DUMPCOMPOSITETYPE 
PREPQUERY_DUMPDOMAIN 
PREPQUERY_DUMPENUMTYPE 
PREPQUERY_DUMPFUNC 
PREPQUERY_DUMPOPR 
PREPQUERY_DUMPRANGETYPE 
PREPQUERY_DUMPTABLEATTACH 
PREPQUERY_GETCOLUMNACLS 
PREPQUERY_GETDOMAINCONSTRAINTS 
NUM_PREP_QUERIES 

Definition at line 64 of file pg_backup.h.

65 {
77  NUM_PREP_QUERIES /* must be last */
78 };
@ PREPQUERY_DUMPFUNC
Definition: pg_backup.h:71
@ PREPQUERY_DUMPTABLEATTACH
Definition: pg_backup.h:74
@ PREPQUERY_DUMPBASETYPE
Definition: pg_backup.h:67
@ PREPQUERY_DUMPRANGETYPE
Definition: pg_backup.h:73
@ PREPQUERY_DUMPOPR
Definition: pg_backup.h:72
@ PREPQUERY_DUMPDOMAIN
Definition: pg_backup.h:69
@ NUM_PREP_QUERIES
Definition: pg_backup.h:77
@ PREPQUERY_DUMPCOMPOSITETYPE
Definition: pg_backup.h:68
@ PREPQUERY_DUMPAGG
Definition: pg_backup.h:66
@ PREPQUERY_GETCOLUMNACLS
Definition: pg_backup.h:75
@ PREPQUERY_GETDOMAINCONSTRAINTS
Definition: pg_backup.h:76
@ PREPQUERY_DUMPENUMTYPE
Definition: pg_backup.h:70

◆ _teSection

enum _teSection
Enumerator
SECTION_NONE 
SECTION_PRE_DATA 
SECTION_DATA 
SECTION_POST_DATA 

Definition at line 55 of file pg_backup.h.

56 {
57  SECTION_NONE = 1, /* comments, ACLs, etc; can be anywhere */
58  SECTION_PRE_DATA, /* stuff to be processed before data */
59  SECTION_DATA, /* table data, large objects, LO comments */
60  SECTION_POST_DATA, /* stuff to be processed after data */
61 } teSection;
@ SECTION_NONE
Definition: pg_backup.h:57
@ SECTION_POST_DATA
Definition: pg_backup.h:60
@ SECTION_PRE_DATA
Definition: pg_backup.h:58
@ SECTION_DATA
Definition: pg_backup.h:59
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 32 of file pg_backup.h.

33 {
35  TRI_NO,
36  TRI_YES,
37 } trivalue;
trivalue
Definition: pg_backup.h:33
@ TRI_YES
Definition: pg_backup.h:36
@ TRI_DEFAULT
Definition: pg_backup.h:34
@ TRI_NO
Definition: pg_backup.h:35

Function Documentation

◆ archprintf()

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

◆ archputs()

void archputs ( const char *  s,
Archive AH 
)

Definition at line 1610 of file pg_backup_archiver.c.

1611 {
1612  WriteData(AH, s, strlen(s));
1613 }
void WriteData(Archive *AHX, const void *data, size_t dLen)

References WriteData().

Referenced by dumpTableData_insert().

◆ CloseArchive()

void CloseArchive ( Archive AHX)

Definition at line 252 of file pg_backup_archiver.c.

253 {
254  ArchiveHandle *AH = (ArchiveHandle *) AHX;
255 
256  AH->ClosePtr(AH);
257 
258  /* Close the output */
259  errno = 0;
260  if (!EndCompressFileHandle(AH->OF))
261  pg_fatal("could not close output file: %m");
262 }
bool EndCompressFileHandle(CompressFileHandle *CFH)
Definition: compress_io.c:289
#define pg_fatal(...)
ClosePtrType ClosePtr

References _archiveHandle::ClosePtr, EndCompressFileHandle(), _archiveHandle::OF, and pg_fatal.

Referenced by main().

◆ ConnectDatabase()

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

Definition at line 110 of file pg_backup_db.c.

113 {
114  ArchiveHandle *AH = (ArchiveHandle *) AHX;
115  trivalue prompt_password;
116  char *password;
117  bool new_pass;
118 
119  if (AH->connection)
120  pg_fatal("already connected to a database");
121 
122  /* Never prompt for a password during a reconnection */
123  prompt_password = isReconnect ? TRI_NO : cparams->promptPassword;
124 
125  password = AH->savedPassword;
126 
127  if (prompt_password == TRI_YES && password == NULL)
128  password = simple_prompt("Password: ", false);
129 
130  /*
131  * Start the connection. Loop until we have a password if requested by
132  * backend.
133  */
134  do
135  {
136  const char *keywords[8];
137  const char *values[8];
138  int i = 0;
139 
140  /*
141  * If dbname is a connstring, its entries can override the other
142  * values obtained from cparams; but in turn, override_dbname can
143  * override the dbname component of it.
144  */
145  keywords[i] = "host";
146  values[i++] = cparams->pghost;
147  keywords[i] = "port";
148  values[i++] = cparams->pgport;
149  keywords[i] = "user";
150  values[i++] = cparams->username;
151  keywords[i] = "password";
152  values[i++] = password;
153  keywords[i] = "dbname";
154  values[i++] = cparams->dbname;
155  if (cparams->override_dbname)
156  {
157  keywords[i] = "dbname";
158  values[i++] = cparams->override_dbname;
159  }
160  keywords[i] = "fallback_application_name";
161  values[i++] = progname;
162  keywords[i] = NULL;
163  values[i++] = NULL;
164  Assert(i <= lengthof(keywords));
165 
166  new_pass = false;
167  AH->connection = PQconnectdbParams(keywords, values, true);
168 
169  if (!AH->connection)
170  pg_fatal("could not connect to database");
171 
172  if (PQstatus(AH->connection) == CONNECTION_BAD &&
174  password == NULL &&
175  prompt_password != TRI_NO)
176  {
177  PQfinish(AH->connection);
178  password = simple_prompt("Password: ", false);
179  new_pass = true;
180  }
181  } while (new_pass);
182 
183  /* check to see that the backend connection was successfully made */
184  if (PQstatus(AH->connection) == CONNECTION_BAD)
185  {
186  if (isReconnect)
187  pg_fatal("reconnection failed: %s",
189  else
190  pg_fatal("%s",
192  }
193 
194  /* Start strict; later phases may override this. */
197 
198  if (password && password != AH->savedPassword)
199  free(password);
200 
201  /*
202  * We want to remember connection's actual password, whether or not we got
203  * it by prompting. So we don't just store the password variable.
204  */
206  {
207  free(AH->savedPassword);
209  }
210 
211  /* check for version mismatch */
213 
215 
216  /* arrange for SIGINT to issue a query cancel on this connection */
218 }
void set_archive_cancel_info(ArchiveHandle *AH, PGconn *conn)
Definition: parallel.c:730
static Datum values[MAXATTR]
Definition: bootstrap.c:152
#define Assert(condition)
Definition: c.h:858
#define lengthof(array)
Definition: c.h:788
#define ALWAYS_SECURE_SEARCH_PATH_SQL
Definition: connect.h:25
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:689
int PQconnectionUsedPassword(const PGconn *conn)
Definition: fe-connect.c:7233
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:7218
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:7167
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:7114
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4888
char * PQpass(const PGconn *conn)
Definition: fe-connect.c:7029
PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg)
Definition: fe-connect.c:7346
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define free(a)
Definition: header.h:65
int i
Definition: isn.c:73
@ CONNECTION_BAD
Definition: libpq-fe.h:62
const char * progname
Definition: main.c:44
static void _check_database_version(ArchiveHandle *AH)
Definition: pg_backup_db.c:33
static void notice_processor(void *arg, const char *message)
Definition: pg_backup_db.c:262
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:305
char * simple_prompt(const char *prompt, bool echo)
Definition: sprompt.c:38
static char * password
Definition: streamutil.c:54
char * override_dbname
Definition: pg_backup.h:91
char * pgport
Definition: pg_backup.h:85
char * pghost
Definition: pg_backup.h:86
trivalue promptPassword
Definition: pg_backup.h:88
char * username
Definition: pg_backup.h:87
char * dbname
Definition: pg_backup.h:84
trivalue
Definition: vacuumlo.c:35
@ TRI_YES
Definition: vacuumlo.c:38
@ TRI_NO
Definition: vacuumlo.c:37

References _check_database_version(), ALWAYS_SECURE_SEARCH_PATH_SQL, Assert, _archiveHandle::connection, CONNECTION_BAD, _connParams::dbname, ExecuteSqlQueryForSingleRow(), free, i, lengthof, notice_processor(), _connParams::override_dbname, password, pg_fatal, 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().

◆ CreateArchive()

Archive* CreateArchive ( const char *  FileSpec,
const ArchiveFormat  fmt,
const pg_compress_specification  compression_spec,
bool  dosync,
ArchiveMode  mode,
SetupWorkerPtrType  setupDumpWorker,
DataDirSyncMethod  sync_method 
)

Definition at line 221 of file pg_backup_archiver.c.

227 {
228  ArchiveHandle *AH = _allocAH(FileSpec, fmt, compression_spec,
230 
231  return (Archive *) AH;
232 }
static DataDirSyncMethod sync_method
Definition: initdb.c:170
static void const char * fmt
static ArchiveHandle * _allocAH(const char *FileSpec, const ArchiveFormat fmt, const pg_compress_specification compression_spec, bool dosync, ArchiveMode mode, SetupWorkerPtrType setupWorkerPtr, DataDirSyncMethod sync_method)
static PgChecksumMode mode
Definition: pg_checksums.c:56
static bool dosync
Definition: pg_dump.c:103
static void setupDumpWorker(Archive *AH)
Definition: pg_dump.c:1351

References _allocAH(), dosync, fmt, mode, setupDumpWorker(), and sync_method.

Referenced by main().

◆ DisconnectDatabase()

void DisconnectDatabase ( Archive AHX)

Definition at line 225 of file pg_backup_db.c.

226 {
227  ArchiveHandle *AH = (ArchiveHandle *) AHX;
228  char errbuf[1];
229 
230  if (!AH->connection)
231  return;
232 
233  if (AH->connCancel)
234  {
235  /*
236  * If we have an active query, send a cancel before closing, ignoring
237  * any errors. This is of no use for a normal exit, but might be
238  * helpful during pg_fatal().
239  */
241  (void) PQcancel(AH->connCancel, errbuf, sizeof(errbuf));
242 
243  /*
244  * Prevent signal handler from sending a cancel after this.
245  */
246  set_archive_cancel_info(AH, NULL);
247  }
248 
249  PQfinish(AH->connection);
250  AH->connection = NULL;
251 }
int PQcancel(PGcancel *cancel, char *errbuf, int errbufsize)
Definition: fe-cancel.c:462
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:7122
@ PQTRANS_ACTIVE
Definition: libpq-fe.h:123
PGcancel *volatile connCancel

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().

◆ dumpOptionsFromRestoreOptions()

DumpOptions* dumpOptionsFromRestoreOptions ( RestoreOptions ropt)

Definition at line 157 of file pg_backup_archiver.c.

158 {
159  DumpOptions *dopt = NewDumpOptions();
160 
161  /* this is the inverse of what's at the end of pg_dump.c's main() */
162  dopt->cparams.dbname = ropt->cparams.dbname ? pg_strdup(ropt->cparams.dbname) : NULL;
163  dopt->cparams.pgport = ropt->cparams.pgport ? pg_strdup(ropt->cparams.pgport) : NULL;
164  dopt->cparams.pghost = ropt->cparams.pghost ? pg_strdup(ropt->cparams.pghost) : NULL;
165  dopt->cparams.username = ropt->cparams.username ? pg_strdup(ropt->cparams.username) : NULL;
167  dopt->outputClean = ropt->dropSchema;
168  dopt->dataOnly = ropt->dataOnly;
169  dopt->schemaOnly = ropt->schemaOnly;
170  dopt->if_exists = ropt->if_exists;
171  dopt->column_inserts = ropt->column_inserts;
172  dopt->dumpSections = ropt->dumpSections;
173  dopt->aclsSkip = ropt->aclsSkip;
174  dopt->outputSuperuser = ropt->superuser;
175  dopt->outputCreateDB = ropt->createDB;
176  dopt->outputNoOwner = ropt->noOwner;
177  dopt->outputNoTableAm = ropt->noTableAm;
178  dopt->outputNoTablespaces = ropt->noTablespace;
179  dopt->disable_triggers = ropt->disable_triggers;
180  dopt->use_setsessauth = ropt->use_setsessauth;
182  dopt->dump_inserts = ropt->dump_inserts;
183  dopt->no_comments = ropt->no_comments;
184  dopt->no_publications = ropt->no_publications;
186  dopt->no_subscriptions = ropt->no_subscriptions;
187  dopt->lockWaitTimeout = ropt->lockWaitTimeout;
190  dopt->sequence_data = ropt->sequence_data;
191 
192  return dopt;
193 }
DumpOptions * NewDumpOptions(void)
bool dataOnly
Definition: pg_backup.h:170
int dump_inserts
Definition: pg_backup.h:174
int column_inserts
Definition: pg_backup.h:178
int use_setsessauth
Definition: pg_backup.h:190
int outputCreateDB
Definition: pg_backup.h:198
bool include_everything
Definition: pg_backup.h:195
int sequence_data
Definition: pg_backup.h:204
int disable_dollar_quoting
Definition: pg_backup.h:177
int no_comments
Definition: pg_backup.h:180
int outputNoTableAm
Definition: pg_backup.h:188
int enable_row_security
Definition: pg_backup.h:191
char * outputSuperuser
Definition: pg_backup.h:202
int dumpSections
Definition: pg_backup.h:171
int no_security_labels
Definition: pg_backup.h:181
int no_publications
Definition: pg_backup.h:182
ConnParams cparams
Definition: pg_backup.h:164
const char * lockWaitTimeout
Definition: pg_backup.h:173
int no_subscriptions
Definition: pg_backup.h:183
bool aclsSkip
Definition: pg_backup.h:172
int outputClean
Definition: pg_backup.h:197
int outputNoTablespaces
Definition: pg_backup.h:189
int disable_triggers
Definition: pg_backup.h:187
int outputNoOwner
Definition: pg_backup.h:201
bool schemaOnly
Definition: pg_backup.h:169
int include_everything
Definition: pg_backup.h:124
ConnParams cparams
Definition: pg_backup.h:144
int no_subscriptions
Definition: pg_backup.h:114
int disable_dollar_quoting
Definition: pg_backup.h:107
int no_security_labels
Definition: pg_backup.h:113
char * superuser
Definition: pg_backup.h:104
const char * lockWaitTimeout
Definition: pg_backup.h:123
int enable_row_security
Definition: pg_backup.h:157
int disable_triggers
Definition: pg_backup.h:100

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::noTableAm, _restoreOptions::noTablespace, _dumpOptions::outputClean, _dumpOptions::outputCreateDB, _dumpOptions::outputNoOwner, _dumpOptions::outputNoTableAm, _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().

◆ EndLO()

int EndLO ( Archive AHX,
Oid  oid 
)

Definition at line 1388 of file pg_backup_archiver.c.

1389 {
1390  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1391 
1392  if (AH->EndLOPtr)
1393  AH->EndLOPtr(AH, AH->currToc, oid);
1394 
1395  return 1;
1396 }
EndLOPtrType EndLOPtr
struct _tocEntry * currToc

References _archiveHandle::currToc, and _archiveHandle::EndLOPtr.

Referenced by dumpLOs().

◆ GetConnection()

PGconn* GetConnection ( Archive AHX)

Definition at line 254 of file pg_backup_db.c.

255 {
256  ArchiveHandle *AH = (ArchiveHandle *) AHX;
257 
258  return AH->connection;
259 }

References _archiveHandle::connection.

◆ InitDumpOptions()

void InitDumpOptions ( DumpOptions opts)

Definition at line 143 of file pg_backup_archiver.c.

144 {
145  memset(opts, 0, sizeof(DumpOptions));
146  /* set any fields that shouldn't default to zeroes */
147  opts->include_everything = true;
148  opts->cparams.promptPassword = TRI_DEFAULT;
149  opts->dumpSections = DUMP_UNSECTIONED;
150 }
static AmcheckOptions opts
Definition: pg_amcheck.c:111
#define DUMP_UNSECTIONED
@ TRI_DEFAULT
Definition: vacuumlo.c:36

References DUMP_UNSECTIONED, opts, and TRI_DEFAULT.

Referenced by main(), and NewDumpOptions().

◆ NewDumpOptions()

DumpOptions* NewDumpOptions ( void  )

Definition at line 131 of file pg_backup_archiver.c.

132 {
134 
136  return opts;
137 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
void InitDumpOptions(DumpOptions *opts)

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

Referenced by dumpOptionsFromRestoreOptions().

◆ NewRestoreOptions()

RestoreOptions* NewRestoreOptions ( void  )

Definition at line 1071 of file pg_backup_archiver.c.

1072 {
1074 
1076 
1077  /* set any fields that shouldn't default to zeroes */
1078  opts->format = archUnknown;
1079  opts->cparams.promptPassword = TRI_DEFAULT;
1080  opts->dumpSections = DUMP_UNSECTIONED;
1081  opts->compression_spec.algorithm = PG_COMPRESSION_NONE;
1082  opts->compression_spec.level = 0;
1083 
1084  return opts;
1085 }
@ PG_COMPRESSION_NONE
Definition: compression.h:23
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53

References archUnknown, DUMP_UNSECTIONED, opts, PG_COMPRESSION_NONE, pg_malloc0(), and TRI_DEFAULT.

Referenced by _CloseArchive(), and main().

◆ OpenArchive()

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

Definition at line 237 of file pg_backup_archiver.c.

238 {
239  ArchiveHandle *AH;
240  pg_compress_specification compression_spec = {0};
241 
242  compression_spec.algorithm = PG_COMPRESSION_NONE;
243  AH = _allocAH(FileSpec, fmt, compression_spec, true,
246 
247  return (Archive *) AH;
248 }
@ DATA_DIR_SYNC_METHOD_FSYNC
Definition: file_utils.h:29
static void setupRestoreWorker(Archive *AHX)
pg_compress_algorithm algorithm
Definition: compression.h:34

References _allocAH(), pg_compress_specification::algorithm, archModeRead, DATA_DIR_SYNC_METHOD_FSYNC, fmt, PG_COMPRESSION_NONE, and setupRestoreWorker().

Referenced by main().

◆ PrintTOCSummary()

void PrintTOCSummary ( Archive AHX)

Definition at line 1262 of file pg_backup_archiver.c.

1263 {
1264  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1265  RestoreOptions *ropt = AH->public.ropt;
1266  TocEntry *te;
1267  pg_compress_specification out_compression_spec = {0};
1268  teSection curSection;
1269  CompressFileHandle *sav;
1270  const char *fmtName;
1271  char stamp_str[64];
1272 
1273  /* TOC is always uncompressed */
1274  out_compression_spec.algorithm = PG_COMPRESSION_NONE;
1275 
1276  sav = SaveOutput(AH);
1277  if (ropt->filename)
1278  SetOutput(AH, ropt->filename, out_compression_spec);
1279 
1280  if (strftime(stamp_str, sizeof(stamp_str), PGDUMP_STRFTIME_FMT,
1281  localtime(&AH->createDate)) == 0)
1282  strcpy(stamp_str, "[unknown]");
1283 
1284  ahprintf(AH, ";\n; Archive created at %s\n", stamp_str);
1285  ahprintf(AH, "; dbname: %s\n; TOC Entries: %d\n; Compression: %s\n",
1286  sanitize_line(AH->archdbname, false),
1287  AH->tocCount,
1289 
1290  switch (AH->format)
1291  {
1292  case archCustom:
1293  fmtName = "CUSTOM";
1294  break;
1295  case archDirectory:
1296  fmtName = "DIRECTORY";
1297  break;
1298  case archTar:
1299  fmtName = "TAR";
1300  break;
1301  default:
1302  fmtName = "UNKNOWN";
1303  }
1304 
1305  ahprintf(AH, "; Dump Version: %d.%d-%d\n",
1307  ahprintf(AH, "; Format: %s\n", fmtName);
1308  ahprintf(AH, "; Integer: %d bytes\n", (int) AH->intSize);
1309  ahprintf(AH, "; Offset: %d bytes\n", (int) AH->offSize);
1310  if (AH->archiveRemoteVersion)
1311  ahprintf(AH, "; Dumped from database version: %s\n",
1312  AH->archiveRemoteVersion);
1313  if (AH->archiveDumpVersion)
1314  ahprintf(AH, "; Dumped by pg_dump version: %s\n",
1315  AH->archiveDumpVersion);
1316 
1317  ahprintf(AH, ";\n;\n; Selected TOC Entries:\n;\n");
1318 
1319  curSection = SECTION_PRE_DATA;
1320  for (te = AH->toc->next; te != AH->toc; te = te->next)
1321  {
1322  /* This bit must match ProcessArchiveRestoreOptions' marking logic */
1323  if (te->section != SECTION_NONE)
1324  curSection = te->section;
1325  te->reqs = _tocEntryRequired(te, curSection, AH);
1326  /* Now, should we print it? */
1327  if (ropt->verbose ||
1328  (te->reqs & (REQ_SCHEMA | REQ_DATA)) != 0)
1329  {
1330  char *sanitized_name;
1331  char *sanitized_schema;
1332  char *sanitized_owner;
1333 
1334  /*
1335  */
1336  sanitized_name = sanitize_line(te->tag, false);
1337  sanitized_schema = sanitize_line(te->namespace, true);
1338  sanitized_owner = sanitize_line(te->owner, false);
1339 
1340  ahprintf(AH, "%d; %u %u %s %s %s %s\n", te->dumpId,
1341  te->catalogId.tableoid, te->catalogId.oid,
1342  te->desc, sanitized_schema, sanitized_name,
1343  sanitized_owner);
1344 
1345  free(sanitized_name);
1346  free(sanitized_schema);
1347  free(sanitized_owner);
1348  }
1349  if (ropt->verbose && te->nDeps > 0)
1350  {
1351  int i;
1352 
1353  ahprintf(AH, ";\tdepends on:");
1354  for (i = 0; i < te->nDeps; i++)
1355  ahprintf(AH, " %d", te->dependencies[i]);
1356  ahprintf(AH, "\n");
1357  }
1358  }
1359 
1360  /* Enforce strict names checking */
1361  if (ropt->strict_names)
1362  StrictNamesCheck(ropt);
1363 
1364  if (ropt->filename)
1365  RestoreOutput(AH, sav);
1366 }
const char * get_compress_algorithm_name(pg_compress_algorithm algorithm)
Definition: compression.c:69
#define PGDUMP_STRFTIME_FMT
Definition: dumputils.h:33
static CompressFileHandle * SaveOutput(ArchiveHandle *AH)
static void StrictNamesCheck(RestoreOptions *ropt)
static void SetOutput(ArchiveHandle *AH, const char *filename, const pg_compress_specification compression_spec)
int ahprintf(ArchiveHandle *AH, const char *fmt,...)
static void RestoreOutput(ArchiveHandle *AH, CompressFileHandle *savedOutput)
static char * sanitize_line(const char *str, bool want_hyphen)
static int _tocEntryRequired(TocEntry *te, teSection curSection, ArchiveHandle *AH)
#define ARCHIVE_MAJOR(version)
#define ARCHIVE_MINOR(version)
#define REQ_SCHEMA
#define ARCHIVE_REV(version)
#define REQ_DATA
RestoreOptions * ropt
Definition: pg_backup.h:215
Oid tableoid
Definition: pg_backup.h:266
ArchiveFormat format
struct _tocEntry * toc
pg_compress_specification compression_spec
const char * filename
Definition: pg_backup.h:117
teSection section
CatalogId catalogId
struct _tocEntry * next
DumpId * dependencies

References _tocEntryRequired(), ahprintf(), pg_compress_specification::algorithm, archCustom, _archiveHandle::archdbname, archDirectory, ARCHIVE_MAJOR, ARCHIVE_MINOR, ARCHIVE_REV, _archiveHandle::archiveDumpVersion, _archiveHandle::archiveRemoteVersion, archTar, _tocEntry::catalogId, _archiveHandle::compression_spec, _archiveHandle::createDate, _tocEntry::dependencies, _tocEntry::desc, _tocEntry::dumpId, _restoreOptions::filename, _archiveHandle::format, free, get_compress_algorithm_name(), i, _archiveHandle::intSize, _tocEntry::nDeps, _tocEntry::next, _archiveHandle::offSize, CatalogId::oid, _tocEntry::owner, PG_COMPRESSION_NONE, PGDUMP_STRFTIME_FMT, _archiveHandle::public, REQ_DATA, REQ_SCHEMA, _tocEntry::reqs, 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().

◆ ProcessArchiveRestoreOptions()

void ProcessArchiveRestoreOptions ( Archive AHX)

Definition at line 279 of file pg_backup_archiver.c.

280 {
281  ArchiveHandle *AH = (ArchiveHandle *) AHX;
282  RestoreOptions *ropt = AH->public.ropt;
283  TocEntry *te;
284  teSection curSection;
285 
286  /* Decide which TOC entries will be dumped/restored, and mark them */
287  curSection = SECTION_PRE_DATA;
288  for (te = AH->toc->next; te != AH->toc; te = te->next)
289  {
290  /*
291  * When writing an archive, we also take this opportunity to check
292  * that we have generated the entries in a sane order that respects
293  * the section divisions. When reading, don't complain, since buggy
294  * old versions of pg_dump might generate out-of-order archives.
295  */
296  if (AH->mode != archModeRead)
297  {
298  switch (te->section)
299  {
300  case SECTION_NONE:
301  /* ok to be anywhere */
302  break;
303  case SECTION_PRE_DATA:
304  if (curSection != SECTION_PRE_DATA)
305  pg_log_warning("archive items not in correct section order");
306  break;
307  case SECTION_DATA:
308  if (curSection == SECTION_POST_DATA)
309  pg_log_warning("archive items not in correct section order");
310  break;
311  case SECTION_POST_DATA:
312  /* ok no matter which section we were in */
313  break;
314  default:
315  pg_fatal("unexpected section code %d",
316  (int) te->section);
317  break;
318  }
319  }
320 
321  if (te->section != SECTION_NONE)
322  curSection = te->section;
323 
324  te->reqs = _tocEntryRequired(te, curSection, AH);
325  }
326 
327  /* Enforce strict names checking */
328  if (ropt->strict_names)
329  StrictNamesCheck(ropt);
330 }
#define pg_log_warning(...)
Definition: pgfnames.c:24

References _tocEntryRequired(), archModeRead, _archiveHandle::mode, _tocEntry::next, pg_fatal, 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().

◆ RestoreArchive()

void RestoreArchive ( Archive AHX)

Definition at line 334 of file pg_backup_archiver.c.

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

References _becomeOwner(), _doSetFixedOutputState(), _selectOutputSchema(), _tocEntryRestorePass(), ahprintf(), pg_compress_specification::algorithm, appendPQExpBuffer(), appendPQExpBufferStr(), _archiveHandle::archiveDumpVersion, _archiveHandle::archiveRemoteVersion, Assert, buildTocEntryArrays(), _tocEntry::catalogId, _archiveHandle::ClonePtr, CommitTransaction(), _restoreOptions::compression_spec, _archiveHandle::compression_spec, ConnectDatabase(), _archiveHandle::connection, _restoreOptions::cparams, _archiveHandle::createDate, _restoreOptions::createDB, createPQExpBuffer(), _archiveHandle::currentTE, _archiveHandle::currSchema, PQExpBufferData::data, _restoreOptions::dataOnly, _tocEntry::desc, destroyPQExpBuffer(), DisconnectDatabase(), DropLOIfExists(), _restoreOptions::dropSchema, _tocEntry::dropStmt, dumpTimestamp(), errmsg(), _restoreOptions::filename, free, _tocEntry::hadDumper, _restoreOptions::if_exists, IssueCommandPerBlob(), 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_COMPRESSION_NONE, pg_fatal, 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_PASS_POST_ACL, 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(), supports_compression(), _tocEntry::tag, _archiveHandle::toc, _archiveHandle::tocsByDumpId, _restoreOptions::txn_size, _archiveHandle::txnCount, _restoreOptions::useDB, Archive::verbose, and _archiveHandle::version.

Referenced by _CloseArchive(), and main().

◆ SetArchiveOptions()

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

Definition at line 266 of file pg_backup_archiver.c.

267 {
268  /* Caller can omit dump options, in which case we synthesize them */
269  if (dopt == NULL && ropt != NULL)
270  dopt = dumpOptionsFromRestoreOptions(ropt);
271 
272  /* Save options for later access */
273  AH->dopt = dopt;
274  AH->ropt = ropt;
275 }
DumpOptions * dumpOptionsFromRestoreOptions(RestoreOptions *ropt)
DumpOptions * dopt
Definition: pg_backup.h:214

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

Referenced by _CloseArchive(), and main().

◆ SortTocFromFile()

void SortTocFromFile ( Archive AHX)

Definition at line 1529 of file pg_backup_archiver.c.

1530 {
1531  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1532  RestoreOptions *ropt = AH->public.ropt;
1533  FILE *fh;
1534  StringInfoData linebuf;
1535 
1536  /* Allocate space for the 'wanted' array, and init it */
1537  ropt->idWanted = (bool *) pg_malloc0(sizeof(bool) * AH->maxDumpId);
1538 
1539  /* Setup the file */
1540  fh = fopen(ropt->tocFile, PG_BINARY_R);
1541  if (!fh)
1542  pg_fatal("could not open TOC file \"%s\": %m", ropt->tocFile);
1543 
1544  initStringInfo(&linebuf);
1545 
1546  while (pg_get_line_buf(fh, &linebuf))
1547  {
1548  char *cmnt;
1549  char *endptr;
1550  DumpId id;
1551  TocEntry *te;
1552 
1553  /* Truncate line at comment, if any */
1554  cmnt = strchr(linebuf.data, ';');
1555  if (cmnt != NULL)
1556  {
1557  cmnt[0] = '\0';
1558  linebuf.len = cmnt - linebuf.data;
1559  }
1560 
1561  /* Ignore if all blank */
1562  if (strspn(linebuf.data, " \t\r\n") == linebuf.len)
1563  continue;
1564 
1565  /* Get an ID, check it's valid and not already seen */
1566  id = strtol(linebuf.data, &endptr, 10);
1567  if (endptr == linebuf.data || id <= 0 || id > AH->maxDumpId ||
1568  ropt->idWanted[id - 1])
1569  {
1570  pg_log_warning("line ignored: %s", linebuf.data);
1571  continue;
1572  }
1573 
1574  /* Find TOC entry */
1575  te = getTocEntryByDumpId(AH, id);
1576  if (!te)
1577  pg_fatal("could not find entry for ID %d",
1578  id);
1579 
1580  /* Mark it wanted */
1581  ropt->idWanted[id - 1] = true;
1582 
1583  /*
1584  * Move each item to the end of the list as it is selected, so that
1585  * they are placed in the desired order. Any unwanted items will end
1586  * up at the front of the list, which may seem unintuitive but it's
1587  * what we need. In an ordinary serial restore that makes no
1588  * difference, but in a parallel restore we need to mark unrestored
1589  * items' dependencies as satisfied before we start examining
1590  * restorable items. Otherwise they could have surprising
1591  * side-effects on the order in which restorable items actually get
1592  * restored.
1593  */
1594  _moveBefore(AH->toc, te);
1595  }
1596 
1597  pg_free(linebuf.data);
1598 
1599  if (fclose(fh) != 0)
1600  pg_fatal("could not close TOC file: %m");
1601 }
#define PG_BINARY_R
Definition: c.h:1275
int DumpId
Definition: pg_backup.h:270
TocEntry * getTocEntryByDumpId(ArchiveHandle *AH, DumpId id)
static void _moveBefore(TocEntry *pos, TocEntry *te)
bool pg_get_line_buf(FILE *stream, StringInfo buf)
Definition: pg_get_line.c:95
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
bool * idWanted
Definition: pg_backup.h:156
char * tocFile
Definition: pg_backup.h:127

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

Referenced by main().

◆ StartLO()

int StartLO ( Archive AHX,
Oid  oid 
)

Definition at line 1374 of file pg_backup_archiver.c.

1375 {
1376  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1377 
1378  if (!AH->StartLOPtr)
1379  pg_fatal("large-object output not supported in chosen format");
1380 
1381  AH->StartLOPtr(AH, AH->currToc, oid);
1382 
1383  return 1;
1384 }
StartLOPtrType StartLOPtr

References _archiveHandle::currToc, pg_fatal, and _archiveHandle::StartLOPtr.

Referenced by dumpLOs().

◆ WriteData()

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

Definition at line 1182 of file pg_backup_archiver.c.

1183 {
1184  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1185 
1186  if (!AH->currToc)
1187  pg_fatal("internal error -- WriteData cannot be called outside the context of a DataDumper routine");
1188 
1189  AH->WriteDataPtr(AH, data, dLen);
1190 }
const void * data
WriteDataPtrType WriteDataPtr

References _archiveHandle::currToc, data, pg_fatal, and _archiveHandle::WriteDataPtr.

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