PostgreSQL Source Code  git master
pg_backup_db.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * pg_backup_db.c
4  *
5  * Implements the basic DB functions used by the archiver.
6  *
7  * IDENTIFICATION
8  * src/bin/pg_dump/pg_backup_db.c
9  *
10  *-------------------------------------------------------------------------
11  */
12 #include "postgres_fe.h"
13 
14 #include <unistd.h>
15 #include <ctype.h>
16 #ifdef HAVE_TERMIOS_H
17 #include <termios.h>
18 #endif
19 
20 #include "common/connect.h"
21 #include "common/string.h"
22 #include "dumputils.h"
23 #include "fe_utils/string_utils.h"
24 #include "parallel.h"
25 #include "pg_backup_archiver.h"
26 #include "pg_backup_db.h"
27 #include "pg_backup_utils.h"
28 
29 static void _check_database_version(ArchiveHandle *AH);
30 static void notice_processor(void *arg, const char *message);
31 
32 static void
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  pg_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("aborting because of server version mismatch");
54  pg_log_error_detail("server version: %s; %s version: %s",
55  remoteversion_str, progname, PG_VERSION);
56  exit(1);
57  }
58 
59  /*
60  * Check if server is in recovery mode, which means we are on a hot
61  * standby.
62  */
64  "SELECT pg_catalog.pg_is_in_recovery()");
65  AH->public.isStandby = (strcmp(PQgetvalue(res, 0, 0), "t") == 0);
66  PQclear(res);
67 }
68 
69 /*
70  * Reconnect to the server. If dbname is not NULL, use that database,
71  * else the one associated with the archive handle.
72  */
73 void
75 {
76  PGconn *oldConn = AH->connection;
77  RestoreOptions *ropt = AH->public.ropt;
78 
79  /*
80  * Save the dbname, if given, in override_dbname so that it will also
81  * affect any later reconnection attempt.
82  */
83  if (dbname)
85 
86  /*
87  * Note: we want to establish the new connection, and in particular update
88  * ArchiveHandle's connCancel, before closing old connection. Otherwise
89  * an ill-timed SIGINT could try to access a dead connection.
90  */
91  AH->connection = NULL; /* dodge error check in ConnectDatabase */
92 
93  ConnectDatabase((Archive *) AH, &ropt->cparams, true);
94 
95  PQfinish(oldConn);
96 }
97 
98 /*
99  * Make, or remake, a database connection with the given parameters.
100  *
101  * The resulting connection handle is stored in AHX->connection.
102  *
103  * An interactive password prompt is automatically issued if required.
104  * We store the results of that in AHX->savedPassword.
105  * Note: it's not really all that sensible to use a single-entry password
106  * cache if the username keeps changing. In current usage, however, the
107  * username never does change, so one savedPassword is sufficient.
108  */
109 void
111  const ConnParams *cparams,
112  bool isReconnect)
113 {
114  ArchiveHandle *AH = (ArchiveHandle *) AHX;
115  trivalue prompt_password;
116  char *password;
117  bool new_pass;
118 
119  if (AH->connection)
120  pg_fatal("already connected to a database");
121 
122  /* Never prompt for a password during a reconnection */
123  prompt_password = isReconnect ? TRI_NO : cparams->promptPassword;
124 
125  password = AH->savedPassword;
126 
127  if (prompt_password == TRI_YES && password == NULL)
128  password = simple_prompt("Password: ", false);
129 
130  /*
131  * Start the connection. Loop until we have a password if requested by
132  * backend.
133  */
134  do
135  {
136  const char *keywords[8];
137  const char *values[8];
138  int i = 0;
139 
140  /*
141  * If dbname is a connstring, its entries can override the other
142  * values obtained from cparams; but in turn, override_dbname can
143  * override the dbname component of it.
144  */
145  keywords[i] = "host";
146  values[i++] = cparams->pghost;
147  keywords[i] = "port";
148  values[i++] = cparams->pgport;
149  keywords[i] = "user";
150  values[i++] = cparams->username;
151  keywords[i] = "password";
152  values[i++] = password;
153  keywords[i] = "dbname";
154  values[i++] = cparams->dbname;
155  if (cparams->override_dbname)
156  {
157  keywords[i] = "dbname";
158  values[i++] = cparams->override_dbname;
159  }
160  keywords[i] = "fallback_application_name";
161  values[i++] = progname;
162  keywords[i] = NULL;
163  values[i++] = NULL;
164  Assert(i <= lengthof(keywords));
165 
166  new_pass = false;
167  AH->connection = PQconnectdbParams(keywords, values, true);
168 
169  if (!AH->connection)
170  pg_fatal("could not connect to database");
171 
172  if (PQstatus(AH->connection) == CONNECTION_BAD &&
174  password == NULL &&
175  prompt_password != TRI_NO)
176  {
177  PQfinish(AH->connection);
178  password = simple_prompt("Password: ", false);
179  new_pass = true;
180  }
181  } while (new_pass);
182 
183  /* check to see that the backend connection was successfully made */
184  if (PQstatus(AH->connection) == CONNECTION_BAD)
185  {
186  if (isReconnect)
187  pg_fatal("reconnection failed: %s",
189  else
190  pg_fatal("%s",
192  }
193 
194  /* Start strict; later phases may override this. */
197 
198  if (password && password != AH->savedPassword)
199  free(password);
200 
201  /*
202  * We want to remember connection's actual password, whether or not we got
203  * it by prompting. So we don't just store the password variable.
204  */
206  {
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 }
219 
220 /*
221  * Close the connection to the database and also cancel off the query if we
222  * have one running.
223  */
224 void
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 pg_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 }
252 
253 PGconn *
255 {
256  ArchiveHandle *AH = (ArchiveHandle *) AHX;
257 
258  return AH->connection;
259 }
260 
261 static void
262 notice_processor(void *arg, const char *message)
263 {
264  pg_log_info("%s", message);
265 }
266 
267 /* Like pg_fatal(), but with a complaint about a particular query. */
268 static void
269 die_on_query_failure(ArchiveHandle *AH, const char *query)
270 {
271  pg_log_error("query failed: %s",
273  pg_log_error_detail("Query was: %s", query);
274  exit(1);
275 }
276 
277 void
278 ExecuteSqlStatement(Archive *AHX, const char *query)
279 {
280  ArchiveHandle *AH = (ArchiveHandle *) AHX;
281  PGresult *res;
282 
283  res = PQexec(AH->connection, query);
285  die_on_query_failure(AH, query);
286  PQclear(res);
287 }
288 
289 PGresult *
290 ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
291 {
292  ArchiveHandle *AH = (ArchiveHandle *) AHX;
293  PGresult *res;
294 
295  res = PQexec(AH->connection, query);
296  if (PQresultStatus(res) != status)
297  die_on_query_failure(AH, query);
298  return res;
299 }
300 
301 /*
302  * Execute an SQL query and verify that we got exactly one row back.
303  */
304 PGresult *
305 ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
306 {
307  PGresult *res;
308  int ntups;
309 
310  res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK);
311 
312  /* Expecting a single result only */
313  ntups = PQntuples(res);
314  if (ntups != 1)
315  pg_fatal(ngettext("query returned %d row instead of one: %s",
316  "query returned %d rows instead of one: %s",
317  ntups),
318  ntups, query);
319 
320  return res;
321 }
322 
323 /*
324  * Convenience function to send a query.
325  * Monitors result to detect COPY statements
326  */
327 static void
328 ExecuteSqlCommand(ArchiveHandle *AH, const char *qry, const char *desc)
329 {
330  PGconn *conn = AH->connection;
331  PGresult *res;
332 
333 #ifdef NOT_USED
334  fprintf(stderr, "Executing: '%s'\n\n", qry);
335 #endif
336  res = PQexec(conn, qry);
337 
338  switch (PQresultStatus(res))
339  {
340  case PGRES_COMMAND_OK:
341  case PGRES_TUPLES_OK:
342  case PGRES_EMPTY_QUERY:
343  /* A-OK */
344  break;
345  case PGRES_COPY_IN:
346  /* Assume this is an expected result */
347  AH->pgCopyIn = true;
348  break;
349  default:
350  /* trouble */
351  warn_or_exit_horribly(AH, "%s: %sCommand was: %s",
352  desc, PQerrorMessage(conn), qry);
353  break;
354  }
355 
356  PQclear(res);
357 }
358 
359 
360 /*
361  * Process non-COPY table data (that is, INSERT commands).
362  *
363  * The commands have been run together as one long string for compressibility,
364  * and we are receiving them in bufferloads with arbitrary boundaries, so we
365  * have to locate command boundaries and save partial commands across calls.
366  * All state must be kept in AH->sqlparse, not in local variables of this
367  * routine. We assume that AH->sqlparse was filled with zeroes when created.
368  *
369  * We have to lex the data to the extent of identifying literals and quoted
370  * identifiers, so that we can recognize statement-terminating semicolons.
371  * We assume that INSERT data will not contain SQL comments, E'' literals,
372  * or dollar-quoted strings, so this is much simpler than a full SQL lexer.
373  *
374  * Note: when restoring from a pre-9.0 dump file, this code is also used to
375  * process BLOB COMMENTS data, which has the same problem of containing
376  * multiple SQL commands that might be split across bufferloads. Fortunately,
377  * that data won't contain anything complicated to lex either.
378  */
379 static void
380 ExecuteSimpleCommands(ArchiveHandle *AH, const char *buf, size_t bufLen)
381 {
382  const char *qry = buf;
383  const char *eos = buf + bufLen;
384 
385  /* initialize command buffer if first time through */
386  if (AH->sqlparse.curCmd == NULL)
388 
389  for (; qry < eos; qry++)
390  {
391  char ch = *qry;
392 
393  /* For neatness, we skip any newlines between commands */
394  if (!(ch == '\n' && AH->sqlparse.curCmd->len == 0))
396 
397  switch (AH->sqlparse.state)
398  {
399  case SQL_SCAN: /* Default state == 0, set in _allocAH */
400  if (ch == ';')
401  {
402  /*
403  * We've found the end of a statement. Send it and reset
404  * the buffer.
405  */
407  "could not execute query");
409  }
410  else if (ch == '\'')
411  {
413  AH->sqlparse.backSlash = false;
414  }
415  else if (ch == '"')
416  {
418  }
419  break;
420 
421  case SQL_IN_SINGLE_QUOTE:
422  /* We needn't handle '' specially */
423  if (ch == '\'' && !AH->sqlparse.backSlash)
424  AH->sqlparse.state = SQL_SCAN;
425  else if (ch == '\\' && !AH->public.std_strings)
427  else
428  AH->sqlparse.backSlash = false;
429  break;
430 
431  case SQL_IN_DOUBLE_QUOTE:
432  /* We needn't handle "" specially */
433  if (ch == '"')
434  AH->sqlparse.state = SQL_SCAN;
435  break;
436  }
437  }
438 }
439 
440 
441 /*
442  * Implement ahwrite() for direct-to-DB restore
443  */
444 int
445 ExecuteSqlCommandBuf(Archive *AHX, const char *buf, size_t bufLen)
446 {
447  ArchiveHandle *AH = (ArchiveHandle *) AHX;
448 
449  if (AH->outputKind == OUTPUT_COPYDATA)
450  {
451  /*
452  * COPY data.
453  *
454  * We drop the data on the floor if libpq has failed to enter COPY
455  * mode; this allows us to behave reasonably when trying to continue
456  * after an error in a COPY command.
457  */
458  if (AH->pgCopyIn &&
459  PQputCopyData(AH->connection, buf, bufLen) <= 0)
460  pg_fatal("error returned by PQputCopyData: %s",
462  }
463  else if (AH->outputKind == OUTPUT_OTHERDATA)
464  {
465  /*
466  * Table data expressed as INSERT commands; or, in old dump files,
467  * BLOB COMMENTS data (which is expressed as COMMENT ON commands).
468  */
469  ExecuteSimpleCommands(AH, buf, bufLen);
470  }
471  else
472  {
473  /*
474  * General SQL commands; we assume that commands will not be split
475  * across calls.
476  *
477  * In most cases the data passed to us will be a null-terminated
478  * string, but if it's not, we have to add a trailing null.
479  */
480  if (buf[bufLen] == '\0')
481  ExecuteSqlCommand(AH, buf, "could not execute query");
482  else
483  {
484  char *str = (char *) pg_malloc(bufLen + 1);
485 
486  memcpy(str, buf, bufLen);
487  str[bufLen] = '\0';
488  ExecuteSqlCommand(AH, str, "could not execute query");
489  free(str);
490  }
491  }
492 
493  return bufLen;
494 }
495 
496 /*
497  * Terminate a COPY operation during direct-to-DB restore
498  */
499 void
500 EndDBCopyMode(Archive *AHX, const char *tocEntryTag)
501 {
502  ArchiveHandle *AH = (ArchiveHandle *) AHX;
503 
504  if (AH->pgCopyIn)
505  {
506  PGresult *res;
507 
508  if (PQputCopyEnd(AH->connection, NULL) <= 0)
509  pg_fatal("error returned by PQputCopyEnd: %s",
511 
512  /* Check command status and return to normal libpq state */
513  res = PQgetResult(AH->connection);
515  warn_or_exit_horribly(AH, "COPY failed for table \"%s\": %s",
516  tocEntryTag, PQerrorMessage(AH->connection));
517  PQclear(res);
518 
519  /* Do this to ensure we've pumped libpq back to idle state */
520  if (PQgetResult(AH->connection) != NULL)
521  pg_log_warning("unexpected extra results during COPY of table \"%s\"",
522  tocEntryTag);
523 
524  AH->pgCopyIn = false;
525  }
526 }
527 
528 void
530 {
531  ArchiveHandle *AH = (ArchiveHandle *) AHX;
532 
533  ExecuteSqlCommand(AH, "BEGIN", "could not start database transaction");
534 }
535 
536 void
538 {
539  ArchiveHandle *AH = (ArchiveHandle *) AHX;
540 
541  ExecuteSqlCommand(AH, "COMMIT", "could not commit database transaction");
542 }
543 
544 void
546 {
547  /*
548  * If we are not restoring to a direct database connection, we have to
549  * guess about how to detect whether the LO exists. Assume new-style.
550  */
551  if (AH->connection == NULL ||
552  PQserverVersion(AH->connection) >= 90000)
553  {
554  ahprintf(AH,
555  "SELECT pg_catalog.lo_unlink(oid) "
556  "FROM pg_catalog.pg_largeobject_metadata "
557  "WHERE oid = '%u';\n",
558  oid);
559  }
560  else
561  {
562  /* Restoring to pre-9.0 server, so do it the old way */
563  ahprintf(AH,
564  "SELECT CASE WHEN EXISTS("
565  "SELECT 1 FROM pg_catalog.pg_largeobject WHERE loid = '%u'"
566  ") THEN pg_catalog.lo_unlink('%u') END;\n",
567  oid, oid);
568  }
569 }
void set_archive_cancel_info(ArchiveHandle *AH, PGconn *conn)
Definition: parallel.c:730
static Datum values[MAXATTR]
Definition: bootstrap.c:152
#define ngettext(s, p, n)
Definition: c.h:1168
#define lengthof(array)
Definition: c.h:775
#define ALWAYS_SECURE_SEARCH_PATH_SQL
Definition: connect.h:25
int PQcancel(PGcancel *cancel, char *errbuf, int errbufsize)
Definition: fe-cancel.c:462
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:6913
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:6938
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:678
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:6903
int PQconnectionUsedPassword(const PGconn *conn)
Definition: fe-connect.c:7014
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:6999
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6948
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6895
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4669
char * PQpass(const PGconn *conn)
Definition: fe-connect.c:6810
PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg)
Definition: fe-connect.c:7127
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3371
int PQputCopyEnd(PGconn *conn, const char *errormsg)
Definition: fe-exec.c:2711
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3441
int PQputCopyData(PGconn *conn, const char *buffer, int nbytes)
Definition: fe-exec.c:2657
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2224
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3836
PGresult * PQgetResult(PGconn *conn)
Definition: fe-exec.c:2038
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
#define free(a)
Definition: header.h:65
int i
Definition: isn.c:73
@ CONNECTION_BAD
Definition: libpq-fe.h:61
ExecStatusType
Definition: libpq-fe.h:98
@ PGRES_COPY_IN
Definition: libpq-fe.h:107
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:100
@ PGRES_EMPTY_QUERY
Definition: libpq-fe.h:99
@ PGRES_TUPLES_OK
Definition: libpq-fe.h:103
@ PQTRANS_ACTIVE
Definition: libpq-fe.h:122
Assert(fmt[strlen(fmt) - 1] !='\n')
exit(1)
#define pg_log_error(...)
Definition: logging.h:106
#define pg_log_info(...)
Definition: logging.h:124
#define pg_log_error_detail(...)
Definition: logging.h:109
const char * progname
Definition: main.c:44
void warn_or_exit_horribly(ArchiveHandle *AH, const char *fmt,...)
int ahprintf(ArchiveHandle *AH, const char *fmt,...)
@ SQL_IN_DOUBLE_QUOTE
@ SQL_IN_SINGLE_QUOTE
@ SQL_SCAN
@ OUTPUT_COPYDATA
@ OUTPUT_OTHERDATA
void ConnectDatabase(Archive *AHX, const ConnParams *cparams, bool isReconnect)
Definition: pg_backup_db.c:110
void ExecuteSqlStatement(Archive *AHX, const char *query)
Definition: pg_backup_db.c:278
void EndDBCopyMode(Archive *AHX, const char *tocEntryTag)
Definition: pg_backup_db.c:500
static void die_on_query_failure(ArchiveHandle *AH, const char *query)
Definition: pg_backup_db.c:269
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:290
static void _check_database_version(ArchiveHandle *AH)
Definition: pg_backup_db.c:33
PGconn * GetConnection(Archive *AHX)
Definition: pg_backup_db.c:254
void DropLOIfExists(ArchiveHandle *AH, Oid oid)
Definition: pg_backup_db.c:545
void ReconnectToServer(ArchiveHandle *AH, const char *dbname)
Definition: pg_backup_db.c:74
static void ExecuteSimpleCommands(ArchiveHandle *AH, const char *buf, size_t bufLen)
Definition: pg_backup_db.c:380
void StartTransaction(Archive *AHX)
Definition: pg_backup_db.c:529
int ExecuteSqlCommandBuf(Archive *AHX, const char *buf, size_t bufLen)
Definition: pg_backup_db.c:445
static void ExecuteSqlCommand(ArchiveHandle *AH, const char *qry, const char *desc)
Definition: pg_backup_db.c:328
static void notice_processor(void *arg, const char *message)
Definition: pg_backup_db.c:262
void DisconnectDatabase(Archive *AHX)
Definition: pg_backup_db.c:225
void CommitTransaction(Archive *AHX)
Definition: pg_backup_db.c:537
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
Definition: pg_backup_db.c:305
void * arg
#define pg_fatal(...)
static char * buf
Definition: pg_test_fsync.c:73
#define pg_log_warning(...)
Definition: pgfnames.c:24
#define fprintf
Definition: port.h:242
unsigned int Oid
Definition: postgres_ext.h:31
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
char * simple_prompt(const char *prompt, bool echo)
Definition: sprompt.c:38
static char * password
Definition: streamutil.c:53
char * dbname
Definition: streamutil.c:51
PGconn * conn
Definition: streamutil.c:54
int remoteVersion
Definition: pg_backup.h:217
char * remoteVersionStr
Definition: pg_backup.h:216
bool isStandby
Definition: pg_backup.h:218
int maxRemoteVersion
Definition: pg_backup.h:221
bool std_strings
Definition: pg_backup.h:228
RestoreOptions * ropt
Definition: pg_backup.h:213
PGcancel *volatile connCancel
sqlparseInfo sqlparse
ArchiverOutput outputKind
char * override_dbname
Definition: pg_backup.h:91
char * pgport
Definition: pg_backup.h:85
char * pghost
Definition: pg_backup.h:86
trivalue promptPassword
Definition: pg_backup.h:88
char * username
Definition: pg_backup.h:87
char * dbname
Definition: pg_backup.h:84
ConnParams cparams
Definition: pg_backup.h:144
PQExpBuffer curCmd
sqlparseState state
trivalue
Definition: vacuumlo.c:35
@ TRI_YES
Definition: vacuumlo.c:38
@ TRI_NO
Definition: vacuumlo.c:37