PostgreSQL Source Code  git master
pg_backup.h File Reference
#include "common/compression.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)
 
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 328 of file pg_backup.h.

◆ InvalidDumpId

#define InvalidDumpId   0

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

◆ DumpId

typedef int DumpId

Definition at line 267 of file pg_backup.h.

◆ DumpOptions

typedef struct _dumpOptions DumpOptions

◆ RestoreOptions

◆ SetupWorkerPtrType

typedef void(* SetupWorkerPtrType) (Archive *AH)

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

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

◆ _archiveMode

Enumerator
archModeAppend 
archModeWrite 
archModeRead 

Definition at line 47 of file pg_backup.h.

48 {
52 } ArchiveMode;
@ archModeWrite
Definition: pg_backup.h:50
@ archModeAppend
Definition: pg_backup.h:49
@ archModeRead
Definition: pg_backup.h:51
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 63 of file pg_backup.h.

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

◆ _teSection

enum _teSection
Enumerator
SECTION_NONE 
SECTION_PRE_DATA 
SECTION_DATA 
SECTION_POST_DATA 

Definition at line 54 of file pg_backup.h.

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

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

Function Documentation

◆ archprintf()

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

◆ archputs()

void archputs ( const char *  s,
Archive AH 
)

Definition at line 1462 of file pg_backup_archiver.c.

1463 {
1464  WriteData(AH, s, strlen(s));
1465 }
void WriteData(Archive *AHX, const void *data, size_t dLen)

References WriteData().

Referenced by dumpTableData_insert().

◆ CloseArchive()

void CloseArchive ( Archive AHX)

Definition at line 272 of file pg_backup_archiver.c.

273 {
274  int res = 0;
275  ArchiveHandle *AH = (ArchiveHandle *) AHX;
276 
277  AH->ClosePtr(AH);
278 
279  /* Close the output */
280  errno = 0; /* in case gzclose() doesn't set it */
281  if (AH->gzOut)
282  res = GZCLOSE(AH->OF);
283  else if (AH->OF != stdout)
284  res = fclose(AH->OF);
285 
286  if (res != 0)
287  pg_fatal("could not close output file: %m");
288 }
#define GZCLOSE(fh)
#define pg_fatal(...)
ClosePtrType ClosePtr

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

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:156
#define lengthof(array)
Definition: c.h:772
#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:652
int PQconnectionUsedPassword(const PGconn *conn)
Definition: fe-connect.c:6809
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:6794
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6743
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6690
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4130
char * PQpass(const PGconn *conn)
Definition: fe-connect.c:6605
PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg)
Definition: fe-connect.c:6911
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:45
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:90
char * pgport
Definition: pg_backup.h:84
char * pghost
Definition: pg_backup.h:85
trivalue promptPassword
Definition: pg_backup.h:87
char * username
Definition: pg_backup.h:86
char * dbname
Definition: pg_backup.h:83
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 
)

Definition at line 243 of file pg_backup_archiver.c.

248 {
249  ArchiveHandle *AH = _allocAH(FileSpec, fmt, compression_spec,
251 
252  return (Archive *) AH;
253 }
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)
static PgChecksumMode mode
Definition: pg_checksums.c:65
static bool dosync
Definition: pg_dump.c:101
static void setupDumpWorker(Archive *AH)
Definition: pg_dump.c:1268

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

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 }
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:6698
int PQcancel(PGcancel *cancel, char *errbuf, int errbufsize)
Definition: fe-connect.c:4349
@ PQTRANS_ACTIVE
Definition: libpq-fe.h:119
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 179 of file pg_backup_archiver.c.

180 {
181  DumpOptions *dopt = NewDumpOptions();
182 
183  /* this is the inverse of what's at the end of pg_dump.c's main() */
184  dopt->cparams.dbname = ropt->cparams.dbname ? pg_strdup(ropt->cparams.dbname) : NULL;
185  dopt->cparams.pgport = ropt->cparams.pgport ? pg_strdup(ropt->cparams.pgport) : NULL;
186  dopt->cparams.pghost = ropt->cparams.pghost ? pg_strdup(ropt->cparams.pghost) : NULL;
187  dopt->cparams.username = ropt->cparams.username ? pg_strdup(ropt->cparams.username) : NULL;
189  dopt->outputClean = ropt->dropSchema;
190  dopt->dataOnly = ropt->dataOnly;
191  dopt->schemaOnly = ropt->schemaOnly;
192  dopt->if_exists = ropt->if_exists;
193  dopt->column_inserts = ropt->column_inserts;
194  dopt->dumpSections = ropt->dumpSections;
195  dopt->aclsSkip = ropt->aclsSkip;
196  dopt->outputSuperuser = ropt->superuser;
197  dopt->outputCreateDB = ropt->createDB;
198  dopt->outputNoOwner = ropt->noOwner;
199  dopt->outputNoTableAm = ropt->noTableAm;
200  dopt->outputNoTablespaces = ropt->noTablespace;
201  dopt->disable_triggers = ropt->disable_triggers;
202  dopt->use_setsessauth = ropt->use_setsessauth;
204  dopt->dump_inserts = ropt->dump_inserts;
205  dopt->no_comments = ropt->no_comments;
206  dopt->no_publications = ropt->no_publications;
208  dopt->no_subscriptions = ropt->no_subscriptions;
209  dopt->lockWaitTimeout = ropt->lockWaitTimeout;
212  dopt->sequence_data = ropt->sequence_data;
213 
214  return dopt;
215 }
DumpOptions * NewDumpOptions(void)
bool dataOnly
Definition: pg_backup.h:167
int dump_inserts
Definition: pg_backup.h:171
int column_inserts
Definition: pg_backup.h:175
int use_setsessauth
Definition: pg_backup.h:187
int outputCreateDB
Definition: pg_backup.h:195
bool include_everything
Definition: pg_backup.h:192
int sequence_data
Definition: pg_backup.h:201
int disable_dollar_quoting
Definition: pg_backup.h:174
int no_comments
Definition: pg_backup.h:177
int outputNoTableAm
Definition: pg_backup.h:185
int enable_row_security
Definition: pg_backup.h:188
char * outputSuperuser
Definition: pg_backup.h:199
int dumpSections
Definition: pg_backup.h:168
int no_security_labels
Definition: pg_backup.h:178
int no_publications
Definition: pg_backup.h:179
ConnParams cparams
Definition: pg_backup.h:161
const char * lockWaitTimeout
Definition: pg_backup.h:170
int no_subscriptions
Definition: pg_backup.h:180
bool aclsSkip
Definition: pg_backup.h:169
int outputClean
Definition: pg_backup.h:194
int outputNoTablespaces
Definition: pg_backup.h:186
int disable_triggers
Definition: pg_backup.h:184
int outputNoOwner
Definition: pg_backup.h:198
bool schemaOnly
Definition: pg_backup.h:166
int include_everything
Definition: pg_backup.h:123
ConnParams cparams
Definition: pg_backup.h:143
int no_subscriptions
Definition: pg_backup.h:113
int disable_dollar_quoting
Definition: pg_backup.h:106
int no_security_labels
Definition: pg_backup.h:112
char * superuser
Definition: pg_backup.h:103
const char * lockWaitTimeout
Definition: pg_backup.h:122
int enable_row_security
Definition: pg_backup.h:154
int disable_triggers
Definition: pg_backup.h:99

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 1251 of file pg_backup_archiver.c.

1252 {
1253  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1254 
1255  if (AH->EndLOPtr)
1256  AH->EndLOPtr(AH, AH->currToc, oid);
1257 
1258  return 1;
1259 }
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 165 of file pg_backup_archiver.c.

166 {
167  memset(opts, 0, sizeof(DumpOptions));
168  /* set any fields that shouldn't default to zeroes */
169  opts->include_everything = true;
170  opts->cparams.promptPassword = TRI_DEFAULT;
171  opts->dumpSections = DUMP_UNSECTIONED;
172 }
static AmcheckOptions opts
Definition: pg_amcheck.c:110
#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 153 of file pg_backup_archiver.c.

154 {
156 
158  return opts;
159 }
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 976 of file pg_backup_archiver.c.

977 {
979 
981 
982  /* set any fields that shouldn't default to zeroes */
983  opts->format = archUnknown;
984  opts->cparams.promptPassword = TRI_DEFAULT;
985  opts->dumpSections = DUMP_UNSECTIONED;
986  opts->compression_spec.algorithm = PG_COMPRESSION_NONE;
987  opts->compression_spec.level = 0;
988 
989  return opts;
990 }
@ PG_COMPRESSION_NONE
Definition: compression.h:19
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 258 of file pg_backup_archiver.c.

259 {
260  ArchiveHandle *AH;
261  pg_compress_specification compression_spec = {0};
262 
263  compression_spec.algorithm = PG_COMPRESSION_NONE;
264  AH = _allocAH(FileSpec, fmt, compression_spec, true,
266 
267  return (Archive *) AH;
268 }
static void setupRestoreWorker(Archive *AHX)
pg_compress_algorithm algorithm
Definition: compression.h:29

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

Referenced by main().

◆ PrintTOCSummary()

void PrintTOCSummary ( Archive AHX)

Definition at line 1129 of file pg_backup_archiver.c.

1130 {
1131  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1132  RestoreOptions *ropt = AH->public.ropt;
1133  TocEntry *te;
1134  pg_compress_specification out_compression_spec = {0};
1135  teSection curSection;
1136  OutputContext sav;
1137  const char *fmtName;
1138  char stamp_str[64];
1139 
1140  /* TOC is always uncompressed */
1141  out_compression_spec.algorithm = PG_COMPRESSION_NONE;
1142 
1143  sav = SaveOutput(AH);
1144  if (ropt->filename)
1145  SetOutput(AH, ropt->filename, out_compression_spec);
1146 
1147  if (strftime(stamp_str, sizeof(stamp_str), PGDUMP_STRFTIME_FMT,
1148  localtime(&AH->createDate)) == 0)
1149  strcpy(stamp_str, "[unknown]");
1150 
1151  ahprintf(AH, ";\n; Archive created at %s\n", stamp_str);
1152  ahprintf(AH, "; dbname: %s\n; TOC Entries: %d\n; Compression: %d\n",
1153  sanitize_line(AH->archdbname, false),
1154  AH->tocCount, AH->compression_spec.level);
1155 
1156  switch (AH->format)
1157  {
1158  case archCustom:
1159  fmtName = "CUSTOM";
1160  break;
1161  case archDirectory:
1162  fmtName = "DIRECTORY";
1163  break;
1164  case archTar:
1165  fmtName = "TAR";
1166  break;
1167  default:
1168  fmtName = "UNKNOWN";
1169  }
1170 
1171  ahprintf(AH, "; Dump Version: %d.%d-%d\n",
1173  ahprintf(AH, "; Format: %s\n", fmtName);
1174  ahprintf(AH, "; Integer: %d bytes\n", (int) AH->intSize);
1175  ahprintf(AH, "; Offset: %d bytes\n", (int) AH->offSize);
1176  if (AH->archiveRemoteVersion)
1177  ahprintf(AH, "; Dumped from database version: %s\n",
1178  AH->archiveRemoteVersion);
1179  if (AH->archiveDumpVersion)
1180  ahprintf(AH, "; Dumped by pg_dump version: %s\n",
1181  AH->archiveDumpVersion);
1182 
1183  ahprintf(AH, ";\n;\n; Selected TOC Entries:\n;\n");
1184 
1185  curSection = SECTION_PRE_DATA;
1186  for (te = AH->toc->next; te != AH->toc; te = te->next)
1187  {
1188  if (te->section != SECTION_NONE)
1189  curSection = te->section;
1190  if (ropt->verbose ||
1191  (_tocEntryRequired(te, curSection, AH) & (REQ_SCHEMA | REQ_DATA)) != 0)
1192  {
1193  char *sanitized_name;
1194  char *sanitized_schema;
1195  char *sanitized_owner;
1196 
1197  /*
1198  */
1199  sanitized_name = sanitize_line(te->tag, false);
1200  sanitized_schema = sanitize_line(te->namespace, true);
1201  sanitized_owner = sanitize_line(te->owner, false);
1202 
1203  ahprintf(AH, "%d; %u %u %s %s %s %s\n", te->dumpId,
1204  te->catalogId.tableoid, te->catalogId.oid,
1205  te->desc, sanitized_schema, sanitized_name,
1206  sanitized_owner);
1207 
1208  free(sanitized_name);
1209  free(sanitized_schema);
1210  free(sanitized_owner);
1211  }
1212  if (ropt->verbose && te->nDeps > 0)
1213  {
1214  int i;
1215 
1216  ahprintf(AH, ";\tdepends on:");
1217  for (i = 0; i < te->nDeps; i++)
1218  ahprintf(AH, " %d", te->dependencies[i]);
1219  ahprintf(AH, "\n");
1220  }
1221  }
1222 
1223  /* Enforce strict names checking */
1224  if (ropt->strict_names)
1225  StrictNamesCheck(ropt);
1226 
1227  if (ropt->filename)
1228  RestoreOutput(AH, sav);
1229 }
#define PGDUMP_STRFTIME_FMT
Definition: dumputils.h:33
static void StrictNamesCheck(RestoreOptions *ropt)
static OutputContext SaveOutput(ArchiveHandle *AH)
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, OutputContext savedContext)
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:212
Oid tableoid
Definition: pg_backup.h:263
ArchiveFormat format
struct _tocEntry * toc
pg_compress_specification compression_spec
const char * filename
Definition: pg_backup.h:116
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, i, _archiveHandle::intSize, pg_compress_specification::level, _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 305 of file pg_backup_archiver.c.

306 {
307  ArchiveHandle *AH = (ArchiveHandle *) AHX;
308  RestoreOptions *ropt = AH->public.ropt;
309  TocEntry *te;
310  teSection curSection;
311 
312  /* Decide which TOC entries will be dumped/restored, and mark them */
313  curSection = SECTION_PRE_DATA;
314  for (te = AH->toc->next; te != AH->toc; te = te->next)
315  {
316  /*
317  * When writing an archive, we also take this opportunity to check
318  * that we have generated the entries in a sane order that respects
319  * the section divisions. When reading, don't complain, since buggy
320  * old versions of pg_dump might generate out-of-order archives.
321  */
322  if (AH->mode != archModeRead)
323  {
324  switch (te->section)
325  {
326  case SECTION_NONE:
327  /* ok to be anywhere */
328  break;
329  case SECTION_PRE_DATA:
330  if (curSection != SECTION_PRE_DATA)
331  pg_log_warning("archive items not in correct section order");
332  break;
333  case SECTION_DATA:
334  if (curSection == SECTION_POST_DATA)
335  pg_log_warning("archive items not in correct section order");
336  break;
337  case SECTION_POST_DATA:
338  /* ok no matter which section we were in */
339  break;
340  default:
341  pg_fatal("unexpected section code %d",
342  (int) te->section);
343  break;
344  }
345  }
346 
347  if (te->section != SECTION_NONE)
348  curSection = te->section;
349 
350  te->reqs = _tocEntryRequired(te, curSection, AH);
351  }
352 
353  /* Enforce strict names checking */
354  if (ropt->strict_names)
355  StrictNamesCheck(ropt);
356 }
#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 360 of file pg_backup_archiver.c.

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

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(), _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_GZIP, 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_toc_entries_parallel(), restore_toc_entries_postfork(), restore_toc_entries_prefork(), restore_toc_entry(), RestoreOutput(), Archive::ropt, SaveOutput(), SetOutput(), _restoreOptions::single_txn, snprintf, _archiveHandle::stage, STAGE_FINALIZING, STAGE_INITIALIZING, STAGE_PROCESSING, StartTransaction(), _tocEntry::tag, _archiveHandle::toc, _archiveHandle::tocsByDumpId, _restoreOptions::useDB, Archive::verbose, and _archiveHandle::version.

Referenced by _CloseArchive(), and main().

◆ SetArchiveOptions()

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

Definition at line 292 of file pg_backup_archiver.c.

293 {
294  /* Caller can omit dump options, in which case we synthesize them */
295  if (dopt == NULL && ropt != NULL)
296  dopt = dumpOptionsFromRestoreOptions(ropt);
297 
298  /* Save options for later access */
299  AH->dopt = dopt;
300  AH->ropt = ropt;
301 }
DumpOptions * dumpOptionsFromRestoreOptions(RestoreOptions *ropt)
DumpOptions * dopt
Definition: pg_backup.h:211

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

Referenced by _CloseArchive(), and main().

◆ SortTocFromFile()

void SortTocFromFile ( Archive AHX)

Definition at line 1381 of file pg_backup_archiver.c.

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

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 1237 of file pg_backup_archiver.c.

1238 {
1239  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1240 
1241  if (!AH->StartLOPtr)
1242  pg_fatal("large-object output not supported in chosen format");
1243 
1244  AH->StartLOPtr(AH, AH->currToc, oid);
1245 
1246  return 1;
1247 }
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 1050 of file pg_backup_archiver.c.

1051 {
1052  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1053 
1054  if (!AH->currToc)
1055  pg_fatal("internal error -- WriteData cannot be called outside the context of a DataDumper routine");
1056 
1057  AH->WriteDataPtr(AH, data, dLen);
1058 }
const void * data
WriteDataPtrType WriteDataPtr

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

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