Skip to content

Commit 43dfe22

Browse files
committed
Added check for target tablespace existence
1 parent a6e5051 commit 43dfe22

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

bin/expected/tablespace.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ WHERE relname ~ '^testts1';
5353
(0 rows)
5454

5555
-- can move the table together with the indexes
56-
\! pg_repack --dbname=contrib_regression --no-order --table=testts1 --tablespace pg_default --moveidx
56+
\! pg_repack --dbname=contrib_regression --no-order --table=testts1 --tablespace testts --moveidx
5757
SELECT relname, spcname
5858
FROM pg_class JOIN pg_tablespace ts ON ts.oid = reltablespace
5959
WHERE relname ~ '^testts1';

bin/pg_repack.c

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ typedef struct repack_index
172172
} repack_index;
173173

174174
static bool is_superuser(void);
175+
static void check_tablespace(void);
175176
static void repack_all_databases(const char *order_by);
176177
static bool repack_one_database(const char *order_by, char *errbuf, size_t errsize);
177178
static void repack_one_table(const repack_table *table, const char *order_by);
@@ -238,6 +239,8 @@ main(int argc, char *argv[])
238239
(errcode(EINVAL),
239240
errmsg("too many arguments")));
240241

242+
check_tablespace();
243+
241244
if (noorder)
242245
orderby = "";
243246

@@ -258,12 +261,6 @@ main(int argc, char *argv[])
258261
errmsg("%s", errbuf)));
259262
}
260263

261-
if (moveidx && tablespace == NULL)
262-
{
263-
ereport(ERROR,
264-
(errcode(EINVAL),
265-
errmsg("cannot specify --moveidx (-S) without --tablespace (-s)")));
266-
}
267264
return 0;
268265
}
269266

@@ -291,6 +288,56 @@ is_superuser(void)
291288
return false;
292289
}
293290

291+
/*
292+
* Check if the tablespace requested exists.
293+
*
294+
* Raise an exception on error.
295+
*/
296+
void
297+
check_tablespace()
298+
{
299+
PGresult *res = NULL;
300+
const char *params[1];
301+
302+
if (tablespace == NULL)
303+
{
304+
/* nothing to check, but let's see the options */
305+
if (moveidx)
306+
{
307+
ereport(ERROR,
308+
(errcode(EINVAL),
309+
errmsg("cannot specify --moveidx (-S) without --tablespace (-s)")));
310+
}
311+
return;
312+
}
313+
314+
/* check if the tablespace exists */
315+
reconnect(ERROR);
316+
params[0] = tablespace;
317+
res = execute_elevel(
318+
"select spcname from pg_tablespace where spcname = $1",
319+
1, params, DEBUG2);
320+
321+
if (PQresultStatus(res) == PGRES_TUPLES_OK)
322+
{
323+
if (PQntuples(res) == 0)
324+
{
325+
ereport(ERROR,
326+
(errcode(EINVAL),
327+
errmsg("the tablespace \"%s\" doesn't exist", tablespace)));
328+
}
329+
}
330+
else
331+
{
332+
ereport(ERROR,
333+
(errcode(EINVAL),
334+
errmsg("error checking the namespace: %s",
335+
PQerrorMessage(connection))));
336+
}
337+
338+
CLEARPGRES(res);
339+
}
340+
294341

295342
/*
296343
* Call repack_one_database for each database.

bin/sql/tablespace.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ FROM pg_class JOIN pg_tablespace ts ON ts.oid = reltablespace
3838
WHERE relname ~ '^testts1';
3939

4040
-- can move the table together with the indexes
41-
\! pg_repack --dbname=contrib_regression --no-order --table=testts1 --tablespace pg_default --moveidx
41+
\! pg_repack --dbname=contrib_regression --no-order --table=testts1 --tablespace testts --moveidx
4242

4343
SELECT relname, spcname
4444
FROM pg_class JOIN pg_tablespace ts ON ts.oid = reltablespace

0 commit comments

Comments
 (0)