From 43dfe229c9b611c7e4608d3821ad02cd0936fe30 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Tue, 16 Apr 2013 19:14:49 +0100 Subject: [PATCH] Added check for target tablespace existence --- bin/expected/tablespace.out | 2 +- bin/pg_repack.c | 59 +++++++++++++++++++++++++++++++++---- bin/sql/tablespace.sql | 2 +- 3 files changed, 55 insertions(+), 8 deletions(-) diff --git a/bin/expected/tablespace.out b/bin/expected/tablespace.out index 9fa432a..d277122 100644 --- a/bin/expected/tablespace.out +++ b/bin/expected/tablespace.out @@ -53,7 +53,7 @@ WHERE relname ~ '^testts1'; (0 rows) -- can move the table together with the indexes -\! pg_repack --dbname=contrib_regression --no-order --table=testts1 --tablespace pg_default --moveidx +\! pg_repack --dbname=contrib_regression --no-order --table=testts1 --tablespace testts --moveidx SELECT relname, spcname FROM pg_class JOIN pg_tablespace ts ON ts.oid = reltablespace WHERE relname ~ '^testts1'; diff --git a/bin/pg_repack.c b/bin/pg_repack.c index c3b8b16..3acdccd 100644 --- a/bin/pg_repack.c +++ b/bin/pg_repack.c @@ -172,6 +172,7 @@ typedef struct repack_index } repack_index; static bool is_superuser(void); +static void check_tablespace(void); static void repack_all_databases(const char *order_by); static bool repack_one_database(const char *order_by, char *errbuf, size_t errsize); static void repack_one_table(const repack_table *table, const char *order_by); @@ -238,6 +239,8 @@ main(int argc, char *argv[]) (errcode(EINVAL), errmsg("too many arguments"))); + check_tablespace(); + if (noorder) orderby = ""; @@ -258,12 +261,6 @@ main(int argc, char *argv[]) errmsg("%s", errbuf))); } - if (moveidx && tablespace == NULL) - { - ereport(ERROR, - (errcode(EINVAL), - errmsg("cannot specify --moveidx (-S) without --tablespace (-s)"))); - } return 0; } @@ -291,6 +288,56 @@ is_superuser(void) return false; } +/* + * Check if the tablespace requested exists. + * + * Raise an exception on error. + */ +void +check_tablespace() +{ + PGresult *res = NULL; + const char *params[1]; + + if (tablespace == NULL) + { + /* nothing to check, but let's see the options */ + if (moveidx) + { + ereport(ERROR, + (errcode(EINVAL), + errmsg("cannot specify --moveidx (-S) without --tablespace (-s)"))); + } + return; + } + + /* check if the tablespace exists */ + reconnect(ERROR); + params[0] = tablespace; + res = execute_elevel( + "select spcname from pg_tablespace where spcname = $1", + 1, params, DEBUG2); + + if (PQresultStatus(res) == PGRES_TUPLES_OK) + { + if (PQntuples(res) == 0) + { + ereport(ERROR, + (errcode(EINVAL), + errmsg("the tablespace \"%s\" doesn't exist", tablespace))); + } + } + else + { + ereport(ERROR, + (errcode(EINVAL), + errmsg("error checking the namespace: %s", + PQerrorMessage(connection)))); + } + + CLEARPGRES(res); +} + /* * Call repack_one_database for each database. diff --git a/bin/sql/tablespace.sql b/bin/sql/tablespace.sql index 291b6a6..c618a4a 100644 --- a/bin/sql/tablespace.sql +++ b/bin/sql/tablespace.sql @@ -38,7 +38,7 @@ FROM pg_class JOIN pg_tablespace ts ON ts.oid = reltablespace WHERE relname ~ '^testts1'; -- can move the table together with the indexes -\! pg_repack --dbname=contrib_regression --no-order --table=testts1 --tablespace pg_default --moveidx +\! pg_repack --dbname=contrib_regression --no-order --table=testts1 --tablespace testts --moveidx SELECT relname, spcname FROM pg_class JOIN pg_tablespace ts ON ts.oid = reltablespace