Force a checkpoint in CREATE DATABASE before starting to copy the files,
[PostgreSQL.git] / src / bin / pg_dump / pg_backup_db.c
blob1bb33576bc850e27946a7b054baa143b148621c8
1 /*-------------------------------------------------------------------------
3 * pg_backup_db.c
5 * Implements the basic DB functions used by the archiver.
7 * IDENTIFICATION
8 * $PostgreSQL$
10 *-------------------------------------------------------------------------
13 #include "pg_backup_db.h"
14 #include "dumputils.h"
16 #include <unistd.h>
18 #include <ctype.h>
20 #ifdef HAVE_TERMIOS_H
21 #include <termios.h>
22 #endif
25 static const char *modulename = gettext_noop("archiver (db)");
27 static void _check_database_version(ArchiveHandle *AH);
28 static PGconn *_connectDB(ArchiveHandle *AH, const char *newdbname, const char *newUser);
29 static void notice_processor(void *arg, const char *message);
30 static char *_sendSQLLine(ArchiveHandle *AH, char *qry, char *eos);
31 static char *_sendCopyLine(ArchiveHandle *AH, char *qry, char *eos);
33 static bool _isIdentChar(unsigned char c);
34 static bool _isDQChar(unsigned char c, bool atStart);
36 #define DB_MAX_ERR_STMT 128
38 static int
39 _parse_version(ArchiveHandle *AH, const char *versionString)
41 int v;
43 v = parse_version(versionString);
44 if (v < 0)
45 die_horribly(AH, modulename, "could not parse version string \"%s\"\n", versionString);
47 return v;
50 static void
51 _check_database_version(ArchiveHandle *AH)
53 int myversion;
54 const char *remoteversion_str;
55 int remoteversion;
57 myversion = _parse_version(AH, PG_VERSION);
59 remoteversion_str = PQparameterStatus(AH->connection, "server_version");
60 if (!remoteversion_str)
61 die_horribly(AH, modulename, "could not get server_version from libpq\n");
63 remoteversion = _parse_version(AH, remoteversion_str);
65 AH->public.remoteVersionStr = strdup(remoteversion_str);
66 AH->public.remoteVersion = remoteversion;
68 if (myversion != remoteversion
69 && (remoteversion < AH->public.minRemoteVersion ||
70 remoteversion > AH->public.maxRemoteVersion))
72 write_msg(NULL, "server version: %s; %s version: %s\n",
73 remoteversion_str, progname, PG_VERSION);
74 die_horribly(AH, NULL, "aborting because of server version mismatch\n");
79 * Reconnect to the server. If dbname is not NULL, use that database,
80 * else the one associated with the archive handle. If username is
81 * not NULL, use that user name, else the one from the handle. If
82 * both the database and the user match the existing connection already,
83 * nothing will be done.
85 * Returns 1 in any case.
87 int
88 ReconnectToServer(ArchiveHandle *AH, const char *dbname, const char *username)
90 PGconn *newConn;
91 const char *newdbname;
92 const char *newusername;
94 if (!dbname)
95 newdbname = PQdb(AH->connection);
96 else
97 newdbname = dbname;
99 if (!username)
100 newusername = PQuser(AH->connection);
101 else
102 newusername = username;
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;
109 newConn = _connectDB(AH, newdbname, newusername);
111 PQfinish(AH->connection);
112 AH->connection = newConn;
114 return 1;
118 * Connect to the db again.
120 static PGconn *
121 _connectDB(ArchiveHandle *AH, const char *reqdb, const char *requser)
123 PGconn *newConn;
124 char *newdb;
125 char *newuser;
126 char *password = NULL;
127 bool new_pass;
129 if (!reqdb)
130 newdb = PQdb(AH->connection);
131 else
132 newdb = (char *) reqdb;
134 if (!requser || (strlen(requser) == 0))
135 newuser = PQuser(AH->connection);
136 else
137 newuser = (char *) requser;
139 ahlog(AH, 1, "connecting to database \"%s\" as user \"%s\"\n", newdb, newuser);
141 if (AH->requirePassword)
143 password = simple_prompt("Password: ", 100, false);
144 if (password == NULL)
145 die_horribly(AH, modulename, "out of memory\n");
150 new_pass = false;
151 newConn = PQsetdbLogin(PQhost(AH->connection), PQport(AH->connection),
152 NULL, NULL, newdb,
153 newuser, password);
154 if (!newConn)
155 die_horribly(AH, modulename, "failed to reconnect to database\n");
157 if (PQstatus(newConn) == CONNECTION_BAD)
159 if (!PQconnectionNeedsPassword(newConn))
160 die_horribly(AH, modulename, "could not reconnect to database: %s",
161 PQerrorMessage(newConn));
162 PQfinish(newConn);
164 if (password)
165 fprintf(stderr, "Password incorrect\n");
167 fprintf(stderr, "Connecting to %s as %s\n",
168 newdb, newuser);
170 if (password)
171 free(password);
172 password = simple_prompt("Password: ", 100, false);
173 new_pass = true;
175 } while (new_pass);
177 if (password)
178 free(password);
180 /* check for version mismatch */
181 _check_database_version(AH);
183 PQsetNoticeProcessor(newConn, notice_processor, NULL);
185 return newConn;
190 * Make a database connection with the given parameters. The
191 * connection handle is returned, the parameters are stored in AHX.
192 * An interactive password prompt is automatically issued if required.
194 PGconn *
195 ConnectDatabase(Archive *AHX,
196 const char *dbname,
197 const char *pghost,
198 const char *pgport,
199 const char *username,
200 int reqPwd)
202 ArchiveHandle *AH = (ArchiveHandle *) AHX;
203 char *password = NULL;
204 bool new_pass;
206 if (AH->connection)
207 die_horribly(AH, modulename, "already connected to a database\n");
209 if (reqPwd)
211 password = simple_prompt("Password: ", 100, false);
212 if (password == NULL)
213 die_horribly(AH, modulename, "out of memory\n");
214 AH->requirePassword = true;
216 else
217 AH->requirePassword = false;
220 * Start the connection. Loop until we have a password if requested by
221 * backend.
225 new_pass = false;
226 AH->connection = PQsetdbLogin(pghost, pgport, NULL, NULL,
227 dbname, username, password);
229 if (!AH->connection)
230 die_horribly(AH, modulename, "failed to connect to database\n");
232 if (PQstatus(AH->connection) == CONNECTION_BAD &&
233 PQconnectionNeedsPassword(AH->connection) &&
234 password == NULL &&
235 !feof(stdin))
237 PQfinish(AH->connection);
238 password = simple_prompt("Password: ", 100, false);
239 new_pass = true;
241 } while (new_pass);
243 if (password)
244 free(password);
246 /* check to see that the backend connection was successfully made */
247 if (PQstatus(AH->connection) == CONNECTION_BAD)
248 die_horribly(AH, modulename, "connection to database \"%s\" failed: %s",
249 PQdb(AH->connection), PQerrorMessage(AH->connection));
251 /* check for version mismatch */
252 _check_database_version(AH);
254 PQsetNoticeProcessor(AH->connection, notice_processor, NULL);
256 return AH->connection;
260 static void
261 notice_processor(void *arg, const char *message)
263 write_msg(NULL, "%s", message);
267 /* Public interface */
268 /* Convenience function to send a query. Monitors result to handle COPY statements */
269 static void
270 ExecuteSqlCommand(ArchiveHandle *AH, const char *qry, const char *desc)
272 PGconn *conn = AH->connection;
273 PGresult *res;
274 char errStmt[DB_MAX_ERR_STMT];
276 #ifdef NOT_USED
277 fprintf(stderr, "Executing: '%s'\n\n", qry);
278 #endif
279 res = PQexec(conn, qry);
281 switch (PQresultStatus(res))
283 case PGRES_COMMAND_OK:
284 case PGRES_TUPLES_OK:
285 /* A-OK */
286 break;
287 case PGRES_COPY_IN:
288 /* Assume this is an expected result */
289 AH->pgCopyIn = true;
290 break;
291 default:
292 /* trouble */
293 strncpy(errStmt, qry, DB_MAX_ERR_STMT);
294 if (errStmt[DB_MAX_ERR_STMT - 1] != '\0')
296 errStmt[DB_MAX_ERR_STMT - 4] = '.';
297 errStmt[DB_MAX_ERR_STMT - 3] = '.';
298 errStmt[DB_MAX_ERR_STMT - 2] = '.';
299 errStmt[DB_MAX_ERR_STMT - 1] = '\0';
301 warn_or_die_horribly(AH, modulename, "%s: %s Command was: %s\n",
302 desc, PQerrorMessage(conn), errStmt);
303 break;
306 PQclear(res);
310 * Used by ExecuteSqlCommandBuf to send one buffered line when running a COPY command.
312 static char *
313 _sendCopyLine(ArchiveHandle *AH, char *qry, char *eos)
315 size_t loc; /* Location of next newline */
316 int pos = 0; /* Current position */
317 int sPos = 0; /* Last pos of a slash char */
318 int isEnd = 0;
320 /* loop to find unquoted newline ending the line of COPY data */
321 for (;;)
323 loc = strcspn(&qry[pos], "\n") + pos;
325 /* If no match, then wait */
326 if (loc >= (eos - qry)) /* None found */
328 appendBinaryPQExpBuffer(AH->pgCopyBuf, qry, (eos - qry));
329 return eos;
333 * fprintf(stderr, "Found cr at %d, prev char was %c, next was %c\n",
334 * loc, qry[loc-1], qry[loc+1]);
337 /* Count the number of preceding slashes */
338 sPos = loc;
339 while (sPos > 0 && qry[sPos - 1] == '\\')
340 sPos--;
342 sPos = loc - sPos;
345 * If an odd number of preceding slashes, then \n was escaped so set
346 * the next search pos, and loop (if any left).
348 if ((sPos & 1) == 1)
350 /* fprintf(stderr, "cr was escaped\n"); */
351 pos = loc + 1;
352 if (pos >= (eos - qry))
354 appendBinaryPQExpBuffer(AH->pgCopyBuf, qry, (eos - qry));
355 return eos;
358 else
359 break;
362 /* We found an unquoted newline */
363 qry[loc] = '\0';
364 appendPQExpBuffer(AH->pgCopyBuf, "%s\n", qry);
365 isEnd = (strcmp(AH->pgCopyBuf->data, "\\.\n") == 0);
368 * Note that we drop the data on the floor if libpq has failed to enter
369 * COPY mode; this allows us to behave reasonably when trying to continue
370 * after an error in a COPY command.
372 if (AH->pgCopyIn &&
373 PQputCopyData(AH->connection, AH->pgCopyBuf->data,
374 AH->pgCopyBuf->len) <= 0)
375 die_horribly(AH, modulename, "error returned by PQputCopyData: %s",
376 PQerrorMessage(AH->connection));
378 resetPQExpBuffer(AH->pgCopyBuf);
380 if (isEnd && AH->pgCopyIn)
382 PGresult *res;
384 if (PQputCopyEnd(AH->connection, NULL) <= 0)
385 die_horribly(AH, modulename, "error returned by PQputCopyEnd: %s",
386 PQerrorMessage(AH->connection));
388 /* Check command status and return to normal libpq state */
389 res = PQgetResult(AH->connection);
390 if (PQresultStatus(res) != PGRES_COMMAND_OK)
391 warn_or_die_horribly(AH, modulename, "COPY failed: %s",
392 PQerrorMessage(AH->connection));
393 PQclear(res);
395 AH->pgCopyIn = false;
398 return qry + loc + 1;
402 * Used by ExecuteSqlCommandBuf to send one buffered line of SQL
403 * (not data for the copy command).
405 static char *
406 _sendSQLLine(ArchiveHandle *AH, char *qry, char *eos)
409 * The following is a mini state machine to assess the end of an SQL
410 * statement. It really only needs to parse good SQL, or at least that's
411 * the theory... End-of-statement is assumed to be an unquoted,
412 * un-commented semi-colon that's not within any parentheses.
414 * Note: the input can be split into bufferloads at arbitrary boundaries.
415 * Therefore all state must be kept in AH->sqlparse, not in local
416 * variables of this routine. We assume that AH->sqlparse was filled with
417 * zeroes when created.
419 for (; qry < eos; qry++)
421 switch (AH->sqlparse.state)
423 case SQL_SCAN: /* Default state == 0, set in _allocAH */
424 if (*qry == ';' && AH->sqlparse.braceDepth == 0)
427 * We've found the end of a statement. Send it and reset
428 * the buffer.
430 appendPQExpBufferChar(AH->sqlBuf, ';'); /* inessential */
431 ExecuteSqlCommand(AH, AH->sqlBuf->data,
432 "could not execute query");
433 resetPQExpBuffer(AH->sqlBuf);
434 AH->sqlparse.lastChar = '\0';
437 * Remove any following newlines - so that embedded COPY
438 * commands don't get a starting newline.
440 qry++;
441 while (qry < eos && *qry == '\n')
442 qry++;
444 /* We've finished one line, so exit */
445 return qry;
447 else if (*qry == '\'')
449 if (AH->sqlparse.lastChar == 'E')
450 AH->sqlparse.state = SQL_IN_E_QUOTE;
451 else
452 AH->sqlparse.state = SQL_IN_SINGLE_QUOTE;
453 AH->sqlparse.backSlash = false;
455 else if (*qry == '"')
457 AH->sqlparse.state = SQL_IN_DOUBLE_QUOTE;
461 * Look for dollar-quotes. We make the assumption that
462 * $-quotes will not have an ident character just before them
463 * in pg_dump output. XXX is this good enough?
465 else if (*qry == '$' && !_isIdentChar(AH->sqlparse.lastChar))
467 AH->sqlparse.state = SQL_IN_DOLLAR_TAG;
468 /* initialize separate buffer with possible tag */
469 if (AH->sqlparse.tagBuf == NULL)
470 AH->sqlparse.tagBuf = createPQExpBuffer();
471 else
472 resetPQExpBuffer(AH->sqlparse.tagBuf);
473 appendPQExpBufferChar(AH->sqlparse.tagBuf, *qry);
475 else if (*qry == '-' && AH->sqlparse.lastChar == '-')
476 AH->sqlparse.state = SQL_IN_SQL_COMMENT;
477 else if (*qry == '*' && AH->sqlparse.lastChar == '/')
478 AH->sqlparse.state = SQL_IN_EXT_COMMENT;
479 else if (*qry == '(')
480 AH->sqlparse.braceDepth++;
481 else if (*qry == ')')
482 AH->sqlparse.braceDepth--;
483 break;
485 case SQL_IN_SQL_COMMENT:
486 if (*qry == '\n')
487 AH->sqlparse.state = SQL_SCAN;
488 break;
490 case SQL_IN_EXT_COMMENT:
493 * This isn't fully correct, because we don't account for
494 * nested slash-stars, but pg_dump never emits such.
496 if (AH->sqlparse.lastChar == '*' && *qry == '/')
497 AH->sqlparse.state = SQL_SCAN;
498 break;
500 case SQL_IN_SINGLE_QUOTE:
501 /* We needn't handle '' specially */
502 if (*qry == '\'' && !AH->sqlparse.backSlash)
503 AH->sqlparse.state = SQL_SCAN;
504 else if (*qry == '\\')
505 AH->sqlparse.backSlash = !AH->sqlparse.backSlash;
506 else
507 AH->sqlparse.backSlash = false;
508 break;
510 case SQL_IN_E_QUOTE:
513 * Eventually we will need to handle '' specially, because
514 * after E'...''... we should still be in E_QUOTE state.
516 * XXX problem: how do we tell whether the dump was made by a
517 * version that thinks backslashes aren't special in non-E
518 * literals??
520 if (*qry == '\'' && !AH->sqlparse.backSlash)
521 AH->sqlparse.state = SQL_SCAN;
522 else if (*qry == '\\')
523 AH->sqlparse.backSlash = !AH->sqlparse.backSlash;
524 else
525 AH->sqlparse.backSlash = false;
526 break;
528 case SQL_IN_DOUBLE_QUOTE:
529 /* We needn't handle "" specially */
530 if (*qry == '"')
531 AH->sqlparse.state = SQL_SCAN;
532 break;
534 case SQL_IN_DOLLAR_TAG:
535 if (*qry == '$')
537 /* Do not add the closing $ to tagBuf */
538 AH->sqlparse.state = SQL_IN_DOLLAR_QUOTE;
539 AH->sqlparse.minTagEndPos = AH->sqlBuf->len + AH->sqlparse.tagBuf->len + 1;
541 else if (_isDQChar(*qry, (AH->sqlparse.tagBuf->len == 1)))
543 /* Valid, so add to tag */
544 appendPQExpBufferChar(AH->sqlparse.tagBuf, *qry);
546 else
549 * Ooops, we're not really in a dollar-tag. Valid tag
550 * chars do not include the various chars we look for in
551 * this state machine, so it's safe to just jump from this
552 * state back to SCAN. We have to back up the qry pointer
553 * so that the current character gets rescanned in SCAN
554 * state; and then "continue" so that the bottom-of-loop
555 * actions aren't done yet.
557 AH->sqlparse.state = SQL_SCAN;
558 qry--;
559 continue;
561 break;
563 case SQL_IN_DOLLAR_QUOTE:
566 * If we are at a $, see whether what precedes it matches
567 * tagBuf. (Remember that the trailing $ of the tag was not
568 * added to tagBuf.) However, don't compare until we have
569 * enough data to be a possible match --- this is needed to
570 * avoid false match on '$a$a$...'
572 if (*qry == '$' &&
573 AH->sqlBuf->len >= AH->sqlparse.minTagEndPos &&
574 strcmp(AH->sqlparse.tagBuf->data,
575 AH->sqlBuf->data + AH->sqlBuf->len - AH->sqlparse.tagBuf->len) == 0)
576 AH->sqlparse.state = SQL_SCAN;
577 break;
580 appendPQExpBufferChar(AH->sqlBuf, *qry);
581 AH->sqlparse.lastChar = *qry;
585 * If we get here, we've processed entire bufferload with no complete SQL
586 * stmt
588 return eos;
592 /* Convenience function to send one or more queries. Monitors result to handle COPY statements */
594 ExecuteSqlCommandBuf(ArchiveHandle *AH, void *qryv, size_t bufLen)
596 char *qry = (char *) qryv;
597 char *eos = qry + bufLen;
600 * fprintf(stderr, "\n\n*****\n Buffer:\n\n%s\n*******************\n\n",
601 * qry);
604 /* Could switch between command and COPY IN mode at each line */
605 while (qry < eos)
608 * If libpq is in CopyIn mode *or* if the archive structure shows we
609 * are sending COPY data, treat the data as COPY data. The pgCopyIn
610 * check is only needed for backwards compatibility with ancient
611 * archive files that might just issue a COPY command without marking
612 * it properly. Note that in an archive entry that has a copyStmt,
613 * all data up to the end of the entry will go to _sendCopyLine, and
614 * therefore will be dropped if libpq has failed to enter COPY mode.
615 * Also, if a "\." data terminator is found, anything remaining in the
616 * archive entry will be dropped.
618 if (AH->pgCopyIn || AH->writingCopyData)
619 qry = _sendCopyLine(AH, qry, eos);
620 else
621 qry = _sendSQLLine(AH, qry, eos);
624 return 1;
627 void
628 StartTransaction(ArchiveHandle *AH)
630 ExecuteSqlCommand(AH, "BEGIN", "could not start database transaction");
633 void
634 CommitTransaction(ArchiveHandle *AH)
636 ExecuteSqlCommand(AH, "COMMIT", "could not commit database transaction");
639 static bool
640 _isIdentChar(unsigned char c)
642 if ((c >= 'a' && c <= 'z')
643 || (c >= 'A' && c <= 'Z')
644 || (c >= '0' && c <= '9')
645 || (c == '_')
646 || (c == '$')
647 || (c >= (unsigned char) '\200') /* no need to check <= \377 */
649 return true;
650 else
651 return false;
654 static bool
655 _isDQChar(unsigned char c, bool atStart)
657 if ((c >= 'a' && c <= 'z')
658 || (c >= 'A' && c <= 'Z')
659 || (c == '_')
660 || (!atStart && c >= '0' && c <= '9')
661 || (c >= (unsigned char) '\200') /* no need to check <= \377 */
663 return true;
664 else
665 return false;