Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c84ba66

Browse files
committed
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.
1 parent edd4131 commit c84ba66

File tree

2 files changed

+69
-7
lines changed

2 files changed

+69
-7
lines changed

doc/src/sgml/ref/pg_dump.sgml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/ref/pg_dump.sgml,v 1.15 2000/03/27 17:14:43 thomas Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/ref/pg_dump.sgml,v 1.16 2000/04/04 05:22:45 tgl Exp $
33
Postgres documentation
44
-->
55

@@ -26,7 +26,7 @@ Postgres documentation
2626
pg_dump [ <replaceable class="parameter">dbname</replaceable> ]
2727
pg_dump [ -h <replaceable class="parameter">host</replaceable> ] [ -p <replaceable class="parameter">port</replaceable> ]
2828
[ -t <replaceable class="parameter">table</replaceable> ]
29-
[ -a ] [ -c ] [ -d ] [ -D ] [ -n ] [ -N ]
29+
[ -a ] [ -c ] [ -d ] [ -D ] [ -i ] [ -n ] [ -N ]
3030
[ -o ] [ -s ] [ -u ] [ -v ] [ -x ]
3131
[ <replaceable class="parameter">dbname</replaceable> ]
3232
</synopsis>
@@ -92,6 +92,22 @@ pg_dump [ -h <replaceable class="parameter">host</replaceable> ] [ -p <replaceab
9292
</listitem>
9393
</varlistentry>
9494

95+
<varlistentry>
96+
<term>-i</term>
97+
<listitem>
98+
<para>
99+
Ignore version mismatch between <application>pg_dump</application>
100+
and the database server. Since <application>pg_dump</application>
101+
knows a great deal about system catalogs, any given version of
102+
<application>pg_dump</application> is only intended to work with
103+
the corresponding release of the database server. Use this option
104+
if you need to override the version check (and if
105+
<application>pg_dump</application> then fails, don't
106+
say you weren't warned).
107+
</para>
108+
</listitem>
109+
</varlistentry>
110+
95111
<varlistentry>
96112
<term>-n</term>
97113
<listitem>

src/bin/pg_dump/pg_dump.c

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*
2323
*
2424
* IDENTIFICATION
25-
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.144 2000/02/07 16:30:58 wieck Exp $
25+
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.145 2000/04/04 05:22:46 tgl Exp $
2626
*
2727
* Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb
2828
*
@@ -139,6 +139,7 @@ help(const char *progname)
139139
" -d, --inserts dump data as INSERT, rather than COPY, commands\n"
140140
" -D, --attribute-inserts dump data as INSERT commands with attribute names\n"
141141
" -h, --host <hostname> server host name\n"
142+
" -i, --ignore-version proceed when database version != pg_dump version\n"
142143
" -n, --no-quotes suppress most quotes around identifiers\n"
143144
" -N, --quotes enable most quotes around identifiers\n"
144145
" -o, --oids dump object ids (oids)\n"
@@ -156,6 +157,7 @@ help(const char *progname)
156157
" -d dump data as INSERT, rather than COPY, commands\n"
157158
" -D dump data as INSERT commands with attribute names\n"
158159
" -h <hostname> server host name\n"
160+
" -i proceed when database version != pg_dump version\n"
159161
" -n suppress most quotes around identifiers\n"
160162
" -N enable most quotes around identifiers\n"
161163
" -o dump object ids (oids)\n"
@@ -533,6 +535,42 @@ prompt_for_password(char *username, char *password)
533535
}
534536

535537

538+
static void
539+
check_database_version (bool ignoreVersion)
540+
{
541+
PGresult *res;
542+
const char *dbversion;
543+
const char *myversion = "PostgreSQL " PG_RELEASE "." PG_VERSION;
544+
int myversionlen = strlen(myversion);
545+
546+
res = PQexec(g_conn, "SELECT version()");
547+
if (!res ||
548+
PQresultStatus(res) != PGRES_TUPLES_OK ||
549+
PQntuples(res) != 1)
550+
{
551+
fprintf(stderr, "check_database_version(): command failed. Explanation from backend: '%s'.\n", PQerrorMessage(g_conn));
552+
exit_nicely(g_conn);
553+
}
554+
dbversion = PQgetvalue(res, 0, 0);
555+
if (strncmp(dbversion, myversion, myversionlen) != 0)
556+
{
557+
fprintf(stderr, "Database version: %s\npg_dump version: %s\n",
558+
dbversion, PG_RELEASE "." PG_VERSION);
559+
if (ignoreVersion)
560+
{
561+
fprintf(stderr, "Proceeding despite version mismatch.\n");
562+
}
563+
else
564+
{
565+
fprintf(stderr, "Aborting because of version mismatch.\n"
566+
"Use --ignore-version if you think it's safe to proceed anyway.\n");
567+
exit_nicely(g_conn);
568+
}
569+
}
570+
PQclear(res);
571+
}
572+
573+
536574
int
537575
main(int argc, char **argv)
538576
{
@@ -551,6 +589,7 @@ main(int argc, char **argv)
551589
char username[100];
552590
char password[100];
553591
bool use_password = false;
592+
bool ignore_version = false;
554593

555594
#ifdef HAVE_GETOPT_LONG
556595
static struct option long_options[] = {
@@ -559,6 +598,7 @@ main(int argc, char **argv)
559598
{"inserts",no_argument, NULL, 'd'},
560599
{"attribute-inserts", no_argument, NULL, 'D'},
561600
{"host", required_argument, NULL, 'h'},
601+
{"ignore-version", no_argument, NULL, 'i'},
562602
{"no-quotes", no_argument, NULL, 'n'},
563603
{"quotes", no_argument, NULL, 'N'},
564604
{"oids", no_argument, NULL, 'o'},
@@ -591,9 +631,9 @@ main(int argc, char **argv)
591631

592632

593633
#ifdef HAVE_GETOPT_LONG
594-
while ((c = getopt_long(argc, argv, "acdDf:h:nNop:st:uvxzV?", long_options, &optindex)) != -1)
634+
while ((c = getopt_long(argc, argv, "acdDf:h:inNop:st:uvxzV?", long_options, &optindex)) != -1)
595635
#else
596-
while ((c = getopt(argc, argv, "acdDf:h:nNop:st:uvxzV?-")) != -1)
636+
while ((c = getopt(argc, argv, "acdDf:h:inNop:st:uvxzV?-")) != -1)
597637
#endif
598638
{
599639
switch (c)
@@ -614,11 +654,14 @@ main(int argc, char **argv)
614654
attrNames = true;
615655
break;
616656
case 'f':
617-
filename = optarg;
618-
break;
657+
filename = optarg;
658+
break;
619659
case 'h': /* server host */
620660
pghost = optarg;
621661
break;
662+
case 'i': /* ignore database version mismatch */
663+
ignore_version = true;
664+
break;
622665
case 'n': /* Do not force double-quotes on
623666
* identifiers */
624667
force_quotes = false;
@@ -773,6 +816,9 @@ main(int argc, char **argv)
773816
exit_nicely(g_conn);
774817
}
775818

819+
/* check for version mismatch */
820+
check_database_version(ignore_version);
821+
776822
/*
777823
* Start serializable transaction to dump consistent data
778824
*/

0 commit comments

Comments
 (0)