mirror of
				https://github.com/postgres/postgres.git
				synced 2025-11-03 09:13:20 +03:00 
			
		
		
		
	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:
		@@ -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.75 2006/10/04 00:30:05 momjian Exp $
 | 
					 *	  $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.75.2.1 2008/08/16 02:25:17 tgl Exp $
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 *-------------------------------------------------------------------------
 | 
					 *-------------------------------------------------------------------------
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
@@ -280,27 +280,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] = '.';
 | 
				
			||||||
@@ -309,14 +313,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);
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*
 | 
					/*
 | 
				
			||||||
@@ -441,7 +442,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';
 | 
				
			||||||
@@ -640,25 +641,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
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user