From 1bcaf267b389a0e05fdfce2fca5c0a192a8cbb51 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Thu, 18 Oct 2012 00:43:45 +0100 Subject: [PATCH] Stop database processing if library version doesn't match the binary Actually this leaves out the case of the SQL schema not consistent with the library/binary installed, and this is a relatively likely case: the user has run "make install" but the repack schema was already loaded from an older version. --- bin/pg_repack.c | 40 +++++++++++++++++++++++++++++++++++++++- doc/pg_repack.rst | 9 +++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/bin/pg_repack.c b/bin/pg_repack.c index 98ce7b5..afcb030 100644 --- a/bin/pg_repack.c +++ b/bin/pg_repack.c @@ -229,7 +229,7 @@ getoid(PGresult *res, int row, int col) static bool repack_one_database(const char *orderby, const char *table, char *errbuf, size_t errsize) { - bool ret = true; + bool ret = false; PGresult *res; int i; int num; @@ -239,6 +239,43 @@ repack_one_database(const char *orderby, const char *table, char *errbuf, size_t reconnect(ERROR); + /* Query the extension version. Exit if no match */ + res = execute_elevel("select repack.version()", 0, NULL, DEBUG2); + if (PQresultStatus(res) == PGRES_TUPLES_OK) + { + const char *libver; + char buf[64]; + + /* the string is something like "pg_repack 1.1.7" */ + libver = getstr(res, 0, 0); + snprintf(buf, sizeof(buf), "%s %s", PROGRAM_NAME, PROGRAM_VERSION); + if (0 != strcmp(buf, libver)) + { + if (errbuf) + snprintf(errbuf, errsize, + "program '%s' does not match database library '%s'", + buf, libver); + goto cleanup; + } + } + else + { + if (sqlstate_equals(res, SQLSTATE_INVALID_SCHEMA_NAME)) + { + /* Schema repack does not exist. Skip the database. */ + if (errbuf) + snprintf(errbuf, errsize, + "%s is not installed in the database", PROGRAM_NAME); + } + else + { + /* Return the error message otherwise */ + if (errbuf) + snprintf(errbuf, errsize, "%s", PQerrorMessage(connection)); + } + goto cleanup; + } + /* Disable statement timeout. */ command("SET statement_timeout = 0", 0, NULL); @@ -346,6 +383,7 @@ repack_one_database(const char *orderby, const char *table, char *errbuf, size_t repack_one_table(&table, orderby); } + ret = true; cleanup: PQclear(res); diff --git a/doc/pg_repack.rst b/doc/pg_repack.rst index 7820593..6682f3c 100644 --- a/doc/pg_repack.rst +++ b/doc/pg_repack.rst @@ -281,6 +281,15 @@ ERROR: pg_repack is not installed Do register pg_repack to the database. +ERROR: program 'pg_repack V1' does not match database library 'pg_repack V2' + There is a mismatch between the ``pg_repack`` binary and the database + library (``.so`` or ``.dll``). + + The mismatch could be due to the wrong binary in the ``$PATH`` or the + wrong database being addressed. Check the program directory and the + database; if they are what expected you may need to repeat pg_repack + installation. + ERROR: relation "table" has no primary key The target table doesn't have PRIMARY KEY.