Skip to content

Commit

Permalink
Fix index attnum mapping in reorder_chunk
Browse files Browse the repository at this point in the history
When we clone an index from a chunk we must not do attnum mapping if the
supplied index template is not on the hypertable. Ideally we would check
for the template being on the chunk but we cannot do that since when we
rebuild a chunk the new chunk has a different id.

Fixes #3651
  • Loading branch information
svenklemm committed Oct 6, 2021
1 parent ef0ff45 commit a118f38
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
`psql` with the `-X` flag to prevent any `.psqlrc` commands from
accidentally triggering the load of a previous DB version.**

## Unreleased

**Bugfixes**
* #3654 Fix index attnum mapping in reorder_chunk

## 2.4.2 (2021-09-21)

This release contains bug fixes since the 2.4.1 release.
Expand Down
14 changes: 13 additions & 1 deletion src/chunk_index.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,24 @@ chunk_relation_index_create(Relation htrel, Relation template_indexrel, Relation
{
IndexInfo *indexinfo = BuildIndexInfo(template_indexrel);
int32 hypertable_id;
bool skip_mapping = false;

/*
* If the supplied template index is not on the hypertable we must not do attnum
* mapping based on the hypertable. Ideally we would check for the template being
* on the chunk but we cannot do that since when we rebuild a chunk the new chunk
* has a different id. But the template index should always be either on the
* hypertable or on a relation with the same physical layout as chunkrel.
*/
if (IndexGetRelation(template_indexrel->rd_id, false) != htrel->rd_id)
skip_mapping = true;

/*
* Convert the IndexInfo's attnos to match the chunk instead of the
* hypertable
*/
if (chunk_index_need_attnos_adjustment(RelationGetDescr(htrel), RelationGetDescr(chunkrel)))
if (!skip_mapping &&
chunk_index_need_attnos_adjustment(RelationGetDescr(htrel), RelationGetDescr(chunkrel)))
ts_adjust_indexinfo_attnos(indexinfo, htrel->rd_id, chunkrel);

hypertable_id = ts_hypertable_relid_to_id(htrel->rd_id);
Expand Down
17 changes: 17 additions & 0 deletions tsl/test/expected/reorder.out
Original file line number Diff line number Diff line change
Expand Up @@ -1344,3 +1344,20 @@ WHERE indisclustered = true ORDER BY 1;
SELECT reorder_chunk('_timescaledb_internal._hyper_2_3_chunk', verbose => TRUE);
ERROR: must be owner of hypertable "ct2"
\set ON_ERROR_STOP 1
-- #3651 incorrect index attribute mapping in reorder_chunk
CREATE TABLE i3651(c1 timestamptz NOT NULL,c2 text, c3 text, c4 text);
SELECT table_name FROM create_hypertable('i3651','c1');
table_name
------------
i3651
(1 row)

ALTER TABLE i3651 drop column c2;
CREATE UNIQUE INDEX i3651_attmap ON i3651(c1,c4);
INSERT INTO i3651 VALUES('2000-01-01','foo','foo'),('2000-01-01','foo','bar');
SELECT reorder_chunk(chunk,'i3651_attmap') from show_chunks('i3651') chunk;
reorder_chunk
---------------

(1 row)

9 changes: 9 additions & 0 deletions tsl/test/sql/reorder.sql
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,12 @@ WHERE indisclustered = true ORDER BY 1;
\set ON_ERROR_STOP 0
SELECT reorder_chunk('_timescaledb_internal._hyper_2_3_chunk', verbose => TRUE);
\set ON_ERROR_STOP 1

-- #3651 incorrect index attribute mapping in reorder_chunk
CREATE TABLE i3651(c1 timestamptz NOT NULL,c2 text, c3 text, c4 text);
SELECT table_name FROM create_hypertable('i3651','c1');
ALTER TABLE i3651 drop column c2;
CREATE UNIQUE INDEX i3651_attmap ON i3651(c1,c4);
INSERT INTO i3651 VALUES('2000-01-01','foo','foo'),('2000-01-01','foo','bar');
SELECT reorder_chunk(chunk,'i3651_attmap') from show_chunks('i3651') chunk;

0 comments on commit a118f38

Please sign in to comment.