PostgreSQL Source Code git master
pg_backup.h File Reference
#include "common/compression.h"
#include "common/file_utils.h"
#include "fe_utils/simple_list.h"
#include "libpq-fe.h"
Include dependency graph for pg_backup.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  _connParams
 
struct  _restoreOptions
 
struct  _dumpOptions
 
struct  Archive
 
struct  CatalogId
 

Macros

#define NUM_PREP_QUERIES   (PREPQUERY_GETDOMAINCONSTRAINTS + 1)
 
#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
}
 

Functions

void ConnectDatabase (Archive *AHX, const ConnParams *cparams, bool isReconnect)
 
void DisconnectDatabase (Archive *AHX)
 
PGconnGetConnection (Archive *AHX)
 
void WriteData (Archive *AHX, const void *data, size_t dLen)
 
int StartLO (Archive *AHX, Oid oid)
 
int EndLO (Archive *AHX, Oid oid)
 
void CloseArchive (Archive *AHX)
 
void SetArchiveOptions (Archive *AH, DumpOptions *dopt, RestoreOptions *ropt)
 
void ProcessArchiveRestoreOptions (Archive *AHX)
 
void RestoreArchive (Archive *AHX)
 
ArchiveOpenArchive (const char *FileSpec, const ArchiveFormat fmt)
 
ArchiveCreateArchive (const char *FileSpec, const ArchiveFormat fmt, const pg_compress_specification compression_spec, bool dosync, ArchiveMode mode, SetupWorkerPtrType setupDumpWorker, DataDirSyncMethod sync_method)
 
void PrintTOCSummary (Archive *AHX)
 
RestoreOptionsNewRestoreOptions (void)
 
DumpOptionsNewDumpOptions (void)
 
void InitDumpOptions (DumpOptions *opts)
 
DumpOptionsdumpOptionsFromRestoreOptions (RestoreOptions *ropt)
 
void SortTocFromFile (Archive *AHX)
 
void archputs (const char *s, Archive *AH)
 
int archprintf (Archive *AH, const char *fmt,...) pg_attribute_printf(2
 

Macro Definition Documentation

◆ appendStringLiteralAH

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

Definition at line 337 of file pg_backup.h.

◆ InvalidDumpId

#define InvalidDumpId   0

Definition at line 277 of file pg_backup.h.

◆ NUM_PREP_QUERIES

#define NUM_PREP_QUERIES   (PREPQUERY_GETDOMAINCONSTRAINTS + 1)

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

◆ DumpId

typedef int DumpId

Definition at line 275 of file pg_backup.h.

◆ DumpOptions

typedef struct _dumpOptions DumpOptions

◆ RestoreOptions

◆ SetupWorkerPtrType

typedef void(* SetupWorkerPtrType) (Archive *AH)

Definition at line 285 of file pg_backup.h.

◆ teSection

typedef enum _teSection teSection

◆ trivalue

typedef enum trivalue trivalue

Enumeration Type Documentation

◆ _archiveFormat

Enumerator
archUnknown 
archCustom 
archTar 
archNull 
archDirectory 

Definition at line 39 of file pg_backup.h.

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

◆ _archiveMode

Enumerator
archModeAppend 
archModeWrite 
archModeRead 

Definition at line 48 of file pg_backup.h.

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

◆ _dumpPreparedQueries

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

Definition at line 64 of file pg_backup.h.

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

◆ _teSection

enum _teSection
Enumerator
SECTION_NONE 
SECTION_PRE_DATA 
SECTION_DATA 
SECTION_POST_DATA 

Definition at line 55 of file pg_backup.h.

56{
57 SECTION_NONE = 1, /* comments, ACLs, etc; can be anywhere */
58 SECTION_PRE_DATA, /* stuff to be processed before data */
59 SECTION_DATA, /* table data, large objects, LO comments */
60 SECTION_POST_DATA, /* stuff to be processed after data */
61} teSection;
@ SECTION_NONE
Definition: pg_backup.h:57
@ SECTION_POST_DATA
Definition: pg_backup.h:60
@ SECTION_PRE_DATA
Definition: pg_backup.h:58
@ SECTION_DATA
Definition: pg_backup.h:59
enum _teSection teSection

◆ trivalue

enum trivalue
Enumerator
TRI_DEFAULT 
TRI_NO 
TRI_YES 
TRI_DEFAULT 
TRI_NO 
TRI_YES 
TRI_DEFAULT 
TRI_NO 
TRI_YES 
TRI_DEFAULT 
TRI_NO 
TRI_YES 

Definition at line 32 of file pg_backup.h.

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

Function Documentation

◆ archprintf()

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

◆ archputs()

void archputs ( const char *  s,
Archive AH 
)

Definition at line 1614 of file pg_backup_archiver.c.

1615{
1616 WriteData(AH, s, strlen(s));
1617}
void WriteData(Archive *AHX, const void *data, size_t dLen)

References WriteData().

Referenced by dumpTableData_insert().

◆ CloseArchive()

void CloseArchive ( Archive AHX)

Definition at line 254 of file pg_backup_archiver.c.

255{
256 ArchiveHandle *AH = (ArchiveHandle *) AHX;
257
258 AH->ClosePtr(AH);
259
260 /* Close the output */
261 errno = 0;
262 if (!EndCompressFileHandle(AH->OF))
263 pg_fatal("could not close output file: %m");
264}
bool EndCompressFileHandle(CompressFileHandle *CFH)
Definition: compress_io.c:288
#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 108 of file pg_backup_db.c.

111{
112 ArchiveHandle *AH = (ArchiveHandle *) AHX;
113 trivalue prompt_password;
114 char *password;
115 bool new_pass;
116
117 if (AH->connection)
118 pg_fatal("already connected to a database");
119
120 /* Never prompt for a password during a reconnection */
121 prompt_password = isReconnect ? TRI_NO : cparams->promptPassword;
122
124
125 if (prompt_password == TRI_YES && password == NULL)
126 password = simple_prompt("Password: ", false);
127
128 /*
129 * Start the connection. Loop until we have a password if requested by
130 * backend.
131 */
132 do
133 {
134 const char *keywords[8];
135 const char *values[8];
136 int i = 0;
137
138 /*
139 * If dbname is a connstring, its entries can override the other
140 * values obtained from cparams; but in turn, override_dbname can
141 * override the dbname component of it.
142 */
143 keywords[i] = "host";
144 values[i++] = cparams->pghost;
145 keywords[i] = "port";
146 values[i++] = cparams->pgport;
147 keywords[i] = "user";
148 values[i++] = cparams->username;
149 keywords[i] = "password";
150 values[i++] = password;
151 keywords[i] = "dbname";
152 values[i++] = cparams->dbname;
153 if (cparams->override_dbname)
154 {
155 keywords[i] = "dbname";
156 values[i++] = cparams->override_dbname;
157 }
158 keywords[i] = "fallback_application_name";
159 values[i++] = progname;
160 keywords[i] = NULL;
161 values[i++] = NULL;
163
164 new_pass = false;
166
167 if (!AH->connection)
168 pg_fatal("could not connect to database");
169
170 if (PQstatus(AH->connection) == CONNECTION_BAD &&
172 password == NULL &&
173 prompt_password != TRI_NO)
174 {
175 PQfinish(AH->connection);
176 password = simple_prompt("Password: ", false);
177 new_pass = true;
178 }
179 } while (new_pass);
180
181 /* check to see that the backend connection was successfully made */
183 {
184 if (isReconnect)
185 pg_fatal("reconnection failed: %s",
187 else
188 pg_fatal("%s",
190 }
191
192 /* Start strict; later phases may override this. */
195
196 if (password && password != AH->savedPassword)
197 free(password);
198
199 /*
200 * We want to remember connection's actual password, whether or not we got
201 * it by prompting. So we don't just store the password variable.
202 */
204 {
205 free(AH->savedPassword);
207 }
208
209 /* check for version mismatch */
211
213
214 /* arrange for SIGINT to issue a query cancel on this connection */
216}
void set_archive_cancel_info(ArchiveHandle *AH, PGconn *conn)
Definition: parallel.c:732
static Datum values[MAXATTR]
Definition: bootstrap.c:151
#define Assert(condition)
Definition: c.h:815
#define lengthof(array)
Definition: c.h:745
#define ALWAYS_SECURE_SEARCH_PATH_SQL
Definition: connect.h:25
int PQconnectionUsedPassword(const PGconn *conn)
Definition: fe-connect.c:7334
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:7319
char * PQpass(const PGconn *conn)
Definition: fe-connect.c:7120
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:7205
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4939
PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg)
Definition: fe-connect.c:7447
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:7268
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:698
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define free(a)
Definition: header.h:65
int i
Definition: isn.c:72
static const JsonPathKeyword keywords[]
@ CONNECTION_BAD
Definition: libpq-fe.h:82
const char * progname
Definition: main.c:44
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:303
static void _check_database_version(ArchiveHandle *AH)
Definition: pg_backup_db.c:31
static void notice_processor(void *arg, const char *message)
Definition: pg_backup_db.c:260
char * simple_prompt(const char *prompt, bool echo)
Definition: sprompt.c:38
static char * password
Definition: streamutil.c:52
char * override_dbname
Definition: pg_backup.h:92
char * pgport
Definition: pg_backup.h:86
char * pghost
Definition: pg_backup.h:87
trivalue promptPassword
Definition: pg_backup.h:89
char * username
Definition: pg_backup.h:88
char * dbname
Definition: pg_backup.h:85
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, keywords, lengthof, notice_processor(), _connParams::override_dbname, password, pg_fatal, pg_strdup(), _connParams::pghost, _connParams::pgport, PQclear(), PQconnectdbParams(), PQconnectionNeedsPassword(), PQconnectionUsedPassword(), PQerrorMessage(), PQfinish(), PQpass(), PQsetNoticeProcessor(), PQstatus(), progname, _connParams::promptPassword, _archiveHandle::savedPassword, set_archive_cancel_info(), simple_prompt(), TRI_NO, TRI_YES, _connParams::username, and values.

Referenced by CloneArchive(), main(), ReconnectToServer(), restore_toc_entries_postfork(), and RestoreArchive().

◆ CreateArchive()

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

Definition at line 223 of file pg_backup_archiver.c.

229{
230 ArchiveHandle *AH = _allocAH(FileSpec, fmt, compression_spec,
232
233 return (Archive *) AH;
234}
static DataDirSyncMethod sync_method
Definition: initdb.c:170
static void const char * fmt
static ArchiveHandle * _allocAH(const char *FileSpec, const ArchiveFormat fmt, const pg_compress_specification compression_spec, bool dosync, ArchiveMode mode, SetupWorkerPtrType setupWorkerPtr, DataDirSyncMethod sync_method)
static PgChecksumMode mode
Definition: pg_checksums.c:55
static bool dosync
Definition: pg_dump.c:146
static void setupDumpWorker(Archive *AH)
Definition: pg_dump.c:1429

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

Referenced by main().

◆ DisconnectDatabase()

void DisconnectDatabase ( Archive AHX)

Definition at line 223 of file pg_backup_db.c.

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

160{
161 DumpOptions *dopt = NewDumpOptions();
162
163 /* this is the inverse of what's at the end of pg_dump.c's main() */
164 dopt->cparams.dbname = ropt->cparams.dbname ? pg_strdup(ropt->cparams.dbname) : NULL;
165 dopt->cparams.pgport = ropt->cparams.pgport ? pg_strdup(ropt->cparams.pgport) : NULL;
166 dopt->cparams.pghost = ropt->cparams.pghost ? pg_strdup(ropt->cparams.pghost) : NULL;
167 dopt->cparams.username = ropt->cparams.username ? pg_strdup(ropt->cparams.username) : NULL;
169 dopt->outputClean = ropt->dropSchema;
170 dopt->dumpData = ropt->dumpData;
171 dopt->dumpSchema = ropt->dumpSchema;
172 dopt->if_exists = ropt->if_exists;
173 dopt->column_inserts = ropt->column_inserts;
174 dopt->dumpSections = ropt->dumpSections;
175 dopt->aclsSkip = ropt->aclsSkip;
176 dopt->outputSuperuser = ropt->superuser;
177 dopt->outputCreateDB = ropt->createDB;
178 dopt->outputNoOwner = ropt->noOwner;
179 dopt->outputNoTableAm = ropt->noTableAm;
180 dopt->outputNoTablespaces = ropt->noTablespace;
182 dopt->use_setsessauth = ropt->use_setsessauth;
184 dopt->dump_inserts = ropt->dump_inserts;
185 dopt->no_comments = ropt->no_comments;
186 dopt->no_publications = ropt->no_publications;
189 dopt->lockWaitTimeout = ropt->lockWaitTimeout;
192 dopt->sequence_data = ropt->sequence_data;
193
194 return dopt;
195}
DumpOptions * NewDumpOptions(void)
int dump_inserts
Definition: pg_backup.h:175
int column_inserts
Definition: pg_backup.h:179
int use_setsessauth
Definition: pg_backup.h:191
int outputCreateDB
Definition: pg_backup.h:199
bool include_everything
Definition: pg_backup.h:196
int sequence_data
Definition: pg_backup.h:205
int disable_dollar_quoting
Definition: pg_backup.h:178
bool dumpSchema
Definition: pg_backup.h:209
int no_comments
Definition: pg_backup.h:181
int outputNoTableAm
Definition: pg_backup.h:189
int enable_row_security
Definition: pg_backup.h:192
char * outputSuperuser
Definition: pg_backup.h:203
int dumpSections
Definition: pg_backup.h:172
int no_security_labels
Definition: pg_backup.h:182
bool dumpData
Definition: pg_backup.h:210
int no_publications
Definition: pg_backup.h:183
ConnParams cparams
Definition: pg_backup.h:167
const char * lockWaitTimeout
Definition: pg_backup.h:174
int no_subscriptions
Definition: pg_backup.h:184
bool aclsSkip
Definition: pg_backup.h:173
int outputClean
Definition: pg_backup.h:198
int outputNoTablespaces
Definition: pg_backup.h:190
int disable_triggers
Definition: pg_backup.h:188
int outputNoOwner
Definition: pg_backup.h:202
int include_everything
Definition: pg_backup.h:123
ConnParams cparams
Definition: pg_backup.h:143
int no_subscriptions
Definition: pg_backup.h:115
int disable_dollar_quoting
Definition: pg_backup.h:108
int no_security_labels
Definition: pg_backup.h:114
char * superuser
Definition: pg_backup.h:105
const char * lockWaitTimeout
Definition: pg_backup.h:122
int enable_row_security
Definition: pg_backup.h:156
int disable_triggers
Definition: pg_backup.h:101

References _restoreOptions::aclsSkip, _dumpOptions::aclsSkip, _restoreOptions::column_inserts, _dumpOptions::column_inserts, _restoreOptions::cparams, _dumpOptions::cparams, _restoreOptions::createDB, _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::dumpData, _dumpOptions::dumpData, _restoreOptions::dumpSchema, _dumpOptions::dumpSchema, _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::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 1392 of file pg_backup_archiver.c.

1393{
1394 ArchiveHandle *AH = (ArchiveHandle *) AHX;
1395
1396 if (AH->EndLOPtr)
1397 AH->EndLOPtr(AH, AH->currToc, oid);
1398
1399 return 1;
1400}
EndLOPtrType EndLOPtr
struct _tocEntry * currToc

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

Referenced by dumpLOs().

◆ GetConnection()

PGconn * GetConnection ( Archive AHX)

Definition at line 252 of file pg_backup_db.c.

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

References _archiveHandle::connection.

◆ InitDumpOptions()

void InitDumpOptions ( DumpOptions opts)

Definition at line 143 of file pg_backup_archiver.c.

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

References DUMP_UNSECTIONED, opts, and TRI_DEFAULT.

Referenced by main(), and NewDumpOptions().

◆ NewDumpOptions()

DumpOptions * NewDumpOptions ( void  )

Definition at line 131 of file pg_backup_archiver.c.

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

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

Referenced by dumpOptionsFromRestoreOptions().

◆ NewRestoreOptions()

RestoreOptions * NewRestoreOptions ( void  )

Definition at line 1073 of file pg_backup_archiver.c.

1074{
1076
1078
1079 /* set any fields that shouldn't default to zeroes */
1080 opts->format = archUnknown;
1081 opts->cparams.promptPassword = TRI_DEFAULT;
1082 opts->dumpSections = DUMP_UNSECTIONED;
1083 opts->compression_spec.algorithm = PG_COMPRESSION_NONE;
1084 opts->compression_spec.level = 0;
1085 opts->dumpSchema = true;
1086 opts->dumpData = true;
1087
1088 return opts;
1089}
@ 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 239 of file pg_backup_archiver.c.

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

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

Referenced by main().

◆ PrintTOCSummary()

void PrintTOCSummary ( Archive AHX)

Definition at line 1266 of file pg_backup_archiver.c.

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

References _tocEntryRequired(), ahprintf(), pg_compress_specification::algorithm, archCustom, _archiveHandle::archdbname, archDirectory, ARCHIVE_MAJOR, ARCHIVE_MINOR, ARCHIVE_REV, _archiveHandle::archiveDumpVersion, _archiveHandle::archiveRemoteVersion, archTar, _tocEntry::catalogId, _archiveHandle::compression_spec, _archiveHandle::createDate, _tocEntry::dependencies, _tocEntry::desc, _tocEntry::dumpId, _restoreOptions::filename, _archiveHandle::format, free, get_compress_algorithm_name(), i, _archiveHandle::intSize, _tocEntry::nDeps, _tocEntry::next, _archiveHandle::offSize, CatalogId::oid, _tocEntry::owner, PG_COMPRESSION_NONE, PGDUMP_STRFTIME_FMT, _archiveHandle::public, REQ_DATA, REQ_SCHEMA, _tocEntry::reqs, RestoreOutput(), Archive::ropt, sanitize_line(), SaveOutput(), _tocEntry::section, SECTION_NONE, SECTION_PRE_DATA, SetOutput(), _restoreOptions::strict_names, StrictNamesCheck(), CatalogId::tableoid, _tocEntry::tag, _archiveHandle::toc, _archiveHandle::tocCount, _restoreOptions::verbose, and _archiveHandle::version.

Referenced by main().

◆ ProcessArchiveRestoreOptions()

void ProcessArchiveRestoreOptions ( Archive AHX)

Definition at line 281 of file pg_backup_archiver.c.

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

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

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, _tocEntry::desc, destroyPQExpBuffer(), DisconnectDatabase(), DropLOIfExists(), _restoreOptions::dropSchema, _tocEntry::dropStmt, _restoreOptions::dumpSchema, dumpTimestamp(), errmsg(), _restoreOptions::filename, free, _tocEntry::hadDumper, _restoreOptions::if_exists, IssueCommandPerBlob(), K_VERS_1_3, K_VERS_1_8, Archive::maxRemoteVersion, Archive::minRemoteVersion, _tocEntry::next, _archiveHandle::noTocComments, Archive::numWorkers, CatalogId::oid, ParallelBackupEnd(), ParallelBackupStart(), pending_list_header_init(), PG_COMPRESSION_NONE, pg_fatal, pg_free(), pg_log_info, pg_log_warning, pg_strdup(), _archiveHandle::PrepParallelRestorePtr, _tocEntry::prev, _archiveHandle::PrintTocDataPtr, _archiveHandle::public, _archiveHandle::ReopenPtr, REQ_DATA, REQ_SCHEMA, _tocEntry::reqs, RESTORE_PASS_ACL, RESTORE_PASS_MAIN, RESTORE_PASS_POST_ACL, restore_toc_entries_parallel(), restore_toc_entries_postfork(), restore_toc_entries_prefork(), restore_toc_entry(), RestoreOutput(), Archive::ropt, SaveOutput(), SetOutput(), _restoreOptions::single_txn, snprintf, _archiveHandle::stage, STAGE_FINALIZING, STAGE_INITIALIZING, STAGE_PROCESSING, StartTransaction(), supports_compression(), _tocEntry::tag, _archiveHandle::toc, _archiveHandle::tocsByDumpId, _restoreOptions::txn_size, _archiveHandle::txnCount, _restoreOptions::useDB, Archive::verbose, and _archiveHandle::version.

Referenced by _CloseArchive(), and main().

◆ SetArchiveOptions()

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

Definition at line 268 of file pg_backup_archiver.c.

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

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

Referenced by _CloseArchive(), and main().

◆ SortTocFromFile()

void SortTocFromFile ( Archive AHX)

Definition at line 1533 of file pg_backup_archiver.c.

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

1379{
1380 ArchiveHandle *AH = (ArchiveHandle *) AHX;
1381
1382 if (!AH->StartLOPtr)
1383 pg_fatal("large-object output not supported in chosen format");
1384
1385 AH->StartLOPtr(AH, AH->currToc, oid);
1386
1387 return 1;
1388}
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 1186 of file pg_backup_archiver.c.

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

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

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