Skip to content

Commit 7ee27d4

Browse files
committed
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.
1 parent ef270fb commit 7ee27d4

File tree

1 file changed

+23
-34
lines changed

1 file changed

+23
-34
lines changed

src/bin/pg_dump/pg_backup_db.c

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Implements the basic DB functions used by the archiver.
66
*
77
* IDENTIFICATION
8-
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.79 2008/04/13 03:49:22 tgl Exp $
8+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.80 2008/08/16 02:25:06 tgl Exp $
99
*
1010
*-------------------------------------------------------------------------
1111
*/
@@ -266,27 +266,31 @@ notice_processor(void *arg, const char *message)
266266

267267
/* Public interface */
268268
/* Convenience function to send a query. Monitors result to handle COPY statements */
269-
static int
270-
ExecuteSqlCommand(ArchiveHandle *AH, PQExpBuffer qry, char *desc)
269+
static void
270+
ExecuteSqlCommand(ArchiveHandle *AH, const char *qry, const char *desc)
271271
{
272272
PGconn *conn = AH->connection;
273273
PGresult *res;
274274
char errStmt[DB_MAX_ERR_STMT];
275275

276-
/* fprintf(stderr, "Executing: '%s'\n\n", qry->data); */
277-
res = PQexec(conn, qry->data);
278-
if (!res)
279-
die_horribly(AH, modulename, "%s: no result from server\n", desc);
276+
#ifdef NOT_USED
277+
fprintf(stderr, "Executing: '%s'\n\n", qry);
278+
#endif
279+
res = PQexec(conn, qry);
280280

281-
if (PQresultStatus(res) != PGRES_COMMAND_OK && PQresultStatus(res) != PGRES_TUPLES_OK)
281+
switch (PQresultStatus(res))
282282
{
283-
if (PQresultStatus(res) == PGRES_COPY_IN)
284-
{
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 */
285289
AH->pgCopyIn = true;
286-
}
287-
else
288-
{
289-
strncpy(errStmt, qry->data, DB_MAX_ERR_STMT);
290+
break;
291+
default:
292+
/* trouble */
293+
strncpy(errStmt, qry, DB_MAX_ERR_STMT);
290294
if (errStmt[DB_MAX_ERR_STMT - 1] != '\0')
291295
{
292296
errStmt[DB_MAX_ERR_STMT - 4] = '.';
@@ -295,14 +299,11 @@ ExecuteSqlCommand(ArchiveHandle *AH, PQExpBuffer qry, char *desc)
295299
errStmt[DB_MAX_ERR_STMT - 1] = '\0';
296300
}
297301
warn_or_die_horribly(AH, modulename, "%s: %s Command was: %s\n",
298-
desc, PQerrorMessage(AH->connection),
299-
errStmt);
300-
}
302+
desc, PQerrorMessage(conn), errStmt);
303+
break;
301304
}
302305

303306
PQclear(res);
304-
305-
return strlen(qry->data);
306307
}
307308

308309
/*
@@ -427,7 +428,7 @@ _sendSQLLine(ArchiveHandle *AH, char *qry, char *eos)
427428
* the buffer.
428429
*/
429430
appendPQExpBufferChar(AH->sqlBuf, ';'); /* inessential */
430-
ExecuteSqlCommand(AH, AH->sqlBuf,
431+
ExecuteSqlCommand(AH, AH->sqlBuf->data,
431432
"could not execute query");
432433
resetPQExpBuffer(AH->sqlBuf);
433434
AH->sqlparse.lastChar = '\0';
@@ -626,25 +627,13 @@ ExecuteSqlCommandBuf(ArchiveHandle *AH, void *qryv, size_t bufLen)
626627
void
627628
StartTransaction(ArchiveHandle *AH)
628629
{
629-
PQExpBuffer qry = createPQExpBuffer();
630-
631-
appendPQExpBuffer(qry, "BEGIN");
632-
633-
ExecuteSqlCommand(AH, qry, "could not start database transaction");
634-
635-
destroyPQExpBuffer(qry);
630+
ExecuteSqlCommand(AH, "BEGIN", "could not start database transaction");
636631
}
637632

638633
void
639634
CommitTransaction(ArchiveHandle *AH)
640635
{
641-
PQExpBuffer qry = createPQExpBuffer();
642-
643-
appendPQExpBuffer(qry, "COMMIT");
644-
645-
ExecuteSqlCommand(AH, qry, "could not commit database transaction");
646-
647-
destroyPQExpBuffer(qry);
636+
ExecuteSqlCommand(AH, "COMMIT", "could not commit database transaction");
648637
}
649638

650639
static bool

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy