timescaledb/sql/partition_triggers.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

31 lines
948 B
PL/PgSQL

-- Checks that the partition's tablespace exists
CREATE OR REPLACE FUNCTION _timescaledb_internal.on_change_partition()
RETURNS TRIGGER LANGUAGE PLPGSQL AS
$BODY$
DECLARE
BEGIN
IF TG_OP = 'INSERT' THEN
IF NEW.tablespace IS NOT NULL THEN
DECLARE
tablespace_row pg_catalog.pg_tablespace;
BEGIN
SELECT * FROM pg_catalog.pg_tablespace t
INTO tablespace_row
WHERE t.spcname = NEW.tablespace;
IF NOT FOUND THEN
RAISE EXCEPTION 'No tablespace % in database %', NEW.tablespace, current_database()
USING ERRCODE = 'IO501';
END IF;
END;
END IF;
RETURN NEW;
ELSIF TG_OP = 'DELETE' THEN
RETURN OLD;
END IF;
PERFORM _timescaledb_internal.on_trigger_error(TG_OP, TG_TABLE_SCHEMA, TG_TABLE_NAME);
END
$BODY$
SET SEARCH_PATH = 'public';