Skip to content

Commit deaae7d

Browse files
committed
Added version_sql() function and consistency check of sql version
1 parent 1bcaf26 commit deaae7d

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

bin/pg_repack.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,15 +240,18 @@ repack_one_database(const char *orderby, const char *table, char *errbuf, size_t
240240
reconnect(ERROR);
241241

242242
/* Query the extension version. Exit if no match */
243-
res = execute_elevel("select repack.version()", 0, NULL, DEBUG2);
243+
res = execute_elevel("select repack.version(), repack.version_sql()",
244+
0, NULL, DEBUG2);
244245
if (PQresultStatus(res) == PGRES_TUPLES_OK)
245246
{
246247
const char *libver;
247248
char buf[64];
248249

249250
/* the string is something like "pg_repack 1.1.7" */
250-
libver = getstr(res, 0, 0);
251251
snprintf(buf, sizeof(buf), "%s %s", PROGRAM_NAME, PROGRAM_VERSION);
252+
253+
/* check the version of the C library */
254+
libver = getstr(res, 0, 0);
252255
if (0 != strcmp(buf, libver))
253256
{
254257
if (errbuf)
@@ -257,6 +260,17 @@ repack_one_database(const char *orderby, const char *table, char *errbuf, size_t
257260
buf, libver);
258261
goto cleanup;
259262
}
263+
264+
/* check the version of the SQL extension */
265+
libver = getstr(res, 0, 1);
266+
if (0 != strcmp(buf, libver))
267+
{
268+
if (errbuf)
269+
snprintf(errbuf, errsize,
270+
"extension '%s' required, found extension '%s'",
271+
buf, libver);
272+
goto cleanup;
273+
}
260274
}
261275
else
262276
{

doc/pg_repack.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,13 @@ ERROR: program 'pg_repack V1' does not match database library 'pg_repack V2'
290290
database; if they are what expected you may need to repeat pg_repack
291291
installation.
292292

293+
ERROR: extension 'pg_repack V1' required, found extension 'pg_repack V2'
294+
The SQL extension found in the database does not match the version
295+
required by the pg_repack program.
296+
297+
You should drop the extension from the database and reload it as described
298+
in the installation_ section.
299+
293300
ERROR: relation "table" has no primary key
294301
The target table doesn't have PRIMARY KEY.
295302

lib/Makefile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ DATA_built = pg_repack.sql
3030
DATA = uninstall_pg_repack.sql
3131
endif
3232

33-
USE_PGXS = 1 # use pgxs if not in contrib directory
33+
USE_PGXS = 1
3434
PGXS := $(shell $(PG_CONFIG) --pgxs)
3535
include $(PGXS)
3636

@@ -40,11 +40,12 @@ LIBS := $(filter-out -lxslt, $(LIBS))
4040

4141
pg_repack.sql: pg_repack.sql.in
4242
echo "BEGIN;\n" > $@; \
43-
sed 's,MODULE_PATHNAME,$$libdir/$(MODULE_big),g' $< >> $@; \
43+
sed 's,MODULE_PATHNAME,$$libdir/$(MODULE_big),g' $< \
44+
| sed 's,REPACK_VERSION,$(REPACK_VERSION),g' >> $@; \
4445
echo "\nCOMMIT;" >> $@;
4546

4647
pg_repack--$(REPACK_VERSION).sql: pg_repack.sql.in
47-
cat $< > $@;
48+
sed 's,REPACK_VERSION,$(REPACK_VERSION),g' $< > $@;
4849

4950
pg_repack.control: pg_repack.control.in
5051
sed 's,REPACK_VERSION,$(REPACK_VERSION),g' $< > $@

lib/pg_repack.sql.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ CREATE FUNCTION repack.version() RETURNS text AS
1212
'MODULE_PATHNAME', 'repack_version'
1313
LANGUAGE C IMMUTABLE STRICT;
1414

15+
CREATE FUNCTION repack.version_sql() RETURNS text AS
16+
$$SELECT 'pg_repack REPACK_VERSION'::text$$
17+
LANGUAGE SQL IMMUTABLE STRICT;
18+
1519
CREATE AGGREGATE repack.array_accum (
1620
sfunc = array_append,
1721
basetype = anyelement,

0 commit comments

Comments
 (0)