Skip to content

Commit f7168bd

Browse files
committed
They are two different problems; the TOC entry is important for any
multiline command or to rerun the command easily later. Whereas displaying the failed SQL command is a matter of fixing the error messages. The latter is complicated by failed COPY commands which, with die-on-errors off, results in the data being processed as a command, so dumping the command will dump all of the data. In the case of long commands, should the whole command be dumped? eg. (eg. several pages of function definition). In the case of the COPY command, I'm not sure what to do. Obviously, it would be best to avoid sending the data, but the data and command are combined (from memory). Also, the 'data' may be in the form of INSERT statements. Attached patch produces the first 125 chars of the command: pg_restore: [archiver (db)] Error while PROCESSING TOC: pg_restore: [archiver (db)] Error from TOC Entry 26; 1255 16449270 FUNCTION plpgsql_call_handler() pjw pg_restore: [archiver (db)] could not execute query: ERROR: function "plpgsql_call_handler" already exists with same argument types Command was: CREATE FUNCTION plpgsql_call_handler() RETURNS language_handler AS '/var/lib/pgsql-8.0b1/lib/plpgsql', 'plpgsql_call_han... pg_restore: [archiver (db)] Error from TOC Entry 27; 1255 16449271 FUNCTION plpgsql_validator(oid) pjw pg_restore: [archiver (db)] could not execute query: ERROR: function "plpgsql_validator" already exists with same argument types Command was: CREATE FUNCTION plpgsql_validator(oid) RETURNS void AS '/var/lib/pgsql-8.0b1/lib/plpgsql', 'plpgsql_validator' LANGU... Philip Warner
1 parent b43fd16 commit f7168bd

File tree

3 files changed

+84
-5
lines changed

3 files changed

+84
-5
lines changed

src/bin/pg_dump/pg_backup_archiver.c

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
*
1717
* IDENTIFICATION
18-
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.93 2004/08/20 04:20:22 momjian Exp $
18+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.94 2004/08/20 20:00:34 momjian Exp $
1919
*
2020
*-------------------------------------------------------------------------
2121
*/
@@ -137,6 +137,7 @@ RestoreArchive(Archive *AHX, RestoreOptions *ropt)
137137
bool defnDumped;
138138

139139
AH->ropt = ropt;
140+
AH->stage = STAGE_INITIALIZING;
140141

141142
/*
142143
* Check for nonsensical option combinations.
@@ -166,6 +167,8 @@ RestoreArchive(Archive *AHX, RestoreOptions *ropt)
166167
ConnectDatabase(AHX, ropt->dbname,
167168
ropt->pghost, ropt->pgport, ropt->username,
168169
ropt->requirePassword, ropt->ignoreVersion);
170+
/* If we're talking to the DB directly, don't send comments since they obscure SQL when displaying errors */
171+
AH->noTocComments = 1;
169172
}
170173

171174
/*
@@ -211,12 +214,16 @@ RestoreArchive(Archive *AHX, RestoreOptions *ropt)
211214
*/
212215
_doSetFixedOutputState(AH);
213216

217+
AH->stage = STAGE_PROCESSING;
218+
214219
/*
215220
* Drop the items at the start, in reverse order
216221
*/
217222
if (ropt->dropSchema)
218223
{
219224
te = AH->toc->prev;
225+
AH->currentTE = te;
226+
220227
while (te != AH->toc)
221228
{
222229
reqs = _tocEntryRequired(te, ropt, false);
@@ -240,6 +247,8 @@ RestoreArchive(Archive *AHX, RestoreOptions *ropt)
240247
te = AH->toc->next;
241248
while (te != AH->toc)
242249
{
250+
AH->currentTE = te;
251+
243252
/* Work out what, if anything, we want from this entry */
244253
reqs = _tocEntryRequired(te, ropt, false);
245254

@@ -375,6 +384,8 @@ RestoreArchive(Archive *AHX, RestoreOptions *ropt)
375384
te = AH->toc->next;
376385
while (te != AH->toc)
377386
{
387+
AH->currentTE = te;
388+
378389
/* Work out what, if anything, we want from this entry */
379390
reqs = _tocEntryRequired(te, ropt, true);
380391

@@ -391,6 +402,8 @@ RestoreArchive(Archive *AHX, RestoreOptions *ropt)
391402
/*
392403
* Clean up & we're done.
393404
*/
405+
AH->stage = STAGE_FINALIZING;
406+
394407
if (ropt->filename || ropt->compression)
395408
ResetOutput(AH, sav);
396409

@@ -1227,6 +1240,39 @@ warn_or_die_horribly(ArchiveHandle *AH,
12271240
const char *modulename, const char *fmt, ...)
12281241
{
12291242
va_list ap;
1243+
1244+
switch(AH->stage) {
1245+
1246+
case STAGE_NONE:
1247+
/* Do nothing special */
1248+
break;
1249+
1250+
case STAGE_INITIALIZING:
1251+
if (AH->stage != AH->lastErrorStage) {
1252+
write_msg(modulename, "Error while INITIALIZING:\n");
1253+
}
1254+
break;
1255+
1256+
case STAGE_PROCESSING:
1257+
if (AH->stage != AH->lastErrorStage) {
1258+
write_msg(modulename, "Error while PROCESSING TOC:\n");
1259+
}
1260+
break;
1261+
1262+
case STAGE_FINALIZING:
1263+
if (AH->stage != AH->lastErrorStage) {
1264+
write_msg(modulename, "Error while FINALIZING:\n");
1265+
}
1266+
break;
1267+
}
1268+
if (AH->currentTE != NULL && AH->currentTE != AH->lastErrorTE) {
1269+
write_msg(modulename, "Error from TOC Entry %d; %u %u %s %s %s\n", AH->currentTE->dumpId,
1270+
AH->currentTE->catalogId.tableoid, AH->currentTE->catalogId.oid,
1271+
AH->currentTE->desc, AH->currentTE->tag, AH->currentTE->owner);
1272+
}
1273+
AH->lastErrorStage = AH->stage;
1274+
AH->lastErrorTE = AH->currentTE;
1275+
12301276
va_start(ap, fmt);
12311277
if (AH->public.exit_on_error)
12321278
{
@@ -2026,6 +2072,9 @@ _doSetFixedOutputState(ArchiveHandle *AH)
20262072
/* Make sure function checking is disabled */
20272073
ahprintf(AH, "SET check_function_bodies = false;\n");
20282074

2075+
/* Avoid annoying notices etc */
2076+
ahprintf(AH, "SET client_min_messages = warning;\n");
2077+
20292078
ahprintf(AH, "\n");
20302079
}
20312080

@@ -2317,6 +2366,9 @@ _printTocEntry(ArchiveHandle *AH, TocEntry *te, RestoreOptions *ropt, bool isDat
23172366
return;
23182367
}
23192368

2369+
if (AH->noTocComments)
2370+
return;
2371+
23202372
/*
23212373
* Avoid dumping the public schema, as it will already be created ...
23222374
* unless we are using --clean mode, in which case it's been deleted

src/bin/pg_dump/pg_backup_archiver.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*
1818
*
1919
* IDENTIFICATION
20-
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.h,v 1.59 2004/08/20 16:07:15 momjian Exp $
20+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.h,v 1.60 2004/08/20 20:00:34 momjian Exp $
2121
*
2222
*-------------------------------------------------------------------------
2323
*/
@@ -152,6 +152,14 @@ typedef struct
152152
PQExpBuffer tagBuf;
153153
} sqlparseInfo;
154154

155+
typedef enum
156+
{
157+
STAGE_NONE = 0,
158+
STAGE_INITIALIZING,
159+
STAGE_PROCESSING,
160+
STAGE_FINALIZING
161+
} ArchiverStage;
162+
155163
typedef struct _archiveHandle
156164
{
157165
Archive public; /* Public part of archive */
@@ -254,6 +262,12 @@ typedef struct _archiveHandle
254262
void *lo_buf;
255263
size_t lo_buf_used;
256264
size_t lo_buf_size;
265+
266+
int noTocComments;
267+
ArchiverStage stage;
268+
ArchiverStage lastErrorStage;
269+
struct _tocEntry *currentTE;
270+
struct _tocEntry *lastErrorTE;
257271
} ArchiveHandle;
258272

259273
typedef struct _tocEntry

src/bin/pg_dump/pg_backup_db.c

Lines changed: 16 additions & 3 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.54 2004/08/20 16:07:15 momjian Exp $
8+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.55 2004/08/20 20:00:34 momjian Exp $
99
*
1010
*-------------------------------------------------------------------------
1111
*/
@@ -40,6 +40,8 @@ static char *_sendCopyLine(ArchiveHandle *AH, char *qry, char *eos);
4040
static int _isIdentChar(char c);
4141
static int _isDQChar(char c, int atStart);
4242

43+
#define DB_MAX_ERR_STMT 128
44+
4345
static int
4446
_parse_version(ArchiveHandle *AH, const char *versionString)
4547
{
@@ -302,6 +304,7 @@ static int
302304
_executeSqlCommand(ArchiveHandle *AH, PGconn *conn, PQExpBuffer qry, char *desc)
303305
{
304306
PGresult *res;
307+
char errStmt[DB_MAX_ERR_STMT];
305308

306309
/* fprintf(stderr, "Executing: '%s'\n\n", qry->data); */
307310
res = PQexec(conn, qry->data);
@@ -318,8 +321,18 @@ _executeSqlCommand(ArchiveHandle *AH, PGconn *conn, PQExpBuffer qry, char *desc)
318321
AH->pgCopyIn = 1;
319322
}
320323
else
321-
warn_or_die_horribly(AH, modulename, "%s: %s",
322-
desc, PQerrorMessage(AH->connection));
324+
{
325+
strncpy(errStmt, qry->data, DB_MAX_ERR_STMT);
326+
if (errStmt[DB_MAX_ERR_STMT-1] != '\0') {
327+
errStmt[DB_MAX_ERR_STMT-4] = '.';
328+
errStmt[DB_MAX_ERR_STMT-3] = '.';
329+
errStmt[DB_MAX_ERR_STMT-2] = '.';
330+
errStmt[DB_MAX_ERR_STMT-1] = '\0';
331+
}
332+
warn_or_die_horribly(AH, modulename, "%s: %s Command was: %s\n",
333+
desc, PQerrorMessage(AH->connection),
334+
errStmt);
335+
}
323336
}
324337

325338
PQclear(res);

0 commit comments

Comments
 (0)