Skip to content

Commit 1bcaf26

Browse files
committed
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.
1 parent c43b6bd commit 1bcaf26

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

bin/pg_repack.c

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ getoid(PGresult *res, int row, int col)
229229
static bool
230230
repack_one_database(const char *orderby, const char *table, char *errbuf, size_t errsize)
231231
{
232-
bool ret = true;
232+
bool ret = false;
233233
PGresult *res;
234234
int i;
235235
int num;
@@ -239,6 +239,43 @@ repack_one_database(const char *orderby, const char *table, char *errbuf, size_t
239239

240240
reconnect(ERROR);
241241

242+
/* Query the extension version. Exit if no match */
243+
res = execute_elevel("select repack.version()", 0, NULL, DEBUG2);
244+
if (PQresultStatus(res) == PGRES_TUPLES_OK)
245+
{
246+
const char *libver;
247+
char buf[64];
248+
249+
/* the string is something like "pg_repack 1.1.7" */
250+
libver = getstr(res, 0, 0);
251+
snprintf(buf, sizeof(buf), "%s %s", PROGRAM_NAME, PROGRAM_VERSION);
252+
if (0 != strcmp(buf, libver))
253+
{
254+
if (errbuf)
255+
snprintf(errbuf, errsize,
256+
"program '%s' does not match database library '%s'",
257+
buf, libver);
258+
goto cleanup;
259+
}
260+
}
261+
else
262+
{
263+
if (sqlstate_equals(res, SQLSTATE_INVALID_SCHEMA_NAME))
264+
{
265+
/* Schema repack does not exist. Skip the database. */
266+
if (errbuf)
267+
snprintf(errbuf, errsize,
268+
"%s is not installed in the database", PROGRAM_NAME);
269+
}
270+
else
271+
{
272+
/* Return the error message otherwise */
273+
if (errbuf)
274+
snprintf(errbuf, errsize, "%s", PQerrorMessage(connection));
275+
}
276+
goto cleanup;
277+
}
278+
242279
/* Disable statement timeout. */
243280
command("SET statement_timeout = 0", 0, NULL);
244281

@@ -346,6 +383,7 @@ repack_one_database(const char *orderby, const char *table, char *errbuf, size_t
346383

347384
repack_one_table(&table, orderby);
348385
}
386+
ret = true;
349387

350388
cleanup:
351389
PQclear(res);

doc/pg_repack.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,15 @@ ERROR: pg_repack is not installed
281281

282282
Do register pg_repack to the database.
283283

284+
ERROR: program 'pg_repack V1' does not match database library 'pg_repack V2'
285+
There is a mismatch between the ``pg_repack`` binary and the database
286+
library (``.so`` or ``.dll``).
287+
288+
The mismatch could be due to the wrong binary in the ``$PATH`` or the
289+
wrong database being addressed. Check the program directory and the
290+
database; if they are what expected you may need to repeat pg_repack
291+
installation.
292+
284293
ERROR: relation "table" has no primary key
285294
The target table doesn't have PRIMARY KEY.
286295

0 commit comments

Comments
 (0)