Skip to content

Commit 4cf9242

Browse files
committed
[yugabyte#8323] YSQL: Add support for USING INDEX TABLESPACE
Summary: Added support for the "USING INDEX TABLESPACE" clause. This will allow setting tablespace for unique indexes. However, since the primary key index is an intrinsic part of the table itself, it cannot have a separate tablespace (it should have the same tablespace as that of the table). Thus we throw a syntax error if there is an attempt to set tablespace for a primary key index using the "USING INDEX TABLESPACE" clause. Test Plan: ybd --scb --sj --java-test org.yb.pgsql.TestTablespaceProperties ybd --scb --sj --java-test org.yb.pgsql.TestPgRegressTablespaces Reviewers: mihnea Reviewed By: mihnea Subscribers: yql Differential Revision: https://phabricator.dev.yugabyte.com/D11533
1 parent 7afd17f commit 4cf9242

File tree

4 files changed

+58
-13
lines changed

4 files changed

+58
-13
lines changed

java/yb-pgsql/src/test/java/org/yb/pgsql/TestTablespaceProperties.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,10 @@ private void createTestData (String prefixName) throws Exception {
112112
"CREATE TABLE " + customTable + "(a SERIAL) TABLESPACE testTablespace");
113113

114114
setupStatement.execute(
115-
"CREATE TABLE " + defaultTable + "(a int)");
115+
"CREATE TABLE " + defaultTable + "(a int CONSTRAINT " + customIndex +
116+
" UNIQUE USING INDEX TABLESPACE testTablespace)");
116117

117118
// Create indexes in default and custom tablespaces.
118-
setupStatement.execute("CREATE INDEX " + customIndex + " on " +
119-
defaultTable + "(a) TABLESPACE testTablespace");
120-
121119
setupStatement.execute("CREATE INDEX " + defaultIndexCustomTable + " on " +
122120
customTable + "(a)");
123121

@@ -510,7 +508,7 @@ List<LocatedTablet> fetchTablets(final String table) throws Exception {
510508
final YBClient client = miniCluster.getClient();
511509
List<Master.ListTablesResponsePB.TableInfo> tables =
512510
client.getTablesList(table).getTableInfoList();
513-
assertEquals(1, tables.size());
511+
assertEquals("More than one table found with name " + table, 1, tables.size());
514512
final YBTable ybtable = client.openTableByUUID(
515513
tables.get(0).getId().toStringUtf8());
516514
return ybtable.getTabletsLocations(30000);

src/postgres/src/backend/parser/gram.y

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3777,6 +3777,12 @@ ColConstraintElem:
37773777
}
37783778
| PRIMARY KEY opt_definition OptConsTableSpace
37793779
{
3780+
if ($4)
3781+
{
3782+
ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR),
3783+
errmsg("Cannot set TABLESPACE for PRIMARY KEY INDEX."),
3784+
errdetail("The tablespace of the indexed table will be used.")));
3785+
}
37803786
Constraint *n = makeNode(Constraint);
37813787
n->contype = CONSTR_PRIMARY;
37823788
n->location = @1;
@@ -4031,6 +4037,12 @@ ConstraintElem:
40314037
n->including = $6;
40324038
n->options = $7;
40334039
n->indexname = NULL;
4040+
if ($8)
4041+
{
4042+
ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR),
4043+
errmsg("Cannot set TABLESPACE for PRIMARY KEY INDEX."),
4044+
errdetail("The tablespace of the indexed table will be used.")));
4045+
}
40344046
n->indexspace = $8;
40354047
processCASbits($9, @9, "PRIMARY KEY",
40364048
&n->deferrable, &n->initdeferred, NULL,
@@ -4313,7 +4325,6 @@ OptTableSpace:
43134325
OptConsTableSpace:
43144326
USING INDEX TABLESPACE name
43154327
{
4316-
parser_ybc_signal_unsupported(@1, "USING INDEX TABLESPACE", 1129);
43174328
$$ = $4;
43184329
}
43194330
| /*EMPTY*/ { $$ = NULL; }

src/postgres/src/test/regress/expected/yb_tablespaces.out

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,32 @@ Indexes:
164164
Tablespace: "regress_tblspace"
165165

166166
SET default_tablespace TO '';
167+
-- Verify that USING INDEX TABLESPACE is not supported for primary keys.
168+
CREATE TABLE testschema.using_index1 (a int PRIMARY KEY USING INDEX TABLESPACE regress_tblspace);
169+
ERROR: Cannot set TABLESPACE for PRIMARY KEY INDEX.
170+
DETAIL: The tablespace of the indexed table will be used.
171+
CREATE TABLE testschema.using_index1 (a int, PRIMARY KEY(a) USING INDEX TABLESPACE regress_tblspace);
172+
ERROR: Cannot set TABLESPACE for PRIMARY KEY INDEX.
173+
DETAIL: The tablespace of the indexed table will be used.
174+
-- Verify that USING INDEX TABLESPACE is supported for other constraints.
175+
CREATE TABLE testschema.using_index2 (a int UNIQUE USING INDEX TABLESPACE regress_tblspace);
176+
CREATE TABLE testschema.using_index3 (a int, UNIQUE(a) USING INDEX TABLESPACE regress_tblspace);
177+
\d testschema.using_index2;
178+
Table "testschema.using_index2"
179+
Column | Type | Collation | Nullable | Default
180+
--------+---------+-----------+----------+---------
181+
a | integer | | |
182+
Indexes:
183+
"using_index2_a_key" UNIQUE CONSTRAINT, lsm (a HASH), tablespace "regress_tblspace"
184+
185+
\d testschema.using_index3;
186+
Table "testschema.using_index3"
187+
Column | Type | Collation | Nullable | Default
188+
--------+---------+-----------+----------+---------
189+
a | integer | | |
190+
Indexes:
191+
"using_index3_a_key" UNIQUE CONSTRAINT, lsm (a HASH), tablespace "regress_tblspace"
192+
167193
-- index
168194
CREATE INDEX foo_idx on testschema.foo(i) TABLESPACE regress_tblspace;
169195
SELECT relname, spcname FROM pg_catalog.pg_tablespace t, pg_catalog.pg_class c
@@ -405,7 +431,7 @@ DROP TABLESPACE regress_tblspace;
405431
DROP ROLE regress_tablespace_user1;
406432
DROP ROLE regress_tablespace_user2;
407433
/*
408-
Testing to make sure that an index on a "near" tablespace whose placements are
434+
Testing to make sure that an index on a "near" tablespace whose placements are
409435
all on the current cloud/region/zone is preferred over "far" indexes.
410436
*/
411437
CREATE TABLESPACE near WITH (replica_placement='{"num_replicas":1, "placement_blocks":[{"cloud":"cloud1","region":"region1","zone":"zone1","min_num_replicas":1}]}');
@@ -418,15 +444,15 @@ CREATE UNIQUE INDEX regionlocal_ind ON foo(x) INCLUDE (y) TABLESPACE regionlocal
418444
CREATE UNIQUE INDEX cloudlocal_ind ON foo(x) INCLUDE (y) TABLESPACE cloudlocal;
419445
CREATE UNIQUE INDEX bad ON foo(x) INCLUDE (y) TABLESPACE far;
420446
EXPLAIN (COSTS OFF) SELECT * FROM foo WHERE x = 5;
421-
QUERY PLAN
447+
QUERY PLAN
422448
-----------------------------------
423449
Index Only Scan using good on foo
424450
Index Cond: (x = 5)
425451
(2 rows)
426452

427453
SET yb_enable_geolocation_costing = off;
428454
EXPLAIN (COSTS OFF) SELECT * FROM foo WHERE x = 5;
429-
QUERY PLAN
455+
QUERY PLAN
430456
----------------------------------
431457
Index Only Scan using bad on foo
432458
Index Cond: (x = 5)
@@ -435,15 +461,15 @@ EXPLAIN (COSTS OFF) SELECT * FROM foo WHERE x = 5;
435461
SET yb_enable_geolocation_costing = on;
436462
DROP INDEX good;
437463
EXPLAIN (COSTS OFF) SELECT * FROM foo WHERE x = 5;
438-
QUERY PLAN
464+
QUERY PLAN
439465
----------------------------------------------
440466
Index Only Scan using regionlocal_ind on foo
441467
Index Cond: (x = 5)
442468
(2 rows)
443469

444470
DROP INDEX regionlocal_ind;
445471
EXPLAIN (COSTS OFF) SELECT * FROM foo WHERE x = 5;
446-
QUERY PLAN
472+
QUERY PLAN
447473
----------------------------------
448474
Index Only Scan using bad on foo
449475
Index Cond: (x = 5)
@@ -454,7 +480,7 @@ CREATE TABLE foo(id int primary key, val int);
454480
CREATE UNIQUE INDEX bad ON foo(id) INCLUDE (val) TABLESPACE far;
455481
CREATE UNIQUE INDEX good ON foo(id) INCLUDE (val) TABLESPACE near;
456482
EXPLAIN (COSTS OFF) SELECT * FROM foo WHERE id = 5;
457-
QUERY PLAN
483+
QUERY PLAN
458484
-----------------------------------
459485
Index Only Scan using good on foo
460486
Index Cond: (id = 5)

src/postgres/src/test/regress/sql/yb_tablespaces.sql

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ CREATE TABLE testschema.foo_pk_default_tblspc (i int, PRIMARY KEY(i));
9999
\d testschema.foo_pk_default_tblspc;
100100
SET default_tablespace TO '';
101101

102+
-- Verify that USING INDEX TABLESPACE is not supported for primary keys.
103+
CREATE TABLE testschema.using_index1 (a int PRIMARY KEY USING INDEX TABLESPACE regress_tblspace);
104+
CREATE TABLE testschema.using_index1 (a int, PRIMARY KEY(a) USING INDEX TABLESPACE regress_tblspace);
105+
106+
-- Verify that USING INDEX TABLESPACE is supported for other constraints.
107+
CREATE TABLE testschema.using_index2 (a int UNIQUE USING INDEX TABLESPACE regress_tblspace);
108+
CREATE TABLE testschema.using_index3 (a int, UNIQUE(a) USING INDEX TABLESPACE regress_tblspace);
109+
\d testschema.using_index2;
110+
\d testschema.using_index3;
111+
102112
-- index
103113
CREATE INDEX foo_idx on testschema.foo(i) TABLESPACE regress_tblspace;
104114
SELECT relname, spcname FROM pg_catalog.pg_tablespace t, pg_catalog.pg_class c
@@ -213,7 +223,7 @@ DROP ROLE regress_tablespace_user1;
213223
DROP ROLE regress_tablespace_user2;
214224

215225
/*
216-
Testing to make sure that an index on a "near" tablespace whose placements are
226+
Testing to make sure that an index on a "near" tablespace whose placements are
217227
all on the current cloud/region/zone is preferred over "far" indexes.
218228
*/
219229
CREATE TABLESPACE near WITH (replica_placement='{"num_replicas":1, "placement_blocks":[{"cloud":"cloud1","region":"region1","zone":"zone1","min_num_replicas":1}]}');

0 commit comments

Comments
 (0)