1 /*-------------------------------------------------------------------------
5 * Implements the basic DB functions used by the archiver.
10 *-------------------------------------------------------------------------
13 #include "pg_backup_db.h"
14 #include "dumputils.h"
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
39 _parse_version(ArchiveHandle
*AH
, const char *versionString
)
43 v
= parse_version(versionString
);
45 die_horribly(AH
, modulename
, "could not parse version string \"%s\"\n", versionString
);
51 _check_database_version(ArchiveHandle
*AH
)
54 const char *remoteversion_str
;
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.
88 ReconnectToServer(ArchiveHandle
*AH
, const char *dbname
, const char *username
)
91 const char *newdbname
;
92 const char *newusername
;
95 newdbname
= PQdb(AH
->connection
);
100 newusername
= PQuser(AH
->connection
);
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)
109 newConn
= _connectDB(AH
, newdbname
, newusername
);
111 PQfinish(AH
->connection
);
112 AH
->connection
= newConn
;
118 * Connect to the db again.
121 _connectDB(ArchiveHandle
*AH
, const char *reqdb
, const char *requser
)
126 char *password
= NULL
;
130 newdb
= PQdb(AH
->connection
);
132 newdb
= (char *) reqdb
;
134 if (!requser
|| (strlen(requser
) == 0))
135 newuser
= PQuser(AH
->connection
);
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");
151 newConn
= PQsetdbLogin(PQhost(AH
->connection
), PQport(AH
->connection
),
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
));
165 fprintf(stderr
, "Password incorrect\n");
167 fprintf(stderr
, "Connecting to %s as %s\n",
172 password
= simple_prompt("Password: ", 100, false);
180 /* check for version mismatch */
181 _check_database_version(AH
);
183 PQsetNoticeProcessor(newConn
, notice_processor
, NULL
);
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.
195 ConnectDatabase(Archive
*AHX
,
199 const char *username
,
202 ArchiveHandle
*AH
= (ArchiveHandle
*) AHX
;
203 char *password
= NULL
;
207 die_horribly(AH
, modulename
, "already connected to a database\n");
211 password
= simple_prompt("Password: ", 100, false);
212 if (password
== NULL
)
213 die_horribly(AH
, modulename
, "out of memory\n");
214 AH
->requirePassword
= true;
217 AH
->requirePassword
= false;
220 * Start the connection. Loop until we have a password if requested by
226 AH
->connection
= PQsetdbLogin(pghost
, pgport
, NULL
, NULL
,
227 dbname
, username
, password
);
230 die_horribly(AH
, modulename
, "failed to connect to database\n");
232 if (PQstatus(AH
->connection
) == CONNECTION_BAD
&&
233 PQconnectionNeedsPassword(AH
->connection
) &&
237 PQfinish(AH
->connection
);
238 password
= simple_prompt("Password: ", 100, false);
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
;
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 */
270 ExecuteSqlCommand(ArchiveHandle
*AH
, const char *qry
, const char *desc
)
272 PGconn
*conn
= AH
->connection
;
274 char errStmt
[DB_MAX_ERR_STMT
];
277 fprintf(stderr
, "Executing: '%s'\n\n", qry
);
279 res
= PQexec(conn
, qry
);
281 switch (PQresultStatus(res
))
283 case PGRES_COMMAND_OK
:
284 case PGRES_TUPLES_OK
:
288 /* Assume this is an expected result */
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
);
310 * Used by ExecuteSqlCommandBuf to send one buffered line when running a COPY command.
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 */
320 /* loop to find unquoted newline ending the line of COPY data */
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
));
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 */
339 while (sPos
> 0 && qry
[sPos
- 1] == '\\')
345 * If an odd number of preceding slashes, then \n was escaped so set
346 * the next search pos, and loop (if any left).
350 /* fprintf(stderr, "cr was escaped\n"); */
352 if (pos
>= (eos
- qry
))
354 appendBinaryPQExpBuffer(AH
->pgCopyBuf
, qry
, (eos
- qry
));
362 /* We found an unquoted newline */
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.
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
)
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
));
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).
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
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.
441 while (qry
< eos
&& *qry
== '\n')
444 /* We've finished one line, so exit */
447 else if (*qry
== '\'')
449 if (AH
->sqlparse
.lastChar
== 'E')
450 AH
->sqlparse
.state
= SQL_IN_E_QUOTE
;
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();
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
--;
485 case SQL_IN_SQL_COMMENT
:
487 AH
->sqlparse
.state
= SQL_SCAN
;
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
;
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
;
507 AH
->sqlparse
.backSlash
= false;
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
520 if (*qry
== '\'' && !AH
->sqlparse
.backSlash
)
521 AH
->sqlparse
.state
= SQL_SCAN
;
522 else if (*qry
== '\\')
523 AH
->sqlparse
.backSlash
= !AH
->sqlparse
.backSlash
;
525 AH
->sqlparse
.backSlash
= false;
528 case SQL_IN_DOUBLE_QUOTE
:
529 /* We needn't handle "" specially */
531 AH
->sqlparse
.state
= SQL_SCAN
;
534 case SQL_IN_DOLLAR_TAG
:
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
);
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
;
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$...'
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
;
580 appendPQExpBufferChar(AH
->sqlBuf
, *qry
);
581 AH
->sqlparse
.lastChar
= *qry
;
585 * If we get here, we've processed entire bufferload with no complete SQL
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",
604 /* Could switch between command and COPY IN mode at each line */
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
);
621 qry
= _sendSQLLine(AH
, qry
, eos
);
628 StartTransaction(ArchiveHandle
*AH
)
630 ExecuteSqlCommand(AH
, "BEGIN", "could not start database transaction");
634 CommitTransaction(ArchiveHandle
*AH
)
636 ExecuteSqlCommand(AH
, "COMMIT", "could not commit database transaction");
640 _isIdentChar(unsigned char c
)
642 if ((c
>= 'a' && c
<= 'z')
643 || (c
>= 'A' && c
<= 'Z')
644 || (c
>= '0' && c
<= '9')
647 || (c
>= (unsigned char) '\200') /* no need to check <= \377 */
655 _isDQChar(unsigned char c
, bool atStart
)
657 if ((c
>= 'a' && c
<= 'z')
658 || (c
>= 'A' && c
<= 'Z')
660 || (!atStart
&& c
>= '0' && c
<= '9')
661 || (c
>= (unsigned char) '\200') /* no need to check <= \377 */