timescaledb/sql/chunk_index.sql
Matvey Arye bfe58b61f7 Refactor towards supporting version upgrades
Clean up the table schema to get rid of legacy tables and functionality
that makes it more difficult to provide an upgrade path.

Notable changes:
* Get rid of legacy tables and code
* Simplify directory structure for SQL code
* Simplify table hierarchy: remove root table and make chunk tables
* inherit directly from main table
* Change chunk table suffix from _data to _chunk
* Simplify schema usage: _timescaledb_internal for internal functions.
* _timescaledb_catalog for metadata tables.
* Remove postgres_fdw dependency
* Improve code comments in sql code
2017-06-08 13:55:05 -04:00

47 lines
1.4 KiB
PL/PgSQL

-- Convert a general index definition to a create index sql command for a
-- particular table and index name.
-- static
CREATE OR REPLACE FUNCTION _timescaledb_internal.get_index_definition_for_table(
schema_name NAME,
table_name NAME,
index_name NAME,
general_defintion TEXT
)
RETURNS TEXT LANGUAGE PLPGSQL AS
$BODY$
DECLARE
sql_code TEXT;
BEGIN
sql_code := replace(general_defintion, '/*TABLE_NAME*/', format('%I.%I', schema_name, table_name));
sql_code = replace(sql_code, '/*INDEX_NAME*/', format('%I', index_name));
RETURN sql_code;
END
$BODY$;
-- Creates a chunk_index_row.
CREATE OR REPLACE FUNCTION _timescaledb_internal.create_chunk_index_row(
schema_name NAME,
table_name NAME,
main_schema_name NAME,
main_index_name NAME,
def TEXT
)
RETURNS VOID LANGUAGE PLPGSQL AS
$BODY$
DECLARE
index_name NAME;
id BIGINT;
sql_code TEXT;
BEGIN
id = nextval(pg_get_serial_sequence('_timescaledb_catalog.chunk_index','id'));
index_name := format('%s-%s', id, main_index_name);
sql_code := _timescaledb_internal.get_index_definition_for_table(schema_name, table_name, index_name, def);
INSERT INTO _timescaledb_catalog.chunk_index (id, schema_name, table_name, index_name, main_schema_name,
main_index_name, definition)
VALUES (id, schema_name, table_name, index_name,main_schema_name, main_index_name, sql_code);
END
$BODY$;