timescaledb/test/sql/index.sql
Erik Nordström 097db3d589 Refactor chunk index handling
This change refactors the chunk index handling to make better use
of standard PostgreSQL catalog information, while removing the
hypertable_index metadata table and associated triggers, including
those on the chunk_index table. The chunk_index table itself is
also simplified.

A benefit of this refactoring is that indexes are no longer
created using string mangling to construct the CREATE INDEX command
for a chunk, based on the string definition of the hypertable
index. Instead, indexes are created in C using proper index-related
internal data structures.

Chunk indexes can now also be renamed and are added in the parent
index tablespace. Changing tablespace on a hypertable index also
recurses to chunks, as expected. Default indexes that are added when
creating a hypertable use the hypertable's tablespace.

Creating Hypertable indexes with the CONCURRENTLY modifier is
currently blocked, due to unclear semantics regarding concurrent
creation over many tables, including how to deal with snapshots.
2017-10-03 10:51:32 +02:00

163 lines
5.1 KiB
SQL

\ir include/create_single_db.sql
CREATE TABLE index_test(time timestamptz, temp float);
SELECT create_hypertable('index_test', 'time');
-- Default indexes created
\d+ index_test
DROP TABLE index_test;
CREATE TABLE index_test(time timestamptz, device integer, temp float);
-- Create index before create_hypertable()
CREATE UNIQUE INDEX index_test_time_idx ON index_test (time);
\set ON_ERROR_STOP 0
-- Creating a hypertable from a table with an index that doesn't cover
-- all partitioning columns should fail
SELECT create_hypertable('index_test', 'time', 'device', 2);
\set ON_ERROR_STOP 1
-- Partitioning on only time should work
SELECT create_hypertable('index_test', 'time');
INSERT INTO index_test VALUES ('2017-01-20T09:00:01', 1, 17.5);
-- Check that index is also created on chunk
\d+ index_test
\d+ _timescaledb_internal._hyper*_chunk
SELECT * FROM _timescaledb_catalog.chunk_index;
-- Create another chunk
INSERT INTO index_test VALUES ('2017-05-20T09:00:01', 3, 17.5);
\d+ _timescaledb_internal._hyper*_chunk
SELECT * FROM _timescaledb_catalog.chunk_index;
-- Delete the index on only one chunk
DROP INDEX _timescaledb_internal._hyper_3_1_chunk_index_test_time_idx;
\d+ _timescaledb_internal._hyper*_chunk
SELECT * FROM _timescaledb_catalog.chunk_index;
-- Recreate table with new partitioning
DROP TABLE index_test;
CREATE TABLE index_test(time timestamptz, device integer, temp float);
-- No pre-existing UNIQUE index, so partitioning on two columns should work
SELECT create_hypertable('index_test', 'time', 'device', 2);
INSERT INTO index_test VALUES ('2017-01-20T09:00:01', 1, 17.5);
\set ON_ERROR_STOP 0
-- Create unique index without all partitioning columns should fail
CREATE UNIQUE INDEX index_test_time_device_idx ON index_test (time);
\set ON_ERROR_STOP 1
CREATE UNIQUE INDEX index_test_time_device_idx ON index_test (time, device);
-- Regular index need not cover all partitioning columns
CREATE INDEX ON index_test (time, temp);
-- New index should have been recursed to chunk
\d+ index_test
\d+ _timescaledb_internal._hyper*_chunk
SELECT * FROM _timescaledb_catalog.chunk_index;
ALTER INDEX index_test_time_idx RENAME TO index_test_time_idx2;
-- Metadata and index should have changed name
\d+ index_test
\d+ _timescaledb_internal._hyper*_chunk
SELECT * FROM _timescaledb_catalog.chunk_index;
DROP INDEX index_test_time_idx2;
DROP INDEX index_test_time_device_idx;
-- Index should have been dropped
\d+ index_test
\d+ _timescaledb_internal._hyper*_chunk
SELECT * FROM _timescaledb_catalog.chunk_index;
-- Create index with long name to see how this is handled on chunks
CREATE INDEX a_hypertable_index_with_a_very_very_long_name_that_truncates ON index_test (time, temp);
CREATE INDEX a_hypertable_index_with_a_very_very_long_name_that_truncates_2 ON index_test (time, temp);
\d+ index_test
\d+ _timescaledb_internal._hyper*_chunk
DROP INDEX a_hypertable_index_with_a_very_very_long_name_that_truncates;
DROP INDEX a_hypertable_index_with_a_very_very_long_name_that_truncates_2;
\set ON_ERROR_STOP 0
-- Create index CONCURRENTLY
CREATE UNIQUE INDEX CONCURRENTLY index_test_time_device_idx ON index_test (time, device);
\set ON_ERROR_STOP 1
-- Test tablespaces. Chunk indexes should end up in same tablespace as
-- main index.
SET client_min_messages = ERROR;
DROP TABLESPACE IF EXISTS tablespace1;
DROP TABLESPACE IF EXISTS tablespace2;
SET client_min_messages = NOTICE;
CREATE TABLESPACE tablespace1 LOCATION :TEST_TABLESPACE1_PATH;
CREATE INDEX index_test_time_idx ON index_test (time) TABLESPACE tablespace1;
\d+ index_test
\d+ _timescaledb_internal._hyper*
CREATE TABLESPACE tablespace2 LOCATION :TEST_TABLESPACE2_PATH;
ALTER INDEX index_test_time_idx SET TABLESPACE tablespace2;
\d+ index_test
\d+ _timescaledb_internal._hyper*_chunk
-- Add constraint index
ALTER TABLE index_test ADD UNIQUE (time, device);
\d+ index_test
\d+ _timescaledb_internal._hyper*_chunk
-- Constraint indexes NOT added to chunk_index table (maybe they
-- should be for consistency?)
SELECT * FROM _timescaledb_catalog.chunk_index;
DROP TABLE index_test;
-- Metadata removed
SELECT * FROM _timescaledb_catalog.chunk_index;
-- Create table in a tablespace
CREATE TABLE index_test(time timestamptz, temp float, device int) TABLESPACE tablespace1;
-- Default indexes should be in the table's tablespace
SELECT create_hypertable('index_test', 'time');
-- Explicitly defining an index tablespace should work and propagate
-- to chunks
CREATE INDEX ON index_test (time, device) TABLESPACE tablespace2;
-- New indexes without explicit tablespaces should use the default
-- tablespace
CREATE INDEX ON index_test (device);
-- Create chunk
INSERT INTO index_test VALUES ('2017-01-20T09:00:01', 17.5);
-- Check that the tablespaces of chunk indexes match the tablespace of
-- the main index
\d+ index_test
\d+ _timescaledb_internal._hyper*_chunk
-- Creating a new index should propagate to existing chunks, including
-- the given tablespace
CREATE INDEX ON index_test (time, temp) TABLESPACE tablespace2;
\d+ index_test
\d+ _timescaledb_internal._hyper*_chunk
-- Cleanup
DROP TABLE index_test CASCADE;
DROP TABLESPACE tablespace1;
DROP TABLESPACE tablespace2;