Fix pg_dump/pg_restore's ExecuteSqlCommand() to behave suitably if PQexec

returns NULL instead of a PGresult.  The former coding would fail, which
is OK, but it neglected to give you the PQerrorMessage that might tell
you why.  In the oldest branches, there was another problem: it'd sometimes
report PQerrorMessage from the wrong connection.
This commit is contained in:
Tom Lane 2008-08-16 02:25:06 +00:00
parent ef270fb9d2
commit 7ee27d49df
1 changed files with 23 additions and 34 deletions

View File

@ -5,7 +5,7 @@
* Implements the basic DB functions used by the archiver. * Implements the basic DB functions used by the archiver.
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.79 2008/04/13 03:49:22 tgl Exp $ * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.80 2008/08/16 02:25:06 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -266,27 +266,31 @@ notice_processor(void *arg, const char *message)
/* Public interface */ /* Public interface */
/* Convenience function to send a query. Monitors result to handle COPY statements */ /* Convenience function to send a query. Monitors result to handle COPY statements */
static int static void
ExecuteSqlCommand(ArchiveHandle *AH, PQExpBuffer qry, char *desc) ExecuteSqlCommand(ArchiveHandle *AH, const char *qry, const char *desc)
{ {
PGconn *conn = AH->connection; PGconn *conn = AH->connection;
PGresult *res; PGresult *res;
char errStmt[DB_MAX_ERR_STMT]; char errStmt[DB_MAX_ERR_STMT];
/* fprintf(stderr, "Executing: '%s'\n\n", qry->data); */ #ifdef NOT_USED
res = PQexec(conn, qry->data); fprintf(stderr, "Executing: '%s'\n\n", qry);
if (!res) #endif
die_horribly(AH, modulename, "%s: no result from server\n", desc); res = PQexec(conn, qry);
if (PQresultStatus(res) != PGRES_COMMAND_OK && PQresultStatus(res) != PGRES_TUPLES_OK) switch (PQresultStatus(res))
{ {
if (PQresultStatus(res) == PGRES_COPY_IN) case PGRES_COMMAND_OK:
{ case PGRES_TUPLES_OK:
/* A-OK */
break;
case PGRES_COPY_IN:
/* Assume this is an expected result */
AH->pgCopyIn = true; AH->pgCopyIn = true;
} break;
else default:
{ /* trouble */
strncpy(errStmt, qry->data, DB_MAX_ERR_STMT); strncpy(errStmt, qry, DB_MAX_ERR_STMT);
if (errStmt[DB_MAX_ERR_STMT - 1] != '\0') if (errStmt[DB_MAX_ERR_STMT - 1] != '\0')
{ {
errStmt[DB_MAX_ERR_STMT - 4] = '.'; errStmt[DB_MAX_ERR_STMT - 4] = '.';
@ -295,14 +299,11 @@ ExecuteSqlCommand(ArchiveHandle *AH, PQExpBuffer qry, char *desc)
errStmt[DB_MAX_ERR_STMT - 1] = '\0'; errStmt[DB_MAX_ERR_STMT - 1] = '\0';
} }
warn_or_die_horribly(AH, modulename, "%s: %s Command was: %s\n", warn_or_die_horribly(AH, modulename, "%s: %s Command was: %s\n",
desc, PQerrorMessage(AH->connection), desc, PQerrorMessage(conn), errStmt);
errStmt); break;
}
} }
PQclear(res); PQclear(res);
return strlen(qry->data);
} }
/* /*
@ -427,7 +428,7 @@ _sendSQLLine(ArchiveHandle *AH, char *qry, char *eos)
* the buffer. * the buffer.
*/ */
appendPQExpBufferChar(AH->sqlBuf, ';'); /* inessential */ appendPQExpBufferChar(AH->sqlBuf, ';'); /* inessential */
ExecuteSqlCommand(AH, AH->sqlBuf, ExecuteSqlCommand(AH, AH->sqlBuf->data,
"could not execute query"); "could not execute query");
resetPQExpBuffer(AH->sqlBuf); resetPQExpBuffer(AH->sqlBuf);
AH->sqlparse.lastChar = '\0'; AH->sqlparse.lastChar = '\0';
@ -626,25 +627,13 @@ ExecuteSqlCommandBuf(ArchiveHandle *AH, void *qryv, size_t bufLen)
void void
StartTransaction(ArchiveHandle *AH) StartTransaction(ArchiveHandle *AH)
{ {
PQExpBuffer qry = createPQExpBuffer(); ExecuteSqlCommand(AH, "BEGIN", "could not start database transaction");
appendPQExpBuffer(qry, "BEGIN");
ExecuteSqlCommand(AH, qry, "could not start database transaction");
destroyPQExpBuffer(qry);
} }
void void
CommitTransaction(ArchiveHandle *AH) CommitTransaction(ArchiveHandle *AH)
{ {
PQExpBuffer qry = createPQExpBuffer(); ExecuteSqlCommand(AH, "COMMIT", "could not commit database transaction");
appendPQExpBuffer(qry, "COMMIT");
ExecuteSqlCommand(AH, qry, "could not commit database transaction");
destroyPQExpBuffer(qry);
} }
static bool static bool