PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pg_backup_db.c File Reference
#include "postgres_fe.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 <unistd.h>
#include <ctype.h>
Include dependency graph for pg_backup_db.c:

Go to the source code of this file.

Macros

#define DB_MAX_ERR_STMT   128
 

Functions

static void _check_database_version (ArchiveHandle *AH)
 
static PGconn_connectDB (ArchiveHandle *AH, const char *newdbname, const char *newUser)
 
static void notice_processor (void *arg, const char *message)
 
int ReconnectToServer (ArchiveHandle *AH, const char *dbname, const char *username)
 
void ConnectDatabase (Archive *AHX, const char *dbname, const char *pghost, const char *pgport, const char *username, trivalue prompt_password)
 
void DisconnectDatabase (Archive *AHX)
 
PGconnGetConnection (Archive *AHX)
 
static void die_on_query_failure (ArchiveHandle *AH, const char *modulename, const char *query)
 
void ExecuteSqlStatement (Archive *AHX, const char *query)
 
PGresultExecuteSqlQuery (Archive *AHX, const char *query, ExecStatusType status)
 
PGresultExecuteSqlQueryForSingleRow (Archive *fout, 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)
 

Variables

static const char * modulename = gettext_noop("archiver (db)")
 

Macro Definition Documentation

#define DB_MAX_ERR_STMT   128

Definition at line 28 of file pg_backup_db.c.

Referenced by ExecuteSqlCommand().

Function Documentation

static void _check_database_version ( ArchiveHandle AH)
static

Definition at line 38 of file pg_backup_db.c.

References _archiveHandle::archiveRemoteVersion, _archiveHandle::connection, ExecuteSqlQueryForSingleRow(), exit_horribly(), Archive::isStandby, Archive::maxRemoteVersion, modulename, NULL, pg_strdup(), PQclear(), PQgetvalue(), PQparameterStatus(), PQserverVersion(), progname, _archiveHandle::public, Archive::remoteVersion, Archive::remoteVersionStr, and write_msg().

Referenced by _connectDB(), and ConnectDatabase().

39 {
40  const char *remoteversion_str;
41  int remoteversion;
42  PGresult *res;
43 
44  remoteversion_str = PQparameterStatus(AH->connection, "server_version");
45  remoteversion = PQserverVersion(AH->connection);
46  if (remoteversion == 0 || !remoteversion_str)
47  exit_horribly(modulename, "could not get server_version from libpq\n");
48 
49  AH->public.remoteVersionStr = pg_strdup(remoteversion_str);
50  AH->public.remoteVersion = remoteversion;
51  if (!AH->archiveRemoteVersion)
53 
54  if (remoteversion != PG_VERSION_NUM
55  && (remoteversion < AH->public.minRemoteVersion ||
56  remoteversion > AH->public.maxRemoteVersion))
57  {
58  write_msg(NULL, "server version: %s; %s version: %s\n",
59  remoteversion_str, progname, PG_VERSION);
60  exit_horribly(NULL, "aborting because of server version mismatch\n");
61  }
62 
63  /*
64  * When running against 9.0 or later, check if we are in recovery mode,
65  * which means we are on a hot standby.
66  */
67  if (remoteversion >= 90000)
68  {
69  res = ExecuteSqlQueryForSingleRow((Archive *) AH, "SELECT pg_catalog.pg_is_in_recovery()");
70 
71  AH->public.isStandby = (strcmp(PQgetvalue(res, 0, 0), "t") == 0);
72  PQclear(res);
73  }
74  else
75  AH->public.isStandby = false;
76 }
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:5924
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:5949
const char * progname
Definition: pg_standby.c:37
int maxRemoteVersion
Definition: pg_backup.h:187
bool isStandby
Definition: pg_backup.h:184
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, char *query)
Definition: pg_backup_db.c:424
static const char * modulename
Definition: pg_backup_db.c:31
void PQclear(PGresult *res)
Definition: fe-exec.c:650
void write_msg(const char *modulename, const char *fmt,...)
#define NULL
Definition: c.h:226
void exit_horribly(const char *modulename, const char *fmt,...)
char * remoteVersionStr
Definition: pg_backup.h:182
int remoteVersion
Definition: pg_backup.h:183
static PGconn * _connectDB ( ArchiveHandle AH,
const char *  newdbname,
const char *  newUser 
)
static

Definition at line 130 of file pg_backup_db.c.

References _check_database_version(), ahlog(), appendConnStrVal(), appendPQExpBuffer(), _archiveHandle::connection, CONNECTION_BAD, connstr, PQExpBufferData::data, exit_horribly(), free, initPQExpBuffer(), modulename, notice_processor(), NULL, password, pg_strdup(), PQconnectdbParams(), PQconnectionNeedsPassword(), PQconnectionUsedPassword(), PQdb(), PQerrorMessage(), PQfinish(), PQhost(), PQpass(), PQport(), PQsetNoticeProcessor(), PQstatus(), PQuser(), progname, _archiveHandle::promptPassword, _archiveHandle::savedPassword, simple_prompt(), termPQExpBuffer(), TRI_NO, TRI_YES, and values.

Referenced by ReconnectToServer().

131 {
133  PGconn *newConn;
134  const char *newdb;
135  const char *newuser;
136  char *password;
137  char passbuf[100];
138  bool new_pass;
139 
140  if (!reqdb)
141  newdb = PQdb(AH->connection);
142  else
143  newdb = reqdb;
144 
145  if (!requser || strlen(requser) == 0)
146  newuser = PQuser(AH->connection);
147  else
148  newuser = requser;
149 
150  ahlog(AH, 1, "connecting to database \"%s\" as user \"%s\"\n",
151  newdb, newuser);
152 
153  password = AH->savedPassword;
154 
155  if (AH->promptPassword == TRI_YES && password == NULL)
156  {
157  simple_prompt("Password: ", passbuf, sizeof(passbuf), false);
158  password = passbuf;
159  }
160 
161  initPQExpBuffer(&connstr);
162  appendPQExpBuffer(&connstr, "dbname=");
163  appendConnStrVal(&connstr, newdb);
164 
165  do
166  {
167  const char *keywords[7];
168  const char *values[7];
169 
170  keywords[0] = "host";
171  values[0] = PQhost(AH->connection);
172  keywords[1] = "port";
173  values[1] = PQport(AH->connection);
174  keywords[2] = "user";
175  values[2] = newuser;
176  keywords[3] = "password";
177  values[3] = password;
178  keywords[4] = "dbname";
179  values[4] = connstr.data;
180  keywords[5] = "fallback_application_name";
181  values[5] = progname;
182  keywords[6] = NULL;
183  values[6] = NULL;
184 
185  new_pass = false;
186  newConn = PQconnectdbParams(keywords, values, true);
187 
188  if (!newConn)
189  exit_horribly(modulename, "failed to reconnect to database\n");
190 
191  if (PQstatus(newConn) == CONNECTION_BAD)
192  {
193  if (!PQconnectionNeedsPassword(newConn))
194  exit_horribly(modulename, "could not reconnect to database: %s",
195  PQerrorMessage(newConn));
196  PQfinish(newConn);
197 
198  if (password)
199  fprintf(stderr, "Password incorrect\n");
200 
201  fprintf(stderr, "Connecting to %s as %s\n",
202  newdb, newuser);
203 
204  if (AH->promptPassword != TRI_NO)
205  {
206  simple_prompt("Password: ", passbuf, sizeof(passbuf), false);
207  password = passbuf;
208  }
209  else
210  exit_horribly(modulename, "connection needs password\n");
211 
212  new_pass = true;
213  }
214  } while (new_pass);
215 
216  /*
217  * We want to remember connection's actual password, whether or not we got
218  * it by prompting. So we don't just store the password variable.
219  */
220  if (PQconnectionUsedPassword(newConn))
221  {
222  if (AH->savedPassword)
223  free(AH->savedPassword);
224  AH->savedPassword = pg_strdup(PQpass(newConn));
225  }
226 
227  termPQExpBuffer(&connstr);
228 
229  /* check for version mismatch */
231 
233 
234  return newConn;
235 }
static char password[100]
Definition: streamutil.c:41
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:5959
PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg)
Definition: fe-connect.c:6135
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
void appendConnStrVal(PQExpBuffer buf, const char *str)
Definition: string_utils.c:536
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3516
char * PQport(const PGconn *conn)
Definition: fe-connect.c:5880
const char * progname
Definition: pg_standby.c:37
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:470
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:5835
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
void simple_prompt(const char *prompt, char *destination, size_t destlen, bool echo)
Definition: sprompt.c:37
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
char * PQpass(const PGconn *conn)
Definition: fe-connect.c:5843
static void notice_processor(void *arg, const char *message)
Definition: pg_backup_db.c:382
static const char * modulename
Definition: pg_backup_db.c:31
char * PQhost(const PGconn *conn)
Definition: fe-connect.c:5860
#define free(a)
Definition: header.h:60
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:5827
#define NULL
Definition: c.h:226
void exit_horribly(const char *modulename, const char *fmt,...)
static Datum values[MAXATTR]
Definition: bootstrap.c:162
static void _check_database_version(ArchiveHandle *AH)
Definition: pg_backup_db.c:38
int PQconnectionUsedPassword(const PGconn *conn)
Definition: fe-connect.c:6008
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:5993
void ahlog(ArchiveHandle *AH, int level, const char *fmt,...)
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:5906
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
static char * connstr
Definition: pg_dumpall.c:63
void CommitTransaction ( Archive AHX)

Definition at line 666 of file pg_backup_db.c.

References ExecuteSqlCommand().

667 {
668  ArchiveHandle *AH = (ArchiveHandle *) AHX;
669 
670  ExecuteSqlCommand(AH, "COMMIT", "could not commit database transaction");
671 }
static void ExecuteSqlCommand(ArchiveHandle *AH, const char *qry, const char *desc)
Definition: pg_backup_db.c:448
void ConnectDatabase ( Archive AHX,
const char *  dbname,
const char *  pghost,
const char *  pgport,
const char *  username,
trivalue  prompt_password 
)

Definition at line 248 of file pg_backup_db.c.

References _check_database_version(), _archiveHandle::connection, CONNECTION_BAD, dbname, exit_horribly(), free, modulename, notice_processor(), NULL, password, pg_strdup(), pghost, pgport, PQconnectdbParams(), PQconnectionNeedsPassword(), PQconnectionUsedPassword(), PQdb(), PQerrorMessage(), PQfinish(), PQpass(), PQsetNoticeProcessor(), PQstatus(), progname, _archiveHandle::promptPassword, _archiveHandle::savedPassword, set_archive_cancel_info(), simple_prompt(), TRI_NO, TRI_YES, username, and values.

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

254 {
255  ArchiveHandle *AH = (ArchiveHandle *) AHX;
256  char *password;
257  char passbuf[100];
258  bool new_pass;
259 
260  if (AH->connection)
261  exit_horribly(modulename, "already connected to a database\n");
262 
263  password = AH->savedPassword;
264 
265  if (prompt_password == TRI_YES && password == NULL)
266  {
267  simple_prompt("Password: ", passbuf, sizeof(passbuf), false);
268  password = passbuf;
269  }
270  AH->promptPassword = prompt_password;
271 
272  /*
273  * Start the connection. Loop until we have a password if requested by
274  * backend.
275  */
276  do
277  {
278  const char *keywords[7];
279  const char *values[7];
280 
281  keywords[0] = "host";
282  values[0] = pghost;
283  keywords[1] = "port";
284  values[1] = pgport;
285  keywords[2] = "user";
286  values[2] = username;
287  keywords[3] = "password";
288  values[3] = password;
289  keywords[4] = "dbname";
290  values[4] = dbname;
291  keywords[5] = "fallback_application_name";
292  values[5] = progname;
293  keywords[6] = NULL;
294  values[6] = NULL;
295 
296  new_pass = false;
297  AH->connection = PQconnectdbParams(keywords, values, true);
298 
299  if (!AH->connection)
300  exit_horribly(modulename, "failed to connect to database\n");
301 
302  if (PQstatus(AH->connection) == CONNECTION_BAD &&
304  password == NULL &&
305  prompt_password != TRI_NO)
306  {
307  PQfinish(AH->connection);
308  simple_prompt("Password: ", passbuf, sizeof(passbuf), false);
309  password = passbuf;
310  new_pass = true;
311  }
312  } while (new_pass);
313 
314  /* check to see that the backend connection was successfully made */
315  if (PQstatus(AH->connection) == CONNECTION_BAD)
316  exit_horribly(modulename, "connection to database \"%s\" failed: %s",
317  PQdb(AH->connection) ? PQdb(AH->connection) : "",
319 
320  /*
321  * We want to remember connection's actual password, whether or not we got
322  * it by prompting. So we don't just store the password variable.
323  */
325  {
326  if (AH->savedPassword)
327  free(AH->savedPassword);
329  }
330 
331  /* check for version mismatch */
333 
335 
336  /* arrange for SIGINT to issue a query cancel on this connection */
338 }
static char password[100]
Definition: streamutil.c:41
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:5959
PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg)
Definition: fe-connect.c:6135
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3516
const char * progname
Definition: pg_standby.c:37
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:470
void simple_prompt(const char *prompt, char *destination, size_t destlen, bool echo)
Definition: sprompt.c:37
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
char * PQpass(const PGconn *conn)
Definition: fe-connect.c:5843
static void notice_processor(void *arg, const char *message)
Definition: pg_backup_db.c:382
char * pghost
Definition: pgbench.c:180
static const char * modulename
Definition: pg_backup_db.c:31
static char * username
Definition: initdb.c:129
#define free(a)
Definition: header.h:60
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:5827
#define NULL
Definition: c.h:226
char * dbname
Definition: streamutil.c:38
void exit_horribly(const char *modulename, const char *fmt,...)
static Datum values[MAXATTR]
Definition: bootstrap.c:162
static void _check_database_version(ArchiveHandle *AH)
Definition: pg_backup_db.c:38
int PQconnectionUsedPassword(const PGconn *conn)
Definition: fe-connect.c:6008
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:5993
void set_archive_cancel_info(ArchiveHandle *AH, PGconn *conn)
Definition: parallel.c:742
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:5906
char * pgport
Definition: pgbench.c:181
static void die_on_query_failure ( ArchiveHandle AH,
const char *  modulename,
const char *  query 
)
static

Definition at line 389 of file pg_backup_db.c.

References _archiveHandle::connection, exit_horribly(), PQerrorMessage(), and write_msg().

Referenced by ExecuteSqlQuery(), and ExecuteSqlStatement().

390 {
391  write_msg(modulename, "query failed: %s",
393  exit_horribly(modulename, "query was: %s\n", query);
394 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:5959
static const char * modulename
Definition: pg_backup_db.c:31
void write_msg(const char *modulename, const char *fmt,...)
void exit_horribly(const char *modulename, const char *fmt,...)
void DisconnectDatabase ( Archive AHX)

Definition at line 345 of file pg_backup_db.c.

References _archiveHandle::connCancel, _archiveHandle::connection, NULL, PQcancel(), PQfinish(), PQTRANS_ACTIVE, PQtransactionStatus(), and set_archive_cancel_info().

Referenced by archive_close_connection(), restore_toc_entries_prefork(), RestoreArchive(), and RunWorker().

346 {
347  ArchiveHandle *AH = (ArchiveHandle *) AHX;
348  char errbuf[1];
349 
350  if (!AH->connection)
351  return;
352 
353  if (AH->connCancel)
354  {
355  /*
356  * If we have an active query, send a cancel before closing. This is
357  * of no use for a normal exit, but might be helpful during
358  * exit_horribly().
359  */
361  PQcancel(AH->connCancel, errbuf, sizeof(errbuf));
362 
363  /*
364  * Prevent signal handler from sending a cancel after this.
365  */
367  }
368 
369  PQfinish(AH->connection);
370  AH->connection = NULL;
371 }
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3516
PGcancel *volatile connCancel
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:5914
#define NULL
Definition: c.h:226
int PQcancel(PGcancel *cancel, char *errbuf, int errbufsize)
Definition: fe-connect.c:3792
void set_archive_cancel_info(ArchiveHandle *AH, PGconn *conn)
Definition: parallel.c:742
void DropBlobIfExists ( ArchiveHandle AH,
Oid  oid 
)

Definition at line 674 of file pg_backup_db.c.

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

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

675 {
676  /*
677  * If we are not restoring to a direct database connection, we have to
678  * guess about how to detect whether the blob exists. Assume new-style.
679  */
680  if (AH->connection == NULL ||
681  PQserverVersion(AH->connection) >= 90000)
682  {
683  ahprintf(AH,
684  "SELECT pg_catalog.lo_unlink(oid) "
685  "FROM pg_catalog.pg_largeobject_metadata "
686  "WHERE oid = '%u';\n",
687  oid);
688  }
689  else
690  {
691  /* Restoring to pre-9.0 server, so do it the old way */
692  ahprintf(AH,
693  "SELECT CASE WHEN EXISTS("
694  "SELECT 1 FROM pg_catalog.pg_largeobject WHERE loid = '%u'"
695  ") THEN pg_catalog.lo_unlink('%u') END;\n",
696  oid, oid);
697  }
698 }
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:5949
int ahprintf(ArchiveHandle *AH, const char *fmt,...)
#define NULL
Definition: c.h:226
void EndDBCopyMode ( Archive AHX,
const char *  tocEntryTag 
)

Definition at line 629 of file pg_backup_db.c.

References _archiveHandle::connection, exit_horribly(), modulename, NULL, _archiveHandle::pgCopyIn, PGRES_COMMAND_OK, PQclear(), PQerrorMessage(), PQgetResult(), PQputCopyEnd(), PQresultStatus(), warn_or_exit_horribly(), and write_msg().

Referenced by restore_toc_entry().

630 {
631  ArchiveHandle *AH = (ArchiveHandle *) AHX;
632 
633  if (AH->pgCopyIn)
634  {
635  PGresult *res;
636 
637  if (PQputCopyEnd(AH->connection, NULL) <= 0)
638  exit_horribly(modulename, "error returned by PQputCopyEnd: %s",
640 
641  /* Check command status and return to normal libpq state */
642  res = PQgetResult(AH->connection);
643  if (PQresultStatus(res) != PGRES_COMMAND_OK)
644  warn_or_exit_horribly(AH, modulename, "COPY failed for table \"%s\": %s",
645  tocEntryTag, PQerrorMessage(AH->connection));
646  PQclear(res);
647 
648  /* Do this to ensure we've pumped libpq back to idle state */
649  if (PQgetResult(AH->connection) != NULL)
650  write_msg(NULL, "WARNING: unexpected extra results during COPY of table \"%s\"\n",
651  tocEntryTag);
652 
653  AH->pgCopyIn = false;
654  }
655 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:5959
int PQputCopyEnd(PGconn *conn, const char *errormsg)
Definition: fe-exec.c:2288
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2596
void warn_or_exit_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt,...)
static const char * modulename
Definition: pg_backup_db.c:31
void PQclear(PGresult *res)
Definition: fe-exec.c:650
void write_msg(const char *modulename, const char *fmt,...)
#define NULL
Definition: c.h:226
void exit_horribly(const char *modulename, const char *fmt,...)
PGresult * PQgetResult(PGconn *conn)
Definition: fe-exec.c:1702
static void ExecuteSimpleCommands ( ArchiveHandle AH,
const char *  buf,
size_t  bufLen 
)
static

Definition at line 509 of file pg_backup_db.c.

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

Referenced by ExecuteSqlCommandBuf().

510 {
511  const char *qry = buf;
512  const char *eos = buf + bufLen;
513 
514  /* initialize command buffer if first time through */
515  if (AH->sqlparse.curCmd == NULL)
517 
518  for (; qry < eos; qry++)
519  {
520  char ch = *qry;
521 
522  /* For neatness, we skip any newlines between commands */
523  if (!(ch == '\n' && AH->sqlparse.curCmd->len == 0))
525 
526  switch (AH->sqlparse.state)
527  {
528  case SQL_SCAN: /* Default state == 0, set in _allocAH */
529  if (ch == ';')
530  {
531  /*
532  * We've found the end of a statement. Send it and reset
533  * the buffer.
534  */
536  "could not execute query");
538  }
539  else if (ch == '\'')
540  {
542  AH->sqlparse.backSlash = false;
543  }
544  else if (ch == '"')
545  {
547  }
548  break;
549 
550  case SQL_IN_SINGLE_QUOTE:
551  /* We needn't handle '' specially */
552  if (ch == '\'' && !AH->sqlparse.backSlash)
553  AH->sqlparse.state = SQL_SCAN;
554  else if (ch == '\\' && !AH->public.std_strings)
556  else
557  AH->sqlparse.backSlash = false;
558  break;
559 
560  case SQL_IN_DOUBLE_QUOTE:
561  /* We needn't handle "" specially */
562  if (ch == '"')
563  AH->sqlparse.state = SQL_SCAN;
564  break;
565  }
566  }
567 }
PQExpBuffer curCmd
sqlparseState state
static void ExecuteSqlCommand(ArchiveHandle *AH, const char *qry, const char *desc)
Definition: pg_backup_db.c:448
static char * buf
Definition: pg_test_fsync.c:65
sqlparseInfo sqlparse
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:396
#define NULL
Definition: c.h:226
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:145
bool std_strings
Definition: pg_backup.h:195
static void ExecuteSqlCommand ( ArchiveHandle AH,
const char *  qry,
const char *  desc 
)
static

Definition at line 448 of file pg_backup_db.c.

References conn, _archiveHandle::connection, DB_MAX_ERR_STMT, modulename, _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().

449 {
450  PGconn *conn = AH->connection;
451  PGresult *res;
452  char errStmt[DB_MAX_ERR_STMT];
453 
454 #ifdef NOT_USED
455  fprintf(stderr, "Executing: '%s'\n\n", qry);
456 #endif
457  res = PQexec(conn, qry);
458 
459  switch (PQresultStatus(res))
460  {
461  case PGRES_COMMAND_OK:
462  case PGRES_TUPLES_OK:
463  case PGRES_EMPTY_QUERY:
464  /* A-OK */
465  break;
466  case PGRES_COPY_IN:
467  /* Assume this is an expected result */
468  AH->pgCopyIn = true;
469  break;
470  default:
471  /* trouble */
472  strncpy(errStmt, qry, DB_MAX_ERR_STMT); /* strncpy required here */
473  if (errStmt[DB_MAX_ERR_STMT - 1] != '\0')
474  {
475  errStmt[DB_MAX_ERR_STMT - 4] = '.';
476  errStmt[DB_MAX_ERR_STMT - 3] = '.';
477  errStmt[DB_MAX_ERR_STMT - 2] = '.';
478  errStmt[DB_MAX_ERR_STMT - 1] = '\0';
479  }
480  warn_or_exit_horribly(AH, modulename, "%s: %s Command was: %s\n",
481  desc, PQerrorMessage(conn), errStmt);
482  break;
483  }
484 
485  PQclear(res);
486 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:5959
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2596
void warn_or_exit_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt,...)
PGconn * conn
Definition: streamutil.c:42
#define DB_MAX_ERR_STMT
Definition: pg_backup_db.c:28
static const char * modulename
Definition: pg_backup_db.c:31
void PQclear(PGresult *res)
Definition: fe-exec.c:650
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1846
int ExecuteSqlCommandBuf ( Archive AHX,
const char *  buf,
size_t  bufLen 
)

Definition at line 574 of file pg_backup_db.c.

References _archiveHandle::connection, ExecuteSimpleCommands(), ExecuteSqlCommand(), exit_horribly(), free, modulename, OUTPUT_COPYDATA, OUTPUT_OTHERDATA, _archiveHandle::outputKind, pg_malloc(), _archiveHandle::pgCopyIn, PQerrorMessage(), and PQputCopyData().

Referenced by ahwrite().

575 {
576  ArchiveHandle *AH = (ArchiveHandle *) AHX;
577 
578  if (AH->outputKind == OUTPUT_COPYDATA)
579  {
580  /*
581  * COPY data.
582  *
583  * We drop the data on the floor if libpq has failed to enter COPY
584  * mode; this allows us to behave reasonably when trying to continue
585  * after an error in a COPY command.
586  */
587  if (AH->pgCopyIn &&
588  PQputCopyData(AH->connection, buf, bufLen) <= 0)
589  exit_horribly(modulename, "error returned by PQputCopyData: %s",
591  }
592  else if (AH->outputKind == OUTPUT_OTHERDATA)
593  {
594  /*
595  * Table data expressed as INSERT commands; or, in old dump files,
596  * BLOB COMMENTS data (which is expressed as COMMENT ON commands).
597  */
598  ExecuteSimpleCommands(AH, buf, bufLen);
599  }
600  else
601  {
602  /*
603  * General SQL commands; we assume that commands will not be split
604  * across calls.
605  *
606  * In most cases the data passed to us will be a null-terminated
607  * string, but if it's not, we have to add a trailing null.
608  */
609  if (buf[bufLen] == '\0')
610  ExecuteSqlCommand(AH, buf, "could not execute query");
611  else
612  {
613  char *str = (char *) pg_malloc(bufLen + 1);
614 
615  memcpy(str, buf, bufLen);
616  str[bufLen] = '\0';
617  ExecuteSqlCommand(AH, str, "could not execute query");
618  free(str);
619  }
620  }
621 
622  return bufLen;
623 }
int PQputCopyData(PGconn *conn, const char *buffer, int nbytes)
Definition: fe-exec.c:2221
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:5959
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:448
static char * buf
Definition: pg_test_fsync.c:65
static const char * modulename
Definition: pg_backup_db.c:31
static void ExecuteSimpleCommands(ArchiveHandle *AH, const char *buf, size_t bufLen)
Definition: pg_backup_db.c:509
#define free(a)
Definition: header.h:60
ArchiverOutput outputKind
void exit_horribly(const char *modulename, const char *fmt,...)
PGresult* ExecuteSqlQuery ( Archive AHX,
const char *  query,
ExecStatusType  status 
)

Definition at line 409 of file pg_backup_db.c.

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

Referenced by buildMatViewRefreshDependencies(), collectComments(), collectSecLabels(), createViewAsClause(), dumpBlobs(), dumpCompositeType(), dumpCompositeTypeColComments(), dumpDatabase(), dumpEnumType(), dumpOpclass(), dumpOpfamily(), dumpRule(), dumpSequence(), dumpSequenceData(), dumpTable(), dumpTableData_copy(), dumpTableData_insert(), dumpTSConfig(), dumpUserMappings(), ExecuteSqlQueryForSingleRow(), expand_schema_name_patterns(), expand_table_name_patterns(), getAccessMethods(), getAggregates(), getBlobs(), getCasts(), getCollations(), getConstraints(), getConversions(), getDefaultACLs(), getDependencies(), getDomainConstraints(), getEventTriggers(), getExtensionMembership(), getExtensions(), getForeignDataWrappers(), getForeignServers(), getFuncs(), getIndexes(), getInherits(), getNamespaces(), getOpclasses(), getOperators(), getOpfamilies(), getPartitions(), getPolicies(), getProcLangs(), getPublications(), getPublicationTables(), getRules(), getSubscriptions(), getTableAttrs(), getTablePartitionKeyInfo(), getTables(), getTransforms(), getTriggers(), getTSConfigurations(), getTSDictionaries(), getTSParsers(), getTSTemplates(), getTypes(), and processExtensionTables().

410 {
411  ArchiveHandle *AH = (ArchiveHandle *) AHX;
412  PGresult *res;
413 
414  res = PQexec(AH->connection, query);
415  if (PQresultStatus(res) != status)
416  die_on_query_failure(AH, modulename, query);
417  return res;
418 }
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2596
static const char * modulename
Definition: pg_backup_db.c:31
static void die_on_query_failure(ArchiveHandle *AH, const char *modulename, const char *query)
Definition: pg_backup_db.c:389
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1846
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:222
PGresult* ExecuteSqlQueryForSingleRow ( Archive fout,
char *  query 
)

Definition at line 424 of file pg_backup_db.c.

References ExecuteSqlQuery(), exit_horribly(), ngettext, NULL, 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(), convertTSFunction(), dumpAgg(), dumpBaseType(), dumpCollation(), dumpConversion(), dumpDatabase(), dumpDomain(), dumpForeignServer(), dumpFunc(), dumpOpclass(), dumpOpfamily(), dumpOpr(), dumpRangeType(), dumpTableSchema(), dumpTSConfig(), dumpTSDictionary(), findLastBuiltinOid_V71(), get_language_name(), get_synchronized_snapshot(), and getFormattedTypeName().

425 {
426  PGresult *res;
427  int ntups;
428 
429  res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK);
430 
431  /* Expecting a single result only */
432  ntups = PQntuples(res);
433  if (ntups != 1)
435  ngettext("query returned %d row instead of one: %s\n",
436  "query returned %d rows instead of one: %s\n",
437  ntups),
438  ntups, query);
439 
440  return res;
441 }
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
#define ngettext(s, p, n)
Definition: c.h:127
#define NULL
Definition: c.h:226
void exit_horribly(const char *modulename, const char *fmt,...)
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:409
void ExecuteSqlStatement ( Archive AHX,
const char *  query 
)

Definition at line 397 of file pg_backup_db.c.

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

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

398 {
399  ArchiveHandle *AH = (ArchiveHandle *) AHX;
400  PGresult *res;
401 
402  res = PQexec(AH->connection, query);
403  if (PQresultStatus(res) != PGRES_COMMAND_OK)
404  die_on_query_failure(AH, modulename, query);
405  PQclear(res);
406 }
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2596
static const char * modulename
Definition: pg_backup_db.c:31
static void die_on_query_failure(ArchiveHandle *AH, const char *modulename, const char *query)
Definition: pg_backup_db.c:389
void PQclear(PGresult *res)
Definition: fe-exec.c:650
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1846
PGconn* GetConnection ( Archive AHX)

Definition at line 374 of file pg_backup_db.c.

References _archiveHandle::connection.

375 {
376  ArchiveHandle *AH = (ArchiveHandle *) AHX;
377 
378  return AH->connection;
379 }
static void notice_processor ( void *  arg,
const char *  message 
)
static

Definition at line 382 of file pg_backup_db.c.

References NULL, and write_msg().

Referenced by _connectDB(), and ConnectDatabase().

383 {
384  write_msg(NULL, "%s", message);
385 }
void write_msg(const char *modulename, const char *fmt,...)
#define NULL
Definition: c.h:226
int ReconnectToServer ( ArchiveHandle AH,
const char *  dbname,
const char *  username 
)

Definition at line 88 of file pg_backup_db.c.

References _connectDB(), _archiveHandle::connection, dbname, PQdb(), PQfinish(), PQuser(), set_archive_cancel_info(), and username.

Referenced by _reconnectToDB().

89 {
90  PGconn *newConn;
91  const char *newdbname;
92  const char *newusername;
93 
94  if (!dbname)
95  newdbname = PQdb(AH->connection);
96  else
97  newdbname = dbname;
98 
99  if (!username)
100  newusername = PQuser(AH->connection);
101  else
102  newusername = username;
103 
104  /* Let's see if the request is already satisfied */
105  if (strcmp(newdbname, PQdb(AH->connection)) == 0 &&
106  strcmp(newusername, PQuser(AH->connection)) == 0)
107  return 1;
108 
109  newConn = _connectDB(AH, newdbname, newusername);
110 
111  /* Update ArchiveHandle's connCancel before closing old connection */
112  set_archive_cancel_info(AH, newConn);
113 
114  PQfinish(AH->connection);
115  AH->connection = newConn;
116 
117  return 1;
118 }
static PGconn * _connectDB(ArchiveHandle *AH, const char *newdbname, const char *newUser)
Definition: pg_backup_db.c:130
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3516
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:5835
static char * username
Definition: initdb.c:129
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:5827
char * dbname
Definition: streamutil.c:38
void set_archive_cancel_info(ArchiveHandle *AH, PGconn *conn)
Definition: parallel.c:742
void StartTransaction ( Archive AHX)

Definition at line 658 of file pg_backup_db.c.

References ExecuteSqlCommand().

659 {
660  ArchiveHandle *AH = (ArchiveHandle *) AHX;
661 
662  ExecuteSqlCommand(AH, "BEGIN", "could not start database transaction");
663 }
static void ExecuteSqlCommand(ArchiveHandle *AH, const char *qry, const char *desc)
Definition: pg_backup_db.c:448

Variable Documentation