PostgreSQL Source Code git master
pg_backup_db.c File Reference
#include "postgres_fe.h"
#include <unistd.h>
#include <ctype.h>
#include "common/connect.h"
#include "common/string.h"
#include "parallel.h"
#include "pg_backup_archiver.h"
#include "pg_backup_db.h"
#include "pg_backup_utils.h"
Include dependency graph for pg_backup_db.c:

Go to the source code of this file.

Functions

static void _check_database_version (ArchiveHandle *AH)
 
static void notice_processor (void *arg, const char *message)
 
void ReconnectToServer (ArchiveHandle *AH, const char *dbname)
 
void ConnectDatabase (Archive *AHX, const ConnParams *cparams, bool isReconnect)
 
void DisconnectDatabase (Archive *AHX)
 
PGconnGetConnection (Archive *AHX)
 
static void die_on_query_failure (ArchiveHandle *AH, const char *query)
 
void ExecuteSqlStatement (Archive *AHX, const char *query)
 
PGresultExecuteSqlQuery (Archive *AHX, const char *query, ExecStatusType status)
 
PGresultExecuteSqlQueryForSingleRow (Archive *fout, const char *query)
 
static void ExecuteSqlCommand (ArchiveHandle *AH, const char *qry, const char *desc)
 
static void ExecuteSimpleCommands (ArchiveHandle *AH, const char *buf, size_t bufLen)
 
int ExecuteSqlCommandBuf (Archive *AHX, const char *buf, size_t bufLen)
 
void EndDBCopyMode (Archive *AHX, const char *tocEntryTag)
 
void StartTransaction (Archive *AHX)
 
void CommitTransaction (Archive *AHX)
 
void IssueCommandPerBlob (ArchiveHandle *AH, TocEntry *te, const char *cmdBegin, const char *cmdEnd)
 
void IssueACLPerBlob (ArchiveHandle *AH, TocEntry *te)
 
void DropLOIfExists (ArchiveHandle *AH, Oid oid)
 

Function Documentation

◆ _check_database_version()

static void _check_database_version ( ArchiveHandle AH)
static

Definition at line 31 of file pg_backup_db.c.

32{
33 const char *remoteversion_str;
34 int remoteversion;
35 PGresult *res;
36
37 remoteversion_str = PQparameterStatus(AH->connection, "server_version");
38 remoteversion = PQserverVersion(AH->connection);
39 if (remoteversion == 0 || !remoteversion_str)
40 pg_fatal("could not get \"server_version\" from libpq");
41
42 AH->public.remoteVersionStr = pg_strdup(remoteversion_str);
43 AH->public.remoteVersion = remoteversion;
44 if (!AH->archiveRemoteVersion)
46
47 if (remoteversion != PG_VERSION_NUM
48 && (remoteversion < AH->public.minRemoteVersion ||
49 remoteversion > AH->public.maxRemoteVersion))
50 {
51 pg_log_error("aborting because of server version mismatch");
52 pg_log_error_detail("server version: %s; %s version: %s",
53 remoteversion_str, progname, PG_VERSION);
54 exit(1);
55 }
56
57 /*
58 * Check if server is in recovery mode, which means we are on a hot
59 * standby.
60 */
62 "SELECT pg_catalog.pg_is_in_recovery()");
63 AH->public.isStandby = (strcmp(PQgetvalue(res, 0, 0), "t") == 0);
64 PQclear(res);
65}
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:7543
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:7508
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3876
void PQclear(PGresult *res)
Definition: fe-exec.c:721
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define pg_log_error(...)
Definition: logging.h:106
#define pg_log_error_detail(...)
Definition: logging.h:109
const char * progname
Definition: main.c:44
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:303
#define pg_fatal(...)
int remoteVersion
Definition: pg_backup.h:229
char * remoteVersionStr
Definition: pg_backup.h:228
bool isStandby
Definition: pg_backup.h:230
int maxRemoteVersion
Definition: pg_backup.h:233

References _archiveHandle::archiveRemoteVersion, _archiveHandle::connection, ExecuteSqlQueryForSingleRow(), Archive::isStandby, Archive::maxRemoteVersion, pg_fatal, pg_log_error, pg_log_error_detail, pg_strdup(), PQclear(), PQgetvalue(), PQparameterStatus(), PQserverVersion(), progname, _archiveHandle::public, Archive::remoteVersion, and Archive::remoteVersionStr.

Referenced by ConnectDatabase().

◆ CommitTransaction()

void CommitTransaction ( Archive AHX)

Definition at line 535 of file pg_backup_db.c.

536{
537 ArchiveHandle *AH = (ArchiveHandle *) AHX;
538
539 ExecuteSqlCommand(AH, "COMMIT", "could not commit database transaction");
540}
static void ExecuteSqlCommand(ArchiveHandle *AH, const char *qry, const char *desc)
Definition: pg_backup_db.c:326

References ExecuteSqlCommand().

Referenced by IssueCommandPerBlob().

◆ 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 lengthof(array)
Definition: c.h:759
#define ALWAYS_SECURE_SEARCH_PATH_SQL
Definition: connect.h:25
int PQconnectionUsedPassword(const PGconn *conn)
Definition: fe-connect.c:7621
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:7606
char * PQpass(const PGconn *conn)
Definition: fe-connect.c:7405
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:7490
void PQfinish(PGconn *conn)
Definition: fe-connect.c:5224
PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg)
Definition: fe-connect.c:7734
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:7553
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:737
Assert(PointerIsAligned(start, uint64))
#define free(a)
Definition: header.h:65
int i
Definition: isn.c:74
static const JsonPathKeyword keywords[]
@ CONNECTION_BAD
Definition: libpq-fe.h:84
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:51
char * override_dbname
Definition: pg_backup.h:93
char * pgport
Definition: pg_backup.h:87
char * pghost
Definition: pg_backup.h:88
trivalue promptPassword
Definition: pg_backup.h:90
char * username
Definition: pg_backup.h:89
char * dbname
Definition: pg_backup.h:86
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().

◆ die_on_query_failure()

static void die_on_query_failure ( ArchiveHandle AH,
const char *  query 
)
static

Definition at line 267 of file pg_backup_db.c.

268{
269 pg_log_error("query failed: %s",
271 pg_log_error_detail("Query was: %s", query);
272 exit(1);
273}

References _archiveHandle::connection, pg_log_error, pg_log_error_detail, and PQerrorMessage().

Referenced by ExecuteSqlQuery(), and ExecuteSqlStatement().

◆ 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:7498
@ PQTRANS_ACTIVE
Definition: libpq-fe.h:147
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().

◆ DropLOIfExists()

void DropLOIfExists ( ArchiveHandle AH,
Oid  oid 
)

Definition at line 671 of file pg_backup_db.c.

672{
673 ahprintf(AH,
674 "SELECT pg_catalog.lo_unlink(oid) "
675 "FROM pg_catalog.pg_largeobject_metadata "
676 "WHERE oid = '%u';\n",
677 oid);
678}
int ahprintf(ArchiveHandle *AH, const char *fmt,...)

References ahprintf().

Referenced by _StartLO(), RestoreArchive(), and StartRestoreLO().

◆ EndDBCopyMode()

void EndDBCopyMode ( Archive AHX,
const char *  tocEntryTag 
)

Definition at line 498 of file pg_backup_db.c.

499{
500 ArchiveHandle *AH = (ArchiveHandle *) AHX;
501
502 if (AH->pgCopyIn)
503 {
504 PGresult *res;
505
506 if (PQputCopyEnd(AH->connection, NULL) <= 0)
507 pg_fatal("error returned by PQputCopyEnd: %s",
509
510 /* Check command status and return to normal libpq state */
511 res = PQgetResult(AH->connection);
513 warn_or_exit_horribly(AH, "COPY failed for table \"%s\": %s",
514 tocEntryTag, PQerrorMessage(AH->connection));
515 PQclear(res);
516
517 /* Do this to ensure we've pumped libpq back to idle state */
518 if (PQgetResult(AH->connection) != NULL)
519 pg_log_warning("unexpected extra results during COPY of table \"%s\"",
520 tocEntryTag);
521
522 AH->pgCopyIn = false;
523 }
524}
PGresult * PQgetResult(PGconn *conn)
Definition: fe-exec.c:2062
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3411
int PQputCopyEnd(PGconn *conn, const char *errormsg)
Definition: fe-exec.c:2749
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:124
void warn_or_exit_horribly(ArchiveHandle *AH, const char *fmt,...)
#define pg_log_warning(...)
Definition: pgfnames.c:24

References _archiveHandle::connection, pg_fatal, pg_log_warning, _archiveHandle::pgCopyIn, PGRES_COMMAND_OK, PQclear(), PQerrorMessage(), PQgetResult(), PQputCopyEnd(), PQresultStatus(), and warn_or_exit_horribly().

Referenced by restore_toc_entry().

◆ ExecuteSimpleCommands()

static void ExecuteSimpleCommands ( ArchiveHandle AH,
const char *  buf,
size_t  bufLen 
)
static

Definition at line 378 of file pg_backup_db.c.

379{
380 const char *qry = buf;
381 const char *eos = buf + bufLen;
382
383 /* initialize command buffer if first time through */
384 if (AH->sqlparse.curCmd == NULL)
386
387 for (; qry < eos; qry++)
388 {
389 char ch = *qry;
390
391 /* For neatness, we skip any newlines between commands */
392 if (!(ch == '\n' && AH->sqlparse.curCmd->len == 0))
394
395 switch (AH->sqlparse.state)
396 {
397 case SQL_SCAN: /* Default state == 0, set in _allocAH */
398 if (ch == ';')
399 {
400 /*
401 * We've found the end of a statement. Send it and reset
402 * the buffer.
403 */
405 "could not execute query");
407 }
408 else if (ch == '\'')
409 {
411 AH->sqlparse.backSlash = false;
412 }
413 else if (ch == '"')
414 {
416 }
417 break;
418
420 /* We needn't handle '' specially */
421 if (ch == '\'' && !AH->sqlparse.backSlash)
422 AH->sqlparse.state = SQL_SCAN;
423 else if (ch == '\\' && !AH->public.std_strings)
425 else
426 AH->sqlparse.backSlash = false;
427 break;
428
430 /* We needn't handle "" specially */
431 if (ch == '"')
432 AH->sqlparse.state = SQL_SCAN;
433 break;
434 }
435 }
436}
@ SQL_IN_DOUBLE_QUOTE
@ SQL_IN_SINGLE_QUOTE
@ SQL_SCAN
static char * buf
Definition: pg_test_fsync.c:72
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:72
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:146
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:378
bool std_strings
Definition: pg_backup.h:240
sqlparseInfo sqlparse
PQExpBuffer curCmd
sqlparseState state

References appendPQExpBufferChar(), sqlparseInfo::backSlash, buf, createPQExpBuffer(), sqlparseInfo::curCmd, PQExpBufferData::data, ExecuteSqlCommand(), PQExpBufferData::len, _archiveHandle::public, resetPQExpBuffer(), SQL_IN_DOUBLE_QUOTE, SQL_IN_SINGLE_QUOTE, SQL_SCAN, _archiveHandle::sqlparse, sqlparseInfo::state, and Archive::std_strings.

Referenced by ExecuteSqlCommandBuf().

◆ ExecuteSqlCommand()

static void ExecuteSqlCommand ( ArchiveHandle AH,
const char *  qry,
const char *  desc 
)
static

Definition at line 326 of file pg_backup_db.c.

327{
328 PGconn *conn = AH->connection;
329 PGresult *res;
330
331#ifdef NOT_USED
332 fprintf(stderr, "Executing: '%s'\n\n", qry);
333#endif
334 res = PQexec(conn, qry);
335
336 switch (PQresultStatus(res))
337 {
338 case PGRES_COMMAND_OK:
339 case PGRES_TUPLES_OK:
341 /* A-OK */
342 break;
343 case PGRES_COPY_IN:
344 /* Assume this is an expected result */
345 AH->pgCopyIn = true;
346 break;
347 default:
348 /* trouble */
349 warn_or_exit_horribly(AH, "%s: %sCommand was: %s",
350 desc, PQerrorMessage(conn), qry);
351 break;
352 }
353
354 PQclear(res);
355}
#define fprintf(file, fmt, msg)
Definition: cubescan.l:21
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2262
@ PGRES_COPY_IN
Definition: libpq-fe.h:131
@ PGRES_EMPTY_QUERY
Definition: libpq-fe.h:123
@ PGRES_TUPLES_OK
Definition: libpq-fe.h:127
PGconn * conn
Definition: streamutil.c:52

References conn, _archiveHandle::connection, fprintf, _archiveHandle::pgCopyIn, PGRES_COMMAND_OK, PGRES_COPY_IN, PGRES_EMPTY_QUERY, PGRES_TUPLES_OK, PQclear(), PQerrorMessage(), PQexec(), PQresultStatus(), and warn_or_exit_horribly().

Referenced by CommitTransaction(), ExecuteSimpleCommands(), ExecuteSqlCommandBuf(), and StartTransaction().

◆ ExecuteSqlCommandBuf()

int ExecuteSqlCommandBuf ( Archive AHX,
const char *  buf,
size_t  bufLen 
)

Definition at line 443 of file pg_backup_db.c.

444{
445 ArchiveHandle *AH = (ArchiveHandle *) AHX;
446
447 if (AH->outputKind == OUTPUT_COPYDATA)
448 {
449 /*
450 * COPY data.
451 *
452 * We drop the data on the floor if libpq has failed to enter COPY
453 * mode; this allows us to behave reasonably when trying to continue
454 * after an error in a COPY command.
455 */
456 if (AH->pgCopyIn &&
457 PQputCopyData(AH->connection, buf, bufLen) <= 0)
458 pg_fatal("error returned by PQputCopyData: %s",
460 }
461 else if (AH->outputKind == OUTPUT_OTHERDATA)
462 {
463 /*
464 * Table data expressed as INSERT commands; or, in old dump files,
465 * BLOB COMMENTS data (which is expressed as COMMENT ON commands).
466 */
467 ExecuteSimpleCommands(AH, buf, bufLen);
468 }
469 else
470 {
471 /*
472 * General SQL commands; we assume that commands will not be split
473 * across calls.
474 *
475 * In most cases the data passed to us will be a null-terminated
476 * string, but if it's not, we have to add a trailing null.
477 */
478 if (buf[bufLen] == '\0')
479 ExecuteSqlCommand(AH, buf, "could not execute query");
480 else
481 {
482 char *str = (char *) pg_malloc(bufLen + 1);
483
484 memcpy(str, buf, bufLen);
485 str[bufLen] = '\0';
486 ExecuteSqlCommand(AH, str, "could not execute query");
487 free(str);
488 }
489 }
490
491 return bufLen;
492}
int PQputCopyData(PGconn *conn, const char *buffer, int nbytes)
Definition: fe-exec.c:2695
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
const char * str
@ OUTPUT_COPYDATA
@ OUTPUT_OTHERDATA
static void ExecuteSimpleCommands(ArchiveHandle *AH, const char *buf, size_t bufLen)
Definition: pg_backup_db.c:378
ArchiverOutput outputKind

References buf, _archiveHandle::connection, ExecuteSimpleCommands(), ExecuteSqlCommand(), free, OUTPUT_COPYDATA, OUTPUT_OTHERDATA, _archiveHandle::outputKind, pg_fatal, pg_malloc(), _archiveHandle::pgCopyIn, PQerrorMessage(), PQputCopyData(), and str.

Referenced by ahwrite().

◆ ExecuteSqlQuery()

PGresult * ExecuteSqlQuery ( Archive AHX,
const char *  query,
ExecStatusType  status 
)

Definition at line 288 of file pg_backup_db.c.

289{
290 ArchiveHandle *AH = (ArchiveHandle *) AHX;
291 PGresult *res;
292
293 res = PQexec(AH->connection, query);
294 if (PQresultStatus(res) != status)
295 die_on_query_failure(AH, query);
296 return res;
297}
static void die_on_query_failure(ArchiveHandle *AH, const char *query)
Definition: pg_backup_db.c:267

References _archiveHandle::connection, die_on_query_failure(), PQexec(), and PQresultStatus().

Referenced by append_depends_on_extension(), buildMatViewRefreshDependencies(), collectBinaryUpgradeClassOids(), collectComments(), collectRoleNames(), collectSecLabels(), collectSequences(), createViewAsClause(), dumpCompositeType(), dumpDatabase(), dumpDatabaseConfig(), dumpEnumType(), dumpOpclass(), dumpOpfamily(), dumpRelationStats(), dumpRule(), dumpSequence(), dumpSequenceData(), dumpTable(), dumpTableData_copy(), dumpTableData_insert(), dumpTSConfig(), dumpUserMappings(), ExecuteSqlQueryForSingleRow(), expand_extension_name_patterns(), expand_foreign_server_name_patterns(), expand_schema_name_patterns(), expand_table_name_patterns(), getAccessMethods(), getAdditionalACLs(), getAggregates(), getCasts(), getCollations(), getConstraints(), getConversions(), getDefaultACLs(), getDependencies(), getDomainConstraints(), getEventTriggers(), getExtendedStatistics(), getExtensionMembership(), getExtensions(), getForeignDataWrappers(), getForeignServers(), getFuncs(), getIndexes(), getInherits(), getLOs(), getNamespaces(), getOpclasses(), getOperators(), getOpfamilies(), getPartitioningInfo(), getPolicies(), getProcLangs(), getPublicationNamespaces(), getPublications(), getPublicationTables(), getRules(), getSubscriptions(), getSubscriptionTables(), getTableAttrs(), getTables(), getTransforms(), getTriggers(), getTSConfigurations(), getTSDictionaries(), getTSParsers(), getTSTemplates(), getTypes(), processExtensionTables(), and set_restrict_relation_kind().

◆ ExecuteSqlQueryForSingleRow()

PGresult * ExecuteSqlQueryForSingleRow ( Archive fout,
const char *  query 
)

Definition at line 303 of file pg_backup_db.c.

304{
305 PGresult *res;
306 int ntups;
307
308 res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK);
309
310 /* Expecting a single result only */
311 ntups = PQntuples(res);
312 if (ntups != 1)
313 pg_fatal(ngettext("query returned %d row instead of one: %s",
314 "query returned %d rows instead of one: %s",
315 ntups),
316 ntups, query);
317
318 return res;
319}
#define ngettext(s, p, n)
Definition: c.h:1152
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3481
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:288

References ExecuteSqlQuery(), ngettext, pg_fatal, PGRES_TUPLES_OK, and PQntuples().

Referenced by _check_database_version(), binary_upgrade_set_type_oids_by_type_oid(), ConnectDatabase(), convertTSFunction(), dumpAgg(), dumpBaseType(), dumpCollation(), dumpConversion(), dumpDatabase(), dumpDomain(), dumpForeignServer(), dumpFunc(), dumpOpclass(), dumpOpfamily(), dumpOpr(), dumpRangeType(), dumpSearchPath(), dumpStatisticsExt(), dumpTableAttach(), dumpTableSchema(), dumpTSConfig(), dumpTSDictionary(), expand_table_name_patterns(), get_language_name(), get_next_possible_free_pg_type_oid(), get_synchronized_snapshot(), getFormattedTypeName(), and setup_connection().

◆ ExecuteSqlStatement()

◆ 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.

◆ IssueACLPerBlob()

void IssueACLPerBlob ( ArchiveHandle AH,
TocEntry te 
)

Definition at line 597 of file pg_backup_db.c.

598{
599 TocEntry *blobte = getTocEntryByDumpId(AH, te->dependencies[0]);
600 char *buf;
601 char *st;
602 char *st2;
603 char *en;
604 bool inquotes;
605
606 if (!blobte)
607 pg_fatal("could not find entry for ID %d", te->dependencies[0]);
608 Assert(strcmp(blobte->desc, "BLOB METADATA") == 0);
609
610 /* Make a writable copy of the ACL commands string */
611 buf = pg_strdup(te->defn);
612
613 /*
614 * We have to parse out the commands sufficiently to locate the blob OIDs
615 * and find the command-ending semicolons. The commands should not
616 * contain anything hard to parse except for double-quoted role names,
617 * which are easy to ignore. Once we've split apart the first and second
618 * halves of a command, apply IssueCommandPerBlob. (This means the
619 * updates on the blobs are interleaved if there's multiple commands, but
620 * that should cause no trouble.)
621 */
622 inquotes = false;
623 st = en = buf;
624 st2 = NULL;
625 while (*en)
626 {
627 /* Ignore double-quoted material */
628 if (*en == '"')
629 inquotes = !inquotes;
630 if (inquotes)
631 {
632 en++;
633 continue;
634 }
635 /* If we found "LARGE OBJECT", that's the end of the first half */
636 if (strncmp(en, "LARGE OBJECT ", 13) == 0)
637 {
638 /* Terminate the first-half string */
639 en += 13;
640 Assert(isdigit((unsigned char) *en));
641 *en++ = '\0';
642 /* Skip the rest of the blob OID */
643 while (isdigit((unsigned char) *en))
644 en++;
645 /* Second half starts here */
646 Assert(st2 == NULL);
647 st2 = en;
648 }
649 /* If we found semicolon, that's the end of the second half */
650 else if (*en == ';')
651 {
652 /* Terminate the second-half string */
653 *en++ = '\0';
654 Assert(st2 != NULL);
655 /* Issue this command for each blob */
656 IssueCommandPerBlob(AH, blobte, st, st2);
657 /* For neatness, skip whitespace before the next command */
658 while (isspace((unsigned char) *en))
659 en++;
660 /* Reset for new command */
661 st = en;
662 st2 = NULL;
663 }
664 else
665 en++;
666 }
667 pg_free(buf);
668}
void pg_free(void *ptr)
Definition: fe_memutils.c:105
TocEntry * getTocEntryByDumpId(ArchiveHandle *AH, DumpId id)
void IssueCommandPerBlob(ArchiveHandle *AH, TocEntry *te, const char *cmdBegin, const char *cmdEnd)
Definition: pg_backup_db.c:550
DumpId * dependencies

References Assert(), buf, _tocEntry::defn, _tocEntry::dependencies, _tocEntry::desc, getTocEntryByDumpId(), IssueCommandPerBlob(), pg_fatal, pg_free(), and pg_strdup().

Referenced by _printTocEntry().

◆ IssueCommandPerBlob()

void IssueCommandPerBlob ( ArchiveHandle AH,
TocEntry te,
const char *  cmdBegin,
const char *  cmdEnd 
)

Definition at line 550 of file pg_backup_db.c.

552{
553 /* Make a writable copy of the command string */
554 char *buf = pg_strdup(te->defn);
555 RestoreOptions *ropt = AH->public.ropt;
556 char *st;
557 char *en;
558
559 st = buf;
560 while ((en = strchr(st, '\n')) != NULL)
561 {
562 *en++ = '\0';
563 ahprintf(AH, "%s%s%s;\n", cmdBegin, st, cmdEnd);
564
565 /* In --transaction-size mode, count each command as an action */
566 if (ropt && ropt->txn_size > 0)
567 {
568 if (++AH->txnCount >= ropt->txn_size)
569 {
570 if (AH->connection)
571 {
574 }
575 else
576 ahprintf(AH, "COMMIT;\nBEGIN;\n\n");
577 AH->txnCount = 0;
578 }
579 }
580
581 st = en;
582 }
583 ahprintf(AH, "\n");
584 pg_free(buf);
585}
void StartTransaction(Archive *AHX)
Definition: pg_backup_db.c:527
void CommitTransaction(Archive *AHX)
Definition: pg_backup_db.c:535
RestoreOptions * ropt
Definition: pg_backup.h:225

References ahprintf(), buf, CommitTransaction(), _archiveHandle::connection, _tocEntry::defn, pg_free(), pg_strdup(), _archiveHandle::public, Archive::ropt, StartTransaction(), _restoreOptions::txn_size, and _archiveHandle::txnCount.

Referenced by _printTocEntry(), IssueACLPerBlob(), and RestoreArchive().

◆ notice_processor()

static void notice_processor ( void *  arg,
const char *  message 
)
static

Definition at line 260 of file pg_backup_db.c.

261{
262 pg_log_info("%s", message);
263}
#define pg_log_info(...)
Definition: logging.h:124

References pg_log_info.

Referenced by ConnectDatabase().

◆ ReconnectToServer()

void ReconnectToServer ( ArchiveHandle AH,
const char *  dbname 
)

Definition at line 72 of file pg_backup_db.c.

73{
74 PGconn *oldConn = AH->connection;
75 RestoreOptions *ropt = AH->public.ropt;
76
77 /*
78 * Save the dbname, if given, in override_dbname so that it will also
79 * affect any later reconnection attempt.
80 */
81 if (dbname)
83
84 /*
85 * Note: we want to establish the new connection, and in particular update
86 * ArchiveHandle's connCancel, before closing old connection. Otherwise
87 * an ill-timed SIGINT could try to access a dead connection.
88 */
89 AH->connection = NULL; /* dodge error check in ConnectDatabase */
90
91 ConnectDatabase((Archive *) AH, &ropt->cparams, true);
92
93 PQfinish(oldConn);
94}
void ConnectDatabase(Archive *AHX, const ConnParams *cparams, bool isReconnect)
Definition: pg_backup_db.c:108
char * dbname
Definition: streamutil.c:49
ConnParams cparams
Definition: pg_backup.h:145

References ConnectDatabase(), _archiveHandle::connection, _restoreOptions::cparams, dbname, _connParams::override_dbname, pg_strdup(), PQfinish(), _archiveHandle::public, and Archive::ropt.

Referenced by _reconnectToDB().

◆ StartTransaction()

void StartTransaction ( Archive AHX)

Definition at line 527 of file pg_backup_db.c.

528{
529 ArchiveHandle *AH = (ArchiveHandle *) AHX;
530
531 ExecuteSqlCommand(AH, "BEGIN", "could not start database transaction");
532}

References ExecuteSqlCommand().

Referenced by IssueCommandPerBlob().