1/*-------------------------------------------------------------------------
5 * Implements the basic DB functions used by the archiver.
8 * src/bin/pg_dump/pg_backup_db.c
10 *-------------------------------------------------------------------------
34 const char *remoteversion_str;
40 if (remoteversion == 0 || !remoteversion_str)
41 pg_fatal(
"could not get \"server_version\" from libpq");
48 if (remoteversion != PG_VERSION_NUM
49 && (remoteversion < AH->
public.minRemoteVersion ||
52 pg_log_error(
"aborting because of server version mismatch");
54 remoteversion_str,
progname, PG_VERSION);
59 * Check if server is in recovery mode, which means we are on a hot
63 "SELECT pg_catalog.pg_is_in_recovery()");
69 * Reconnect to the server. If dbname is not NULL, use that database,
70 * else the one associated with the archive handle.
79 * Save the dbname, if given, in override_dbname so that it will also
80 * affect any later reconnection attempt.
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.
90 AH->
connection = NULL;
/* dodge error check in ConnectDatabaseAhx */
98 * Make, or remake, a database connection with the given parameters.
100 * The resulting connection handle is stored in AHX->connection.
102 * An interactive password prompt is automatically issued if required.
103 * We store the results of that in AHX->savedPassword.
104 * Note: it's not really all that sensible to use a single-entry password
105 * cache if the username keeps changing. In current usage, however, the
106 * username never does change, so one savedPassword is sufficient.
118 pg_fatal(
"already connected to a database");
120 /* Never prompt for a password during a reconnection */
130 prompt_password,
true,
133 /* Start strict; later phases may override this. */
141 * We want to remember connection's actual password, whether or not we got
142 * it by prompting. So we don't just store the password variable.
150 /* check for version mismatch */
155 /* arrange for SIGINT to issue a query cancel on this connection */
160 * Close the connection to the database and also cancel off the query if we
175 * If we have an active query, send a cancel before closing, ignoring
176 * any errors. This is of no use for a normal exit, but might be
177 * helpful during pg_fatal().
183 * Prevent signal handler from sending a cancel after this.
206/* Like pg_fatal(), but with a complaint about a particular query. */
241 * Execute an SQL query and verify that we got exactly one row back.
251 /* Expecting a single result only */
255 "query returned %d rows instead of one: %s",
263 * Convenience function to send a query.
264 * Monitors result to detect COPY statements
273 fprintf(stderr,
"Executing: '%s'\n\n", qry);
285 /* Assume this is an expected result */
300 * Process non-COPY table data (that is, INSERT commands).
302 * The commands have been run together as one long string for compressibility,
303 * and we are receiving them in bufferloads with arbitrary boundaries, so we
304 * have to locate command boundaries and save partial commands across calls.
305 * All state must be kept in AH->sqlparse, not in local variables of this
306 * routine. We assume that AH->sqlparse was filled with zeroes when created.
308 * We have to lex the data to the extent of identifying literals and quoted
309 * identifiers, so that we can recognize statement-terminating semicolons.
310 * We assume that INSERT data will not contain SQL comments, E'' literals,
311 * or dollar-quoted strings, so this is much simpler than a full SQL lexer.
313 * Note: when restoring from a pre-9.0 dump file, this code is also used to
314 * process BLOB COMMENTS data, which has the same problem of containing
315 * multiple SQL commands that might be split across bufferloads. Fortunately,
316 * that data won't contain anything complicated to lex either.
321 const char *qry =
buf;
322 const char *eos =
buf + bufLen;
324 /* initialize command buffer if first time through */
328 for (; qry < eos; qry++)
332 /* For neatness, we skip any newlines between commands */
338 case SQL_SCAN:
/* Default state == 0, set in _allocAH */
342 * We've found the end of a statement. Send it and reset
346 "could not execute query");
361 /* We needn't handle '' specially */
371 /* We needn't handle "" specially */
381 * Implement ahwrite() for direct-to-DB restore
393 * We drop the data on the floor if libpq has failed to enter COPY
394 * mode; this allows us to behave reasonably when trying to continue
395 * after an error in a COPY command.
399 pg_fatal(
"error returned by PQputCopyData: %s",
405 * Table data expressed as INSERT commands; or, in old dump files,
406 * BLOB COMMENTS data (which is expressed as COMMENT ON commands).
413 * General SQL commands; we assume that commands will not be split
416 * In most cases the data passed to us will be a null-terminated
417 * string, but if it's not, we have to add a trailing null.
419 if (
buf[bufLen] ==
'0円')
436 * Terminate a COPY operation during direct-to-DB restore
448 pg_fatal(
"error returned by PQputCopyEnd: %s",
451 /* Check command status and return to normal libpq state */
458 /* Do this to ensure we've pumped libpq back to idle state */
460 pg_log_warning(
"unexpected extra results during COPY of table \"%s\"",
484 * Issue per-blob commands for the large object(s) listed in the TocEntry
486 * The TocEntry's defn string is assumed to consist of large object OIDs,
487 * one per line. Wrap these in the given SQL command fragments and issue
488 * the commands. (cmdEnd need not include a semicolon.)
492 const char *cmdBegin,
const char *cmdEnd)
494 /* Make a writable copy of the command string */
501 while ((en = strchr(st,
'\n')) != NULL)
504 ahprintf(AH,
"%s%s%s;\n", cmdBegin, st, cmdEnd);
506 /* In --transaction-size mode, count each command as an action */
517 ahprintf(AH,
"COMMIT;\nBEGIN;\n\n");
529 * Process a "LARGE OBJECTS" ACL TocEntry.
531 * To save space in the dump file, the TocEntry contains only one copy
532 * of the required GRANT/REVOKE commands, written to apply to the first
533 * blob in the group (although we do not depend on that detail here).
534 * We must expand the text to generate commands for all the blobs listed
535 * in the associated BLOB METADATA entry.
549 Assert(strcmp(blobte->
desc,
"BLOB METADATA") == 0);
551 /* Make a writable copy of the ACL commands string */
555 * We have to parse out the commands sufficiently to locate the blob OIDs
556 * and find the command-ending semicolons. The commands should not
557 * contain anything hard to parse except for double-quoted role names,
558 * which are easy to ignore. Once we've split apart the first and second
559 * halves of a command, apply IssueCommandPerBlob. (This means the
560 * updates on the blobs are interleaved if there's multiple commands, but
561 * that should cause no trouble.)
568 /* Ignore double-quoted material */
570 inquotes = !inquotes;
576 /* If we found "LARGE OBJECT", that's the end of the first half */
577 if (strncmp(en,
"LARGE OBJECT ", 13) == 0)
579 /* Terminate the first-half string */
581 Assert(isdigit((
unsigned char) *en));
583 /* Skip the rest of the blob OID */
584 while (isdigit((
unsigned char) *en))
586 /* Second half starts here */
590 /* If we found semicolon, that's the end of the second half */
593 /* Terminate the second-half string */
596 /* Issue this command for each blob */
598 /* For neatness, skip whitespace before the next command */
599 while (isspace((
unsigned char) *en))
601 /* Reset for new command */
615 "SELECT pg_catalog.lo_unlink(oid) "
616 "FROM pg_catalog.pg_largeobject_metadata "
617 "WHERE oid = '%u';\n",
void set_archive_cancel_info(ArchiveHandle *AH, PGconn *conn)
#define ngettext(s, p, n)
#define ALWAYS_SECURE_SEARCH_PATH_SQL
PGconn * ConnectDatabase(const char *dbname, const char *connection_string, const char *pghost, const char *pgport, const char *pguser, trivalue prompt_password, bool fail_on_error, const char *progname, const char **connstr, int *server_version, char *password, char *override_dbname)
#define fprintf(file, fmt, msg)
int PQcancel(PGcancel *cancel, char *errbuf, int errbufsize)
int PQserverVersion(const PGconn *conn)
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
int PQconnectionUsedPassword(const PGconn *conn)
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
char * PQpass(const PGconn *conn)
void PQfinish(PGconn *conn)
PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg)
char * PQerrorMessage(const PGconn *conn)
int PQputCopyEnd(PGconn *conn, const char *errormsg)
int PQputCopyData(PGconn *conn, const char *buffer, int nbytes)
PGresult * PQexec(PGconn *conn, const char *query)
void * pg_malloc(size_t size)
char * pg_strdup(const char *in)
Assert(PointerIsAligned(start, uint64))
#define pg_log_error(...)
#define pg_log_error_detail(...)
void warn_or_exit_horribly(ArchiveHandle *AH, const char *fmt,...)
TocEntry * getTocEntryByDumpId(ArchiveHandle *AH, DumpId id)
int ahprintf(ArchiveHandle *AH, const char *fmt,...)
void ExecuteSqlStatement(Archive *AHX, const char *query)
PGconn * GetConnection(Archive *AHX)
void IssueACLPerBlob(ArchiveHandle *AH, TocEntry *te)
void EndDBCopyMode(Archive *AHX, const char *tocEntryTag)
static void die_on_query_failure(ArchiveHandle *AH, const char *query)
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
PGresult * ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
static void _check_database_version(ArchiveHandle *AH)
void DropLOIfExists(ArchiveHandle *AH, Oid oid)
void ReconnectToServer(ArchiveHandle *AH, const char *dbname)
void ConnectDatabaseAhx(Archive *AHX, const ConnParams *cparams, bool isReconnect)
static void ExecuteSimpleCommands(ArchiveHandle *AH, const char *buf, size_t bufLen)
void StartTransaction(Archive *AHX)
void IssueCommandPerBlob(ArchiveHandle *AH, TocEntry *te, const char *cmdBegin, const char *cmdEnd)
int ExecuteSqlCommandBuf(Archive *AHX, const char *buf, size_t bufLen)
static void ExecuteSqlCommand(ArchiveHandle *AH, const char *qry, const char *desc)
static void notice_processor(void *arg, const char *message)
void DisconnectDatabase(Archive *AHX)
void CommitTransaction(Archive *AHX)
#define pg_log_warning(...)
PQExpBuffer createPQExpBuffer(void)
void resetPQExpBuffer(PQExpBuffer str)
void appendPQExpBufferChar(PQExpBuffer str, char ch)
char * simple_prompt(const char *prompt, bool echo)
PGcancel *volatile connCancel
ArchiverOutput outputKind
char * archiveRemoteVersion