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 "dumputils.h"
#include "fe_utils/string_utils.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 DropBlobIfExists (ArchiveHandle *AH, Oid oid)
 

Function Documentation

◆ _check_database_version()

static void _check_database_version ( ArchiveHandle AH)
static

Definition at line 33 of file pg_backup_db.c.

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

Referenced by ConnectDatabase().

34 {
35  const char *remoteversion_str;
36  int remoteversion;
37  PGresult *res;
38 
39  remoteversion_str = PQparameterStatus(AH->connection, "server_version");
40  remoteversion = PQserverVersion(AH->connection);
41  if (remoteversion == 0 || !remoteversion_str)
42  fatal("could not get server_version from libpq");
43 
44  AH->public.remoteVersionStr = pg_strdup(remoteversion_str);
45  AH->public.remoteVersion = remoteversion;
46  if (!AH->archiveRemoteVersion)
48 
49  if (remoteversion != PG_VERSION_NUM
50  && (remoteversion < AH->public.minRemoteVersion ||
51  remoteversion > AH->public.maxRemoteVersion))
52  {
53  pg_log_error("server version: %s; %s version: %s",
54  remoteversion_str, progname, PG_VERSION);
55  fatal("aborting because of server version mismatch");
56  }
57 
58  /*
59  * When running against 9.0 or later, check if we are in recovery mode,
60  * which means we are on a hot standby.
61  */
62  if (remoteversion >= 90000)
63  {
64  res = ExecuteSqlQueryForSingleRow((Archive *) AH, "SELECT pg_catalog.pg_is_in_recovery()");
65 
66  AH->public.isStandby = (strcmp(PQgetvalue(res, 0, 0), "t") == 0);
67  PQclear(res);
68  }
69  else
70  AH->public.isStandby = false;
71 }
const char * progname
Definition: main.c:46
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3642
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:6709
#define pg_log_error(...)
Definition: logging.h:80
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:6734
int maxRemoteVersion
Definition: pg_backup.h:200
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:309
bool isStandby
Definition: pg_backup.h:197
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define fatal(...)
char * remoteVersionStr
Definition: pg_backup.h:195
int remoteVersion
Definition: pg_backup.h:196

◆ CommitTransaction()

void CommitTransaction ( Archive AHX)

Definition at line 541 of file pg_backup_db.c.

References ExecuteSqlCommand().

542 {
543  ArchiveHandle *AH = (ArchiveHandle *) AHX;
544 
545  ExecuteSqlCommand(AH, "COMMIT", "could not commit database transaction");
546 }
static void ExecuteSqlCommand(ArchiveHandle *AH, const char *qry, const char *desc)
Definition: pg_backup_db.c:332

◆ ConnectDatabase()

void ConnectDatabase ( Archive AHX,
const ConnParams cparams,
bool  isReconnect 
)

Definition at line 114 of file pg_backup_db.c.

References _check_database_version(), ALWAYS_SECURE_SEARCH_PATH_SQL, Assert, _archiveHandle::connection, CONNECTION_BAD, _connParams::dbname, ExecuteSqlQueryForSingleRow(), fatal, free, i, lengthof, notice_processor(), _connParams::override_dbname, password, 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().

117 {
118  ArchiveHandle *AH = (ArchiveHandle *) AHX;
119  trivalue prompt_password;
120  char *password;
121  bool new_pass;
122 
123  if (AH->connection)
124  fatal("already connected to a database");
125 
126  /* Never prompt for a password during a reconnection */
127  prompt_password = isReconnect ? TRI_NO : cparams->promptPassword;
128 
129  password = AH->savedPassword;
130 
131  if (prompt_password == TRI_YES && password == NULL)
132  password = simple_prompt("Password: ", false);
133 
134  /*
135  * Start the connection. Loop until we have a password if requested by
136  * backend.
137  */
138  do
139  {
140  const char *keywords[8];
141  const char *values[8];
142  int i = 0;
143 
144  /*
145  * If dbname is a connstring, its entries can override the other
146  * values obtained from cparams; but in turn, override_dbname can
147  * override the dbname component of it.
148  */
149  keywords[i] = "host";
150  values[i++] = cparams->pghost;
151  keywords[i] = "port";
152  values[i++] = cparams->pgport;
153  keywords[i] = "user";
154  values[i++] = cparams->username;
155  keywords[i] = "password";
156  values[i++] = password;
157  keywords[i] = "dbname";
158  values[i++] = cparams->dbname;
159  if (cparams->override_dbname)
160  {
161  keywords[i] = "dbname";
162  values[i++] = cparams->override_dbname;
163  }
164  keywords[i] = "fallback_application_name";
165  values[i++] = progname;
166  keywords[i] = NULL;
167  values[i++] = NULL;
168  Assert(i <= lengthof(keywords));
169 
170  new_pass = false;
171  AH->connection = PQconnectdbParams(keywords, values, true);
172 
173  if (!AH->connection)
174  fatal("could not connect to database");
175 
176  if (PQstatus(AH->connection) == CONNECTION_BAD &&
178  password == NULL &&
179  prompt_password != TRI_NO)
180  {
181  PQfinish(AH->connection);
182  password = simple_prompt("Password: ", false);
183  new_pass = true;
184  }
185  } while (new_pass);
186 
187  /* check to see that the backend connection was successfully made */
188  if (PQstatus(AH->connection) == CONNECTION_BAD)
189  {
190  if (isReconnect)
191  fatal("reconnection failed: %s",
193  else
194  fatal("%s",
196  }
197 
198  /* Start strict; later phases may override this. */
201 
202  if (password && password != AH->savedPassword)
203  free(password);
204 
205  /*
206  * We want to remember connection's actual password, whether or not we got
207  * it by prompting. So we don't just store the password variable.
208  */
210  {
211  if (AH->savedPassword)
212  free(AH->savedPassword);
214  }
215 
216  /* check for version mismatch */
218 
220 
221  /* arrange for SIGINT to issue a query cancel on this connection */
223 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6744
const char * progname
Definition: main.c:46
PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg)
Definition: fe-connect.c:6912
char * pgport
Definition: pg_backup.h:66
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4231
char * simple_prompt(const char *prompt, bool echo)
Definition: sprompt.c:38
#define lengthof(array)
Definition: c.h:734
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:657
trivalue promptPassword
Definition: pg_backup.h:69
char * dbname
Definition: pg_backup.h:65
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:309
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
char * PQpass(const PGconn *conn)
Definition: fe-connect.c:6606
static char * password
Definition: streamutil.c:53
static void notice_processor(void *arg, const char *message)
Definition: pg_backup_db.c:267
char * override_dbname
Definition: pg_backup.h:72
char * pghost
Definition: pg_backup.h:67
char * username
Definition: pg_backup.h:68
trivalue
Definition: vacuumlo.c:34
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define free(a)
Definition: header.h:65
#define Assert(condition)
Definition: c.h:804
#define fatal(...)
#define ALWAYS_SECURE_SEARCH_PATH_SQL
Definition: connect.h:25
static Datum values[MAXATTR]
Definition: bootstrap.c:156
static void _check_database_version(ArchiveHandle *AH)
Definition: pg_backup_db.c:33
int PQconnectionUsedPassword(const PGconn *conn)
Definition: fe-connect.c:6810
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:6795
void set_archive_cancel_info(ArchiveHandle *AH, PGconn *conn)
Definition: parallel.c:735
int i
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6691

◆ die_on_query_failure()

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

Definition at line 274 of file pg_backup_db.c.

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

Referenced by ExecuteSqlQuery(), and ExecuteSqlStatement().

275 {
276  pg_log_error("query failed: %s",
278  fatal("query was: %s", query);
279 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6744
#define pg_log_error(...)
Definition: logging.h:80
#define fatal(...)

◆ DisconnectDatabase()

void DisconnectDatabase ( Archive AHX)

Definition at line 230 of file pg_backup_db.c.

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().

231 {
232  ArchiveHandle *AH = (ArchiveHandle *) AHX;
233  char errbuf[1];
234 
235  if (!AH->connection)
236  return;
237 
238  if (AH->connCancel)
239  {
240  /*
241  * If we have an active query, send a cancel before closing, ignoring
242  * any errors. This is of no use for a normal exit, but might be
243  * helpful during fatal().
244  */
246  (void) PQcancel(AH->connCancel, errbuf, sizeof(errbuf));
247 
248  /*
249  * Prevent signal handler from sending a cancel after this.
250  */
251  set_archive_cancel_info(AH, NULL);
252  }
253 
254  PQfinish(AH->connection);
255  AH->connection = NULL;
256 }
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4231
PGcancel *volatile connCancel
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:6699
int PQcancel(PGcancel *cancel, char *errbuf, int errbufsize)
Definition: fe-connect.c:4507
void set_archive_cancel_info(ArchiveHandle *AH, PGconn *conn)
Definition: parallel.c:735

◆ DropBlobIfExists()

void DropBlobIfExists ( ArchiveHandle AH,
Oid  oid 
)

Definition at line 549 of file pg_backup_db.c.

References ahprintf(), _archiveHandle::connection, and PQserverVersion().

Referenced by _StartBlob(), RestoreArchive(), and StartRestoreBlob().

550 {
551  /*
552  * If we are not restoring to a direct database connection, we have to
553  * guess about how to detect whether the blob exists. Assume new-style.
554  */
555  if (AH->connection == NULL ||
556  PQserverVersion(AH->connection) >= 90000)
557  {
558  ahprintf(AH,
559  "SELECT pg_catalog.lo_unlink(oid) "
560  "FROM pg_catalog.pg_largeobject_metadata "
561  "WHERE oid = '%u';\n",
562  oid);
563  }
564  else
565  {
566  /* Restoring to pre-9.0 server, so do it the old way */
567  ahprintf(AH,
568  "SELECT CASE WHEN EXISTS("
569  "SELECT 1 FROM pg_catalog.pg_largeobject WHERE loid = '%u'"
570  ") THEN pg_catalog.lo_unlink('%u') END;\n",
571  oid, oid);
572  }
573 }
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:6734
int ahprintf(ArchiveHandle *AH, const char *fmt,...)

◆ EndDBCopyMode()

void EndDBCopyMode ( Archive AHX,
const char *  tocEntryTag 
)

Definition at line 504 of file pg_backup_db.c.

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

Referenced by restore_toc_entry().

505 {
506  ArchiveHandle *AH = (ArchiveHandle *) AHX;
507 
508  if (AH->pgCopyIn)
509  {
510  PGresult *res;
511 
512  if (PQputCopyEnd(AH->connection, NULL) <= 0)
513  fatal("error returned by PQputCopyEnd: %s",
515 
516  /* Check command status and return to normal libpq state */
517  res = PQgetResult(AH->connection);
518  if (PQresultStatus(res) != PGRES_COMMAND_OK)
519  warn_or_exit_horribly(AH, "COPY failed for table \"%s\": %s",
520  tocEntryTag, PQerrorMessage(AH->connection));
521  PQclear(res);
522 
523  /* Do this to ensure we've pumped libpq back to idle state */
524  if (PQgetResult(AH->connection) != NULL)
525  pg_log_warning("unexpected extra results during COPY of table \"%s\"",
526  tocEntryTag);
527 
528  AH->pgCopyIn = false;
529  }
530 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6744
void warn_or_exit_horribly(ArchiveHandle *AH, const char *fmt,...)
int PQputCopyEnd(PGconn *conn, const char *errormsg)
Definition: fe-exec.c:2600
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3178
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define fatal(...)
#define pg_log_warning(...)
Definition: pgfnames.c:24
PGresult * PQgetResult(PGconn *conn)
Definition: fe-exec.c:1978

◆ ExecuteSimpleCommands()

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

Definition at line 384 of file pg_backup_db.c.

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().

385 {
386  const char *qry = buf;
387  const char *eos = buf + bufLen;
388 
389  /* initialize command buffer if first time through */
390  if (AH->sqlparse.curCmd == NULL)
392 
393  for (; qry < eos; qry++)
394  {
395  char ch = *qry;
396 
397  /* For neatness, we skip any newlines between commands */
398  if (!(ch == '\n' && AH->sqlparse.curCmd->len == 0))
400 
401  switch (AH->sqlparse.state)
402  {
403  case SQL_SCAN: /* Default state == 0, set in _allocAH */
404  if (ch == ';')
405  {
406  /*
407  * We've found the end of a statement. Send it and reset
408  * the buffer.
409  */
411  "could not execute query");
413  }
414  else if (ch == '\'')
415  {
417  AH->sqlparse.backSlash = false;
418  }
419  else if (ch == '"')
420  {
422  }
423  break;
424 
425  case SQL_IN_SINGLE_QUOTE:
426  /* We needn't handle '' specially */
427  if (ch == '\'' && !AH->sqlparse.backSlash)
428  AH->sqlparse.state = SQL_SCAN;
429  else if (ch == '\\' && !AH->public.std_strings)
431  else
432  AH->sqlparse.backSlash = false;
433  break;
434 
435  case SQL_IN_DOUBLE_QUOTE:
436  /* We needn't handle "" specially */
437  if (ch == '"')
438  AH->sqlparse.state = SQL_SCAN;
439  break;
440  }
441  }
442 }
PQExpBuffer curCmd
sqlparseState state
static void ExecuteSqlCommand(ArchiveHandle *AH, const char *qry, const char *desc)
Definition: pg_backup_db.c:332
static char * buf
Definition: pg_test_fsync.c:68
sqlparseInfo sqlparse
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:380
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:148
bool std_strings
Definition: pg_backup.h:207

◆ ExecuteSqlCommand()

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

Definition at line 332 of file pg_backup_db.c.

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().

333 {
334  PGconn *conn = AH->connection;
335  PGresult *res;
336 
337 #ifdef NOT_USED
338  fprintf(stderr, "Executing: '%s'\n\n", qry);
339 #endif
340  res = PQexec(conn, qry);
341 
342  switch (PQresultStatus(res))
343  {
344  case PGRES_COMMAND_OK:
345  case PGRES_TUPLES_OK:
346  case PGRES_EMPTY_QUERY:
347  /* A-OK */
348  break;
349  case PGRES_COPY_IN:
350  /* Assume this is an expected result */
351  AH->pgCopyIn = true;
352  break;
353  default:
354  /* trouble */
355  warn_or_exit_horribly(AH, "%s: %sCommand was: %s",
356  desc, PQerrorMessage(conn), qry);
357  break;
358  }
359 
360  PQclear(res);
361 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6744
void warn_or_exit_horribly(ArchiveHandle *AH, const char *fmt,...)
#define fprintf
Definition: port.h:221
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3178
PGconn * conn
Definition: streamutil.c:54
void PQclear(PGresult *res)
Definition: fe-exec.c:694
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2193

◆ ExecuteSqlCommandBuf()

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

Definition at line 449 of file pg_backup_db.c.

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

Referenced by ahwrite().

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

◆ ExecuteSqlQuery()

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

Definition at line 294 of file pg_backup_db.c.

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

Referenced by append_depends_on_extension(), buildMatViewRefreshDependencies(), collectComments(), collectSecLabels(), createViewAsClause(), dumpBlobs(), dumpCompositeType(), dumpCompositeTypeColComments(), dumpDatabase(), dumpDatabaseConfig(), dumpEnumType(), dumpOpclass(), dumpOpfamily(), 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(), getAggregates(), getBlobs(), getCasts(), getCollations(), getConstraints(), getConversions(), getDefaultACLs(), getDependencies(), getDomainConstraints(), getEventTriggers(), getExtendedStatistics(), getExtensionMembership(), getExtensions(), getForeignDataWrappers(), getForeignServers(), getFuncs(), getIndexes(), getInherits(), getNamespaces(), getOpclasses(), getOperators(), getOpfamilies(), getPolicies(), getProcLangs(), getPublications(), getPublicationTables(), getRules(), getSubscriptions(), getTableAttrs(), getTables(), getTransforms(), getTriggers(), getTSConfigurations(), getTSDictionaries(), getTSParsers(), getTSTemplates(), getTypes(), and processExtensionTables().

295 {
296  ArchiveHandle *AH = (ArchiveHandle *) AHX;
297  PGresult *res;
298 
299  res = PQexec(AH->connection, query);
300  if (PQresultStatus(res) != status)
301  die_on_query_failure(AH, query);
302  return res;
303 }
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3178
static void die_on_query_failure(ArchiveHandle *AH, const char *query)
Definition: pg_backup_db.c:274
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2193
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227

◆ ExecuteSqlQueryForSingleRow()

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

Definition at line 309 of file pg_backup_db.c.

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

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

310 {
311  PGresult *res;
312  int ntups;
313 
314  res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK);
315 
316  /* Expecting a single result only */
317  ntups = PQntuples(res);
318  if (ntups != 1)
319  fatal(ngettext("query returned %d row instead of one: %s",
320  "query returned %d rows instead of one: %s",
321  ntups),
322  ntups, query);
323 
324  return res;
325 }
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3248
#define ngettext(s, p, n)
Definition: c.h:1182
#define fatal(...)
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:294

◆ ExecuteSqlStatement()

void ExecuteSqlStatement ( Archive AHX,
const char *  query 
)

Definition at line 282 of file pg_backup_db.c.

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

Referenced by dumpBlobs(), dumpTableData_insert(), expand_table_name_patterns(), getTables(), and setup_connection().

283 {
284  ArchiveHandle *AH = (ArchiveHandle *) AHX;
285  PGresult *res;
286 
287  res = PQexec(AH->connection, query);
288  if (PQresultStatus(res) != PGRES_COMMAND_OK)
289  die_on_query_failure(AH, query);
290  PQclear(res);
291 }
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3178
static void die_on_query_failure(ArchiveHandle *AH, const char *query)
Definition: pg_backup_db.c:274
void PQclear(PGresult *res)
Definition: fe-exec.c:694
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2193

◆ GetConnection()

PGconn* GetConnection ( Archive AHX)

Definition at line 259 of file pg_backup_db.c.

References _archiveHandle::connection.

260 {
261  ArchiveHandle *AH = (ArchiveHandle *) AHX;
262 
263  return AH->connection;
264 }

◆ notice_processor()

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

Definition at line 267 of file pg_backup_db.c.

References pg_log_generic(), and PG_LOG_INFO.

Referenced by ConnectDatabase().

268 {
269  pg_log_generic(PG_LOG_INFO, "%s", message);
270 }
void pg_log_generic(enum pg_log_level level, const char *pg_restrict fmt,...)
Definition: logging.c:197

◆ ReconnectToServer()

void ReconnectToServer ( ArchiveHandle AH,
const char *  dbname 
)

Definition at line 78 of file pg_backup_db.c.

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

Referenced by _reconnectToDB().

79 {
80  PGconn *oldConn = AH->connection;
81  RestoreOptions *ropt = AH->public.ropt;
82 
83  /*
84  * Save the dbname, if given, in override_dbname so that it will also
85  * affect any later reconnection attempt.
86  */
87  if (dbname)
89 
90  /*
91  * Note: we want to establish the new connection, and in particular update
92  * ArchiveHandle's connCancel, before closing old connection. Otherwise
93  * an ill-timed SIGINT could try to access a dead connection.
94  */
95  AH->connection = NULL; /* dodge error check in ConnectDatabase */
96 
97  ConnectDatabase((Archive *) AH, &ropt->cparams, true);
98 
99  PQfinish(oldConn);
100 }
RestoreOptions * ropt
Definition: pg_backup.h:192
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4231
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
char * override_dbname
Definition: pg_backup.h:72
ConnParams cparams
Definition: pg_backup.h:124
void ConnectDatabase(Archive *AHX, const ConnParams *cparams, bool isReconnect)
Definition: pg_backup_db.c:114
char * dbname
Definition: streamutil.c:51

◆ StartTransaction()

void StartTransaction ( Archive AHX)

Definition at line 533 of file pg_backup_db.c.

References ExecuteSqlCommand().

534 {
535  ArchiveHandle *AH = (ArchiveHandle *) AHX;
536 
537  ExecuteSqlCommand(AH, "BEGIN", "could not start database transaction");
538 }
static void ExecuteSqlCommand(ArchiveHandle *AH, const char *qry, const char *desc)
Definition: pg_backup_db.c:332