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

◆ InvalidDumpId

#define InvalidDumpId   0

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

◆ DumpId

typedef int DumpId

Definition at line 268 of file pg_backup.h.

◆ DumpOptions

typedef struct _dumpOptions DumpOptions

◆ RestoreOptions

◆ SetupWorkerPtrType

typedef void(* SetupWorkerPtrType) (Archive *AH)

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

1490 {
1491  WriteData(AH, s, strlen(s));
1492 }
void WriteData(Archive *AHX, const void *data, size_t dLen)

References WriteData().

Referenced by dumpTableData_insert().

◆ CloseArchive()

void CloseArchive ( Archive AHX)

Definition at line 249 of file pg_backup_archiver.c.

250 {
251  ArchiveHandle *AH = (ArchiveHandle *) AHX;
252 
253  AH->ClosePtr(AH);
254 
255  /* Close the output */
256  errno = 0;
257  if (!EndCompressFileHandle(AH->OF))
258  pg_fatal("could not close output file: %m");
259 }
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 lengthof(array)
Definition: c.h:775
#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:678
int PQconnectionUsedPassword(const PGconn *conn)
Definition: fe-connect.c:7014
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:6999
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6948
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6895
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4669
char * PQpass(const PGconn *conn)
Definition: fe-connect.c:6810
PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg)
Definition: fe-connect.c:7127
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:61
Assert(fmt[strlen(fmt) - 1] !='\n')
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:53
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 218 of file pg_backup_archiver.c.

224 {
225  ArchiveHandle *AH = _allocAH(FileSpec, fmt, compression_spec,
227 
228  return (Archive *) AH;
229 }
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:1330

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:6903
@ PQTRANS_ACTIVE
Definition: libpq-fe.h:122
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 154 of file pg_backup_archiver.c.

155 {
156  DumpOptions *dopt = NewDumpOptions();
157 
158  /* this is the inverse of what's at the end of pg_dump.c's main() */
159  dopt->cparams.dbname = ropt->cparams.dbname ? pg_strdup(ropt->cparams.dbname) : NULL;
160  dopt->cparams.pgport = ropt->cparams.pgport ? pg_strdup(ropt->cparams.pgport) : NULL;
161  dopt->cparams.pghost = ropt->cparams.pghost ? pg_strdup(ropt->cparams.pghost) : NULL;
162  dopt->cparams.username = ropt->cparams.username ? pg_strdup(ropt->cparams.username) : NULL;
164  dopt->outputClean = ropt->dropSchema;
165  dopt->dataOnly = ropt->dataOnly;
166  dopt->schemaOnly = ropt->schemaOnly;
167  dopt->if_exists = ropt->if_exists;
168  dopt->column_inserts = ropt->column_inserts;
169  dopt->dumpSections = ropt->dumpSections;
170  dopt->aclsSkip = ropt->aclsSkip;
171  dopt->outputSuperuser = ropt->superuser;
172  dopt->outputCreateDB = ropt->createDB;
173  dopt->outputNoOwner = ropt->noOwner;
174  dopt->outputNoTableAm = ropt->noTableAm;
175  dopt->outputNoTablespaces = ropt->noTablespace;
176  dopt->disable_triggers = ropt->disable_triggers;
177  dopt->use_setsessauth = ropt->use_setsessauth;
179  dopt->dump_inserts = ropt->dump_inserts;
180  dopt->no_comments = ropt->no_comments;
181  dopt->no_publications = ropt->no_publications;
183  dopt->no_subscriptions = ropt->no_subscriptions;
184  dopt->lockWaitTimeout = ropt->lockWaitTimeout;
187  dopt->sequence_data = ropt->sequence_data;
188 
189  return dopt;
190 }
DumpOptions * NewDumpOptions(void)
bool dataOnly
Definition: pg_backup.h:168
int dump_inserts
Definition: pg_backup.h:172
int column_inserts
Definition: pg_backup.h:176
int use_setsessauth
Definition: pg_backup.h:188
int outputCreateDB
Definition: pg_backup.h:196
bool include_everything
Definition: pg_backup.h:193
int sequence_data
Definition: pg_backup.h:202
int disable_dollar_quoting
Definition: pg_backup.h:175
int no_comments
Definition: pg_backup.h:178
int outputNoTableAm
Definition: pg_backup.h:186
int enable_row_security
Definition: pg_backup.h:189
char * outputSuperuser
Definition: pg_backup.h:200
int dumpSections
Definition: pg_backup.h:169
int no_security_labels
Definition: pg_backup.h:179
int no_publications
Definition: pg_backup.h:180
ConnParams cparams
Definition: pg_backup.h:162
const char * lockWaitTimeout
Definition: pg_backup.h:171
int no_subscriptions
Definition: pg_backup.h:181
bool aclsSkip
Definition: pg_backup.h:170
int outputClean
Definition: pg_backup.h:195
int outputNoTablespaces
Definition: pg_backup.h:187
int disable_triggers
Definition: pg_backup.h:185
int outputNoOwner
Definition: pg_backup.h:199
bool schemaOnly
Definition: pg_backup.h:167
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:155
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 1278 of file pg_backup_archiver.c.

1279 {
1280  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1281 
1282  if (AH->EndLOPtr)
1283  AH->EndLOPtr(AH, AH->currToc, oid);
1284 
1285  return 1;
1286 }
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 140 of file pg_backup_archiver.c.

141 {
142  memset(opts, 0, sizeof(DumpOptions));
143  /* set any fields that shouldn't default to zeroes */
144  opts->include_everything = true;
145  opts->cparams.promptPassword = TRI_DEFAULT;
146  opts->dumpSections = DUMP_UNSECTIONED;
147 }
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 128 of file pg_backup_archiver.c.

129 {
131 
133  return opts;
134 }
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 965 of file pg_backup_archiver.c.

966 {
968 
970 
971  /* set any fields that shouldn't default to zeroes */
972  opts->format = archUnknown;
973  opts->cparams.promptPassword = TRI_DEFAULT;
974  opts->dumpSections = DUMP_UNSECTIONED;
975  opts->compression_spec.algorithm = PG_COMPRESSION_NONE;
976  opts->compression_spec.level = 0;
977 
978  return opts;
979 }
@ 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 234 of file pg_backup_archiver.c.

235 {
236  ArchiveHandle *AH;
237  pg_compress_specification compression_spec = {0};
238 
239  compression_spec.algorithm = PG_COMPRESSION_NONE;
240  AH = _allocAH(FileSpec, fmt, compression_spec, true,
243 
244  return (Archive *) AH;
245 }
@ 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 1155 of file pg_backup_archiver.c.

1156 {
1157  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1158  RestoreOptions *ropt = AH->public.ropt;
1159  TocEntry *te;
1160  pg_compress_specification out_compression_spec = {0};
1161  teSection curSection;
1162  CompressFileHandle *sav;
1163  const char *fmtName;
1164  char stamp_str[64];
1165 
1166  /* TOC is always uncompressed */
1167  out_compression_spec.algorithm = PG_COMPRESSION_NONE;
1168 
1169  sav = SaveOutput(AH);
1170  if (ropt->filename)
1171  SetOutput(AH, ropt->filename, out_compression_spec);
1172 
1173  if (strftime(stamp_str, sizeof(stamp_str), PGDUMP_STRFTIME_FMT,
1174  localtime(&AH->createDate)) == 0)
1175  strcpy(stamp_str, "[unknown]");
1176 
1177  ahprintf(AH, ";\n; Archive created at %s\n", stamp_str);
1178  ahprintf(AH, "; dbname: %s\n; TOC Entries: %d\n; Compression: %s\n",
1179  sanitize_line(AH->archdbname, false),
1180  AH->tocCount,
1182 
1183  switch (AH->format)
1184  {
1185  case archCustom:
1186  fmtName = "CUSTOM";
1187  break;
1188  case archDirectory:
1189  fmtName = "DIRECTORY";
1190  break;
1191  case archTar:
1192  fmtName = "TAR";
1193  break;
1194  default:
1195  fmtName = "UNKNOWN";
1196  }
1197 
1198  ahprintf(AH, "; Dump Version: %d.%d-%d\n",
1200  ahprintf(AH, "; Format: %s\n", fmtName);
1201  ahprintf(AH, "; Integer: %d bytes\n", (int) AH->intSize);
1202  ahprintf(AH, "; Offset: %d bytes\n", (int) AH->offSize);
1203  if (AH->archiveRemoteVersion)
1204  ahprintf(AH, "; Dumped from database version: %s\n",
1205  AH->archiveRemoteVersion);
1206  if (AH->archiveDumpVersion)
1207  ahprintf(AH, "; Dumped by pg_dump version: %s\n",
1208  AH->archiveDumpVersion);
1209 
1210  ahprintf(AH, ";\n;\n; Selected TOC Entries:\n;\n");
1211 
1212  curSection = SECTION_PRE_DATA;
1213  for (te = AH->toc->next; te != AH->toc; te = te->next)
1214  {
1215  if (te->section != SECTION_NONE)
1216  curSection = te->section;
1217  if (ropt->verbose ||
1218  (_tocEntryRequired(te, curSection, AH) & (REQ_SCHEMA | REQ_DATA)) != 0)
1219  {
1220  char *sanitized_name;
1221  char *sanitized_schema;
1222  char *sanitized_owner;
1223 
1224  /*
1225  */
1226  sanitized_name = sanitize_line(te->tag, false);
1227  sanitized_schema = sanitize_line(te->namespace, true);
1228  sanitized_owner = sanitize_line(te->owner, false);
1229 
1230  ahprintf(AH, "%d; %u %u %s %s %s %s\n", te->dumpId,
1231  te->catalogId.tableoid, te->catalogId.oid,
1232  te->desc, sanitized_schema, sanitized_name,
1233  sanitized_owner);
1234 
1235  free(sanitized_name);
1236  free(sanitized_schema);
1237  free(sanitized_owner);
1238  }
1239  if (ropt->verbose && te->nDeps > 0)
1240  {
1241  int i;
1242 
1243  ahprintf(AH, ";\tdepends on:");
1244  for (i = 0; i < te->nDeps; i++)
1245  ahprintf(AH, " %d", te->dependencies[i]);
1246  ahprintf(AH, "\n");
1247  }
1248  }
1249 
1250  /* Enforce strict names checking */
1251  if (ropt->strict_names)
1252  StrictNamesCheck(ropt);
1253 
1254  if (ropt->filename)
1255  RestoreOutput(AH, sav);
1256 }
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:213
Oid tableoid
Definition: pg_backup.h:264
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, 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 276 of file pg_backup_archiver.c.

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

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

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, 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::useDB, Archive::verbose, and _archiveHandle::version.

Referenced by _CloseArchive(), and main().

◆ SetArchiveOptions()

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

Definition at line 263 of file pg_backup_archiver.c.

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

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

Referenced by _CloseArchive(), and main().

◆ SortTocFromFile()

void SortTocFromFile ( Archive AHX)

Definition at line 1408 of file pg_backup_archiver.c.

1409 {
1410  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1411  RestoreOptions *ropt = AH->public.ropt;
1412  FILE *fh;
1413  StringInfoData linebuf;
1414 
1415  /* Allocate space for the 'wanted' array, and init it */
1416  ropt->idWanted = (bool *) pg_malloc0(sizeof(bool) * AH->maxDumpId);
1417 
1418  /* Setup the file */
1419  fh = fopen(ropt->tocFile, PG_BINARY_R);
1420  if (!fh)
1421  pg_fatal("could not open TOC file \"%s\": %m", ropt->tocFile);
1422 
1423  initStringInfo(&linebuf);
1424 
1425  while (pg_get_line_buf(fh, &linebuf))
1426  {
1427  char *cmnt;
1428  char *endptr;
1429  DumpId id;
1430  TocEntry *te;
1431 
1432  /* Truncate line at comment, if any */
1433  cmnt = strchr(linebuf.data, ';');
1434  if (cmnt != NULL)
1435  {
1436  cmnt[0] = '\0';
1437  linebuf.len = cmnt - linebuf.data;
1438  }
1439 
1440  /* Ignore if all blank */
1441  if (strspn(linebuf.data, " \t\r\n") == linebuf.len)
1442  continue;
1443 
1444  /* Get an ID, check it's valid and not already seen */
1445  id = strtol(linebuf.data, &endptr, 10);
1446  if (endptr == linebuf.data || id <= 0 || id > AH->maxDumpId ||
1447  ropt->idWanted[id - 1])
1448  {
1449  pg_log_warning("line ignored: %s", linebuf.data);
1450  continue;
1451  }
1452 
1453  /* Find TOC entry */
1454  te = getTocEntryByDumpId(AH, id);
1455  if (!te)
1456  pg_fatal("could not find entry for ID %d",
1457  id);
1458 
1459  /* Mark it wanted */
1460  ropt->idWanted[id - 1] = true;
1461 
1462  /*
1463  * Move each item to the end of the list as it is selected, so that
1464  * they are placed in the desired order. Any unwanted items will end
1465  * up at the front of the list, which may seem unintuitive but it's
1466  * what we need. In an ordinary serial restore that makes no
1467  * difference, but in a parallel restore we need to mark unrestored
1468  * items' dependencies as satisfied before we start examining
1469  * restorable items. Otherwise they could have surprising
1470  * side-effects on the order in which restorable items actually get
1471  * restored.
1472  */
1473  _moveBefore(AH->toc, te);
1474  }
1475 
1476  pg_free(linebuf.data);
1477 
1478  if (fclose(fh) != 0)
1479  pg_fatal("could not close TOC file: %m");
1480 }
#define PG_BINARY_R
Definition: c.h:1262
int DumpId
Definition: pg_backup.h:268
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:154
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 1264 of file pg_backup_archiver.c.

1265 {
1266  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1267 
1268  if (!AH->StartLOPtr)
1269  pg_fatal("large-object output not supported in chosen format");
1270 
1271  AH->StartLOPtr(AH, AH->currToc, oid);
1272 
1273  return 1;
1274 }
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 1076 of file pg_backup_archiver.c.

1077 {
1078  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1079 
1080  if (!AH->currToc)
1081  pg_fatal("internal error -- WriteData cannot be called outside the context of a DataDumper routine");
1082 
1083  AH->WriteDataPtr(AH, data, dLen);
1084 }
const void * data
WriteDataPtrType WriteDataPtr

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

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