Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow hypertable constraint creation USING INDEX
Browse files Browse the repository at this point in the history
Prior to this commit, we disallow
```sql
ALTER TABLE foo ADD CONSTRAINT foo_pkey
PRIMARY KEY USING INDEX foo_pkey
```

Signed-off-by: Arunprasad Rajkumar <ar.arunprasad@gmail.com>
arajkumar committed Jun 25, 2024
1 parent 5836445 commit 99090d7
Showing 11 changed files with 343 additions and 78 deletions.
1 change: 1 addition & 0 deletions .unreleased/fix_7040
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixes: #7040 Allow hypertable constraint creation USING INDEX
45 changes: 36 additions & 9 deletions sql/chunk_constraint.sql
Original file line number Diff line number Diff line change
@@ -4,7 +4,8 @@

-- create constraint on newly created chunk based on hypertable constraint
CREATE OR REPLACE FUNCTION _timescaledb_functions.chunk_constraint_add_table_constraint(
chunk_constraint_row _timescaledb_catalog.chunk_constraint
chunk_constraint_row _timescaledb_catalog.chunk_constraint,
using_index BOOLEAN
)
RETURNS VOID LANGUAGE PLPGSQL AS
$BODY$
@@ -17,6 +18,8 @@ DECLARE
def TEXT;
indx_tablespace NAME;
tablespace_def TEXT;
chunk_constraint_index_name NAME;
row_record RECORD;
BEGIN
SELECT * INTO STRICT chunk_row FROM _timescaledb_catalog.chunk c WHERE c.id = chunk_constraint_row.chunk_id;
SELECT * INTO STRICT hypertable_row FROM _timescaledb_catalog.hypertable h WHERE h.id = chunk_row.hypertable_id;
@@ -30,15 +33,39 @@ BEGIN
conrelid = format('%I.%I', hypertable_row.schema_name, hypertable_row.table_name)::regclass::oid;

IF constraint_type IN ('p','u') THEN
-- since primary keys and unique constraints are backed by an index
-- they might have an index tablespace assigned
-- the tablspace is not part of the constraint definition so
-- we have to append it explicitly to preserve it
SELECT T.spcname INTO indx_tablespace
FROM pg_constraint C, pg_class I, pg_tablespace T
WHERE C.oid = constraint_oid AND C.contype IN ('p', 'u') AND I.oid = C.conindid AND I.reltablespace = T.oid;
IF using_index THEN
-- indexes created for constraints are named after the constraint
-- so we can find the index name by looking up the constraint name.
SELECT index_name INTO STRICT chunk_constraint_index_name FROM _timescaledb_catalog.chunk_index
WHERE chunk_id = chunk_row.id AND hypertable_index_name = chunk_constraint_row.hypertable_constraint_name;

def := pg_get_constraintdef(constraint_oid);
IF chunk_constraint_index_name IS NULL THEN
RAISE 'index not found for constraint %', chunk_constraint_row;
END IF;

CASE constraint_type
WHEN 'p' THEN
def := pg_catalog.format(
$$ PRIMARY KEY USING INDEX %I $$,
chunk_constraint_index_name
);
WHEN 'u' THEN
def := pg_catalog.format(
$$ UNIQUE USING INDEX %I $$,
chunk_constraint_index_name
);
END CASE;
ELSE
-- since primary keys and unique constraints are backed by an index
-- they might have an index tablespace assigned
-- the tablspace is not part of the constraint definition so
-- we have to append it explicitly to preserve it
SELECT T.spcname INTO indx_tablespace
FROM pg_constraint C, pg_class I, pg_tablespace T
WHERE C.oid = constraint_oid AND C.contype IN ('p', 'u') AND I.oid = C.conindid AND I.reltablespace = T.oid;

def := pg_get_constraintdef(constraint_oid);
END IF;

ELSIF constraint_type = 't' THEN
-- constraint triggers are copied separately with normal triggers
75 changes: 75 additions & 0 deletions sql/updates/reverse-dev.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
DROP FUNCTION _timescaledb_functions.chunk_constraint_add_table_constraint(
chunk_constraint_row _timescaledb_catalog.chunk_constraint
);

CREATE FUNCTION _timescaledb_functions.chunk_constraint_add_table_constraint(
chunk_constraint_row _timescaledb_catalog.chunk_constraint
)
RETURNS VOID LANGUAGE PLPGSQL AS
$BODY$
DECLARE
chunk_row _timescaledb_catalog.chunk;
hypertable_row _timescaledb_catalog.hypertable;
constraint_oid OID;
constraint_type CHAR;
check_sql TEXT;
def TEXT;
indx_tablespace NAME;
tablespace_def TEXT;
BEGIN
SELECT * INTO STRICT chunk_row FROM _timescaledb_catalog.chunk c WHERE c.id = chunk_constraint_row.chunk_id;
SELECT * INTO STRICT hypertable_row FROM _timescaledb_catalog.hypertable h WHERE h.id = chunk_row.hypertable_id;

IF chunk_constraint_row.dimension_slice_id IS NOT NULL THEN
RAISE 'cannot create dimension constraint %', chunk_constraint_row;
ELSIF chunk_constraint_row.hypertable_constraint_name IS NOT NULL THEN

SELECT oid, contype INTO STRICT constraint_oid, constraint_type FROM pg_constraint
WHERE conname=chunk_constraint_row.hypertable_constraint_name AND
conrelid = format('%I.%I', hypertable_row.schema_name, hypertable_row.table_name)::regclass::oid;

IF constraint_type IN ('p','u') THEN
-- since primary keys and unique constraints are backed by an index
-- they might have an index tablespace assigned
-- the tablspace is not part of the constraint definition so
-- we have to append it explicitly to preserve it
SELECT T.spcname INTO indx_tablespace
FROM pg_constraint C, pg_class I, pg_tablespace T
WHERE C.oid = constraint_oid AND C.contype IN ('p', 'u') AND I.oid = C.conindid AND I.reltablespace = T.oid;

def := pg_get_constraintdef(constraint_oid);

ELSIF constraint_type = 't' THEN
-- constraint triggers are copied separately with normal triggers
def := NULL;
ELSE
def := pg_get_constraintdef(constraint_oid);
END IF;

ELSE
RAISE 'unknown constraint type';
END IF;

IF def IS NOT NULL THEN
-- to allow for custom types with operators outside of pg_catalog
-- we set search_path to @extschema@
SET LOCAL search_path TO @extschema@, pg_temp;
EXECUTE pg_catalog.format(
$$ ALTER TABLE %I.%I ADD CONSTRAINT %I %s $$,
chunk_row.schema_name, chunk_row.table_name, chunk_constraint_row.constraint_name, def
);

-- if constraint (primary or unique) needs a tablespace then add it
-- via a separate ALTER INDEX SET TABLESPACE command. We cannot append it
-- to the "def" string above since it leads to a SYNTAX error when
-- "DEFERRABLE" or "INITIALLY DEFERRED" are used in the constraint
IF indx_tablespace IS NOT NULL THEN
EXECUTE pg_catalog.format(
$$ ALTER INDEX %I.%I SET TABLESPACE %I $$,
chunk_row.schema_name, chunk_constraint_row.constraint_name, indx_tablespace
);
END IF;

END IF;
END
$BODY$ SET search_path TO pg_catalog, pg_temp;
21 changes: 13 additions & 8 deletions src/chunk_constraint.c
Original file line number Diff line number Diff line change
@@ -374,7 +374,7 @@ create_dimension_check_constraint(const Dimension *dim, const DimensionSlice *sl
* Add a constraint to a chunk table.
*/
static Oid
chunk_constraint_create_on_table(const ChunkConstraint *cc, Oid chunk_oid)
chunk_constraint_create_on_table(const ChunkConstraint *cc, Oid chunk_oid, bool using_index)
{
HeapTuple tuple;
Datum values[Natts_chunk_constraint];
@@ -389,7 +389,9 @@ chunk_constraint_create_on_table(const ChunkConstraint *cc, Oid chunk_oid)
RelationClose(rel);

ts_catalog_database_info_become_owner(ts_catalog_database_info_get(), &sec_ctx);
CatalogInternalCall1(DDL_ADD_CHUNK_CONSTRAINT, HeapTupleGetDatum(tuple));
CatalogInternalCall2(DDL_ADD_CHUNK_CONSTRAINT,
HeapTupleGetDatum(tuple),
BoolGetDatum(using_index));
ts_catalog_restore_user(&sec_ctx);
heap_freetuple(tuple);

@@ -402,14 +404,14 @@ chunk_constraint_create_on_table(const ChunkConstraint *cc, Oid chunk_oid)
*/
static Oid
create_non_dimensional_constraint(const ChunkConstraint *cc, Oid chunk_oid, int32 chunk_id,
Oid hypertable_oid, int32 hypertable_id)
Oid hypertable_oid, int32 hypertable_id, bool using_index)
{
Oid chunk_constraint_oid;

Assert(!is_dimension_constraint(cc));

ts_process_utility_set_expect_chunk_modification(true);
chunk_constraint_oid = chunk_constraint_create_on_table(cc, chunk_oid);
chunk_constraint_oid = chunk_constraint_create_on_table(cc, chunk_oid, using_index);
ts_process_utility_set_expect_chunk_modification(false);

/*
@@ -431,7 +433,7 @@ create_non_dimensional_constraint(const ChunkConstraint *cc, Oid chunk_oid, int3
{
FormData_pg_constraint *constr = (FormData_pg_constraint *) GETSTRUCT(tuple);

if (OidIsValid(constr->conindid) && constr->contype != CONSTRAINT_FOREIGN)
if (OidIsValid(constr->conindid) && constr->contype != CONSTRAINT_FOREIGN && !using_index)
ts_chunk_index_create_from_constraint(hypertable_id,
hypertable_constraint_oid,
chunk_id,
@@ -494,7 +496,8 @@ ts_chunk_constraints_create(const Hypertable *ht, const Chunk *chunk)
chunk->table_id,
chunk->fd.id,
ht->main_table_relid,
ht->fd.id);
ht->fd.id,
false);
}
}

@@ -855,7 +858,8 @@ ts_chunk_constraints_add_inheritable_check_constraints(ChunkConstraints *ccs, in
}

void
ts_chunk_constraint_create_on_chunk(const Hypertable *ht, const Chunk *chunk, Oid constraint_oid)
ts_chunk_constraint_create_on_chunk(const Hypertable *ht, const Chunk *chunk, Oid constraint_oid,
bool using_index)
{
HeapTuple tuple;
Form_pg_constraint con;
@@ -880,7 +884,8 @@ ts_chunk_constraint_create_on_chunk(const Hypertable *ht, const Chunk *chunk, Oi
chunk->table_id,
chunk->fd.id,
ht->main_table_relid,
ht->fd.id);
ht->fd.id,
using_index);
}

ReleaseSysCache(tuple);
2 changes: 1 addition & 1 deletion src/chunk_constraint.h
Original file line number Diff line number Diff line change
@@ -60,7 +60,7 @@ extern TSDLLEXPORT int ts_chunk_constraints_add_inheritable_check_constraints(
extern TSDLLEXPORT void ts_chunk_constraints_insert_metadata(const ChunkConstraints *ccs);
extern TSDLLEXPORT void ts_chunk_constraints_create(const Hypertable *ht, const Chunk *chunk);
extern void ts_chunk_constraint_create_on_chunk(const Hypertable *ht, const Chunk *chunk,
Oid constraint_oid);
Oid constraint_oid, bool using_index);
extern int ts_chunk_constraint_delete_by_hypertable_constraint_name(
int32 chunk_id, const char *hypertable_constraint_name, bool delete_metadata,
bool drop_constraint);
14 changes: 6 additions & 8 deletions src/chunk_index.c
Original file line number Diff line number Diff line change
@@ -987,12 +987,11 @@ ts_chunk_index_adjust_meta(int32 chunk_id, const char *ht_index_name, const char
}

int
ts_chunk_index_rename(Chunk *chunk, Oid chunk_indexrelid, const char *new_name)
ts_chunk_index_rename(Chunk *chunk, const char *old_name, const char *new_name)
{
ScanKeyData scankey[2];
const char *indexname = get_rel_name(chunk_indexrelid);
ChunkIndexRenameInfo renameinfo = {
.oldname = indexname,
.oldname = old_name,
.newname = new_name,
};

@@ -1005,7 +1004,7 @@ ts_chunk_index_rename(Chunk *chunk, Oid chunk_indexrelid, const char *new_name)
Anum_chunk_index_chunk_id_index_name_idx_index_name,
BTEqualStrategyNumber,
F_NAMEEQ,
CStringGetDatum(indexname));
CStringGetDatum(old_name));

return chunk_index_scan_update(CHUNK_INDEX_CHUNK_ID_INDEX_NAME_IDX,
scankey,
@@ -1016,12 +1015,11 @@ ts_chunk_index_rename(Chunk *chunk, Oid chunk_indexrelid, const char *new_name)
}

int
ts_chunk_index_rename_parent(Hypertable *ht, Oid hypertable_indexrelid, const char *new_name)
ts_chunk_index_rename_parent(Hypertable *ht, const char *old_name, const char *new_name)
{
ScanKeyData scankey[2];
const char *indexname = get_rel_name(hypertable_indexrelid);
ChunkIndexRenameInfo renameinfo = {
.oldname = indexname,
.oldname = old_name,
.newname = new_name,
.isparent = true,
};
@@ -1035,7 +1033,7 @@ ts_chunk_index_rename_parent(Hypertable *ht, Oid hypertable_indexrelid, const ch
Anum_chunk_index_hypertable_id_hypertable_index_name_idx_hypertable_index_name,
BTEqualStrategyNumber,
F_NAMEEQ,
CStringGetDatum(indexname));
CStringGetDatum(old_name));

return chunk_index_scan_update(CHUNK_INDEX_HYPERTABLE_ID_HYPERTABLE_INDEX_NAME_IDX,
scankey,
5 changes: 2 additions & 3 deletions src/chunk_index.h
Original file line number Diff line number Diff line change
@@ -40,9 +40,8 @@ extern int ts_chunk_index_delete(int32 chunk_id, const char *indexname, bool dro
extern int ts_chunk_index_delete_by_chunk_id(int32 chunk_id, bool drop_index);
extern void ts_chunk_index_delete_by_name(const char *schema, const char *index_name,
bool drop_index);
extern int ts_chunk_index_rename(Chunk *chunk, Oid chunk_indexrelid, const char *new_name);
extern int ts_chunk_index_rename_parent(Hypertable *ht, Oid hypertable_indexrelid,
const char *new_name);
extern int ts_chunk_index_rename(Chunk *chunk, const char *old_name, const char *new_name);
extern int ts_chunk_index_rename_parent(Hypertable *ht, const char *old_name, const char *new_name);
extern int ts_chunk_index_adjust_meta(int32 chunk_id, const char *ht_index_name,
const char *old_name, const char *new_name);
extern int ts_chunk_index_set_tablespace(Hypertable *ht, Oid hypertable_indexrelid,
74 changes: 66 additions & 8 deletions src/process_utility.c
Original file line number Diff line number Diff line change
@@ -1968,9 +1968,10 @@ process_rename_index(ProcessUtilityArgs *args, Cache *hcache, Oid relid, RenameS

ht = ts_hypertable_cache_get_entry(hcache, tablerelid, CACHE_FLAG_MISSING_OK);

const char *indexname = get_rel_name(relid);
if (NULL != ht)
{
ts_chunk_index_rename_parent(ht, relid, stmt->newname);
ts_chunk_index_rename_parent(ht, indexname, stmt->newname);

add_hypertable_to_process_args(args, ht);
}
@@ -1979,7 +1980,7 @@ process_rename_index(ProcessUtilityArgs *args, Cache *hcache, Oid relid, RenameS
Chunk *chunk = ts_chunk_get_by_relid(tablerelid, false);

if (NULL != chunk)
ts_chunk_index_rename(chunk, relid, stmt->newname);
ts_chunk_index_rename(chunk, indexname, stmt->newname);
}
}

@@ -2181,13 +2182,33 @@ process_altertable_change_owner(Hypertable *ht, AlterTableCmd *cmd)
}
}

static void
process_add_constraint_chunk_using_index(Hypertable *ht, Oid chunk_relid, void *arg)
{
Oid hypertable_constraint_oid = *((Oid *) arg);
Chunk *chunk = ts_chunk_get_by_relid(chunk_relid, true);

ts_chunk_constraint_create_on_chunk(ht, chunk, hypertable_constraint_oid, true);
}

static void
process_altertable_add_constraint_using_index(Hypertable *ht, const char *constraint_name)
{
Oid hypertable_constraint_oid =
get_relation_constraint_oid(ht->main_table_relid, constraint_name, false);

Assert(constraint_name != NULL);

foreach_chunk(ht, process_add_constraint_chunk_using_index, &hypertable_constraint_oid);
}

static void
process_add_constraint_chunk(Hypertable *ht, Oid chunk_relid, void *arg)
{
Oid hypertable_constraint_oid = *((Oid *) arg);
Chunk *chunk = ts_chunk_get_by_relid(chunk_relid, true);

ts_chunk_constraint_create_on_chunk(ht, chunk, hypertable_constraint_oid);
ts_chunk_constraint_create_on_chunk(ht, chunk, hypertable_constraint_oid, false);
}

static void
@@ -3365,6 +3386,33 @@ process_altertable_start_table(ProcessUtilityArgs *args)

if (ht)
verify_constraint_hypertable(ht, cmd->def);

Constraint *constr = (Constraint *) cmd->def;
ConstrType contype = constr->contype;
const char *conname = constr->conname;
const char *indexname = constr->indexname;

/* This is a unique or primary constraint using an existing index */
if ((contype == CONSTR_UNIQUE || contype == CONSTR_PRIMARY) && indexname != NULL)
{
/*
* Postgres renames the index to match the constraint name.
* We need to do the same on our catalog tables to maintain
* consistency.
*/
if (ht != NULL)
{
ts_chunk_index_rename_parent(ht, indexname, conname);
}
else
{
Chunk *chunk = ts_chunk_get_by_relid(relid, false);

if (NULL != chunk)
ts_chunk_index_rename(chunk, indexname, conname);
}
}

break;
case AT_AlterColumnType:
Assert(IsA(cmd->def, ColumnDef));
@@ -3619,11 +3667,21 @@ process_altertable_end_subcmd(Hypertable *ht, Node *parsetree, ObjectAddress *ob
process_altertable_change_owner(ht, cmd);
break;
case AT_AddIndexConstraint:
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("hypertables do not support adding a constraint "
"using an existing index")));
break;
{
IndexStmt *stmt = (IndexStmt *) cmd->def;

Assert(IsA(cmd->def, IndexStmt));

const char *idxname = stmt->idxname;

if (idxname == NULL)
{
idxname = get_rel_name(obj->objectId);
}

process_altertable_add_constraint_using_index(ht, idxname);
}
break;
case AT_AddIndex:
{
IndexStmt *stmt = (IndexStmt *) cmd->def;
2 changes: 1 addition & 1 deletion src/ts_catalog/catalog.c
Original file line number Diff line number Diff line change
@@ -280,7 +280,7 @@ typedef struct InternalFunctionDef
static const InternalFunctionDef internal_function_definitions[_MAX_INTERNAL_FUNCTIONS] = {
[DDL_ADD_CHUNK_CONSTRAINT] = {
.name = "chunk_constraint_add_table_constraint",
.args = 1,
.args = 2,
},
[DDL_CONSTRAINT_CLONE] = {
.name = "constraint_clone",
133 changes: 96 additions & 37 deletions test/expected/constraint.out

Large diffs are not rendered by default.

49 changes: 46 additions & 3 deletions test/sql/constraint.sql
Original file line number Diff line number Diff line change
@@ -125,13 +125,13 @@ SELECT * FROM test.show_constraints('_timescaledb_internal._hyper_2_4_chunk');
CREATE UNIQUE INDEX hyper_unique_with_looooooooooooooooooooooooooooooooo_time_idx
ON hyper_unique_with_looooooooooooooooooooooooooooooooooooong_name (time);

\set ON_ERROR_STOP 0
-- Try adding constraint using existing index
ALTER TABLE hyper_unique_with_looooooooooooooooooooooooooooooooooooong_name
ADD CONSTRAINT hyper_unique_with_looooooooooooooooooooooooooooooooooo_time_key UNIQUE
USING INDEX hyper_unique_with_looooooooooooooooooooooooooooooooo_time_idx;
\set ON_ERROR_STOP 1
DROP INDEX hyper_unique_with_looooooooooooooooooooooooooooooooo_time_idx;

ALTER TABLE hyper_unique_with_looooooooooooooooooooooooooooooooooooong_name
DROP CONSTRAINT hyper_unique_with_looooooooooooooooooooooooooooooooooo_time_key;

--now can create
ALTER TABLE hyper_unique_with_looooooooooooooooooooooooooooooooooooong_name
@@ -632,3 +632,46 @@ DROP TABLE tbl;
DROP TABLE fk_tbl;

DROP TABLESPACE IF EXISTS tablespace1;


----------------------- CONSTRAINT USING INDEX ------------------
CREATE TABLE tbl_constraint_using_index (
time BIGINT NOT NULL,
device_id TEXT NOT NULL,
sensor_1 NUMERIC NULL DEFAULT 1 CHECK (sensor_1 > 10)
);

SELECT create_hypertable('tbl_constraint_using_index', 'time', chunk_time_interval => 10);

INSERT INTO tbl_constraint_using_index(time, device_id, sensor_1)
VALUES
(11, 'dev1', 11),
(21, 'dev2', 12),
(31, 'dev3', 13);

CREATE UNIQUE INDEX tbl_constraint_using_index_time_device_id_idx ON tbl_constraint_using_index(time, device_id);

-- Create UNIQUE CONSTRAINT USING INDEX
ALTER TABLE tbl_constraint_using_index ADD CONSTRAINT tbl_constraint_using_index_time_device_id UNIQUE USING INDEX tbl_constraint_using_index_time_device_id_idx;

SELECT * FROM test.show_constraints('_timescaledb_internal._hyper_16_19_chunk');

-- Test that the constraints are added for newly created chunks
INSERT INTO tbl_constraint_using_index(time, device_id, sensor_1)
VALUES
(41, 'dev4', 14);

-- Below insert should fail due to unique constraint violation
\set ON_ERROR_STOP 0
INSERT INTO tbl_constraint_using_index(time, device_id, sensor_1)
VALUES
(41, 'dev4', 14);
\set ON_ERROR_STOP 1

SELECT * FROM test.show_constraints('_timescaledb_internal._hyper_16_20_chunk');

ALTER TABLE tbl_constraint_using_index DROP CONSTRAINT tbl_constraint_using_index_time_device_id;

-- Create Primary Key CONSTRAINT USING INDEX
CREATE UNIQUE INDEX tbl_constraint_using_index_pkey ON tbl_constraint_using_index(time, device_id);
ALTER TABLE tbl_constraint_using_index ADD CONSTRAINT tbl_constraint_using_index_pkey PRIMARY KEY USING INDEX tbl_constraint_using_index_pkey;

0 comments on commit 99090d7

Please sign in to comment.