From c84ba6638eda656efc6ffdf279bc66fbdcc36c9d Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Tue, 4 Apr 2000 05:22:46 +0000 Subject: [PATCH] Add a check to pg_dump to see whether backend is same version as pg_dump. If not, abort by default. Abort can be prevented by using -i or --ignore-version switch. --- doc/src/sgml/ref/pg_dump.sgml | 20 +++++++++++-- src/bin/pg_dump/pg_dump.c | 56 +++++++++++++++++++++++++++++++---- 2 files changed, 69 insertions(+), 7 deletions(-) diff --git a/doc/src/sgml/ref/pg_dump.sgml b/doc/src/sgml/ref/pg_dump.sgml index f659f2009f..14a986d6a4 100644 --- a/doc/src/sgml/ref/pg_dump.sgml +++ b/doc/src/sgml/ref/pg_dump.sgml @@ -1,5 +1,5 @@ @@ -26,7 +26,7 @@ Postgres documentation pg_dump [ dbname ] pg_dump [ -h host ] [ -p port ] [ -t table ] - [ -a ] [ -c ] [ -d ] [ -D ] [ -n ] [ -N ] + [ -a ] [ -c ] [ -d ] [ -D ] [ -i ] [ -n ] [ -N ] [ -o ] [ -s ] [ -u ] [ -v ] [ -x ] [ dbname ] @@ -92,6 +92,22 @@ pg_dump [ -h host ] [ -p + + -i + + + Ignore version mismatch between pg_dump + and the database server. Since pg_dump + knows a great deal about system catalogs, any given version of + pg_dump is only intended to work with + the corresponding release of the database server. Use this option + if you need to override the version check (and if + pg_dump then fails, don't + say you weren't warned). + + + + -n diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index 5bf495e5b0..1ca5072836 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -22,7 +22,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.144 2000/02/07 16:30:58 wieck Exp $ + * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.145 2000/04/04 05:22:46 tgl Exp $ * * Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb * @@ -139,6 +139,7 @@ help(const char *progname) " -d, --inserts dump data as INSERT, rather than COPY, commands\n" " -D, --attribute-inserts dump data as INSERT commands with attribute names\n" " -h, --host server host name\n" + " -i, --ignore-version proceed when database version != pg_dump version\n" " -n, --no-quotes suppress most quotes around identifiers\n" " -N, --quotes enable most quotes around identifiers\n" " -o, --oids dump object ids (oids)\n" @@ -156,6 +157,7 @@ help(const char *progname) " -d dump data as INSERT, rather than COPY, commands\n" " -D dump data as INSERT commands with attribute names\n" " -h server host name\n" + " -i proceed when database version != pg_dump version\n" " -n suppress most quotes around identifiers\n" " -N enable most quotes around identifiers\n" " -o dump object ids (oids)\n" @@ -533,6 +535,42 @@ prompt_for_password(char *username, char *password) } +static void +check_database_version (bool ignoreVersion) +{ + PGresult *res; + const char *dbversion; + const char *myversion = "PostgreSQL " PG_RELEASE "." PG_VERSION; + int myversionlen = strlen(myversion); + + res = PQexec(g_conn, "SELECT version()"); + if (!res || + PQresultStatus(res) != PGRES_TUPLES_OK || + PQntuples(res) != 1) + { + fprintf(stderr, "check_database_version(): command failed. Explanation from backend: '%s'.\n", PQerrorMessage(g_conn)); + exit_nicely(g_conn); + } + dbversion = PQgetvalue(res, 0, 0); + if (strncmp(dbversion, myversion, myversionlen) != 0) + { + fprintf(stderr, "Database version: %s\npg_dump version: %s\n", + dbversion, PG_RELEASE "." PG_VERSION); + if (ignoreVersion) + { + fprintf(stderr, "Proceeding despite version mismatch.\n"); + } + else + { + fprintf(stderr, "Aborting because of version mismatch.\n" + "Use --ignore-version if you think it's safe to proceed anyway.\n"); + exit_nicely(g_conn); + } + } + PQclear(res); +} + + int main(int argc, char **argv) { @@ -551,6 +589,7 @@ main(int argc, char **argv) char username[100]; char password[100]; bool use_password = false; + bool ignore_version = false; #ifdef HAVE_GETOPT_LONG static struct option long_options[] = { @@ -559,6 +598,7 @@ main(int argc, char **argv) {"inserts",no_argument, NULL, 'd'}, {"attribute-inserts", no_argument, NULL, 'D'}, {"host", required_argument, NULL, 'h'}, + {"ignore-version", no_argument, NULL, 'i'}, {"no-quotes", no_argument, NULL, 'n'}, {"quotes", no_argument, NULL, 'N'}, {"oids", no_argument, NULL, 'o'}, @@ -591,9 +631,9 @@ main(int argc, char **argv) #ifdef HAVE_GETOPT_LONG - while ((c = getopt_long(argc, argv, "acdDf:h:nNop:st:uvxzV?", long_options, &optindex)) != -1) + while ((c = getopt_long(argc, argv, "acdDf:h:inNop:st:uvxzV?", long_options, &optindex)) != -1) #else - while ((c = getopt(argc, argv, "acdDf:h:nNop:st:uvxzV?-")) != -1) + while ((c = getopt(argc, argv, "acdDf:h:inNop:st:uvxzV?-")) != -1) #endif { switch (c) @@ -614,11 +654,14 @@ main(int argc, char **argv) attrNames = true; break; case 'f': - filename = optarg; - break; + filename = optarg; + break; case 'h': /* server host */ pghost = optarg; break; + case 'i': /* ignore database version mismatch */ + ignore_version = true; + break; case 'n': /* Do not force double-quotes on * identifiers */ force_quotes = false; @@ -773,6 +816,9 @@ main(int argc, char **argv) exit_nicely(g_conn); } + /* check for version mismatch */ + check_database_version(ignore_version); + /* * Start serializable transaction to dump consistent data */