@@ -172,6 +172,7 @@ typedef struct repack_index
172
172
} repack_index ;
173
173
174
174
static bool is_superuser (void );
175
+ static void check_tablespace (void );
175
176
static void repack_all_databases (const char * order_by );
176
177
static bool repack_one_database (const char * order_by , char * errbuf , size_t errsize );
177
178
static void repack_one_table (const repack_table * table , const char * order_by );
@@ -238,6 +239,8 @@ main(int argc, char *argv[])
238
239
(errcode (EINVAL ),
239
240
errmsg ("too many arguments" )));
240
241
242
+ check_tablespace ();
243
+
241
244
if (noorder )
242
245
orderby = "" ;
243
246
@@ -258,12 +261,6 @@ main(int argc, char *argv[])
258
261
errmsg ("%s" , errbuf )));
259
262
}
260
263
261
- if (moveidx && tablespace == NULL )
262
- {
263
- ereport (ERROR ,
264
- (errcode (EINVAL ),
265
- errmsg ("cannot specify --moveidx (-S) without --tablespace (-s)" )));
266
- }
267
264
return 0 ;
268
265
}
269
266
@@ -291,6 +288,56 @@ is_superuser(void)
291
288
return false;
292
289
}
293
290
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
+
294
341
295
342
/*
296
343
* Call repack_one_database for each database.
0 commit comments