From ec7c4c1b66d1ed9eba5ec22fdb7fe4618ece4d25 Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Thu, 22 Apr 2004 02:39:10 +0000 Subject: [PATCH] Please find attached a small patch so that "pg_restore" ignores some sql errors. This is the second submission, which integrates Tom comments about localisation and exit code. I also added some comments about one sql command which is not ignored. Fabien COELHO --- src/bin/pg_dump/pg_backup.h | 7 ++++++- src/bin/pg_dump/pg_backup_archiver.c | 35 ++++++++++++++++++++++++++++++----- src/bin/pg_dump/pg_backup_archiver.h | 3 ++- src/bin/pg_dump/pg_backup_db.c | 6 +++--- src/bin/pg_dump/pg_restore.c | 18 ++++++++++++++++-- 5 files changed, 57 insertions(+), 12 deletions(-) diff --git a/src/bin/pg_dump/pg_backup.h b/src/bin/pg_dump/pg_backup.h index 6109c9befd..317646252c 100644 --- a/src/bin/pg_dump/pg_backup.h +++ b/src/bin/pg_dump/pg_backup.h @@ -15,7 +15,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup.h,v 1.29 2004/03/24 03:06:08 momjian Exp $ + * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup.h,v 1.30 2004/04/22 02:39:09 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -57,6 +57,11 @@ typedef struct _Archive int remoteVersion; int minRemoteVersion; int maxRemoteVersion; + + /* error handling */ + bool die_on_errors; /* whether to die on sql errors... */ + int n_errors; /* number of errors (if no die) */ + /* The rest is private */ } Archive; diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c index 57b808b7eb..d4f01f4000 100644 --- a/src/bin/pg_dump/pg_backup_archiver.c +++ b/src/bin/pg_dump/pg_backup_archiver.c @@ -15,7 +15,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.85 2004/03/24 03:06:08 momjian Exp $ + * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.86 2004/04/22 02:39:10 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -1197,6 +1197,24 @@ die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt,...) va_end(ap); } +/* on some error, we may decide to go on... */ +void +warn_or_die_horribly(ArchiveHandle *AH, + const char *modulename, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + if (AH->public.die_on_errors) + { + _die_horribly(AH, modulename, fmt, ap); + } + else + { + _write_msg(modulename, fmt, ap); + AH->public.n_errors++; + } + va_end(ap); +} static void _moveAfter(ArchiveHandle *AH, TocEntry *pos, TocEntry *te) @@ -1651,6 +1669,10 @@ _allocAH(const char *FileSpec, const ArchiveFormat fmt, die_horribly(AH, modulename, "unrecognized file format \"%d\"\n", fmt); } + /* sql error handling */ + AH->public.die_on_errors = true; + AH->public.n_errors = 0; + return AH; } @@ -2011,6 +2033,7 @@ _doSetSessionAuth(ArchiveHandle *AH, const char *user) res = PQexec(AH->connection, cmd->data); if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) + /* NOT warn_or_die_horribly... use -O instead to skip this. */ die_horribly(AH, modulename, "could not set session user to \"%s\": %s", user, PQerrorMessage(AH->connection)); @@ -2042,8 +2065,9 @@ _doSetWithOids(ArchiveHandle *AH, const bool withOids) res = PQexec(AH->connection, cmd->data); if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) - die_horribly(AH, modulename, "could not set default_with_oids: %s", - PQerrorMessage(AH->connection)); + warn_or_die_horribly(AH, modulename, + "could not set default_with_oids: %s", + PQerrorMessage(AH->connection)); PQclear(res); } @@ -2181,8 +2205,9 @@ _selectOutputSchema(ArchiveHandle *AH, const char *schemaName) res = PQexec(AH->connection, qry->data); if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) - die_horribly(AH, modulename, "could not set search_path to \"%s\": %s", - schemaName, PQerrorMessage(AH->connection)); + warn_or_die_horribly(AH, modulename, + "could not set search_path to \"%s\": %s", + schemaName, PQerrorMessage(AH->connection)); PQclear(res); } diff --git a/src/bin/pg_dump/pg_backup_archiver.h b/src/bin/pg_dump/pg_backup_archiver.h index 2de300d3b2..7466feab0b 100644 --- a/src/bin/pg_dump/pg_backup_archiver.h +++ b/src/bin/pg_dump/pg_backup_archiver.h @@ -17,7 +17,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.h,v 1.57 2004/03/24 03:06:08 momjian Exp $ + * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.h,v 1.58 2004/04/22 02:39:10 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -281,6 +281,7 @@ typedef struct _tocEntry extern const char *progname; extern void die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt,...) __attribute__((format(printf, 3, 4))); +extern void warn_or_die_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt,...) __attribute__((format(printf, 3, 4))); extern void write_msg(const char *modulename, const char *fmt,...) __attribute__((format(printf, 2, 3))); extern void WriteTOC(ArchiveHandle *AH); diff --git a/src/bin/pg_dump/pg_backup_db.c b/src/bin/pg_dump/pg_backup_db.c index b2677e8b3a..df2f8af3ff 100644 --- a/src/bin/pg_dump/pg_backup_db.c +++ b/src/bin/pg_dump/pg_backup_db.c @@ -5,7 +5,7 @@ * Implements the basic DB functions used by the archiver. * * IDENTIFICATION - * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.52 2004/03/03 21:28:54 tgl Exp $ + * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.53 2004/04/22 02:39:10 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -316,8 +316,8 @@ _executeSqlCommand(ArchiveHandle *AH, PGconn *conn, PQExpBuffer qry, char *desc) AH->pgCopyIn = 1; } else - die_horribly(AH, modulename, "%s: %s", - desc, PQerrorMessage(AH->connection)); + warn_or_die_horribly(AH, modulename, "%s: %s", + desc, PQerrorMessage(AH->connection)); } PQclear(res); diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c index 8e051104c3..d8dfcc4c48 100644 --- a/src/bin/pg_dump/pg_restore.c +++ b/src/bin/pg_dump/pg_restore.c @@ -34,7 +34,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/bin/pg_dump/pg_restore.c,v 1.55 2003/12/06 03:00:16 tgl Exp $ + * $PostgreSQL: pgsql/src/bin/pg_dump/pg_restore.c,v 1.56 2004/04/22 02:39:10 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -77,6 +77,7 @@ main(int argc, char **argv) { RestoreOptions *opts; int c; + int exit_code; Archive *AH; char *inputFileSpec; extern int optind; @@ -323,6 +324,11 @@ main(int argc, char **argv) /* Let the archiver know how noisy to be */ AH->verbose = opts->verbose; + /* restore keeps submitting sql commands as "pg_restore ... | psql ... " + * this behavior choice could be turned into an option. + */ + AH->die_on_errors = false; + if (opts->tocFile) SortTocFromFile(AH, opts); @@ -331,9 +337,17 @@ main(int argc, char **argv) else RestoreArchive(AH, opts); + /* done, print a summary of ignored errors */ + if (AH->n_errors) + fprintf(stderr, _("WARNING, errors ignored on restore: %d\n"), + AH->n_errors); + + /* AH may be freed in CloseArchive? */ + exit_code = AH->n_errors? 1: 0; + CloseArchive(AH); - return 0; + return exit_code; } static void -- 2.11.0