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

1508 {
1509  WriteData(AH, s, strlen(s));
1510 }
void WriteData(Archive *AHX, const void *data, size_t dLen)

References WriteData().

Referenced by dumpTableData_insert().

◆ CloseArchive()

void CloseArchive ( Archive AHX)

Definition at line 267 of file pg_backup_archiver.c.

268 {
269  ArchiveHandle *AH = (ArchiveHandle *) AHX;
270 
271  AH->ClosePtr(AH);
272 
273  /* Close the output */
274  errno = 0;
275  if (!EndCompressFileHandle(AH->OF))
276  pg_fatal("could not close output file: %m");
277 }
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: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:678
int PQconnectionUsedPassword(const PGconn *conn)
Definition: fe-connect.c:7311
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:7296
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:7245
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:7192
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4599
char * PQpass(const PGconn *conn)
Definition: fe-connect.c:7107
PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg)
Definition: fe-connect.c:7424
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 238 of file pg_backup_archiver.c.

243 {
244  ArchiveHandle *AH = _allocAH(FileSpec, fmt, compression_spec,
246 
247  return (Archive *) AH;
248 }
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:102
static void setupDumpWorker(Archive *AH)
Definition: pg_dump.c:1310

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:7200
int PQcancel(PGcancel *cancel, char *errbuf, int errbufsize)
Definition: fe-connect.c:4818
@ 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 174 of file pg_backup_archiver.c.

175 {
176  DumpOptions *dopt = NewDumpOptions();
177 
178  /* this is the inverse of what's at the end of pg_dump.c's main() */
179  dopt->cparams.dbname = ropt->cparams.dbname ? pg_strdup(ropt->cparams.dbname) : NULL;
180  dopt->cparams.pgport = ropt->cparams.pgport ? pg_strdup(ropt->cparams.pgport) : NULL;
181  dopt->cparams.pghost = ropt->cparams.pghost ? pg_strdup(ropt->cparams.pghost) : NULL;
182  dopt->cparams.username = ropt->cparams.username ? pg_strdup(ropt->cparams.username) : NULL;
184  dopt->outputClean = ropt->dropSchema;
185  dopt->dataOnly = ropt->dataOnly;
186  dopt->schemaOnly = ropt->schemaOnly;
187  dopt->if_exists = ropt->if_exists;
188  dopt->column_inserts = ropt->column_inserts;
189  dopt->dumpSections = ropt->dumpSections;
190  dopt->aclsSkip = ropt->aclsSkip;
191  dopt->outputSuperuser = ropt->superuser;
192  dopt->outputCreateDB = ropt->createDB;
193  dopt->outputNoOwner = ropt->noOwner;
194  dopt->outputNoTableAm = ropt->noTableAm;
195  dopt->outputNoTablespaces = ropt->noTablespace;
196  dopt->disable_triggers = ropt->disable_triggers;
197  dopt->use_setsessauth = ropt->use_setsessauth;
199  dopt->dump_inserts = ropt->dump_inserts;
200  dopt->no_comments = ropt->no_comments;
201  dopt->no_publications = ropt->no_publications;
203  dopt->no_subscriptions = ropt->no_subscriptions;
204  dopt->lockWaitTimeout = ropt->lockWaitTimeout;
207  dopt->sequence_data = ropt->sequence_data;
208 
209  return dopt;
210 }
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 1296 of file pg_backup_archiver.c.

1297 {
1298  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1299 
1300  if (AH->EndLOPtr)
1301  AH->EndLOPtr(AH, AH->currToc, oid);
1302 
1303  return 1;
1304 }
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 160 of file pg_backup_archiver.c.

161 {
162  memset(opts, 0, sizeof(DumpOptions));
163  /* set any fields that shouldn't default to zeroes */
164  opts->include_everything = true;
165  opts->cparams.promptPassword = TRI_DEFAULT;
166  opts->dumpSections = DUMP_UNSECTIONED;
167 }
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 148 of file pg_backup_archiver.c.

149 {
151 
153  return opts;
154 }
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 983 of file pg_backup_archiver.c.

984 {
986 
988 
989  /* set any fields that shouldn't default to zeroes */
990  opts->format = archUnknown;
991  opts->cparams.promptPassword = TRI_DEFAULT;
992  opts->dumpSections = DUMP_UNSECTIONED;
993  opts->compression_spec.algorithm = PG_COMPRESSION_NONE;
994  opts->compression_spec.level = 0;
995 
996  return opts;
997 }
@ 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 253 of file pg_backup_archiver.c.

254 {
255  ArchiveHandle *AH;
256  pg_compress_specification compression_spec = {0};
257 
258  compression_spec.algorithm = PG_COMPRESSION_NONE;
259  AH = _allocAH(FileSpec, fmt, compression_spec, true,
261 
262  return (Archive *) AH;
263 }
static void setupRestoreWorker(Archive *AHX)
pg_compress_algorithm algorithm
Definition: compression.h:34

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

Referenced by main().

◆ PrintTOCSummary()

void PrintTOCSummary ( Archive AHX)

Definition at line 1173 of file pg_backup_archiver.c.

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

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

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

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

282 {
283  /* Caller can omit dump options, in which case we synthesize them */
284  if (dopt == NULL && ropt != NULL)
285  dopt = dumpOptionsFromRestoreOptions(ropt);
286 
287  /* Save options for later access */
288  AH->dopt = dopt;
289  AH->ropt = ropt;
290 }
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 1426 of file pg_backup_archiver.c.

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

1283 {
1284  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1285 
1286  if (!AH->StartLOPtr)
1287  pg_fatal("large-object output not supported in chosen format");
1288 
1289  AH->StartLOPtr(AH, AH->currToc, oid);
1290 
1291  return 1;
1292 }
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 1094 of file pg_backup_archiver.c.

1095 {
1096  ArchiveHandle *AH = (ArchiveHandle *) AHX;
1097 
1098  if (!AH->currToc)
1099  pg_fatal("internal error -- WriteData cannot be called outside the context of a DataDumper routine");
1100 
1101  AH->WriteDataPtr(AH, data, dLen);
1102 }
const void * data
WriteDataPtrType WriteDataPtr

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

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