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.

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  * Check if server is in recovery mode, which means we are on a hot
60  * standby.
61  */
63  "SELECT pg_catalog.pg_is_in_recovery()");
64  AH->public.isStandby = (strcmp(PQgetvalue(res, 0, 0), "t") == 0);
65  PQclear(res);
66 }
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:6880
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:6905
void PQclear(PGresult *res)
Definition: fe-exec.c:694
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3642
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define pg_log_error(...)
Definition: logging.h:80
const char * progname
Definition: main.c:46
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:304
#define fatal(...)
int remoteVersion
Definition: pg_backup.h:214
char * remoteVersionStr
Definition: pg_backup.h:213
bool isStandby
Definition: pg_backup.h:215
int maxRemoteVersion
Definition: pg_backup.h:218

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, Archive::remoteVersionStr, and res.

Referenced by ConnectDatabase().

◆ CommitTransaction()

void CommitTransaction ( Archive AHX)

Definition at line 536 of file pg_backup_db.c.

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

References ExecuteSqlCommand().

◆ ConnectDatabase()

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

Definition at line 109 of file pg_backup_db.c.

112 {
113  ArchiveHandle *AH = (ArchiveHandle *) AHX;
114  trivalue prompt_password;
115  char *password;
116  bool new_pass;
117 
118  if (AH->connection)
119  fatal("already connected to a database");
120 
121  /* Never prompt for a password during a reconnection */
122  prompt_password = isReconnect ? TRI_NO : cparams->promptPassword;
123 
124  password = AH->savedPassword;
125 
126  if (prompt_password == TRI_YES && password == NULL)
127  password = simple_prompt("Password: ", false);
128 
129  /*
130  * Start the connection. Loop until we have a password if requested by
131  * backend.
132  */
133  do
134  {
135  const char *keywords[8];
136  const char *values[8];
137  int i = 0;
138 
139  /*
140  * If dbname is a connstring, its entries can override the other
141  * values obtained from cparams; but in turn, override_dbname can
142  * override the dbname component of it.
143  */
144  keywords[i] = "host";
145  values[i++] = cparams->pghost;
146  keywords[i] = "port";
147  values[i++] = cparams->pgport;
148  keywords[i] = "user";
149  values[i++] = cparams->username;
150  keywords[i] = "password";
151  values[i++] = password;
152  keywords[i] = "dbname";
153  values[i++] = cparams->dbname;
154  if (cparams->override_dbname)
155  {
156  keywords[i] = "dbname";
157  values[i++] = cparams->override_dbname;
158  }
159  keywords[i] = "fallback_application_name";
160  values[i++] = progname;
161  keywords[i] = NULL;
162  values[i++] = NULL;
163  Assert(i <= lengthof(keywords));
164 
165  new_pass = false;
166  AH->connection = PQconnectdbParams(keywords, values, true);
167 
168  if (!AH->connection)
169  fatal("could not connect to database");
170 
171  if (PQstatus(AH->connection) == CONNECTION_BAD &&
173  password == NULL &&
174  prompt_password != TRI_NO)
175  {
176  PQfinish(AH->connection);
177  password = simple_prompt("Password: ", false);
178  new_pass = true;
179  }
180  } while (new_pass);
181 
182  /* check to see that the backend connection was successfully made */
183  if (PQstatus(AH->connection) == CONNECTION_BAD)
184  {
185  if (isReconnect)
186  fatal("reconnection failed: %s",
188  else
189  fatal("%s",
191  }
192 
193  /* Start strict; later phases may override this. */
196 
197  if (password && password != AH->savedPassword)
198  free(password);
199 
200  /*
201  * We want to remember connection's actual password, whether or not we got
202  * it by prompting. So we don't just store the password variable.
203  */
205  {
206  if (AH->savedPassword)
207  free(AH->savedPassword);
209  }
210 
211  /* check for version mismatch */
213 
215 
216  /* arrange for SIGINT to issue a query cancel on this connection */
218 }
void set_archive_cancel_info(ArchiveHandle *AH, PGconn *conn)
Definition: parallel.c:735
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define lengthof(array)
Definition: c.h:734
#define ALWAYS_SECURE_SEARCH_PATH_SQL
Definition: connect.h:25
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:657
int PQconnectionUsedPassword(const PGconn *conn)
Definition: fe-connect.c:6981
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:6966
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6915
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6862
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4256
char * PQpass(const PGconn *conn)
Definition: fe-connect.c:6777
PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg)
Definition: fe-connect.c:7083
#define free(a)
Definition: header.h:65
int i
Definition: isn.c:73
@ CONNECTION_BAD
Definition: libpq-fe.h:59
Assert(fmt[strlen(fmt) - 1] !='\n')
static void _check_database_version(ArchiveHandle *AH)
Definition: pg_backup_db.c:33
static void notice_processor(void *arg, const char *message)
Definition: pg_backup_db.c:262
char * simple_prompt(const char *prompt, bool echo)
Definition: sprompt.c:38
static char * password
Definition: streamutil.c:53
char * override_dbname
Definition: pg_backup.h:89
char * pgport
Definition: pg_backup.h:83
char * pghost
Definition: pg_backup.h:84
trivalue promptPassword
Definition: pg_backup.h:86
char * username
Definition: pg_backup.h:85
char * dbname
Definition: pg_backup.h:82
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(), 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().

◆ die_on_query_failure()

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

Definition at line 269 of file pg_backup_db.c.

270 {
271  pg_log_error("query failed: %s",
273  fatal("query was: %s", query);
274 }

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

Referenced by ExecuteSqlQuery(), and ExecuteSqlStatement().

◆ DisconnectDatabase()

void DisconnectDatabase ( Archive AHX)

Definition at line 225 of file pg_backup_db.c.

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

◆ DropBlobIfExists()

void DropBlobIfExists ( ArchiveHandle AH,
Oid  oid 
)

Definition at line 544 of file pg_backup_db.c.

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

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

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

◆ EndDBCopyMode()

void EndDBCopyMode ( Archive AHX,
const char *  tocEntryTag 
)

Definition at line 499 of file pg_backup_db.c.

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

References _archiveHandle::connection, fatal, pg_log_warning, _archiveHandle::pgCopyIn, PGRES_COMMAND_OK, PQclear(), PQerrorMessage(), PQgetResult(), PQputCopyEnd(), PQresultStatus(), res, 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 379 of file pg_backup_db.c.

380 {
381  const char *qry = buf;
382  const char *eos = buf + bufLen;
383 
384  /* initialize command buffer if first time through */
385  if (AH->sqlparse.curCmd == NULL)
387 
388  for (; qry < eos; qry++)
389  {
390  char ch = *qry;
391 
392  /* For neatness, we skip any newlines between commands */
393  if (!(ch == '\n' && AH->sqlparse.curCmd->len == 0))
395 
396  switch (AH->sqlparse.state)
397  {
398  case SQL_SCAN: /* Default state == 0, set in _allocAH */
399  if (ch == ';')
400  {
401  /*
402  * We've found the end of a statement. Send it and reset
403  * the buffer.
404  */
406  "could not execute query");
408  }
409  else if (ch == '\'')
410  {
412  AH->sqlparse.backSlash = false;
413  }
414  else if (ch == '"')
415  {
417  }
418  break;
419 
420  case SQL_IN_SINGLE_QUOTE:
421  /* We needn't handle '' specially */
422  if (ch == '\'' && !AH->sqlparse.backSlash)
423  AH->sqlparse.state = SQL_SCAN;
424  else if (ch == '\\' && !AH->public.std_strings)
426  else
427  AH->sqlparse.backSlash = false;
428  break;
429 
430  case SQL_IN_DOUBLE_QUOTE:
431  /* We needn't handle "" specially */
432  if (ch == '"')
433  AH->sqlparse.state = SQL_SCAN;
434  break;
435  }
436  }
437 }
@ SQL_IN_DOUBLE_QUOTE
@ SQL_IN_SINGLE_QUOTE
@ SQL_SCAN
static char * buf
Definition: pg_test_fsync.c:70
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:148
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:380
bool std_strings
Definition: pg_backup.h:225
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 327 of file pg_backup_db.c.

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

References conn, _archiveHandle::connection, fprintf, _archiveHandle::pgCopyIn, PGRES_COMMAND_OK, PGRES_COPY_IN, PGRES_EMPTY_QUERY, PGRES_TUPLES_OK, PQclear(), PQerrorMessage(), PQexec(), PQresultStatus(), res, 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 444 of file pg_backup_db.c.

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

References buf, _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().

◆ ExecuteSqlQuery()

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

Definition at line 289 of file pg_backup_db.c.

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

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

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

◆ ExecuteSqlQueryForSingleRow()

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

Definition at line 304 of file pg_backup_db.c.

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

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

Referenced by _check_database_version(), binary_upgrade_set_pg_class_oids(), 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 254 of file pg_backup_db.c.

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

References _archiveHandle::connection.

◆ notice_processor()

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

Definition at line 262 of file pg_backup_db.c.

263 {
264  pg_log_generic(PG_LOG_INFO, "%s", message);
265 }
void pg_log_generic(enum pg_log_level level, const char *pg_restrict fmt,...)
Definition: logging.c:197
@ PG_LOG_INFO
Definition: logging.h:33

References pg_log_generic(), and PG_LOG_INFO.

Referenced by ConnectDatabase().

◆ ReconnectToServer()

void ReconnectToServer ( ArchiveHandle AH,
const char *  dbname 
)

Definition at line 73 of file pg_backup_db.c.

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

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 528 of file pg_backup_db.c.

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

References ExecuteSqlCommand().