From 05e79909e4eedbdcd9a655e73cbc172d59da80b2 Mon Sep 17 00:00:00 2001 From: "Paul A. Jungwirth" Date: Tue, 17 Jun 2025 23:23:23 -0700 Subject: [PATCH v58 10/10] Add PERIODs - Added parsing for SQL:2011 syntax to define an application-time PERIOD on a table (in both CREATE TABLE and ALTER TABLE). Make sure we create the PERIOD after columns are known (since PERIODs can refer to them) but before constraints are handled (since PERIODs can appear in them). - Added ALTER TABLE DROP support for PERIODs. - Created postgres.pg_period table. - Created information_schema.periods view. - Added pg_dump support. - Added tests and documentation. - Automatically define a constraint for each PERIOD requiring the start column to be less than the end column. - When creating a PERIOD, choose an appropriate range type we can use to implement PERIOD-related operations. You can choose one explicitly if there is ambiguity (due to multiple range types created over the same base type). Author: Paul Jungwirth --- doc/src/sgml/catalogs.sgml | 112 + doc/src/sgml/ddl.sgml | 46 + doc/src/sgml/information_schema.sgml | 63 + doc/src/sgml/plpgsql.sgml | 2 +- doc/src/sgml/ref/alter_table.sgml | 31 +- doc/src/sgml/ref/comment.sgml | 2 + doc/src/sgml/ref/create_table.sgml | 65 +- doc/src/sgml/ref/delete.sgml | 21 +- doc/src/sgml/ref/update.sgml | 21 +- doc/src/sgml/trigger.sgml | 9 +- src/backend/catalog/Makefile | 1 + src/backend/catalog/aclchk.c | 2 + src/backend/catalog/dependency.c | 13 + src/backend/catalog/heap.c | 75 + src/backend/catalog/information_schema.sql | 23 +- src/backend/catalog/meson.build | 1 + src/backend/catalog/objectaddress.c | 72 + src/backend/catalog/pg_constraint.c | 10 +- src/backend/catalog/pg_period.c | 132 + src/backend/catalog/sql_features.txt | 2 +- src/backend/commands/comment.c | 10 + src/backend/commands/dropcmds.c | 1 + src/backend/commands/event_trigger.c | 2 + src/backend/commands/seclabel.c | 1 + src/backend/commands/tablecmds.c | 978 +++- src/backend/commands/trigger.c | 2 + src/backend/commands/view.c | 4 +- src/backend/executor/nodeModifyTable.c | 41 +- src/backend/nodes/nodeFuncs.c | 3 + src/backend/parser/analyze.c | 130 +- src/backend/parser/gram.y | 45 +- src/backend/parser/parse_relation.c | 10 + src/backend/parser/parse_utilcmd.c | 182 +- src/backend/utils/adt/Makefile | 1 + src/backend/utils/adt/period.c | 58 + src/backend/utils/cache/lsyscache.c | 88 + src/bin/pg_dump/pg_backup_archiver.c | 1 + src/bin/pg_dump/pg_dump.c | 180 +- src/bin/pg_dump/pg_dump.h | 15 + src/bin/pg_dump/pg_dump_sort.c | 24 + src/bin/psql/describe.c | 36 + src/include/catalog/Makefile | 1 + src/include/catalog/heap.h | 4 + src/include/catalog/meson.build | 1 + src/include/catalog/pg_constraint.h | 3 +- src/include/catalog/pg_index.h | 1 - src/include/catalog/pg_period.h | 58 + src/include/catalog/pg_range.h | 2 + src/include/commands/tablecmds.h | 4 +- src/include/nodes/execnodes.h | 4 + src/include/nodes/parsenodes.h | 39 +- src/include/nodes/primnodes.h | 2 + src/include/parser/kwlist.h | 2 +- src/include/parser/parse_utilcmd.h | 1 + src/include/utils/lsyscache.h | 3 + src/include/utils/period.h | 21 + .../test_ddl_deparse/test_ddl_deparse.c | 6 + src/test/regress/expected/for_portion_of.out | 58 + src/test/regress/expected/periods.out | 318 ++ src/test/regress/expected/privileges.out | 29 + .../regress/expected/without_overlaps.out | 4500 ++++++++++++++++- src/test/regress/parallel_schedule | 2 +- src/test/regress/sql/for_portion_of.sql | 50 + src/test/regress/sql/periods.sql | 198 + src/test/regress/sql/privileges.sql | 28 + src/test/regress/sql/without_overlaps.sql | 3063 ++++++++++- src/tools/pgindent/typedefs.list | 3 + 67 files changed, 10695 insertions(+), 221 deletions(-) create mode 100644 src/backend/catalog/pg_period.c create mode 100644 src/backend/utils/adt/period.c create mode 100644 src/include/catalog/pg_period.h create mode 100644 src/include/utils/period.h create mode 100644 src/test/regress/expected/periods.out create mode 100644 src/test/regress/sql/periods.sql diff --git a/doc/src/sgml/catalogs.sgml b/doc/src/sgml/catalogs.sgml index 6c8a0f173c9..eff736626b1 100644 --- a/doc/src/sgml/catalogs.sgml +++ b/doc/src/sgml/catalogs.sgml @@ -230,6 +230,11 @@ information about partition key of tables + + pg_period + periods + + pg_policy row-security policies @@ -5773,6 +5778,113 @@ SCRAM-SHA-256$<iteration count>:&l are simple references. + + + + + + + + + <structname>pg_period</structname> + + + pg_period + + + + The catalog pg_period stores + information about system and application time periods. + + + + Periods are described in . + + + + <structname>pg_period</structname> Columns + + + + + + Column Type + + + Description + + + + + + + + oid oid + + + Row identifier + + + + + + pername text + + + Period name + + + + + + perrelid oid + (references pg_class.oid) + + + The table this period belongs to + + + + + + perstart int2 + (references pg_attribute.attnum) + + + The number of the start column + + + + + + perend int2 + (references pg_attribute.attnum) + + + The number of the end column + + + + + + perrange int2 + (references pg_attribute.attnum) + + + The number of the GENERATED column that implements the PERIOD. + + + + + + perconstraint oid + (references pg_constraint.oid) + + + The OID of the period's CHECK constraint + + +
diff --git a/doc/src/sgml/ddl.sgml b/doc/src/sgml/ddl.sgml index 13c174b47a9..cf1730d7bca 100644 --- a/doc/src/sgml/ddl.sgml +++ b/doc/src/sgml/ddl.sgml @@ -1601,6 +1601,52 @@ CREATE TABLE circles ( tables. + + Periods + + + periods + + + + A period is metadata attached to a table + uniting two columns, a start time and end time, into one range-like + construct. Periods are used to represent system time and application time. A system-time + period must be named system_time, and an + application-time period can be named anything else. Their names must + not conflict with column names from the same table. + + + + Application-time periods are referenced in several temporal operations described in + this chapter: temporal primary keys, unique constraints, foreign keys, + update commands, and delete commands. + In all cases, PostgreSQL supports either periods + or regular columns with a rangetype or + multirangetype. + + + + Application periods are defined on a table using the following syntax: + + + +CREATE TABLE billing_addresses ( + customer_id integer, + address_id integer, + valid_from date, + valid_til date, + PERIOD FOR valid_at (valid_from, valid_til) +); + + + + Periods for SYSTEM_TIME are currently not implemented. + + + Application Time diff --git a/doc/src/sgml/information_schema.sgml b/doc/src/sgml/information_schema.sgml index 19dffe7be6a..91b26881c65 100644 --- a/doc/src/sgml/information_schema.sgml +++ b/doc/src/sgml/information_schema.sgml @@ -4171,6 +4171,69 @@ ORDER BY c.ordinal_position;
+ + <literal>periods</literal> + + + The view periods contains information about the + periods of all tables in the current database. The start and end column + names are only shown if the current user has access to them (by way of being + the owner or having some privilege). + + + + <literal>periods</literal> Columns + + + + + Name + Data Type + Description + + + + + + table_catalog + sql_identifier + Name of the database containing the period (always the current database) + + + + table_schema + sql_identifier + Name of the schema containing the period + + + + table_name + sql_identifier + Name of the table containing the period + + + + period_name + sql_identifier + Name of the period + + + + start_column_name + sql_identifier + Name of the start column for the period + + + + end_column_name + sql_identifier + Name of the end column for the period + + + +
+
+ <literal>referential_constraints</literal> diff --git a/doc/src/sgml/plpgsql.sgml b/doc/src/sgml/plpgsql.sgml index f5199872e2e..dab9864c94b 100644 --- a/doc/src/sgml/plpgsql.sgml +++ b/doc/src/sgml/plpgsql.sgml @@ -4252,7 +4252,7 @@ ASSERT condition , TG_PERIOD_NAME text - the column name used in a FOR PORTION OF clause, + the column/period name used in a FOR PORTION OF clause, or else NULL. diff --git a/doc/src/sgml/ref/alter_table.sgml b/doc/src/sgml/ref/alter_table.sgml index bea9f90138b..3309a0a9901 100644 --- a/doc/src/sgml/ref/alter_table.sgml +++ b/doc/src/sgml/ref/alter_table.sgml @@ -62,6 +62,8 @@ ALTER TABLE [ IF EXISTS ] name ALTER CONSTRAINT constraint_name [ INHERIT | NO INHERIT ] VALIDATE CONSTRAINT constraint_name DROP CONSTRAINT [ IF EXISTS ] constraint_name [ RESTRICT | CASCADE ] + ADD PERIOD FOR period_name ( start_column, end_column ) [ WITH ( period_option = value [, ... ] ) ] + DROP PERIOD FOR period_name [ RESTRICT | CASCADE ] DISABLE TRIGGER [ trigger_name | ALL | USER ] ENABLE TRIGGER [ trigger_name | ALL | USER ] ENABLE REPLICA TRIGGER trigger_name @@ -116,10 +118,10 @@ WITH ( MODULUS numeric_literal, REM [ CONSTRAINT constraint_name ] { CHECK ( expression ) [ NO INHERIT ] | NOT NULL column_name [ NO INHERIT ] | - UNIQUE [ NULLS [ NOT ] DISTINCT ] ( column_name [, ... ] [, column_name WITHOUT OVERLAPS ] ) index_parameters | - PRIMARY KEY ( column_name [, ... ] [, column_name WITHOUT OVERLAPS ] ) index_parameters | + UNIQUE [ NULLS [ NOT ] DISTINCT ] ( column_name [, ... ] [, { column_name | period_name } WITHOUT OVERLAPS ] ) index_parameters | + PRIMARY KEY ( column_name [, ... ] [, { column_name | period_name } WITHOUT OVERLAPS ] ) index_parameters | EXCLUDE [ USING index_method ] ( exclude_element WITH operator [, ... ] ) index_parameters [ WHERE ( predicate ) ] | - FOREIGN KEY ( column_name [, ... ] [, PERIOD column_name ] ) REFERENCES reftable [ ( refcolumn [, ... ] [, PERIOD refcolumn ] ) ] + FOREIGN KEY ( column_name [, ... ] [, PERIOD { column_name | period_name } ] ) REFERENCES reftable [ ( refcolumn [, ... ] [, PERIOD { refcolumn | period_name } ] ) ] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE referential_action ] [ ON UPDATE referential_action ] } [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] [ ENFORCED | NOT ENFORCED ] @@ -624,6 +626,29 @@ WITH ( MODULUS numeric_literal, REM + + ADD PERIOD FOR + + + This form adds a new period to a table using the same syntax as + . + + + + + + DROP PERIOD FOR + + + This form drops the specified period on a table. The start and end + columns will not be dropped by this command but the + CHECK constraint will be. You will need to say + CASCADE if anything outside the table depends on the + column. + + + + DISABLE/ENABLE [ REPLICA | ALWAYS ] TRIGGER diff --git a/doc/src/sgml/ref/comment.sgml b/doc/src/sgml/ref/comment.sgml index 5b43c56b133..49c2df99449 100644 --- a/doc/src/sgml/ref/comment.sgml +++ b/doc/src/sgml/ref/comment.sgml @@ -44,6 +44,7 @@ COMMENT ON OPERATOR operator_name (left_type, right_type) | OPERATOR CLASS object_name USING index_method | OPERATOR FAMILY object_name USING index_method | + PERIOD relation_name.period_name | POLICY policy_name ON table_name | [ PROCEDURAL ] LANGUAGE object_name | PROCEDURE procedure_name [ ( [ [ argmode ] [ argname ] argtype [, ...] ] ) ] | @@ -341,6 +342,7 @@ COMMENT ON OPERATOR ^ (text, text) IS 'Performs intersection of two texts'; COMMENT ON OPERATOR - (NONE, integer) IS 'Unary minus'; COMMENT ON OPERATOR CLASS int4ops USING btree IS '4 byte integer operators for btrees'; COMMENT ON OPERATOR FAMILY integer_ops USING btree IS 'all integer operators for btrees'; +COMMENT ON PERIOD my_table.my_column IS 'Sales promotion validity'; COMMENT ON POLICY my_policy ON mytable IS 'Filter rows by users'; COMMENT ON PROCEDURE my_proc (integer, integer) IS 'Runs a report'; COMMENT ON PUBLICATION alltables IS 'Publishes all operations on all tables'; diff --git a/doc/src/sgml/ref/create_table.sgml b/doc/src/sgml/ref/create_table.sgml index 975d5daa0c1..f3dcda78cf4 100644 --- a/doc/src/sgml/ref/create_table.sgml +++ b/doc/src/sgml/ref/create_table.sgml @@ -23,6 +23,7 @@ PostgreSQL documentation CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] table_name ( [ { column_name data_type [ STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN | DEFAULT } ] [ COMPRESSION compression_method ] [ COLLATE collation ] [ column_constraint [ ... ] ] + | period_definition | table_constraint | LIKE source_table [ like_option ... ] } [, ... ] @@ -37,6 +38,7 @@ CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXI CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] table_name OF type_name [ ( { column_name [ WITH OPTIONS ] [ column_constraint [ ... ] ] + | period_definition | table_constraint } [, ... ] ) ] @@ -49,6 +51,7 @@ CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXI CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] table_name PARTITION OF parent_table [ ( { column_name [ WITH OPTIONS ] [ column_constraint [ ... ] ] + | period_definition | table_constraint } [, ... ] ) ] { FOR VALUES partition_bound_spec | DEFAULT } @@ -73,15 +76,20 @@ CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXI [ ON DELETE referential_action ] [ ON UPDATE referential_action ] } [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] [ ENFORCED | NOT ENFORCED ] +and period_definition is: + +PERIOD FOR { period_name | SYSTEM_TIME } ( column_name, column_name ) +[ WITH ( period_option = value [, ... ] ) ] + and table_constraint is: [ CONSTRAINT constraint_name ] { CHECK ( expression ) [ NO INHERIT ] | NOT NULL column_name [ NO INHERIT ] | - UNIQUE [ NULLS [ NOT ] DISTINCT ] ( column_name [, ... ] [, column_name WITHOUT OVERLAPS ] ) index_parameters | - PRIMARY KEY ( column_name [, ... ] [, column_name WITHOUT OVERLAPS ] ) index_parameters | + UNIQUE [ NULLS [ NOT ] DISTINCT ] ( column_name [, ... ] [, { column_name | period_name } WITHOUT OVERLAPS ] ) index_parameters | + PRIMARY KEY ( column_name [, ... ] [, { column_name | period_name } WITHOUT OVERLAPS ] ) index_parameters | EXCLUDE [ USING index_method ] ( exclude_element WITH operator [, ... ] ) index_parameters [ WHERE ( predicate ) ] | - FOREIGN KEY ( column_name [, ... ] [, PERIOD column_name ] ) REFERENCES reftable [ ( refcolumn [, ... ] [, PERIOD refcolumn ] ) ] + FOREIGN KEY ( column_name [, ... ] [, PERIOD { column_name | period_name } ] ) REFERENCES reftable [ ( refcolumn [, ... ] [, PERIOD { refcolumn | period_name } ] ) ] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE referential_action ] [ ON UPDATE referential_action ] } [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] [ ENFORCED | NOT ENFORCED ] @@ -140,6 +148,14 @@ WITH ( MODULUS numeric_literal, REM name as any existing data type in the same schema. + + Periods may be defined on tables, specifying that two existing columns + represent start and end values for the period. Periods may have any name + that doesn't conflict with a column name, but the name + SYSTEM_TIME is special, used for versioning tables. + System periods are not yet implemented. See for more details. + + The optional constraint clauses specify constraints (tests) that new or updated rows must satisfy for an insert or update operation @@ -800,6 +816,37 @@ WITH ( MODULUS numeric_literal, REM + + PERIOD FOR period_name ( column_name, column_name ) [ WITH ( period_option = value [, ... ] ) ] + + + A period definition gives semantic meaning to two existing columns of + the table. It defines a "start column" and an "end column" where the + start value is strictly less than the end value. A + CHECK constraint is automatically created to enforce + this. You can specify the name of that constraint with the + check_constraint_name period_option. + + + + Both columns must have exactly the same type and must have a range type + defined from their base type. If there are several range types for that + base type, you must specify which one you want by using the + rangetype period_option. + Any base type is allowed, as long as it has a range type, although it is + expected that most periods will use temporal types like timestamptz + or date. + + + + A hidden GENERATED column is used to implement the period, + with the same name as the period. If such a column already exists, you + can use the colexists period_option + with value true to avoid creating it. + + + + CONSTRAINT constraint_name @@ -1184,8 +1231,8 @@ WITH ( MODULUS numeric_literal, REM REFERENCES reftable [ ( refcolumn ) ] [ MATCH matchtype ] [ ON DELETE referential_action ] [ ON UPDATE referential_action ] (column constraint) - FOREIGN KEY ( column_name [, ... ] [, PERIOD column_name ] ) - REFERENCES reftable [ ( refcolumn [, ... ] [, PERIOD refcolumn ] ) ] + FOREIGN KEY ( column_name [, ... ] [, PERIOD { column_name | period_name } ] ) + REFERENCES reftable [ ( refcolumn [, ... ] [, PERIOD { refcolumn | period_name } ] ) ] [ MATCH matchtype ] [ ON DELETE referential_action ] [ ON UPDATE referential_action ] @@ -1205,7 +1252,7 @@ WITH ( MODULUS numeric_literal, REM - If the last column is marked with PERIOD, it is + If the last key part is marked with PERIOD, it is treated in a special way. While the non-PERIOD columns are compared for equality (and there must be at least one of them), the PERIOD column is not. Instead, the @@ -1213,8 +1260,10 @@ WITH ( MODULUS numeric_literal, REM records (based on the non-PERIOD parts of the key) whose combined PERIOD values completely cover the referencing record's. In other words, the reference must have a - referent for its entire duration. This column must be a range or - multirange type. In addition, the referenced table must have a primary + referent for its entire duration. This part must be either a + PERIOD + or a column with a range or multirange type. + In addition, the referenced table must have a primary key or unique constraint declared with WITHOUT OVERLAPS. Finally, if the foreign key has a PERIOD column_name specification diff --git a/doc/src/sgml/ref/delete.sgml b/doc/src/sgml/ref/delete.sgml index f425309fd5d..6abb1b5dc09 100644 --- a/doc/src/sgml/ref/delete.sgml +++ b/doc/src/sgml/ref/delete.sgml @@ -23,7 +23,7 @@ PostgreSQL documentation [ WITH [ RECURSIVE ] with_query [, ...] ] DELETE FROM [ ONLY ] table_name [ * ] - [ FOR PORTION OF range_name for_portion_of_target ] + [ FOR PORTION OF range_or_period_name for_portion_of_target ] [ [ AS ] alias ] [ USING from_item [, ...] ] [ WHERE condition | WHERE CURRENT OF cursor_name ] @@ -58,7 +58,8 @@ DELETE FROM [ ONLY ] table_name [ * - If the table has a range or multirange column, + If the table has a range or multirange column, or a + PERIOD, you may supply a FOR PORTION OF clause, and your delete will only affect rows that overlap the given interval. Furthermore, if a row's history extends outside the FOR PORTION OF bounds, then your delete @@ -157,10 +158,10 @@ DELETE FROM [ ONLY ] table_name [ * - range_name + range_or_period_name - The range or multirange column to use when performing a temporal delete. + The range or multirange column or period to use when performing a temporal delete. @@ -169,14 +170,14 @@ DELETE FROM [ ONLY ] table_name [ * for_portion_of_target - The interval to delete. If you are targeting a range column, + The interval to delete. If you are targeting a range column or PERIOD, you may give this in the form FROM start_time TO end_time. Otherwise you must use (expression) where the expression yields a value of the same type as - range_name. + range_or_period_name. @@ -186,8 +187,8 @@ DELETE FROM [ ONLY ] table_name [ * The earliest time (inclusive) to change in a temporal delete. - This must be a value matching the base type of the range from - range_name. A + This must be a value matching the base type of the range or period from + range_or_period_name. A NULL here indicates a delete whose beginning is unbounded (as with range types). @@ -199,8 +200,8 @@ DELETE FROM [ ONLY ] table_name [ * The latest time (exclusive) to change in a temporal delete. - This must be a value matching the base type of the range from - range_name. A + This must be a value matching the base type of the range or period from + range_or_period_name. A NULL here indicates a delete whose end is unbounded (as with range types). diff --git a/doc/src/sgml/ref/update.sgml b/doc/src/sgml/ref/update.sgml index ad3224c2df2..fc22de5b48b 100644 --- a/doc/src/sgml/ref/update.sgml +++ b/doc/src/sgml/ref/update.sgml @@ -23,7 +23,7 @@ PostgreSQL documentation [ WITH [ RECURSIVE ] with_query [, ...] ] UPDATE [ ONLY ] table_name [ * ] - [ FOR PORTION OF range_name for_portion_of_target ] + [ FOR PORTION OF range_or_period_name for_portion_of_target ] [ [ AS ] alias ] SET { column_name = { expression | DEFAULT } | ( column_name [, ...] ) = [ ROW ] ( { expression | DEFAULT } [, ...] ) | @@ -55,7 +55,8 @@ UPDATE [ ONLY ] table_name [ * ] - If the table has a range or multirange column, + If the table has a range or multirange column, or a + PERIOD, you may supply a FOR PORTION OF clause, and your update will only affect rows that overlap the given interval. Furthermore, if a row's history extends outside the FOR PORTION OF bounds, then your update @@ -158,10 +159,10 @@ UPDATE [ ONLY ] table_name [ * ] - range_name + range_or_period_name - The range or multirange column to use when performing a temporal update. + The range or multirange column or period to use when performing a temporal update. @@ -170,14 +171,14 @@ UPDATE [ ONLY ] table_name [ * ] for_portion_of_target - The interval to update. If you are targeting a range column, + The interval to update. If you are targeting a range column or PERIOD, you may give this in the form FROM start_time TO end_time. Otherwise you must use (expression) where the expression yields a value of the same type as - range_name. + range_or_period_name. @@ -187,8 +188,8 @@ UPDATE [ ONLY ] table_name [ * ] The earliest time (inclusive) to change in a temporal update. - This must be a value matching the base type of the range from - range_name. A + This must be a value matching the base type of the range or period from + range_or_period_name. A NULL here indicates an update whose beginning is unbounded (as with range types). @@ -200,8 +201,8 @@ UPDATE [ ONLY ] table_name [ * ] The latest time (exclusive) to change in a temporal update. - This must be a value matching the base type of the range from - range_name. A + This must be a value matching the base type of the range or period from + range_or_period_name. A NULL here indicates an update whose end is unbounded (as with range types). diff --git a/doc/src/sgml/trigger.sgml b/doc/src/sgml/trigger.sgml index 0044a97a3fd..da9ae0ffefd 100644 --- a/doc/src/sgml/trigger.sgml +++ b/doc/src/sgml/trigger.sgml @@ -860,16 +860,21 @@ typedef struct ForPortionOfState char *fp_rangeName; /* the column named in FOR PORTION OF */ Oid fp_rangeType; /* the type of the FOR PORTION OF expression */ - int fp_rangeAttno; /* the attno of the range column */ + int fp_rangeAttno; /* the attno of the range column (or 0 for a PERIOD) */ + int fp_periodStartAttno; /* the attno of the PERIOD start column (or 0 for a range) */ + int fp_periodEndAttno; /* the attno of the PERIOD end column (or 0 for a range) */ Datum fp_targetRange; /* the range/multirange from FOR PORTION OF */ TypeCacheEntry *fp_leftoverstypcache; /* type cache entry of the range */ } ForPortionOfState; - where fp_rangeName is the range + where fp_rangeName is the period or range column named in the FOR PORTION OF clause, fp_rangeType is its range type, fp_rangeAttno is its attribute number, + fp_periodStartAttno and + fp_periodEndAttno are the attnos of the period's + start and end columns (or 0 if a range column was used), and fp_targetRange is a rangetype value created by evaluating the FOR PORTION OF bounds. diff --git a/src/backend/catalog/Makefile b/src/backend/catalog/Makefile index c090094ed08..53eb67034f3 100644 --- a/src/backend/catalog/Makefile +++ b/src/backend/catalog/Makefile @@ -39,6 +39,7 @@ OBJS = \ pg_namespace.o \ pg_operator.o \ pg_parameter_acl.o \ + pg_period.o \ pg_proc.o \ pg_publication.o \ pg_range.o \ diff --git a/src/backend/catalog/aclchk.c b/src/backend/catalog/aclchk.c index cd139bd65a6..8d006a830d6 100644 --- a/src/backend/catalog/aclchk.c +++ b/src/backend/catalog/aclchk.c @@ -2774,6 +2774,7 @@ aclcheck_error(AclResult aclerr, ObjectType objtype, case OBJECT_DEFAULT: case OBJECT_DEFACL: case OBJECT_DOMCONSTRAINT: + case OBJECT_PERIOD: case OBJECT_PUBLICATION_NAMESPACE: case OBJECT_PUBLICATION_REL: case OBJECT_ROLE: @@ -2915,6 +2916,7 @@ aclcheck_error(AclResult aclerr, ObjectType objtype, case OBJECT_DEFACL: case OBJECT_DOMCONSTRAINT: case OBJECT_PARAMETER_ACL: + case OBJECT_PERIOD: case OBJECT_PUBLICATION_NAMESPACE: case OBJECT_PUBLICATION_REL: case OBJECT_ROLE: diff --git a/src/backend/catalog/dependency.c b/src/backend/catalog/dependency.c index 7dded634eb8..0111df6081d 100644 --- a/src/backend/catalog/dependency.c +++ b/src/backend/catalog/dependency.c @@ -48,6 +48,7 @@ #include "catalog/pg_operator.h" #include "catalog/pg_opfamily.h" #include "catalog/pg_parameter_acl.h" +#include "catalog/pg_period.h" #include "catalog/pg_policy.h" #include "catalog/pg_proc.h" #include "catalog/pg_publication.h" @@ -622,6 +623,14 @@ findDependentObjects(const ObjectAddress *object, return; } + /* + * If a table attribute is an internal part of something + * else (e.g. the GENERATED column used by a PERIOD), and + * we are deleting the whole table, then it's okay. + */ + if (foundDep->objsubid && !object->objectSubId) + break; + /* * We postpone actually issuing the error message until * after this loop, so that we can make the behavior @@ -1400,6 +1409,10 @@ doDeletion(const ObjectAddress *object, int flags) RemoveAttrDefaultById(object->objectId); break; + case PeriodRelationId: + RemovePeriodById(object->objectId); + break; + case LargeObjectRelationId: LargeObjectDrop(object->objectId); break; diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c index fd6537567ea..99cda809d02 100644 --- a/src/backend/catalog/heap.c +++ b/src/backend/catalog/heap.c @@ -49,6 +49,7 @@ #include "catalog/pg_namespace.h" #include "catalog/pg_opclass.h" #include "catalog/pg_partitioned_table.h" +#include "catalog/pg_period.h" #include "catalog/pg_statistic.h" #include "catalog/pg_subscription_rel.h" #include "catalog/pg_tablespace.h" @@ -2135,6 +2136,80 @@ SetAttrMissing(Oid relid, char *attname, char *value) table_close(tablerel, AccessExclusiveLock); } +/* + * Store a period of relation rel. + * + * Returns the OID of the new pg_period tuple. + */ +Oid +StorePeriod(Relation rel, const char *periodname, AttrNumber startnum, + AttrNumber endnum, AttrNumber rangenum, Oid conoid) +{ + Datum values[Natts_pg_period]; + bool nulls[Natts_pg_period]; + Relation pg_period; + HeapTuple tuple; + Oid oid; + NameData pername; + ObjectAddress myself, + referenced; + + Assert(rangenum != InvalidAttrNumber); + + namestrcpy(&pername, periodname); + + MemSet(values, 0, sizeof(values)); + MemSet(nulls, false, sizeof(nulls)); + + pg_period = table_open(PeriodRelationId, RowExclusiveLock); + + oid = GetNewOidWithIndex(pg_period, AttrDefaultOidIndexId, Anum_pg_period_oid); + values[Anum_pg_period_oid - 1] = ObjectIdGetDatum(oid); + values[Anum_pg_period_pername - 1] = NameGetDatum(&pername); + values[Anum_pg_period_perrelid - 1] = RelationGetRelid(rel); + values[Anum_pg_period_perstart - 1] = startnum; + values[Anum_pg_period_perend - 1] = endnum; + values[Anum_pg_period_perrange - 1] = rangenum; + values[Anum_pg_period_perconstraint - 1] = conoid; + + tuple = heap_form_tuple(RelationGetDescr(pg_period), values, nulls); + CatalogTupleInsert(pg_period, tuple); + + ObjectAddressSet(myself, PeriodRelationId, oid); + + /* Drop the period when the table is dropped. */ + ObjectAddressSet(referenced, RelationRelationId, RelationGetRelid(rel)); + recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO); + + /* Forbid dropping the columns of the period. */ + ObjectAddressSubSet(referenced, RelationRelationId, RelationGetRelid(rel), startnum); + recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL); + ObjectAddressSubSet(referenced, RelationRelationId, RelationGetRelid(rel), endnum); + recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL); + + /* + * The range column is an implementation detail, but we can't use + * DEPENDENCY_INTERNAL because dropping the table will check for + * dependencies on all subobjects too (in findDependentObjects). But if we + * make an AUTO dependency one way we will auto-drop the column when we + * drop the PERIOD, and a NORMAL dependency the other way we will forbid + * dropping the column directly. + */ + ObjectAddressSubSet(referenced, RelationRelationId, RelationGetRelid(rel), rangenum); + recordDependencyOn(&referenced, &myself, DEPENDENCY_INTERNAL); + + /* + * The constraint is an implementation detail, so we mark it as such. + * (Note that myself and referenced are reversed for this one.) + */ + ObjectAddressSet(referenced, ConstraintRelationId, conoid); + recordDependencyOn(&referenced, &myself, DEPENDENCY_INTERNAL); + + table_close(pg_period, RowExclusiveLock); + + return oid; +} + /* * Store a check-constraint expression for the given relation. * diff --git a/src/backend/catalog/information_schema.sql b/src/backend/catalog/information_schema.sql index a7bffca93d1..282ead6ed4c 100644 --- a/src/backend/catalog/information_schema.sql +++ b/src/backend/catalog/information_schema.sql @@ -1207,7 +1207,28 @@ GRANT SELECT ON parameters TO PUBLIC; * PERIODS view */ --- feature not supported +CREATE VIEW periods AS + SELECT current_database()::information_schema.sql_identifier AS table_catalog, + nc.nspname::information_schema.sql_identifier AS table_schema, + c.relname::information_schema.sql_identifier AS table_name, + p.pername::information_schema.sql_identifier AS period_name, + CASE WHEN pg_has_role(c.relowner, 'USAGE') + OR has_column_privilege(sa.attrelid, sa.attnum, 'SELECT, INSERT, UPDATE, REFERENCES') + THEN sa.attname::information_schema.sql_identifier + END AS start_column_name, + CASE WHEN pg_has_role(c.relowner, 'USAGE') + OR has_column_privilege(ea.attrelid, ea.attnum, 'SELECT, INSERT, UPDATE, REFERENCES') + THEN ea.attname::information_schema.sql_identifier + END AS end_column_name + FROM pg_period AS p + JOIN pg_class AS c ON c.oid = p.perrelid + JOIN pg_namespace AS nc ON nc.oid = c.relnamespace + JOIN pg_attribute AS sa ON (sa.attrelid, sa.attnum) = (p.perrelid, p.perstart) + JOIN pg_attribute AS ea ON (ea.attrelid, ea.attnum) = (p.perrelid, p.perend) + WHERE NOT pg_is_other_temp_schema(nc.oid) + AND c.relkind IN ('r', 'v'); + +GRANT SELECT ON periods TO PUBLIC; /* diff --git a/src/backend/catalog/meson.build b/src/backend/catalog/meson.build index 1958ea9238a..6cdf89d0e93 100644 --- a/src/backend/catalog/meson.build +++ b/src/backend/catalog/meson.build @@ -26,6 +26,7 @@ backend_sources += files( 'pg_namespace.c', 'pg_operator.c', 'pg_parameter_acl.c', + 'pg_period.c', 'pg_proc.c', 'pg_publication.c', 'pg_range.c', diff --git a/src/backend/catalog/objectaddress.c b/src/backend/catalog/objectaddress.c index c75b7131ed7..e2b099e2360 100644 --- a/src/backend/catalog/objectaddress.c +++ b/src/backend/catalog/objectaddress.c @@ -45,6 +45,7 @@ #include "catalog/pg_operator.h" #include "catalog/pg_opfamily.h" #include "catalog/pg_parameter_acl.h" +#include "catalog/pg_period.h" #include "catalog/pg_policy.h" #include "catalog/pg_proc.h" #include "catalog/pg_publication.h" @@ -726,6 +727,9 @@ static const struct object_type_map { "domain constraint", OBJECT_DOMCONSTRAINT }, + { + "period", OBJECT_PERIOD + }, { "conversion", OBJECT_CONVERSION }, @@ -970,6 +974,7 @@ get_object_address(ObjectType objtype, Node *object, case OBJECT_TRIGGER: case OBJECT_TABCONSTRAINT: case OBJECT_POLICY: + case OBJECT_PERIOD: address = get_object_address_relobject(objtype, castNode(List, object), &relation, missing_ok); break; @@ -1472,6 +1477,13 @@ get_object_address_relobject(ObjectType objtype, List *object, InvalidOid; address.objectSubId = 0; break; + case OBJECT_PERIOD: + address.classId = PeriodRelationId; + address.objectId = relation ? + get_relation_period_oid(reloid, depname, missing_ok) : + InvalidOid; + address.objectSubId = 0; + break; default: elog(ERROR, "unrecognized object type: %d", (int) objtype); } @@ -2293,6 +2305,7 @@ pg_get_object_address(PG_FUNCTION_ARGS) case OBJECT_RULE: case OBJECT_TRIGGER: case OBJECT_TABCONSTRAINT: + case OBJECT_PERIOD: case OBJECT_OPCLASS: case OBJECT_OPFAMILY: objnode = (Node *) name; @@ -2403,6 +2416,7 @@ check_object_ownership(Oid roleid, ObjectType objtype, ObjectAddress address, case OBJECT_TRIGGER: case OBJECT_POLICY: case OBJECT_TABCONSTRAINT: + case OBJECT_PERIOD: if (!object_ownercheck(RelationRelationId, RelationGetRelid(relation), roleid)) aclcheck_error(ACLCHECK_NOT_OWNER, objtype, RelationGetRelationName(relation)); @@ -3076,6 +3090,38 @@ getObjectDescription(const ObjectAddress *object, bool missing_ok) break; } + case PeriodRelationId: + { + HeapTuple perTup; + Form_pg_period per; + + perTup = SearchSysCache1(PERIODOID, + ObjectIdGetDatum(object->objectId)); + if (!HeapTupleIsValid(perTup)) + elog(ERROR, "cache lookup failed for period %u", + object->objectId); + per = (Form_pg_period) GETSTRUCT(perTup); + + if (OidIsValid(per->perrelid)) + { + StringInfoData rel; + + initStringInfo(&rel); + getRelationDescription(&rel, per->perrelid, false); + appendStringInfo(&buffer, _("period %s on %s"), + NameStr(per->pername), rel.data); + pfree(rel.data); + } + else + { + appendStringInfo(&buffer, _("period %s"), + NameStr(per->pername)); + } + + ReleaseSysCache(perTup); + break; + } + case ConversionRelationId: { HeapTuple conTup; @@ -4529,6 +4575,10 @@ getObjectTypeDescription(const ObjectAddress *object, bool missing_ok) missing_ok); break; + case PeriodRelationId: + appendStringInfoString(&buffer, "period"); + break; + case ConversionRelationId: appendStringInfoString(&buffer, "conversion"); break; @@ -5034,6 +5084,28 @@ getObjectIdentityParts(const ObjectAddress *object, break; } + case PeriodRelationId: + { + HeapTuple perTup; + Form_pg_period per; + + perTup = SearchSysCache1(PERIODOID, + ObjectIdGetDatum(object->objectId)); + if (!HeapTupleIsValid(perTup)) + elog(ERROR, "cache lookup failed for period %u", + object->objectId); + per = (Form_pg_period) GETSTRUCT(perTup); + + appendStringInfo(&buffer, "%s on ", + quote_identifier(NameStr(per->pername))); + getRelationIdentity(&buffer, per->perrelid, objname, false); + if (objname) + *objname = lappend(*objname, pstrdup(NameStr(per->pername))); + + ReleaseSysCache(perTup); + break; + } + case ConversionRelationId: { HeapTuple conTup; diff --git a/src/backend/catalog/pg_constraint.c b/src/backend/catalog/pg_constraint.c index 77152252ea1..2c0451d6930 100644 --- a/src/backend/catalog/pg_constraint.c +++ b/src/backend/catalog/pg_constraint.c @@ -812,9 +812,14 @@ AdjustNotNullInheritance(Oid relid, AttrNumber attnum, * This is seldom needed, so we just scan pg_constraint each time. * * 'include_noinh' determines whether to include NO INHERIT constraints or not. + * + * 'excludes' contains a list of attnos whose constraints we should exclude, + * for example constraints on PERIODs' hidden GENERATED columns (since those + * aren't exposed to users). */ List * -RelationGetNotNullConstraints(Oid relid, bool cooked, bool include_noinh) +RelationGetNotNullConstraints(Oid relid, bool cooked, bool include_noinh, + Bitmapset *excludes) { List *notnulls = NIL; Relation constrRel; @@ -842,6 +847,9 @@ RelationGetNotNullConstraints(Oid relid, bool cooked, bool include_noinh) colnum = extractNotNullColumn(htup); + if (bms_is_member(colnum, excludes)) + continue; + if (cooked) { CookedConstraint *cooked; diff --git a/src/backend/catalog/pg_period.c b/src/backend/catalog/pg_period.c new file mode 100644 index 00000000000..24b1938532b --- /dev/null +++ b/src/backend/catalog/pg_period.c @@ -0,0 +1,132 @@ +/*------------------------------------------------------------------------- + * + * pg_period.c + * routines to support manipulation of the pg_period relation + * + * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group + * + * + * IDENTIFICATION + * src/backend/catalog/pg_period.c + * + *------------------------------------------------------------------------- + */ +#include "postgres.h" + +#include "access/genam.h" +#include "access/heapam.h" +#include "access/htup_details.h" +#include "catalog/indexing.h" +#include "catalog/pg_period.h" +#include "utils/fmgroids.h" +#include "utils/lsyscache.h" +#include "utils/rel.h" +#include "utils/syscache.h" + + +/* + * Delete a single period record. + */ +void +RemovePeriodById(Oid periodId) +{ + Relation pg_period; + HeapTuple tup; + + pg_period = table_open(PeriodRelationId, RowExclusiveLock); + + tup = SearchSysCache1(PERIODOID, ObjectIdGetDatum(periodId)); + if (!HeapTupleIsValid(tup)) /* should not happen */ + elog(ERROR, "cache lookup failed for period %u", periodId); + + /* Fry the period itself */ + CatalogTupleDelete(pg_period, &tup->t_self); + + /* Clean up */ + ReleaseSysCache(tup); + table_close(pg_period, RowExclusiveLock); +} + +/* + * get_relation_period_oid + * Find a period on the specified relation with the specified name. + * Returns period's OID. + */ +Oid +get_relation_period_oid(Oid relid, const char *pername, bool missing_ok) +{ + Relation pg_period; + HeapTuple tuple; + SysScanDesc scan; + ScanKeyData skey[2]; + Oid perOid = InvalidOid; + + /* Fetch the period tuple from pg_period. */ + pg_period = table_open(PeriodRelationId, AccessShareLock); + + ScanKeyInit(&skey[0], + Anum_pg_period_perrelid, + BTEqualStrategyNumber, F_OIDEQ, + ObjectIdGetDatum(relid)); + ScanKeyInit(&skey[1], + Anum_pg_period_pername, + BTEqualStrategyNumber, F_NAMEEQ, + CStringGetDatum(pername)); + + scan = systable_beginscan(pg_period, PeriodRelidNameIndexId, true, + NULL, 2, skey); + + /* There can be at most one matching row */ + if (HeapTupleIsValid(tuple = systable_getnext(scan))) + perOid = ((Form_pg_period) GETSTRUCT(tuple))->oid; + + systable_endscan(scan); + + /* If no such period exists, complain */ + if (!OidIsValid(perOid) && !missing_ok) + ereport(ERROR, + (errcode(ERRCODE_UNDEFINED_OBJECT), + errmsg("period \"%s\" for table \"%s\" does not exist", + pername, get_rel_name(relid)))); + + table_close(pg_period, AccessShareLock); + + return perOid; +} + +/* + * get_period_attnos + * Get the attno of the GENERATED rangetype column + * for all PERIODs in this table. + */ +extern Bitmapset * +get_period_attnos(Oid relid) +{ + Bitmapset *attnos = NULL; + Relation pg_period; + HeapTuple tuple; + SysScanDesc scan; + ScanKeyData skey[1]; + + pg_period = table_open(PeriodRelationId, AccessShareLock); + + ScanKeyInit(&skey[0], + Anum_pg_period_perrelid, + BTEqualStrategyNumber, F_OIDEQ, + ObjectIdGetDatum(relid)); + + scan = systable_beginscan(pg_period, PeriodRelidNameIndexId, true, + NULL, 1, skey); + + while (HeapTupleIsValid(tuple = systable_getnext(scan))) + { + Form_pg_period period = (Form_pg_period) GETSTRUCT(tuple); + + attnos = bms_add_member(attnos, period->perrange); + } + + systable_endscan(scan); + table_close(pg_period, AccessShareLock); + + return attnos; +} diff --git a/src/backend/catalog/sql_features.txt b/src/backend/catalog/sql_features.txt index 3a8ad201607..016b67bcf1c 100644 --- a/src/backend/catalog/sql_features.txt +++ b/src/backend/catalog/sql_features.txt @@ -448,7 +448,7 @@ T176 Sequence generator support NO supported except for NEXT VALUE FOR T177 Sequence generator support: simple restart option YES T178 Identity columns: simple restart option YES T180 System-versioned tables NO -T181 Application-time period tables NO +T181 Application-time period tables YES T191 Referential action RESTRICT YES T200 Trigger DDL NO similar but not fully compatible T201 Comparable data types for referential constraints YES diff --git a/src/backend/commands/comment.c b/src/backend/commands/comment.c index 5c783cc61f1..36306c618b2 100644 --- a/src/backend/commands/comment.c +++ b/src/backend/commands/comment.c @@ -102,6 +102,16 @@ CommentObject(CommentStmt *stmt) RelationGetRelationName(relation)), errdetail_relkind_not_supported(relation->rd_rel->relkind))); break; + + case OBJECT_PERIOD: + /* Periods can only go on tables */ + if (relation->rd_rel->relkind != RELKIND_RELATION) + ereport(ERROR, + (errcode(ERRCODE_WRONG_OBJECT_TYPE), + errmsg("\"%s\" is not a table", + RelationGetRelationName(relation)))); + break; + default: break; } diff --git a/src/backend/commands/dropcmds.c b/src/backend/commands/dropcmds.c index ceb9a229b63..526c3ff5142 100644 --- a/src/backend/commands/dropcmds.c +++ b/src/backend/commands/dropcmds.c @@ -504,6 +504,7 @@ does_not_exist_skipping(ObjectType objtype, Node *object) case OBJECT_DOMCONSTRAINT: case OBJECT_LARGEOBJECT: case OBJECT_PARAMETER_ACL: + case OBJECT_PERIOD: case OBJECT_PUBLICATION_NAMESPACE: case OBJECT_PUBLICATION_REL: case OBJECT_TABCONSTRAINT: diff --git a/src/backend/commands/event_trigger.c b/src/backend/commands/event_trigger.c index f34868da5ab..514da04be77 100644 --- a/src/backend/commands/event_trigger.c +++ b/src/backend/commands/event_trigger.c @@ -2301,6 +2301,7 @@ stringify_grant_objtype(ObjectType objtype) case OBJECT_OPCLASS: case OBJECT_OPERATOR: case OBJECT_OPFAMILY: + case OBJECT_PERIOD: case OBJECT_POLICY: case OBJECT_PUBLICATION: case OBJECT_PUBLICATION_NAMESPACE: @@ -2385,6 +2386,7 @@ stringify_adefprivs_objtype(ObjectType objtype) case OBJECT_OPERATOR: case OBJECT_OPFAMILY: case OBJECT_PARAMETER_ACL: + case OBJECT_PERIOD: case OBJECT_POLICY: case OBJECT_PUBLICATION: case OBJECT_PUBLICATION_NAMESPACE: diff --git a/src/backend/commands/seclabel.c b/src/backend/commands/seclabel.c index cee5d7bbb9c..f8650eb4fe4 100644 --- a/src/backend/commands/seclabel.c +++ b/src/backend/commands/seclabel.c @@ -79,6 +79,7 @@ SecLabelSupportsObjectType(ObjectType objtype) case OBJECT_OPERATOR: case OBJECT_OPFAMILY: case OBJECT_PARAMETER_ACL: + case OBJECT_PERIOD: case OBJECT_POLICY: case OBJECT_PUBLICATION_NAMESPACE: case OBJECT_PUBLICATION_REL: diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 17635eb8a3a..a349e80a7b4 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -45,6 +45,7 @@ #include "catalog/pg_largeobject_metadata.h" #include "catalog/pg_namespace.h" #include "catalog/pg_opclass.h" +#include "catalog/pg_period.h" #include "catalog/pg_policy.h" #include "catalog/pg_proc.h" #include "catalog/pg_publication_rel.h" @@ -156,6 +157,13 @@ typedef enum AlterTablePass AT_PASS_OLD_INDEX, /* re-add existing indexes */ AT_PASS_OLD_CONSTR, /* re-add existing constraints */ /* We could support a RENAME COLUMN pass here, but not currently used */ + + /* + * We must add PERIODs after columns, in case they reference a newly-added + * column, and before constraints, in case a newly-added PK/FK references + * them. + */ + AT_PASS_ADD_PERIOD, /* ADD PERIOD */ AT_PASS_ADD_CONSTR, /* ADD constraints (initial examination) */ AT_PASS_COL_ATTRS, /* set column attributes, eg NOT NULL */ AT_PASS_ADD_INDEXCONSTR, /* ADD index-based constraints */ @@ -375,6 +383,7 @@ static List *MergeAttributes(List *columns, const List *supers, char relpersiste bool is_partition, List **supconstr, List **supnotnulls); static List *MergeCheckConstraint(List *constraints, const char *name, Node *expr, bool is_enforced); +static List *MergePeriods(char *relname, List *periods, List *tableElts, List *supers); static void MergeChildAttribute(List *inh_columns, int exist_attno, int newcol_attno, const ColumnDef *newdef); static ColumnDef *MergeInheritedAttribute(List *inh_columns, int exist_attno, const ColumnDef *newdef); static void MergeAttributesIntoExisting(Relation child_rel, Relation parent_rel, bool ispartition); @@ -497,6 +506,8 @@ static ObjectAddress ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, AlterTableUtilityContext *context); static bool check_for_column_name_collision(Relation rel, const char *colname, bool if_not_exists); +static bool check_for_period_name_collision(Relation rel, const char *pername, + bool colexists, bool if_not_exists); static void add_column_datatype_dependency(Oid relid, int32 attnum, Oid typid); static void add_column_collation_dependency(Oid relid, int32 attnum, Oid collid); static ObjectAddress ATExecDropNotNull(Relation rel, const char *colName, bool recurse, @@ -514,6 +525,15 @@ static ObjectAddress ATExecColumnDefault(Relation rel, const char *colName, Node *newDefault, LOCKMODE lockmode); static ObjectAddress ATExecCookedColumnDefault(Relation rel, AttrNumber attnum, Node *newDefault); +static void ATPrepAddPeriod(List **wqueue, Relation rel, AlterTableCmd *cmd, + LOCKMODE lockmode, AlterTableUtilityContext *context); +static ObjectAddress ATExecAddPeriod(List **wqueue, AlteredTableInfo *tab, + Relation rel, PeriodDef *period, + LOCKMODE lockmode, AlterTableUtilityContext *context); +static void ATExecDropPeriod(Relation rel, const char *periodName, + DropBehavior behavior, + bool recurse, bool recursing, + bool missing_ok); static ObjectAddress ATExecAddIdentity(Relation rel, const char *colName, Node *def, LOCKMODE lockmode, bool recurse, bool recursing); static ObjectAddress ATExecSetIdentity(Relation rel, const char *colName, @@ -739,6 +759,10 @@ static List *GetParentedForeignKeyRefs(Relation partition); static void ATDetachCheckNoForeignKeyRefs(Relation partition); static char GetAttributeCompression(Oid atttypid, const char *compression); static char GetAttributeStorage(Oid atttypid, const char *storagemode); +static void AddRelationNewPeriod(Relation rel, PeriodDef *period); +static void ValidatePeriod(Relation rel, PeriodDef *period); +static Constraint *make_constraint_for_period(Relation rel, PeriodDef *period); +static ColumnDef *make_range_column_for_period(PeriodDef *period); /* ---------------------------------------------------------------- @@ -968,6 +992,95 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId, stmt->partbound != NULL, &old_constraints, &old_notnulls); + /* + * Using the column list (including inherited columns), find the start/end + * columns for each period. PERIODs should be inherited too (but aren't + * yet). + */ + stmt->periods = MergePeriods(relname, stmt->periods, stmt->tableElts, inheritOids); + + /* + * For each PERIOD we need a GENERATED column. Usually we must create + * this, so we add it to tableElts. If the user says the column already + * exists, make sure it is sensible. These columns are not inherited, so + * we don't worry about conflicts in tableElts. + * + * We allow this colexists option to support pg_upgrade, so we have more + * control over the GENERATED column (whose attnum must match the old + * value). + * + * Since the GENERATED column must be NOT NULL, we add a constraint to + * nnconstraints. + */ + foreach(listptr, stmt->periods) + { + PeriodDef *period = (PeriodDef *) lfirst(listptr); + + if (period->colexists) + { + ListCell *cell; + bool found = false; + + /* Find the existing column to use */ + foreach(cell, stmt->tableElts) + { + ColumnDef *colDef = lfirst(cell); + + if (strcmp(period->periodname, colDef->colname) == 0) + { + /* + * Make sure the existing column matches what we would have + * created. First all, it must be GENERATED. + */ + if (colDef->generated == '\0') + ereport(ERROR, (errmsg("Period %s uses a non-generated column", + period->periodname))); + + /* The GENERATED column must be STORED. */ + if (colDef->generated != ATTRIBUTE_GENERATED_STORED) + ereport(ERROR, (errmsg("Period %s uses a generated column that is not STORED", + period->periodname))); + + /* The GENERATED column must not allow nulls. */ + if (!colDef->is_not_null && !IsBinaryUpgrade) + ereport(ERROR, (errmsg("Period %s uses a generated column that allows nulls", + period->periodname))); + + /* The GENERATED column must match our rangetype. */ + if (period->rngtypid != typenameTypeId(NULL, colDef->typeName)) + ereport(ERROR, (errmsg("Period %s uses a generated column with the wrong type", + period->periodname))); + + /* + * The GENERATED column must not be inherited. + * XXX: If the PERIOD is inherited, they must come from the + * same parent. + */ + if (!colDef->is_local) + ereport(ERROR, (errmsg("Period %s uses a generated column that is inherited", + period->periodname))); + /* + * XXX: We should check the GENERATED expression also, but + * that is hard to do because one is cooked and one is raw. + */ + + found = true; + } + } + + if (!found) + ereport(ERROR, (errmsg("No column found with name %s", period->periodname))); + } + else + { + ColumnDef *col = make_range_column_for_period(period); + Constraint *constr = makeNotNullConstraint(makeString(col->colname)); + + stmt->tableElts = lappend(stmt->tableElts, col); + stmt->nnconstraints = lappend(stmt->nnconstraints, constr); + } + } + /* * Create a tuple descriptor from the relation schema. Note that this * deals with column names, types, and in-descriptor NOT NULL flags, but @@ -1336,7 +1449,7 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId, true, true, false, queryString); /* - * Finally, merge the not-null constraints that are declared directly with + * Now merge the not-null constraints that are declared directly with * those that come from parent relations (making sure to count inheritance * appropriately for each), create them, and set the attnotnull flag on * columns that don't yet have it. @@ -1346,6 +1459,21 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId, foreach_int(attrnum, nncols) set_attnotnull(NULL, rel, attrnum, true, false); + /* + * Finally, create periods for the table. This must come after we create + * columns and before we create index constraints. It will automatically + * create a CHECK constraint for the period. + */ + foreach(listptr, stmt->periods) + { + PeriodDef *period = (PeriodDef *) lfirst(listptr); + + /* Don't update the count of check constraints twice */ + CommandCounterIncrement(); + + AddRelationNewPeriod(rel, period); + } + ObjectAddressSet(address, RelationRelationId, relationId); /* @@ -1445,6 +1573,336 @@ BuildDescForRelation(const List *columns) return desc; } +/* + * make_constraint_for_period + * + * Builds a CHECK Constraint to ensure start < end. + * Returns the CHECK Constraint. + * Also fills in period->constraintname if needed. + * + * If either bound is NULL, the constraint passes + * (since that indicates an unbounded range). + */ +static Constraint * +make_constraint_for_period(Relation rel, PeriodDef *period) +{ + ColumnRef *scol, + *ecol; + Constraint *constr; + TypeCacheEntry *type; + + if (period->constraintname == NULL) + period->constraintname = ChooseConstraintName(RelationGetRelationName(rel), + period->periodname, + "check", + RelationGetNamespace(rel), + NIL); + scol = makeNode(ColumnRef); + scol->fields = list_make1(makeString(pstrdup(period->startcolname))); + scol->location = 0; + + ecol = makeNode(ColumnRef); + ecol->fields = list_make1(makeString(pstrdup(period->endcolname))); + ecol->location = 0; + + type = lookup_type_cache(period->coltypid, TYPECACHE_LT_OPR); + if (type->lt_opr == InvalidOid) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("column \"%s\" cannot be used in a PERIOD because its type %s has no less than operator", + period->startcolname, format_type_be(period->coltypid)))); + + constr = makeNode(Constraint); + constr->contype = CONSTR_CHECK; + constr->conname = period->constraintname; + constr->deferrable = false; + constr->initdeferred = false; + constr->location = -1; + constr->is_no_inherit = false; + constr->raw_expr = (Node *) makeSimpleA_Expr(AEXPR_OP, + get_opname(type->lt_opr), + (Node *) scol, + (Node *) ecol, + 0); + constr->cooked_expr = NULL; + constr->skip_validation = false; + constr->is_enforced = true; + constr->initially_valid = true; + + return constr; +} + +/* + * make_range_column_for_period + * + * Builds a GENERATED ALWAYS range column based on the PERIOD + * start/end columns. Returns the ColumnDef. + */ +ColumnDef * +make_range_column_for_period(PeriodDef *period) +{ + char *range_type_namespace; + char *range_type_name; + ColumnDef *col = makeNode(ColumnDef); + ColumnRef *startvar, + *endvar; + Expr *rangeConstructor; + + if (!get_typname_and_namespace(period->rngtypid, &range_type_name, + &range_type_namespace)) + elog(ERROR, "missing range type %d", period->rngtypid); + + startvar = makeNode(ColumnRef); + startvar->fields = list_make1(makeString(pstrdup(period->startcolname))); + endvar = makeNode(ColumnRef); + endvar->fields = list_make1(makeString(pstrdup(period->endcolname))); + rangeConstructor = (Expr *) makeFuncCall( + list_make2(makeString(range_type_namespace), makeString(range_type_name)), + list_make2(startvar, endvar), + COERCE_EXPLICIT_CALL, + period->location); + + col->colname = pstrdup(period->periodname); + col->typeName = makeTypeName(range_type_name); + col->compression = NULL; + col->inhcount = 0; + col->is_local = true; + col->is_not_null = true; + col->is_from_type = false; + col->storage = 0; + col->storage_name = NULL; + col->raw_default = (Node *) rangeConstructor; + col->cooked_default = NULL; + col->identity = 0; + col->generated = ATTRIBUTE_GENERATED_STORED; + col->collClause = NULL; + col->collOid = InvalidOid; + col->fdwoptions = NIL; + col->location = period->location; + + return col; +} + +/* + * ValidatePeriod + * + * Look up the attributes used by the PERIOD, + * make sure they exist, are not system columns, + * and have the same type and collation. + * + * Add our findings to these PeriodDef fields: + * + * coltypid - the type of PERIOD columns. + * startattnum - the attnum of the start column. + * endattnum - the attnum of the end column. + * rngtypid - the range type to use. + * rngattnum - the attnum of a pre-existing range column, or Invalid. + */ +static void +ValidatePeriod(Relation rel, PeriodDef *period) +{ + HeapTuple starttuple; + HeapTuple endtuple; + Form_pg_attribute atttuple; + Oid attcollation; + Oid endtypid; + Oid endcollation; + + /* Find the start column */ + starttuple = SearchSysCacheAttName(RelationGetRelid(rel), period->startcolname); + if (!HeapTupleIsValid(starttuple)) + ereport(ERROR, + (errcode(ERRCODE_UNDEFINED_COLUMN), + errmsg("column \"%s\" of relation \"%s\" does not exist", + period->startcolname, RelationGetRelationName(rel)))); + atttuple = (Form_pg_attribute) GETSTRUCT(starttuple); + period->coltypid = atttuple->atttypid; + attcollation = atttuple->attcollation; + period->startattnum = atttuple->attnum; + ReleaseSysCache(starttuple); + + /* Make sure it's not a system column */ + if (period->startattnum <= 0) + ereport(ERROR, + (errcode(ERRCODE_INVALID_OBJECT_DEFINITION), + errmsg("cannot use system column \"%s\" in period", + period->startcolname))); + + /* Find the end column */ + endtuple = SearchSysCacheAttName(RelationGetRelid(rel), period->endcolname); + if (!HeapTupleIsValid(endtuple)) + ereport(ERROR, + (errcode(ERRCODE_UNDEFINED_COLUMN), + errmsg("column \"%s\" of relation \"%s\" does not exist", + period->endcolname, RelationGetRelationName(rel)))); + atttuple = (Form_pg_attribute) GETSTRUCT(endtuple); + endtypid = atttuple->atttypid; + endcollation = atttuple->attcollation; + period->endattnum = atttuple->attnum; + ReleaseSysCache(endtuple); + + /* Make sure it's not a system column */ + if (period->endattnum <= 0) + ereport(ERROR, + (errcode(ERRCODE_INVALID_OBJECT_DEFINITION), + errmsg("cannot use system column \"%s\" in period", + period->endcolname))); + + /* Both columns must be of same type */ + if (period->coltypid != endtypid) + ereport(ERROR, + (errcode(ERRCODE_DATATYPE_MISMATCH), + errmsg("start and end columns of period must be of same type"))); + + /* Both columns must have the same collation */ + if (attcollation != endcollation) + ereport(ERROR, + (errcode(ERRCODE_COLLATION_MISMATCH), + errmsg("start and end columns of period must have same collation"))); + + /* Get the range type based on the start/end cols or the user's choice */ + period->rngtypid = choose_rangetype_for_period(period); + + /* + * If the GENERATED columns should already exist, make sure it is + * sensible. + */ + if (period->colexists) + { + HeapTuple rngtuple = SearchSysCacheAttName(RelationGetRelid(rel), + period->periodname); + + if (!HeapTupleIsValid(rngtuple)) + ereport(ERROR, + (errcode(ERRCODE_UNDEFINED_COLUMN), + errmsg("column \"%s\" of relation \"%s\" does not exist", + period->periodname, RelationGetRelationName(rel)))); + atttuple = (Form_pg_attribute) GETSTRUCT(rngtuple); + + /* + * Make sure the existing column matches what we would have created. + * First of all, it must be GENERATED. + */ + if (atttuple->attgenerated == '\0') + ereport(ERROR, (errmsg("Period %s uses a non-generated column", + period->periodname))); + + /* The GENERATED column must be STORED. */ + if (atttuple->attgenerated != ATTRIBUTE_GENERATED_STORED) + ereport(ERROR, (errmsg("Period %s uses a generated column that is not STORED", + period->periodname))); + + /* The GENERATED column must not allow nulls. */ + if (!atttuple->attnotnull && !IsBinaryUpgrade) + ereport(ERROR, (errmsg("Period %s uses a generated column that allows nulls", + period->periodname))); + + /* The GENERATED column must match our rangetype. */ + if (period->rngtypid != atttuple->atttypid) + ereport(ERROR, (errmsg("Period %s uses a generated column with the wrong type", + period->periodname))); + + /* + * The GENERATED column must not be inherited. + * XXX: If the PERIOD is inherited, they must come from the same parent. + */ + if (!atttuple->attislocal) + ereport(ERROR, (errmsg("Period %s uses a generated column that is inherited", + period->periodname))); + + /* + * XXX: We should check the GENERATED expression also, but that is + * hard to do because one is cooked and one is raw. + */ + + period->rngattnum = atttuple->attnum; + + ReleaseSysCache(rngtuple); + } +} + +/* + * choose_rangetype_for_period + * + * Find a suitable range type for operations involving this period. + * Use the rangetype option if provided, otherwise try to find a + * non-ambiguous existing type. + */ +Oid +choose_rangetype_for_period(PeriodDef *period) +{ + Oid rngtypid; + + if (period->rangetypename != NULL) + { + /* Make sure it exists */ + rngtypid = TypenameGetTypidExtended(period->rangetypename, false); + if (rngtypid == InvalidOid) + ereport(ERROR, (errcode(ERRCODE_UNDEFINED_OBJECT), + errmsg("Range type %s not found", + period->rangetypename))); + + /* Make sure it is a range type */ + if (!type_is_range(rngtypid)) + ereport(ERROR, + (errcode(ERRCODE_DATATYPE_MISMATCH), + errmsg("Type %s is not a range type", + period->rangetypename))); + + /* Make sure it matches the column type */ + if (get_range_subtype(rngtypid) != period->coltypid) + ereport(ERROR, + (errcode(ERRCODE_DATATYPE_MISMATCH), + errmsg("Range type %s does not match column type %s", + period->rangetypename, + format_type_be(period->coltypid)))); + } + else + { + rngtypid = get_subtype_range(period->coltypid); + if (rngtypid == InvalidOid) + ereport(ERROR, + (errcode(ERRCODE_UNDEFINED_OBJECT), + errmsg("no range type for %s found for period %s", + format_type_be(period->coltypid), + period->periodname), + errhint("You can define a custom range type with CREATE TYPE"))); + + } + + return rngtypid; +} + +static void +AddRelationNewPeriod(Relation rel, PeriodDef *period) +{ + Relation attrelation; + Oid conoid; + Constraint *constr; + List *newconstrs; + + attrelation = table_open(AttributeRelationId, RowExclusiveLock); + + /* Find the GENERATED range column */ + + period->rngattnum = get_attnum(RelationGetRelid(rel), period->periodname); + if (period->rngattnum == InvalidAttrNumber) + elog(ERROR, "missing attribute %s", period->periodname); + + /* The parser has already found period->coltypid */ + + constr = make_constraint_for_period(rel, period); + newconstrs = AddRelationNewConstraints(rel, NIL, list_make1(constr), false, + true, true, NULL); + conoid = ((CookedConstraint *) linitial(newconstrs))->conoid; + + /* Save it */ + StorePeriod(rel, period->periodname, period->startattnum, period->endattnum, + period->rngattnum, conoid); + + table_close(attrelation, RowExclusiveLock); +} + /* * Emit the right error or warning message for a "DROP" command issued on a * non-existent relation @@ -2747,7 +3205,7 @@ MergeAttributes(List *columns, const List *supers, char relpersistence, * that's not marked NO INHERIT (even if not valid). */ nnconstrs = RelationGetNotNullConstraints(RelationGetRelid(relation), - true, false); + true, false, NULL); foreach_ptr(CookedConstraint, cc, nnconstrs) nncols = bms_add_member(nncols, cc->attnum); @@ -3140,6 +3598,172 @@ MergeAttributes(List *columns, const List *supers, char relpersistence, } +/*---------- + * MergePeriods + * Returns new period list given initial periods and superclasses. + * + * For now we don't support inheritence with PERIODs, + * but we might make it work eventually. + * + * We can omit lots of checks here and assume MergeAttributes already did them, + * for example that child & parents are not a mix of permanent and temp. + */ +static List * +MergePeriods(char *relname, List *periods, List *tableElts, List *supers) +{ + ListCell *entry; + + /* If we have a PERIOD then supers must be empty. */ + + if (list_length(periods) > 0 && list_length(supers) > 0) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("Inheriting is not supported when a table has a PERIOD"))); + + /* If any parent table has a PERIOD, then fail. */ + + foreach(entry, supers) + { + Oid parent = lfirst_oid(entry); + Relation relation; + Relation pg_period; + SysScanDesc scan; + ScanKeyData skey[1]; + HeapTuple tuple; + + /* caller already got lock */ + relation = table_open(parent, NoLock); + pg_period = table_open(PeriodRelationId, AccessShareLock); + + ScanKeyInit(&skey[0], + Anum_pg_period_perrelid, + BTEqualStrategyNumber, F_OIDEQ, + ObjectIdGetDatum(parent)); + + scan = systable_beginscan(pg_period, PeriodRelidNameIndexId, true, + NULL, 1, skey); + + if (HeapTupleIsValid(tuple = systable_getnext(scan))) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("Inheriting from a table with a PERIOD is not supported"))); + + systable_endscan(scan); + table_close(pg_period, AccessShareLock); + table_close(relation, NoLock); + } + + /* + * Find the start & end columns and get their attno and type. In the same + * pass, make sure the period doesn't conflict with any column names. Also + * make sure the same period name isn't used more than once. + */ + foreach(entry, periods) + { + PeriodDef *period = lfirst(entry); + ListCell *entry2; + int i = 1; + Oid startcoltypid = InvalidOid; + Oid endcoltypid = InvalidOid; + Oid startcolcollation = InvalidOid; + Oid endcolcollation = InvalidOid; + + period->startattnum = InvalidAttrNumber; + period->endattnum = InvalidAttrNumber; + + if (SystemAttributeByName(period->periodname) != NULL) + ereport(ERROR, + (errcode(ERRCODE_DUPLICATE_COLUMN), + errmsg("period name \"%s\" conflicts with a system column name", + period->periodname))); + + foreach(entry2, periods) + { + PeriodDef *period2 = lfirst(entry2); + + if (period != period2 && strcmp(period->periodname, period2->periodname) == 0) + ereport(ERROR, + (errcode(ERRCODE_DUPLICATE_OBJECT), + errmsg("period name \"%s\" specified more than once", + period->periodname))); + } + + foreach(entry2, tableElts) + { + ColumnDef *col = lfirst(entry2); + int32 atttypmod; + AclResult aclresult; + + if (!period->colexists && strcmp(period->periodname, col->colname) == 0) + ereport(ERROR, + (errcode(ERRCODE_DUPLICATE_COLUMN), + errmsg("period name \"%s\" conflicts with a column name", + period->periodname))); + + if (strcmp(period->startcolname, col->colname) == 0) + { + period->startattnum = i; + + typenameTypeIdAndMod(NULL, col->typeName, &startcoltypid, + &atttypmod); + + aclresult = object_aclcheck(TypeRelationId, startcoltypid, + GetUserId(), ACL_USAGE); + if (aclresult != ACLCHECK_OK) + aclcheck_error_type(aclresult, startcoltypid); + + startcolcollation = GetColumnDefCollation(NULL, col, startcoltypid); + } + + if (strcmp(period->endcolname, col->colname) == 0) + { + period->endattnum = i; + + typenameTypeIdAndMod(NULL, col->typeName, &endcoltypid, + &atttypmod); + + aclresult = object_aclcheck(TypeRelationId, endcoltypid, + GetUserId(), ACL_USAGE); + if (aclresult != ACLCHECK_OK) + aclcheck_error_type(aclresult, endcoltypid); + + endcolcollation = GetColumnDefCollation(NULL, col, endcoltypid); + } + + i++; + } + + /* Did we find the columns? */ + if (period->startattnum == InvalidAttrNumber) + ereport(ERROR, + (errcode(ERRCODE_UNDEFINED_COLUMN), + errmsg("column \"%s\" of relation \"%s\" does not exist", + period->startcolname, relname))); + if (period->endattnum == InvalidAttrNumber) + ereport(ERROR, + (errcode(ERRCODE_UNDEFINED_COLUMN), + errmsg("column \"%s\" of relation \"%s\" does not exist", + period->endcolname, relname))); + + /* Both columns must be of same type */ + if (startcoltypid != endcoltypid) + ereport(ERROR, + (errcode(ERRCODE_DATATYPE_MISMATCH), + errmsg("start and end columns of period must be of same type"))); + + /* Both columns must have the same collation */ + if (startcolcollation != endcolcollation) + ereport(ERROR, + (errcode(ERRCODE_COLLATION_MISMATCH), + errmsg("start and end columns of period must have same collation"))); + + period->coltypid = startcoltypid; + period->rngtypid = choose_rangetype_for_period(period); + } + + return periods; +} + /* * MergeCheckConstraint * Try to merge an inherited CHECK constraint with previous ones @@ -4547,12 +5171,12 @@ AlterTable(AlterTableStmt *stmt, LOCKMODE lockmode, * existing query plans. On the assumption it's not used for such, we * don't have to reject pending AFTER triggers, either. * - * Also, since we don't have an AlterTableUtilityContext, this cannot be + * Also, if you don't pass an AlterTableUtilityContext, this cannot be * used for any subcommand types that require parse transformation or * could generate subcommands that have to be passed to ProcessUtility. */ void -AlterTableInternal(Oid relid, List *cmds, bool recurse) +AlterTableInternal(Oid relid, List *cmds, bool recurse, AlterTableUtilityContext *context) { Relation rel; LOCKMODE lockmode = AlterTableGetLockLevel(cmds); @@ -4561,7 +5185,7 @@ AlterTableInternal(Oid relid, List *cmds, bool recurse) EventTriggerAlterTableRelid(relid); - ATController(NULL, rel, cmds, recurse, lockmode, NULL); + ATController(NULL, rel, cmds, recurse, lockmode, context); } /* @@ -4654,6 +5278,9 @@ AlterTableGetLockLevel(List *cmds) case AT_EnableReplicaRule: /* may change SELECT rules */ case AT_EnableRule: /* may change SELECT rules */ case AT_DisableRule: /* may change SELECT rules */ + case AT_AddPeriod: /* shares namespace with columns, adds + * constraint */ + case AT_DropPeriod: cmd_lockmode = AccessExclusiveLock; break; @@ -4973,6 +5600,17 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd *cmd, /* This command never recurses */ pass = AT_PASS_ADD_OTHERCONSTR; break; + case AT_AddPeriod: /* ALTER TABLE ... ADD PERIOD FOR name (start, + * end) */ + ATSimplePermissions(cmd->subtype, rel, ATT_TABLE); + ATPrepAddPeriod(wqueue, rel, cmd, lockmode, context); + /* No recursion: inheritance not supported with PERIODs */ + pass = AT_PASS_ADD_PERIOD; + break; + case AT_DropPeriod: /* ALTER TABLE ... DROP PERIOD FOR name */ + ATSimplePermissions(cmd->subtype, rel, ATT_TABLE); + pass = AT_PASS_DROP; + break; case AT_AddIdentity: ATSimplePermissions(cmd->subtype, rel, ATT_TABLE | ATT_PARTITIONED_TABLE | ATT_VIEW | @@ -5386,6 +6024,14 @@ ATExecCmd(List **wqueue, AlteredTableInfo *tab, case AT_CookedColumnDefault: /* add a pre-cooked default */ address = ATExecCookedColumnDefault(rel, cmd->num, cmd->def); break; + case AT_AddPeriod: + address = ATExecAddPeriod(wqueue, tab, rel, (PeriodDef *) cmd->def, + lockmode, context); + break; + case AT_DropPeriod: + ATExecDropPeriod(rel, cmd->name, cmd->behavior, false, false, + cmd->missing_ok); + break; case AT_AddIdentity: cmd = ATParseTransformCmd(wqueue, tab, rel, cmd, false, lockmode, cur_pass, context); @@ -6592,6 +7238,8 @@ alter_table_type_to_string(AlterTableType cmdtype) case AT_AddColumn: case AT_AddColumnToView: return "ADD COLUMN"; + case AT_AddPeriod: + return "ADD PERIOD"; case AT_ColumnDefault: case AT_CookedColumnDefault: return "ALTER COLUMN ... SET DEFAULT"; @@ -6615,6 +7263,8 @@ alter_table_type_to_string(AlterTableType cmdtype) return "ALTER COLUMN ... SET COMPRESSION"; case AT_DropColumn: return "DROP COLUMN"; + case AT_DropPeriod: + return "DROP PERIOD"; case AT_AddIndex: case AT_ReAddIndex: return NULL; /* not real grammar */ @@ -7633,14 +8283,30 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel, /* * If a new or renamed column will collide with the name of an existing * column and if_not_exists is false then error out, else do nothing. + * + * See also check_for_period_name_collision. */ static bool check_for_column_name_collision(Relation rel, const char *colname, bool if_not_exists) { - HeapTuple attTuple; + HeapTuple attTuple, + perTuple; int attnum; + /* If the name exists as a period, we're done. */ + perTuple = SearchSysCache2(PERIODNAME, + ObjectIdGetDatum(RelationGetRelid(rel)), + PointerGetDatum(colname)); + if (HeapTupleIsValid(perTuple)) + { + ReleaseSysCache(perTuple); + ereport(ERROR, + (errcode(ERRCODE_DUPLICATE_COLUMN), + errmsg("column name \"%s\" conflicts with a period name", + colname))); + } + /* * this test is deliberately not attisdropped-aware, since if one tries to * add a column matching a dropped column name, it's gonna fail anyway. @@ -7684,6 +8350,78 @@ check_for_column_name_collision(Relation rel, const char *colname, return true; } +/* + * If a new period name will collide with the name of an existing column or + * period [and if_not_exists is false] then error out, else do nothing. + * + * See also check_for_column_name_collision. + */ +static bool +check_for_period_name_collision(Relation rel, const char *pername, + bool colexists, bool if_not_exists) +{ + HeapTuple attTuple, + perTuple; + int attnum; + + /* XXX: implement IF [NOT] EXISTS for periods */ + Assert(!if_not_exists); + + /* If there is already a period with this name, then we're done. */ + perTuple = SearchSysCache2(PERIODNAME, + ObjectIdGetDatum(RelationGetRelid(rel)), + PointerGetDatum(pername)); + if (HeapTupleIsValid(perTuple)) + { + if (if_not_exists) + { + ReleaseSysCache(perTuple); + + ereport(NOTICE, + (errcode(ERRCODE_DUPLICATE_COLUMN), + errmsg("period \"%s\" of relation \"%s\" already exists, skipping", + pername, RelationGetRelationName(rel)))); + return false; + } + + ereport(ERROR, + (errcode(ERRCODE_DUPLICATE_COLUMN), + errmsg("period \"%s\" of relation \"%s\" already exists", + pername, RelationGetRelationName(rel)))); + } + + /* + * this test is deliberately not attisdropped-aware, since if one tries to + * add a column matching a dropped column name, it's gonna fail anyway. + */ + attTuple = SearchSysCache2(ATTNAME, + ObjectIdGetDatum(RelationGetRelid(rel)), + PointerGetDatum(pername)); + if (HeapTupleIsValid(attTuple)) + { + attnum = ((Form_pg_attribute) GETSTRUCT(attTuple))->attnum; + ReleaseSysCache(attTuple); + + /* + * We throw a different error message for conflicts with system column + * names, since they are normally not shown and the user might + * otherwise be confused about the reason for the conflict. + */ + if (attnum <= 0) + ereport(ERROR, + (errcode(ERRCODE_DUPLICATE_COLUMN), + errmsg("period name \"%s\" conflicts with a system column name", + pername))); + if (!colexists) + ereport(ERROR, + (errcode(ERRCODE_DUPLICATE_COLUMN), + errmsg("period name \"%s\" conflicts with a column name", + pername))); + } + + return true; +} + /* * Install a column's dependency on its datatype. */ @@ -8223,6 +8961,209 @@ ATExecCookedColumnDefault(Relation rel, AttrNumber attnum, return address; } +/* + * Prepare to add a PERIOD to a table, by adding all its constituent objects. + * + * We need a CHECK constraint enforcing we start before we end. + * + * Usually we also create a GENERATED column with a NOT NULL constraint, + * unless the command indicates we have one already. + * + * PERIODs are not supported in inheritance hierarchies, so we don't need + * to worry about recursion. + * + * ATExecAddPeriod will need the oid of the CHECK constraint and the attnum + * of the range column (whether new or not) to record the dependency. + */ +static void +ATPrepAddPeriod(List **wqueue, Relation rel, AlterTableCmd *cmd, + LOCKMODE lockmode, AlterTableUtilityContext *context) +{ + PeriodDef *period = (PeriodDef *) cmd->def; + + /* + * PERIOD FOR SYSTEM_TIME is not yet implemented, but make sure no one + * uses the name. + */ + if (strcmp(period->periodname, "system_time") == 0) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("PERIOD FOR SYSTEM_TIME is not supported"))); + + if (strcmp(period->startcolname, period->endcolname) == 0) + ereport(ERROR, (errmsg("column \"%s\" can't be the start and end column for period \"%s\"", + period->startcolname, period->periodname))); + + /* Parse options */ + transformPeriodOptions(period); +} + +/* + * ALTER TABLE ADD PERIOD + * + * Return the address of the period. + */ +static ObjectAddress +ATExecAddPeriod(List **wqueue, AlteredTableInfo *tab, Relation rel, + PeriodDef *period, LOCKMODE lockmode, + AlterTableUtilityContext *context) +{ + Relation attrelation; + ObjectAddress address = InvalidObjectAddress; + Constraint *constr; + ColumnDef *rangecol; + Oid conoid, + periodoid; + List *cmds = NIL; + AlterTableCmd *cmd; + + /* + * The period name must not already exist. We can't check this in + * ATPrepAddPeriod because the same ALTER TABLE command might do a DROP + * PERIOD as well. + */ + (void) check_for_period_name_collision(rel, period->periodname, + period->colexists, false); + + attrelation = table_open(AttributeRelationId, RowExclusiveLock); + ValidatePeriod(rel, period); + + /* Make the CHECK constraint */ + constr = make_constraint_for_period(rel, period); + cmd = makeNode(AlterTableCmd); + cmd->subtype = AT_AddConstraint; + cmd->def = (Node *) constr; + cmds = lappend(cmds, cmd); + AlterTableInternal(RelationGetRelid(rel), cmds, true, context); + conoid = get_relation_constraint_oid(RelationGetRelid(rel), + period->constraintname, false); + + + if (!period->colexists) + { + cmds = NIL; + + /* + * Make the range column. + * + * We have already passed the add-column pass, so we have to create it + * ourself. + */ + rangecol = make_range_column_for_period(period); + cmd = makeNode(AlterTableCmd); + cmd->subtype = AT_AddColumn; + cmd->def = (Node *) rangecol; + cmd->name = period->periodname; + cmd->recurse = false; /* No, let the PERIOD recurse instead. */ + ATExecAddColumn(wqueue, tab, rel, &cmd, false, false, lockmode, + AT_PASS_ADD_PERIOD, context); + + /* The range column should be NOT NULL. */ + cmd = makeNode(AlterTableCmd); + cmd->subtype = AT_AddConstraint; + cmd->def = (Node *) makeNotNullConstraint(makeString(period->periodname)); + cmd->recurse = false; /* No, let the PERIOD recurse instead. */ + ATPrepCmd(wqueue, rel, cmd, false, false, lockmode, context); + + /* Look up the GENERATED attnum */ + period->rngattnum = get_attnum(RelationGetRelid(rel), period->periodname); + if (period->rngattnum == InvalidAttrNumber) + elog(ERROR, "missing attribute %s", period->periodname); + } + + /* Save the Period */ + periodoid = StorePeriod(rel, period->periodname, period->startattnum, + period->endattnum, period->rngattnum, conoid); + + ObjectAddressSet(address, PeriodRelationId, periodoid); + + table_close(attrelation, RowExclusiveLock); + + return address; +} + +/* + * ALTER TABLE DROP PERIOD + * + * Like DROP COLUMN, we can't use the normal ALTER TABLE recursion mechanism. + */ +static void +ATExecDropPeriod(Relation rel, const char *periodName, + DropBehavior behavior, + bool recurse, bool recursing, + bool missing_ok) +{ + Relation pg_period; + Form_pg_period period; + SysScanDesc scan; + ScanKeyData key; + HeapTuple tuple; + bool found = false; + + /* At top level, permission check was done in ATPrepCmd, else do it */ + if (recursing) + ATSimplePermissions(AT_DropPeriod, rel, ATT_TABLE); + + pg_period = table_open(PeriodRelationId, RowExclusiveLock); + + /* + * Find and drop the target period + */ + ScanKeyInit(&key, + Anum_pg_period_perrelid, + BTEqualStrategyNumber, F_OIDEQ, + ObjectIdGetDatum(RelationGetRelid(rel))); + scan = systable_beginscan(pg_period, PeriodRelidNameIndexId, + true, NULL, 1, &key); + + while (HeapTupleIsValid(tuple = systable_getnext(scan))) + { + ObjectAddress perobj; + + period = (Form_pg_period) GETSTRUCT(tuple); + + if (strcmp(NameStr(period->pername), periodName) != 0) + continue; + + /* + * Perform the actual period deletion + */ + perobj.classId = PeriodRelationId; + perobj.objectId = period->oid; + perobj.objectSubId = 0; + + performDeletion(&perobj, behavior, 0); + + found = true; + + /* period found and dropped -- no need to keep looping */ + break; + } + + systable_endscan(scan); + + if (!found) + { + if (!missing_ok) + { + ereport(ERROR, + (errcode(ERRCODE_UNDEFINED_OBJECT), + errmsg("period \"%s\" on relation \"%s\" does not exist", + periodName, RelationGetRelationName(rel)))); + } + else + { + ereport(NOTICE, + (errmsg("period \"%s\" on relation \"%s\" does not exist, skipping", + periodName, RelationGetRelationName(rel)))); + table_close(pg_period, RowExclusiveLock); + return; + } + } + + table_close(pg_period, RowExclusiveLock); +} + /* * ALTER TABLE ALTER COLUMN ADD IDENTITY * @@ -10210,8 +11151,9 @@ ATAddForeignKeyConstraint(List **wqueue, AlteredTableInfo *tab, Relation rel, for (i = 0; i < numfks; i++) { char attgenerated = TupleDescAttr(RelationGetDescr(rel), fkattnum[i] - 1)->attgenerated; + Bitmapset *periods = get_period_attnos(RelationGetRelid(rel)); - if (attgenerated) + if (attgenerated && !bms_is_member(fkattnum[i], periods)) { /* * Check restrictions on UPDATE/DELETE actions, per SQL standard @@ -15108,6 +16050,16 @@ RememberAllDependentForRebuilding(AlteredTableInfo *tab, AlterTableType subtype, RememberConstraintForRebuilding(foundObject.objectId, tab); break; + case PeriodRelationId: + if (subtype == AT_AlterColumnType) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot alter type of a column used by a period"), + errdetail("%s depends on column \"%s\"", + getObjectDescription(&foundObject, false), + colName))); + break; + case ProcedureRelationId: /* @@ -15197,6 +16149,16 @@ RememberAllDependentForRebuilding(AlteredTableInfo *tab, AlterTableType subtype, } else { + /* + * If this GENERATED column is implementing a PERIOD, + * keep going and we'll fail from the PERIOD instead. + * This gives a more clear error message. + */ + Bitmapset *periodatts = get_period_attnos(RelationGetRelid(rel)); + + if (bms_is_member(col.objectSubId, periodatts)) + break; + /* * This must be a reference from the expression of a * generated column elsewhere in the same table. @@ -17133,7 +18095,7 @@ AlterTableMoveAll(AlterTableMoveAllStmt *stmt) EventTriggerAlterTableStart((Node *) stmt); /* OID is set by AlterTableInternal */ - AlterTableInternal(lfirst_oid(l), cmds, false); + AlterTableInternal(lfirst_oid(l), cmds, false, NULL); EventTriggerAlterTableEnd(); } diff --git a/src/backend/commands/trigger.c b/src/backend/commands/trigger.c index 43b9d82e63a..968a91fd34b 100644 --- a/src/backend/commands/trigger.c +++ b/src/backend/commands/trigger.c @@ -6143,6 +6143,8 @@ CopyForPortionOfState(ForPortionOfState *src) dst->fp_rangeName = pstrdup(src->fp_rangeName); dst->fp_rangeType = src->fp_rangeType; dst->fp_rangeAttno = src->fp_rangeAttno; + dst->fp_periodStartAttno = src->fp_periodStartAttno; + dst->fp_periodEndAttno = src->fp_periodEndAttno; r = DatumGetRangeTypeP(src->fp_targetRange); typcache = lookup_type_cache(RangeTypeGetOid(r), TYPECACHE_RANGE_INFO); diff --git a/src/backend/commands/view.c b/src/backend/commands/view.c index 6f0301555e0..c7276836220 100644 --- a/src/backend/commands/view.c +++ b/src/backend/commands/view.c @@ -163,7 +163,7 @@ DefineVirtualRelation(RangeVar *relation, List *tlist, bool replace, } /* EventTriggerAlterTableStart called by ProcessUtilitySlow */ - AlterTableInternal(viewOid, atcmds, true); + AlterTableInternal(viewOid, atcmds, true, NULL); /* Make the new view columns visible */ CommandCounterIncrement(); @@ -195,7 +195,7 @@ DefineVirtualRelation(RangeVar *relation, List *tlist, bool replace, atcmds = list_make1(atcmd); /* EventTriggerAlterTableStart called by ProcessUtilitySlow */ - AlterTableInternal(viewOid, atcmds, true); + AlterTableInternal(viewOid, atcmds, true, NULL); /* * There is very little to do here to update the view's dependencies. diff --git a/src/backend/executor/nodeModifyTable.c b/src/backend/executor/nodeModifyTable.c index 350714353c9..61743a5da88 100644 --- a/src/backend/executor/nodeModifyTable.c +++ b/src/backend/executor/nodeModifyTable.c @@ -1390,6 +1390,7 @@ ExecForPortionOfLeftovers(ModifyTableContext *context, TransitionCaptureState *oldTcs; FmgrInfo flinfo; ReturnSetInfo rsi; + bool hasPeriod = false; bool didInit = false; bool shouldFree = false; @@ -1501,6 +1502,8 @@ ExecForPortionOfLeftovers(ModifyTableContext *context, ExecForceStoreHeapTuple(oldtuple, leftoverSlot, false); } + hasPeriod = forPortionOf->startVar; + /* * Save some mtstate things so we can restore them below. XXX: * Should we create our own ModifyTableState instead? @@ -1512,8 +1515,42 @@ ExecForPortionOfLeftovers(ModifyTableContext *context, didInit = true; } - leftoverSlot->tts_values[forPortionOf->rangeVar->varattno - 1] = leftover; - leftoverSlot->tts_isnull[forPortionOf->rangeVar->varattno - 1] = false; + if (hasPeriod) + { + RangeType *leftoverRange; + RangeBound leftoverLower; + RangeBound leftoverUpper; + bool leftoverEmpty; + AttrNumber startAttno; + AttrNumber endAttno; + + leftoverRange = DatumGetRangeTypeP(leftover); + range_deserialize(typcache, leftoverRange, &leftoverLower, &leftoverUpper, &leftoverEmpty); + + startAttno = forPortionOf->startVar->varattno; + endAttno = forPortionOf->endVar->varattno; + + if (leftoverLower.infinite) + leftoverSlot->tts_isnull[startAttno - 1] = true; + else + { + leftoverSlot->tts_isnull[startAttno - 1] = false; + leftoverSlot->tts_values[startAttno - 1] = leftoverLower.val; + } + + if (leftoverUpper.infinite) + leftoverSlot->tts_isnull[endAttno - 1] = true; + else + { + leftoverSlot->tts_isnull[endAttno - 1] = false; + leftoverSlot->tts_values[endAttno - 1] = leftoverUpper.val; + } + } + else + { + leftoverSlot->tts_values[forPortionOf->rangeVar->varattno - 1] = leftover; + leftoverSlot->tts_isnull[forPortionOf->rangeVar->varattno - 1] = false; + } ExecMaterializeSlot(leftoverSlot); /* diff --git a/src/backend/nodes/nodeFuncs.c b/src/backend/nodes/nodeFuncs.c index e40e8eecf73..8d2c7db4b27 100644 --- a/src/backend/nodes/nodeFuncs.c +++ b/src/backend/nodes/nodeFuncs.c @@ -1729,6 +1729,9 @@ exprLocation(const Node *expr) case T_Constraint: loc = ((const Constraint *) expr)->location; break; + case T_PeriodDef: + loc = ((const PeriodDef *) expr)->location; + break; case T_FunctionParameter: loc = ((const FunctionParameter *) expr)->location; break; diff --git a/src/backend/parser/analyze.c b/src/backend/parser/analyze.c index a542c81b45f..1a087aecae3 100644 --- a/src/backend/parser/analyze.c +++ b/src/backend/parser/analyze.c @@ -24,10 +24,12 @@ #include "postgres.h" +#include "access/htup_details.h" #include "access/stratnum.h" #include "access/sysattr.h" #include "catalog/pg_am.h" #include "catalog/pg_operator.h" +#include "catalog/pg_period.h" #include "catalog/pg_proc.h" #include "catalog/pg_type.h" #include "commands/defrem.h" @@ -50,6 +52,7 @@ #include "parser/parse_relation.h" #include "parser/parse_target.h" #include "parser/parse_type.h" +#include "parser/parser.h" #include "parser/parsetree.h" #include "utils/backend_status.h" #include "utils/builtins.h" @@ -1321,6 +1324,10 @@ transformForPortionOfClause(ParseState *pstate, char *range_type_namespace = NULL; char *range_type_name = NULL; int range_attno = InvalidAttrNumber; + AttrNumber start_attno = InvalidAttrNumber; + AttrNumber end_attno = InvalidAttrNumber; + char *startcolname = NULL; + char *endcolname = NULL; Form_pg_attribute attr; Oid opclass; Oid opfamily; @@ -1364,6 +1371,54 @@ transformForPortionOfClause(ParseState *pstate, if (!get_typname_and_namespace(attr->atttypid, &range_type_name, &range_type_namespace)) elog(ERROR, "cache lookup failed for type %u", attr->atttypid); + /* + * If we are using a PERIOD, we need the start & end columns. If the + * attribute it not a GENERATED column, we needn't query pg_period. + */ + if (attr->attgenerated == ATTRIBUTE_GENERATED_STORED) + { + HeapTuple perTuple = SearchSysCache2(PERIODNAME, + ObjectIdGetDatum(RelationGetRelid(targetrel)), + PointerGetDatum(range_name)); + + if (HeapTupleIsValid(perTuple)) + { + Form_pg_period per = (Form_pg_period) GETSTRUCT(perTuple); + Form_pg_attribute perattr; + + start_attno = per->perstart; + end_attno = per->perend; + + perattr = TupleDescAttr(targetrel->rd_att, start_attno - 1); + startcolname = NameStr(perattr->attname); + + result->startVar = makeVar( + rtindex, + start_attno, + perattr->atttypid, + perattr->atttypmod, + perattr->attcollation, + 0); + + perattr = TupleDescAttr(targetrel->rd_att, end_attno - 1); + endcolname = NameStr(perattr->attname); + result->endVar = makeVar( + rtindex, + end_attno, + perattr->atttypid, + perattr->atttypmod, + perattr->attcollation, + 0); + + ReleaseSysCache(perTuple); + } + } + + if (start_attno == InvalidAttrNumber) + { + result->startVar = NULL; + result->endVar = NULL; + } if (forPortionOf->target) @@ -1435,7 +1490,10 @@ transformForPortionOfClause(ParseState *pstate, { /* * Now make sure we update the start/end time of the record. For a - * range col (r) this is `r = r * targetRange`. + * range col (r) this is `r = r * targetRange`. For a PERIOD with cols + * (s, e) this is `s = lower(tsrange(s, e) * targetRange)` and `e = + * upper(tsrange(s, e) * targetRange` (of course not necessarily with + * tsrange, but with whatever range type is used there). */ Oid intersectoperoid; List *funcArgs = NIL; @@ -1470,14 +1528,72 @@ transformForPortionOfClause(ParseState *pstate, rangeTLEExpr = makeFuncExpr(funcid, attr->atttypid, funcArgs, InvalidOid, InvalidOid, COERCE_EXPLICIT_CALL); - /* Make a TLE to set the range column */ + /* Make a TLE to set the range column or start/end columns */ result->rangeTargetList = NIL; - tle = makeTargetEntry((Expr *) rangeTLEExpr, range_attno, range_name, false); - result->rangeTargetList = lappend(result->rangeTargetList, tle); - /* Mark the range column as requiring update permissions */ - target_perminfo->updatedCols = bms_add_member(target_perminfo->updatedCols, - range_attno - FirstLowInvalidHeapAttributeNumber); + if (result->startVar) + { + FuncExpr *boundTLEExpr; + Oid arg_types[1] = {ANYRANGEOID}; + FuncDetailCode fdresult; + int fgc_flags; + Oid rettype; + bool retset; + int nvargs; + Oid vatype; + Oid *declared_arg_types; + Oid elemtypid = get_range_subtype(attr->atttypid); + + /* set the start column */ + fdresult = func_get_detail(SystemFuncName("lower"), NIL, NIL, 1, + arg_types, + false, false, false, &fgc_flags, + &funcid, &rettype, &retset, + &nvargs, &vatype, + &declared_arg_types, NULL); + if (fdresult != FUNCDETAIL_NORMAL) + elog(ERROR, "failed to find lower(anyrange) function"); + boundTLEExpr = makeFuncExpr(funcid, + elemtypid, + list_make1(rangeTLEExpr), + InvalidOid, InvalidOid, COERCE_EXPLICIT_CALL); + tle = makeTargetEntry((Expr *) boundTLEExpr, start_attno, startcolname, false); + result->rangeTargetList = lappend(result->rangeTargetList, tle); + + /* set the end column */ + fdresult = func_get_detail(SystemFuncName("upper"), NIL, NIL, 1, + arg_types, + false, false, false, &fgc_flags, + &funcid, &rettype, &retset, + &nvargs, &vatype, + &declared_arg_types, NULL); + if (fdresult != FUNCDETAIL_NORMAL) + elog(ERROR, "failed to find upper(anyrange) function"); + boundTLEExpr = makeFuncExpr(funcid, + elemtypid, + list_make1(rangeTLEExpr), + InvalidOid, InvalidOid, COERCE_EXPLICIT_CALL); + tle = makeTargetEntry((Expr *) boundTLEExpr, end_attno, endcolname, false); + result->rangeTargetList = lappend(result->rangeTargetList, tle); + + /* + * Mark the start/end columns as requiring update permissions. As + * usual, we don't check permissions for the GENERATED column. + */ + target_perminfo->updatedCols = bms_add_member(target_perminfo->updatedCols, + start_attno - FirstLowInvalidHeapAttributeNumber); + target_perminfo->updatedCols = bms_add_member(target_perminfo->updatedCols, + end_attno - FirstLowInvalidHeapAttributeNumber); + } + else + { + tle = makeTargetEntry((Expr *) rangeTLEExpr, range_attno, range_name, false); + result->rangeTargetList = lappend(result->rangeTargetList, tle); + + /* Mark the range column as requiring update permissions */ + target_perminfo->updatedCols = bms_add_member(target_perminfo->updatedCols, + range_attno - FirstLowInvalidHeapAttributeNumber); + } } else result->rangeTargetList = NIL; diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index 1aabf82ac88..e20cf7e1077 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -599,7 +599,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query); %type col_name_keyword reserved_keyword %type bare_label_keyword -%type DomainConstraint TableConstraint TableLikeClause +%type DomainConstraint TableConstraint TableLikeClause TablePeriod %type TableLikeOptionList TableLikeOption %type column_compression opt_column_compression column_storage opt_column_storage %type ColQualList @@ -2692,6 +2692,24 @@ alter_table_cmd: n->def = (Node *) $4; $$ = (Node *) n; } + /* ALTER TABLE ADD PERIOD FOR (, ) */ + | ADD_P TablePeriod + { + AlterTableCmd *n = makeNode(AlterTableCmd); + n->subtype = AT_AddPeriod; + n->def = $2; + $$ = (Node *)n; + } + /* ALTER TABLE DROP PERIOD FOR [RESTRICT|CASCADE] */ + | DROP PERIOD FOR name opt_drop_behavior + { + AlterTableCmd *n = makeNode(AlterTableCmd); + n->subtype = AT_DropPeriod; + n->name = $4; + n->behavior = $5; + n->missing_ok = false; + $$ = (Node *)n; + } /* ALTER TABLE ADD CONSTRAINT ... */ | ADD_P TableConstraint { @@ -3854,8 +3872,10 @@ TableElement: columnDef { $$ = $1; } | TableLikeClause { $$ = $1; } | TableConstraint { $$ = $1; } + | TablePeriod { $$ = $1; } ; + TypedTableElement: columnOptions { $$ = $1; } | TableConstraint { $$ = $1; } @@ -4233,6 +4253,19 @@ TableLikeOption: ; +TablePeriod: + PERIOD FOR name '(' name ',' name ')' opt_definition + { + PeriodDef *n = makeNode(PeriodDef); + n->periodname = $3; + n->startcolname = $5; + n->endcolname = $7; + n->options = $9; + n->location = @1; + $$ = (Node *) n; + } + ; + /* ConstraintElem specifies constraint syntax which is not embedded into * a column definition. ColConstraintElem specifies the embedded form. * - thomas 1997-12-03 @@ -7367,6 +7400,14 @@ CommentStmt: n->comment = $9; $$ = (Node *) n; } + | COMMENT ON PERIOD any_name IS comment_text + { + CommentStmt *n = makeNode(CommentStmt); + n->objtype = OBJECT_PERIOD; + n->object = (Node *) $4; + n->comment = $6; + $$ = (Node *) n; + } | COMMENT ON LARGE_P OBJECT_P NumericOnly IS comment_text { CommentStmt *n = makeNode(CommentStmt); @@ -18086,7 +18127,6 @@ unreserved_keyword: | PASSING | PASSWORD | PATH - | PERIOD | PLAN | PLANS | POLICY @@ -18392,6 +18432,7 @@ reserved_keyword: | ONLY | OR | ORDER + | PERIOD | PLACING | PRIMARY | REFERENCES diff --git a/src/backend/parser/parse_relation.c b/src/backend/parser/parse_relation.c index 3c80bf1b9ce..eb1d0643b09 100644 --- a/src/backend/parser/parse_relation.c +++ b/src/backend/parser/parse_relation.c @@ -22,6 +22,7 @@ #include "access/table.h" #include "catalog/heap.h" #include "catalog/namespace.h" +#include "catalog/pg_period.h" #include "catalog/pg_type.h" #include "funcapi.h" #include "nodes/makefuncs.h" @@ -3293,6 +3294,7 @@ expandNSItemAttrs(ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, bool require_col_privs, int location) { RangeTblEntry *rte = nsitem->p_rte; + Bitmapset *periodatts = NULL; RTEPermissionInfo *perminfo = nsitem->p_perminfo; List *names, *vars; @@ -3316,12 +3318,20 @@ expandNSItemAttrs(ParseState *pstate, ParseNamespaceItem *nsitem, perminfo->requiredPerms |= ACL_SELECT; } + /* Get PERIOD columns to exclude */ + if (rte->rtekind == RTE_RELATION) + periodatts = get_period_attnos(rte->relid); + forboth(name, names, var, vars) { char *label = strVal(lfirst(name)); Var *varnode = (Var *) lfirst(var); TargetEntry *te; + /* If this column is from a PERIOD, skip it */ + if (bms_is_member(varnode->varattno, periodatts)) + continue; + te = makeTargetEntry((Expr *) varnode, (AttrNumber) pstate->p_next_resno++, label, diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index e96b38a59d5..67e36ac1383 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -37,6 +37,7 @@ #include "catalog/pg_constraint.h" #include "catalog/pg_opclass.h" #include "catalog/pg_operator.h" +#include "catalog/pg_period.h" #include "catalog/pg_statistic_ext.h" #include "catalog/pg_type.h" #include "commands/comment.h" @@ -80,6 +81,7 @@ typedef struct bool isforeign; /* true if CREATE/ALTER FOREIGN TABLE */ bool isalter; /* true if altering existing table */ List *columns; /* ColumnDef items */ + List *periods; /* PeriodDef items */ List *ckconstraints; /* CHECK constraints */ List *nnconstraints; /* NOT NULL constraints */ List *fkconstraints; /* FOREIGN KEY constraints */ @@ -110,6 +112,8 @@ typedef struct static void transformColumnDefinition(CreateStmtContext *cxt, ColumnDef *column); +static void transformTablePeriod(CreateStmtContext *cxt, + PeriodDef *period); static void transformTableConstraint(CreateStmtContext *cxt, Constraint *constraint); static void transformTableLikeClause(CreateStmtContext *cxt, @@ -240,6 +244,7 @@ transformCreateStmt(CreateStmt *stmt, const char *queryString) cxt.inhRelations = stmt->inhRelations; cxt.isalter = false; cxt.columns = NIL; + cxt.periods = NIL; cxt.ckconstraints = NIL; cxt.nnconstraints = NIL; cxt.fkconstraints = NIL; @@ -279,6 +284,10 @@ transformCreateStmt(CreateStmt *stmt, const char *queryString) transformColumnDefinition(&cxt, (ColumnDef *) element); break; + case T_PeriodDef: + transformTablePeriod(&cxt, (PeriodDef *) element); + break; + case T_Constraint: transformTableConstraint(&cxt, (Constraint *) element); break; @@ -367,6 +376,7 @@ transformCreateStmt(CreateStmt *stmt, const char *queryString) * Output results. */ stmt->tableElts = cxt.columns; + stmt->periods = cxt.periods; stmt->constraints = cxt.ckconstraints; stmt->nnconstraints = cxt.nnconstraints; @@ -1027,6 +1037,92 @@ transformColumnDefinition(CreateStmtContext *cxt, ColumnDef *column) } } +void +transformPeriodOptions(PeriodDef *period) +{ + ListCell *option; + DefElem *dconstraintname = NULL; + DefElem *drangetypename = NULL; + DefElem *dcolexists = NULL; + + foreach(option, period->options) + { + DefElem *defel = (DefElem *) lfirst(option); + + if (strcmp(defel->defname, "check_constraint_name") == 0) + { + if (dconstraintname) + ereport(ERROR, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("conflicting or redundant options"))); + dconstraintname = defel; + } + else if (strcmp(defel->defname, "rangetype") == 0) + { + if (drangetypename) + ereport(ERROR, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("conflicting or redundant options"))); + drangetypename = defel; + } + else if (strcmp(defel->defname, "colexists") == 0) + { + if (dcolexists) + ereport(ERROR, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("conflicting or redundant options"))); + dcolexists = defel; + } + else + ereport(ERROR, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("option \"%s\" not recognized", defel->defname))); + } + + if (dconstraintname != NULL) + period->constraintname = defGetString(dconstraintname); + else + period->constraintname = NULL; + + if (drangetypename != NULL) + period->rangetypename = defGetString(drangetypename); + else + period->rangetypename = NULL; + + if (dcolexists != NULL) + period->colexists = defGetBoolean(dcolexists); + else + period->colexists = false; +} + +/* + * transformTablePeriod + * transform a PeriodDef node within CREATE TABLE + */ +static void +transformTablePeriod(CreateStmtContext *cxt, PeriodDef *period) +{ + if (strcmp(period->periodname, "system_time") == 0) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("PERIOD FOR SYSTEM_TIME is not supported"), + parser_errposition(cxt->pstate, + period->location))); + + if (strcmp(period->startcolname, period->endcolname) == 0) + ereport(ERROR, (errmsg("column \"%s\" can't be the start and end column for period \"%s\"", + period->startcolname, period->periodname))); + + /* + * Determine the column info and range type so that + * transformIndexConstraints knows how to create PRIMARY KEY/UNIQUE + * constraints using this PERIOD. + */ + transformPeriodOptions(period); + + cxt->periods = lappend(cxt->periods, period); +} + /* * transformTableConstraint * transform a Constraint node within CREATE TABLE or ALTER TABLE @@ -1128,6 +1224,7 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla AttrNumber parent_attno; Relation relation; TupleDesc tupleDesc; + Bitmapset *periodatts; AclResult aclresult; char *comment; ParseCallbackState pcbstate; @@ -1173,6 +1270,7 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla } tupleDesc = RelationGetDescr(relation); + periodatts = get_period_attnos(RelationGetRelid(relation)); /* * Insert the copied attributes into the cxt for the new table definition. @@ -1182,10 +1280,18 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla for (parent_attno = 1; parent_attno <= tupleDesc->natts; parent_attno++) { - Form_pg_attribute attribute = TupleDescAttr(tupleDesc, - parent_attno - 1); + Form_pg_attribute attribute; ColumnDef *def; + /* + * If this column is from a PERIOD, skip it (since LIKE never copies + * PERIODs). + */ + if (bms_is_member(parent_attno, periodatts)) + continue; + + attribute = TupleDescAttr(tupleDesc, parent_attno - 1); + /* * Ignore dropped columns in the parent. */ @@ -1271,13 +1377,16 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla /* * Reproduce not-null constraints, if any, by copying them. We do this * regardless of options given. + * + * Since we skipped PERIODs' GENERATED columns above, we must skip their + * not-null constraints here. */ if (tupleDesc->constr && tupleDesc->constr->has_not_null) { List *lst; lst = RelationGetNotNullConstraints(RelationGetRelid(relation), false, - true); + true, periodatts); cxt->nnconstraints = list_concat(cxt->nnconstraints, lst); /* Copy comments on not-null constraints */ @@ -2609,6 +2718,7 @@ transformIndexConstraint(Constraint *constraint, CreateStmtContext *cxt) { char *key = strVal(lfirst(lc)); bool found = false; + bool hasperiod = false; ColumnDef *column = NULL; ListCell *columns; IndexElem *iparam; @@ -2627,6 +2737,24 @@ transformIndexConstraint(Constraint *constraint, CreateStmtContext *cxt) if (!found) column = NULL; + /* If the key is WITHOUT OVERLAPS, a PERIOD will work too. */ + if (!found && constraint->without_overlaps && + lc == list_last_cell(constraint->keys)) + { + PeriodDef *period = NULL; + + foreach(columns, cxt->periods) + { + period = lfirst_node(PeriodDef, columns); + if (strcmp(period->periodname, key) == 0) + { + found = true; + hasperiod = true; + break; + } + } + } + if (found) { /* @@ -2643,24 +2771,37 @@ transformIndexConstraint(Constraint *constraint, CreateStmtContext *cxt) if (constraint->contype == CONSTR_PRIMARY && !cxt->isalter) { - if (column->is_not_null) + if (column) { - foreach_node(Constraint, nn, cxt->nnconstraints) + if (column->is_not_null) { - if (strcmp(strVal(linitial(nn->keys)), key) == 0) + foreach_node(Constraint, nn, cxt->nnconstraints) { - if (nn->is_no_inherit) - ereport(ERROR, - errcode(ERRCODE_SYNTAX_ERROR), - errmsg("conflicting NO INHERIT declaration for not-null constraint on column \"%s\"", - key)); - break; + if (strcmp(strVal(linitial(nn->keys)), key) == 0) + { + if (nn->is_no_inherit) + ereport(ERROR, + errcode(ERRCODE_SYNTAX_ERROR), + errmsg("conflicting NO INHERIT declaration for not-null constraint on column \"%s\"", + key)); + break; + } } } + else + { + column->is_not_null = true; + cxt->nnconstraints = + lappend(cxt->nnconstraints, + makeNotNullConstraint(makeString(key))); + } } - else + else if (hasperiod) { - column->is_not_null = true; + /* + * If we're using a PERIOD, we better make sure it is + * NOT NULL + */ cxt->nnconstraints = lappend(cxt->nnconstraints, makeNotNullConstraint(makeString(key))); @@ -2787,7 +2928,13 @@ transformIndexConstraint(Constraint *constraint, CreateStmtContext *cxt) } } } - if (found) + + /* + * In CREATE TABLE we don't know PERIODs' rangetype yet, but + * we know it will be a range/multirange. So if we have a + * PERIOD then we're safe. + */ + if (found && !hasperiod) { if (!OidIsValid(typid) && column) typid = typenameTypeId(NULL, column->typeName); @@ -3113,6 +3260,10 @@ transformIndexStmt(Oid relid, IndexStmt *stmt, const char *queryString) } } + /* take care of the period */ + if (stmt->period) + stmt->period->oid = get_period_oid(relid, stmt->period->periodname, false); + /* * Check that only the base rel is mentioned. (This should be dead code * now that add_missing_from is history.) @@ -3570,6 +3721,7 @@ transformAlterTableStmt(Oid relid, AlterTableStmt *stmt, cxt.inhRelations = NIL; cxt.isalter = true; cxt.columns = NIL; + cxt.periods = NIL; cxt.ckconstraints = NIL; cxt.nnconstraints = NIL; cxt.fkconstraints = NIL; diff --git a/src/backend/utils/adt/Makefile b/src/backend/utils/adt/Makefile index cc68ac545a5..5d8cb353012 100644 --- a/src/backend/utils/adt/Makefile +++ b/src/backend/utils/adt/Makefile @@ -80,6 +80,7 @@ OBJS = \ oracle_compat.o \ orderedsetaggs.o \ partitionfuncs.o \ + period.o \ pg_locale.o \ pg_locale_builtin.o \ pg_locale_icu.o \ diff --git a/src/backend/utils/adt/period.c b/src/backend/utils/adt/period.c new file mode 100644 index 00000000000..6f899068d03 --- /dev/null +++ b/src/backend/utils/adt/period.c @@ -0,0 +1,58 @@ +/*------------------------------------------------------------------------- + * + * period.c + * Functions to support periods. + * + * + * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * + * IDENTIFICATION + * src/backend/utils/adt/period.c + * + *------------------------------------------------------------------------- + */ +#include "postgres.h" + +#include "executor/tuptable.h" +#include "fmgr.h" +#include "nodes/primnodes.h" +#include "utils/fmgrprotos.h" +#include "utils/period.h" +#include "utils/rangetypes.h" + +Datum +period_to_range(TupleTableSlot *slot, int startattno, int endattno, Oid rangetype) +{ + Datum startvalue; + Datum endvalue; + Datum result; + bool startisnull; + bool endisnull; + + LOCAL_FCINFO(fcinfo, 2); + FmgrInfo flinfo; + FuncExpr *f; + + InitFunctionCallInfoData(*fcinfo, &flinfo, 2, InvalidOid, NULL, NULL); + f = makeNode(FuncExpr); + f->funcresulttype = rangetype; + flinfo.fn_expr = (Node *) f; + flinfo.fn_extra = NULL; + + /* compute oldvalue */ + startvalue = slot_getattr(slot, startattno, &startisnull); + endvalue = slot_getattr(slot, endattno, &endisnull); + + fcinfo->args[0].value = startvalue; + fcinfo->args[0].isnull = startisnull; + fcinfo->args[1].value = endvalue; + fcinfo->args[1].isnull = endisnull; + + result = range_constructor2(fcinfo); + if (fcinfo->isnull) + elog(ERROR, "function %u returned NULL", flinfo.fn_oid); + + return result; +} diff --git a/src/backend/utils/cache/lsyscache.c b/src/backend/utils/cache/lsyscache.c index 43158afac15..34c98188b0d 100644 --- a/src/backend/utils/cache/lsyscache.c +++ b/src/backend/utils/cache/lsyscache.c @@ -33,6 +33,7 @@ #include "catalog/pg_opclass.h" #include "catalog/pg_opfamily.h" #include "catalog/pg_operator.h" +#include "catalog/pg_period.h" #include "catalog/pg_proc.h" #include "catalog/pg_publication.h" #include "catalog/pg_range.h" @@ -1088,6 +1089,68 @@ get_attoptions(Oid relid, int16 attnum) return result; } +/* ---------- PG_PERIOD CACHE ---------- */ + +/* + * get_periodname - given its OID, look up a period + * + * If missing_ok is false, throw an error if the period is not found. + * If true, just return InvalidOid. + */ +char * +get_periodname(Oid periodid, bool missing_ok) +{ + HeapTuple tp; + + tp = SearchSysCache1(PERIODOID, + ObjectIdGetDatum(periodid)); + if (HeapTupleIsValid(tp)) + { + Form_pg_period period_tup = (Form_pg_period) GETSTRUCT(tp); + char *result; + + result = pstrdup(NameStr(period_tup->pername)); + ReleaseSysCache(tp); + return result; + } + + if (!missing_ok) + elog(ERROR, "cache lookup failed for period %d", + periodid); + return NULL; +} + +/* + * get_period_oid - gets its relation and name, look up a period + * + * If missing_ok is false, throw an error if the cast is not found. If + * true, just return InvalidOid. + */ +Oid +get_period_oid(Oid relid, const char *periodname, bool missing_ok) +{ + HeapTuple tp; + + tp = SearchSysCache2(PERIODNAME, + ObjectIdGetDatum(relid), + PointerGetDatum(periodname)); + + if (HeapTupleIsValid(tp)) + { + Form_pg_period period_tup = (Form_pg_period) GETSTRUCT(tp); + Oid result; + + result = period_tup->oid; + ReleaseSysCache(tp); + return result; + } + + if (!missing_ok) + elog(ERROR, "cache lookup failed for period %s", + periodname); + return InvalidOid; +} + /* ---------- PG_CAST CACHE ---------- */ /* @@ -3692,6 +3755,31 @@ get_multirange_range(Oid multirangeOid) return InvalidOid; } +Oid +get_subtype_range(Oid subtypeOid) +{ + CatCList *catlist; + Oid result = InvalidOid; + + catlist = SearchSysCacheList1(RANGESUBTYPE, ObjectIdGetDatum(subtypeOid)); + + if (catlist->n_members == 1) + { + HeapTuple tuple = &catlist->members[0]->tuple; + Form_pg_range rngtup = (Form_pg_range) GETSTRUCT(tuple); + + result = rngtup->rngtypid; + ReleaseCatCacheList(catlist); + } + else if (catlist->n_members > 1) + ereport(ERROR, + (errcode(ERRCODE_INDETERMINATE_DATATYPE), + errmsg("ambiguous range for type %s", + format_type_be(subtypeOid)))); + + return result; +} + /* ---------- PG_INDEX CACHE ---------- */ /* diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c index 59eaecb4ed7..820e89e4fd5 100644 --- a/src/bin/pg_dump/pg_backup_archiver.c +++ b/src/bin/pg_dump/pg_backup_archiver.c @@ -3873,6 +3873,7 @@ _getObjectDescription(PQExpBuffer buf, const TocEntry *te) strcmp(type, "DATABASE PROPERTIES") == 0 || strcmp(type, "DEFAULT") == 0 || strcmp(type, "FK CONSTRAINT") == 0 || + strcmp(type, "PERIOD") == 0 || strcmp(type, "INDEX") == 0 || strcmp(type, "RULE") == 0 || strcmp(type, "TRIGGER") == 0 || diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index 47913178a93..8c360547832 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -7184,6 +7184,7 @@ getTables(Archive *fout, int *numTables) int i_reltype; int i_relowner; int i_relchecks; + int i_nperiod; int i_relhasindex; int i_relhasrules; int i_relpages; @@ -7271,6 +7272,14 @@ getTables(Archive *fout, int *numTables) appendPQExpBufferStr(query, "c.relhasoids, "); + /* In PG19 upwards we have PERIODs. */ + if (fout->remoteVersion >= 190000) + appendPQExpBufferStr(query, + "(SELECT count(*) FROM pg_period WHERE perrelid = c.oid) AS nperiods, "); + else + appendPQExpBufferStr(query, + "0 AS nperiods, "); + if (fout->remoteVersion >= 90300) appendPQExpBufferStr(query, "c.relispopulated, "); @@ -7408,6 +7417,7 @@ getTables(Archive *fout, int *numTables) i_reltype = PQfnumber(res, "reltype"); i_relowner = PQfnumber(res, "relowner"); i_relchecks = PQfnumber(res, "relchecks"); + i_nperiod = PQfnumber(res, "nperiods"); i_relhasindex = PQfnumber(res, "relhasindex"); i_relhasrules = PQfnumber(res, "relhasrules"); i_relpages = PQfnumber(res, "relpages"); @@ -7497,6 +7507,7 @@ getTables(Archive *fout, int *numTables) } tblinfo[i].reltablespace = pg_strdup(PQgetvalue(res, i, i_reltablespace)); tblinfo[i].hasoids = (strcmp(PQgetvalue(res, i, i_relhasoids), "t") == 0); + tblinfo[i].nperiod = atoi(PQgetvalue(res, i, i_nperiod)); tblinfo[i].hastriggers = (strcmp(PQgetvalue(res, i, i_relhastriggers), "t") == 0); tblinfo[i].relpersistence = *(PQgetvalue(res, i, i_relpersistence)); tblinfo[i].relispopulated = (strcmp(PQgetvalue(res, i, i_relispopulated), "t") == 0); @@ -7955,7 +7966,7 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int numTables) appendPQExpBufferStr(query, "false AS indnullsnotdistinct, "); - if (fout->remoteVersion >= 180000) + if (fout->remoteVersion >= 190000) appendPQExpBufferStr(query, "c.conperiod "); else @@ -9212,7 +9223,7 @@ getTransforms(Archive *fout) /* * getTableAttrs - * for each interesting table, read info about its attributes - * (names, types, default values, CHECK constraints, etc) + * (names, types, default values, CHECK constraints, PERIODs, etc) * * modifies tblinfo */ @@ -9267,6 +9278,8 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables) for (int i = 0; i < numTables; i++) { TableInfo *tbinfo = &tblinfo[i]; + int ndumpablechecks; /* number of CHECK constraints that do + * not belong to a period */ /* Don't bother to collect info for sequences */ if (tbinfo->relkind == RELKIND_SEQUENCE) @@ -9293,7 +9306,8 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables) appendPQExpBufferChar(tbloids, ','); appendPQExpBuffer(tbloids, "%u", tbinfo->dobj.catId.oid); - if (tbinfo->ncheck > 0) + ndumpablechecks = tbinfo->ncheck - tbinfo->nperiod; + if (ndumpablechecks > 0) { /* Also make a list of the ones with check constraints */ if (checkoids->len > 1) /* do we have more than the '{'? */ @@ -9828,15 +9842,36 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables) pg_log_info("finding table check constraints"); resetPQExpBuffer(q); - appendPQExpBuffer(q, - "SELECT c.tableoid, c.oid, conrelid, conname, " - "pg_catalog.pg_get_constraintdef(c.oid) AS consrc, " - "conislocal, convalidated " - "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n" - "JOIN pg_catalog.pg_constraint c ON (src.tbloid = c.conrelid)\n" - "WHERE contype = 'c' " - "ORDER BY c.conrelid, c.conname", - checkoids->data); + if (fout->remoteVersion >= 190000) + { + /* + * PERIODs were added in v19 and we don't dump CHECK constraints + * for them. + */ + appendPQExpBuffer(q, + "SELECT c.tableoid, c.oid, conrelid, conname, " + "pg_catalog.pg_get_constraintdef(c.oid) AS consrc, " + "conislocal, convalidated " + "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n" + "JOIN pg_catalog.pg_constraint c ON (src.tbloid = c.conrelid)\n" + "WHERE contype = 'c' " + " AND NOT EXISTS (SELECT FROM pg_period " + " WHERE (perrelid, perconstraint) = (conrelid, c.oid)) " + "ORDER BY c.conrelid, c.conname", + checkoids->data); + } + else + { + appendPQExpBuffer(q, + "SELECT c.tableoid, c.oid, conrelid, conname, " + "pg_catalog.pg_get_constraintdef(c.oid) AS consrc, " + "conislocal, convalidated " + "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n" + "JOIN pg_catalog.pg_constraint c ON (src.tbloid = c.conrelid)\n" + "WHERE contype = 'c' " + "ORDER BY c.conrelid, c.conname", + checkoids->data); + } res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK); @@ -9858,6 +9893,7 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables) Oid conrelid = atooid(PQgetvalue(res, j, i_conrelid)); TableInfo *tbinfo = NULL; int numcons; + int ndumpablechecks; /* Count rows for this table */ for (numcons = 1; numcons < numConstrs - j; numcons++) @@ -9877,12 +9913,13 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables) if (curtblindx >= numTables) pg_fatal("unrecognized table OID %u", conrelid); - if (numcons != tbinfo->ncheck) + ndumpablechecks = tbinfo->ncheck - tbinfo->nperiod; + if (numcons != ndumpablechecks) { pg_log_error(ngettext("expected %d check constraint on table \"%s\" but found %d", "expected %d check constraints on table \"%s\" but found %d", - tbinfo->ncheck), - tbinfo->ncheck, tbinfo->dobj.name, numcons); + ndumpablechecks), + ndumpablechecks, tbinfo->dobj.name, numcons); pg_log_error_hint("The system catalogs might be corrupted."); exit_nicely(1); } @@ -9941,6 +9978,80 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables) PQclear(res); } + for (int i = 0; i < numTables; i++) + { + TableInfo *tbinfo = &tblinfo[i]; + + /* + * Get info about PERIOD definitions + */ + if (tbinfo->nperiod > 0) + { + PeriodInfo *periods; + int numPeriods; + int j; + + /* We shouldn't have any periods before v19 */ + Assert(fout->remoteVersion >= 190000); + + pg_log_info("finding periods for table \"%s.%s\"", + tbinfo->dobj.namespace->dobj.name, + tbinfo->dobj.name); + + resetPQExpBuffer(q); + appendPQExpBuffer(q, + "SELECT p.tableoid, p.oid, p.pername, " + " sa.attname AS perstart, ea.attname AS perend, " + " r.typname AS rngtype, " + " c.conname AS conname " + "FROM pg_catalog.pg_period AS p " + "JOIN pg_catalog.pg_attribute AS sa ON (sa.attrelid, sa.attnum) = (p.perrelid, p.perstart) " + "JOIN pg_catalog.pg_attribute AS ea ON (ea.attrelid, ea.attnum) = (p.perrelid, p.perend) " + "JOIN pg_catalog.pg_attribute AS ra ON (ra.attrelid, ra.attnum) = (p.perrelid, p.perrange) " + "JOIN pg_catalog.pg_type AS r ON r.oid = ra.atttypid " + "JOIN pg_catalog.pg_constraint AS c ON c.oid = p.perconstraint " + "WHERE p.perrelid = '%u'::pg_catalog.oid " + "ORDER BY p.pername", + tbinfo->dobj.catId.oid); + + res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK); + + /* + * If we didn't get the number of rows we thought we were going + * to, then those JOINs didn't work. + */ + numPeriods = PQntuples(res); + if (numPeriods != tbinfo->nperiod) + { + pg_log_info(ngettext("expected %d period on table \"%s\" but found %d", + "expected %d periods on table \"%s\" but found %d", + tbinfo->nperiod), + tbinfo->nperiod, tbinfo->dobj.name, numPeriods); + pg_log_info("(The system catalogs might be corrupted.)"); + exit_nicely(1); + } + + periods = (PeriodInfo *) pg_malloc(numPeriods * sizeof(PeriodInfo)); + tbinfo->periods = periods; + + for (j = 0; j < numPeriods; j++) + { + periods[j].dobj.objType = DO_PERIOD; + periods[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, 0)); + periods[j].dobj.catId.oid = atooid(PQgetvalue(res, j, 1)); + AssignDumpId(&periods[j].dobj); + periods[j].dobj.name = pg_strdup(PQgetvalue(res, j, 2)); + periods[j].dobj.namespace = tbinfo->dobj.namespace; + periods[j].pertable = tbinfo; + periods[j].perstart = pg_strdup(PQgetvalue(res, j, 3)); + periods[j].perend = pg_strdup(PQgetvalue(res, j, 4)); + periods[j].rngtype = pg_strdup(PQgetvalue(res, j, 5)); + periods[j].conname = pg_strdup(PQgetvalue(res, j, 6)); + } + PQclear(res); + } + } + destroyPQExpBuffer(q); destroyPQExpBuffer(tbloids); destroyPQExpBuffer(checkoids); @@ -11731,6 +11842,8 @@ dumpDumpableObject(Archive *fout, DumpableObject *dobj) case DO_FK_CONSTRAINT: dumpConstraint(fout, (const ConstraintInfo *) dobj); break; + case DO_PERIOD: + break; case DO_PROCLANG: dumpProcLang(fout, (const ProcLangInfo *) dobj); break; @@ -17388,6 +17501,36 @@ dumpTableSchema(Archive *fout, const TableInfo *tbinfo) } } + /* + * Add non-inherited PERIOD definitions, if any. + */ + for (j = 0; j < tbinfo->nperiod; j++) + { + PeriodInfo *period = &(tbinfo->periods[j]); + + char *name = pg_strdup(fmtId(period->dobj.name)); + char *start = pg_strdup(fmtId(period->perstart)); + char *end = pg_strdup(fmtId(period->perend)); + char *rngtype = pg_strdup(fmtId(period->rngtype)); + char *conname = pg_strdup(fmtId(period->conname)); + + if (actual_atts == 0) + appendPQExpBufferStr(q, " (\n "); + else + appendPQExpBufferStr(q, ",\n "); + + /* + * Always say colexists so we can just print the GENERATED + * column + */ + appendPQExpBuffer(q, "PERIOD FOR %s (%s, %s) " + "WITH (rangetype = %s, check_constraint_name = %s, colexists = true)", + name, start, end, + rngtype, conname); + + actual_atts++; + } + /* * Add non-inherited CHECK constraints, if any. * @@ -17396,7 +17539,7 @@ dumpTableSchema(Archive *fout, const TableInfo *tbinfo) * PARTITION that we'll emit later expects the constraint to be * there. (No need to fix conislocal: ATTACH PARTITION does that) */ - for (j = 0; j < tbinfo->ncheck; j++) + for (j = 0; j < tbinfo->ncheck - tbinfo->nperiod; j++) { ConstraintInfo *constr = &(tbinfo->checkexprs[j]); @@ -17708,7 +17851,7 @@ dumpTableSchema(Archive *fout, const TableInfo *tbinfo) */ resetPQExpBuffer(extra); firstitem = true; - for (k = 0; k < tbinfo->ncheck; k++) + for (k = 0; k < tbinfo->ncheck - tbinfo->nperiod; k++) { ConstraintInfo *constr = &(tbinfo->checkexprs[k]); @@ -18052,7 +18195,7 @@ dumpTableSchema(Archive *fout, const TableInfo *tbinfo) } /* Dump comments on inlined table constraints */ - for (j = 0; j < tbinfo->ncheck; j++) + for (j = 0; j < tbinfo->ncheck - tbinfo->nperiod; j++) { ConstraintInfo *constr = &(tbinfo->checkexprs[j]); @@ -20176,6 +20319,7 @@ addBoundaryDependencies(DumpableObject **dobjs, int numObjs, case DO_TRIGGER: case DO_EVENT_TRIGGER: case DO_DEFAULT_ACL: + case DO_PERIOD: case DO_POLICY: case DO_PUBLICATION: case DO_PUBLICATION_REL: diff --git a/src/bin/pg_dump/pg_dump.h b/src/bin/pg_dump/pg_dump.h index 72a00e1bc20..d376c87cd07 100644 --- a/src/bin/pg_dump/pg_dump.h +++ b/src/bin/pg_dump/pg_dump.h @@ -60,6 +60,7 @@ typedef enum DO_TRIGGER, DO_CONSTRAINT, DO_FK_CONSTRAINT, /* see note for ConstraintInfo */ + DO_PERIOD, DO_PROCLANG, DO_CAST, DO_TABLE_DATA, @@ -322,12 +323,14 @@ typedef struct _tableInfo bool rowsec; /* is row security enabled? */ bool forcerowsec; /* is row security forced? */ bool hasoids; /* does it have OIDs? */ + bool hasperiods; /* does it have any periods? */ uint32 frozenxid; /* table's relfrozenxid */ uint32 minmxid; /* table's relminmxid */ Oid toast_oid; /* toast table's OID, or 0 if none */ uint32 toast_frozenxid; /* toast table's relfrozenxid, if any */ uint32 toast_minmxid; /* toast table's relminmxid */ int ncheck; /* # of CHECK expressions */ + int nperiod; /* # of PERIOD definitions */ Oid reltype; /* OID of table's composite type, if any */ Oid reloftype; /* underlying type for typed table */ Oid foreign_server; /* foreign server oid, if applicable */ @@ -358,6 +361,7 @@ typedef struct _tableInfo char *attstorage; /* attribute storage scheme */ char *typstorage; /* type storage scheme */ bool *attisdropped; /* true if attr is dropped; don't dump it */ + bool *attisperiod; /* true if attr is a PERIOD; don't dump it */ char *attidentity; char *attgenerated; int *attlen; /* attribute length, used by binary_upgrade */ @@ -379,6 +383,7 @@ typedef struct _tableInfo struct _attrDefInfo **attrdefs; /* DEFAULT expressions */ struct _constraintInfo *checkexprs; /* CHECK constraints */ struct _relStatsInfo *stats; /* only set for matviews */ + struct _periodInfo *periods; /* PERIOD definitions */ bool needs_override; /* has GENERATED ALWAYS AS IDENTITY */ char *amname; /* relation access method */ @@ -528,6 +533,16 @@ typedef struct _constraintInfo bool separate; /* true if must dump as separate item */ } ConstraintInfo; +typedef struct _periodInfo +{ + DumpableObject dobj; + TableInfo *pertable; + char *perstart; /* the name of the start column */ + char *perend; /* the name of the end column */ + char *rngtype; /* the name of the range type */ + char *conname; /* the name of the CHECK constraint */ +} PeriodInfo; + typedef struct _procLangInfo { DumpableObject dobj; diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c index 164c76e0864..ebc20a226c7 100644 --- a/src/bin/pg_dump/pg_dump_sort.c +++ b/src/bin/pg_dump/pg_dump_sort.c @@ -86,6 +86,7 @@ enum dbObjectTypePriorities PRIO_CONSTRAINT, PRIO_INDEX, PRIO_INDEX_ATTACH, + PRIO_PERIOD, PRIO_STATSEXT, PRIO_RULE, PRIO_TRIGGER, @@ -121,6 +122,7 @@ static const int dbObjectTypePriority[] = [DO_ATTRDEF] = PRIO_ATTRDEF, [DO_INDEX] = PRIO_INDEX, [DO_INDEX_ATTACH] = PRIO_INDEX_ATTACH, + [DO_PERIOD] = PRIO_PERIOD, [DO_STATSEXT] = PRIO_STATSEXT, [DO_RULE] = PRIO_RULE, [DO_TRIGGER] = PRIO_TRIGGER, @@ -454,6 +456,23 @@ DOTypeNameCompare(const void *p1, const void *p2) if (cmpval != 0) return cmpval; } + else if (obj1->objType == DO_PERIOD) + { + PeriodInfo *pobj1 = *(PeriodInfo *const *) p1; + PeriodInfo *pobj2 = *(PeriodInfo *const *) p2; + + /* Sort by table namespace . . . */ + cmpval = strcmp(pobj1->pertable->dobj.namespace->dobj.name, + pobj2->pertable->dobj.namespace->dobj.name); + if (cmpval != 0) + return cmpval; + + /* . . . and table name */ + cmpval = strcmp(pobj1->pertable->dobj.name, + pobj2->pertable->dobj.name); + if (cmpval != 0) + return cmpval; + } /* * Shouldn't get here except after catalog corruption, but if we do, sort @@ -1626,6 +1645,11 @@ describeDumpableObject(DumpableObject *obj, char *buf, int bufsize) "FK CONSTRAINT %s (ID %d OID %u)", obj->name, obj->dumpId, obj->catId.oid); return; + case DO_PERIOD: + snprintf(buf, bufsize, + "PERIOD %s (ID %d OID %u)", + obj->name, obj->dumpId, obj->catId.oid); + return; case DO_PROCLANG: snprintf(buf, bufsize, "PROCEDURAL LANGUAGE %s (ID %d OID %u)", diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index 36f24502842..f35b0a9e367 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -2024,6 +2024,8 @@ describeOneTableDetails(const char *schemaname, appendPQExpBufferStr(&buf, "\nFROM pg_catalog.pg_attribute a"); appendPQExpBuffer(&buf, "\nWHERE a.attrelid = '%s' AND a.attnum > 0 AND NOT a.attisdropped", oid); + if (pset.sversion >= 180000) + appendPQExpBuffer(&buf, "\nAND NOT EXISTS (SELECT 1 FROM pg_catalog.pg_period p WHERE p.perrelid = a.attrelid AND p.perrange = a.attnum)"); appendPQExpBufferStr(&buf, "\nORDER BY a.attnum;"); res = PSQLexec(buf.data); @@ -2452,6 +2454,40 @@ describeOneTableDetails(const char *schemaname, PGresult *result = NULL; int tuples = 0; + /* print periods */ + if (pset.sversion >= 180000) + { + printfPQExpBuffer(&buf, + "SELECT quote_ident(p.pername), quote_ident(s.attname) AS startatt, quote_ident(e.attname) AS endatt\n" + "FROM pg_period AS p\n" + "JOIN pg_attribute AS s ON (s.attrelid, s.attnum) = (p.perrelid, p.perstart)\n" + "JOIN pg_attribute AS e ON (e.attrelid, e.attnum) = (p.perrelid, p.perend)\n" + "WHERE p.perrelid = '%s'\n" + "ORDER BY 1;", + oid); + result = PSQLexec(buf.data); + if (!result) + goto error_return; + else + tuples = PQntuples(result); + + if (tuples > 0) + { + printTableAddFooter(&cont, _("Periods:")); + for (i = 0; i < tuples; i++) + { + /* untranslated constraint name and def */ + printfPQExpBuffer(&buf, " %s (%s, %s)", + PQgetvalue(result, i, 0), + PQgetvalue(result, i, 1), + PQgetvalue(result, i, 2)); + + printTableAddFooter(&cont, buf.data); + } + } + PQclear(result); + } + /* print indexes */ if (tableinfo.hasindex) { diff --git a/src/include/catalog/Makefile b/src/include/catalog/Makefile index 2bbc7805fe3..b9aee2ca49e 100644 --- a/src/include/catalog/Makefile +++ b/src/include/catalog/Makefile @@ -65,6 +65,7 @@ CATALOG_HEADERS := \ pg_foreign_server.h \ pg_user_mapping.h \ pg_foreign_table.h \ + pg_period.h \ pg_policy.h \ pg_replication_origin.h \ pg_default_acl.h \ diff --git a/src/include/catalog/heap.h b/src/include/catalog/heap.h index dbd339e9df4..3ec5a583d78 100644 --- a/src/include/catalog/heap.h +++ b/src/include/catalog/heap.h @@ -125,6 +125,10 @@ extern void StoreAttrMissingVal(Relation rel, AttrNumber attnum, Datum missingval); extern void SetAttrMissing(Oid relid, char *attname, char *value); +extern Oid StorePeriod(Relation rel, const char *period, + AttrNumber startnum, AttrNumber endnum, + AttrNumber rangenum, Oid conoid); + extern Node *cookDefault(ParseState *pstate, Node *raw_default, Oid atttypid, diff --git a/src/include/catalog/meson.build b/src/include/catalog/meson.build index ec1cf467f6f..87cb4ce3300 100644 --- a/src/include/catalog/meson.build +++ b/src/include/catalog/meson.build @@ -61,6 +61,7 @@ catalog_headers = [ 'pg_collation.h', 'pg_parameter_acl.h', 'pg_partitioned_table.h', + 'pg_period.h', 'pg_range.h', 'pg_transform.h', 'pg_sequence.h', diff --git a/src/include/catalog/pg_constraint.h b/src/include/catalog/pg_constraint.h index f8a01d89617..2f7f9a54c2e 100644 --- a/src/include/catalog/pg_constraint.h +++ b/src/include/catalog/pg_constraint.h @@ -266,7 +266,8 @@ extern AttrNumber extractNotNullColumn(HeapTuple constrTup); extern bool AdjustNotNullInheritance(Oid relid, AttrNumber attnum, bool is_local, bool is_no_inherit, bool is_notvalid); extern List *RelationGetNotNullConstraints(Oid relid, bool cooked, - bool include_noinh); + bool include_noinh, + Bitmapset *excludes); extern void RemoveConstraintById(Oid conId); extern void RenameConstraintById(Oid conId, const char *newname); diff --git a/src/include/catalog/pg_index.h b/src/include/catalog/pg_index.h index 731d3938169..890ff551176 100644 --- a/src/include/catalog/pg_index.h +++ b/src/include/catalog/pg_index.h @@ -48,7 +48,6 @@ CATALOG(pg_index,2610,IndexRelationId) BKI_SCHEMA_MACRO /* variable-length fields start here, but we allow direct access to indkey */ int2vector indkey BKI_FORCE_NOT_NULL; /* column numbers of indexed cols, * or 0 */ - #ifdef CATALOG_VARLEN oidvector indcollation BKI_LOOKUP_OPT(pg_collation) BKI_FORCE_NOT_NULL; /* collation identifiers */ oidvector indclass BKI_LOOKUP(pg_opclass) BKI_FORCE_NOT_NULL; /* opclass identifiers */ diff --git a/src/include/catalog/pg_period.h b/src/include/catalog/pg_period.h new file mode 100644 index 00000000000..65a74dfa18b --- /dev/null +++ b/src/include/catalog/pg_period.h @@ -0,0 +1,58 @@ +/*------------------------------------------------------------------------- + * + * pg_period.h + * definition of the "period" system catalog (pg_period) + * + * + * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group + * + * src/include/catalog/pg_period.h + * + * NOTES + * The Catalog.pm module reads this file and derives schema + * information. + * + *------------------------------------------------------------------------- + */ +#ifndef PG_PERIOD_H +#define PG_PERIOD_H + +#include "catalog/genbki.h" +#include "catalog/pg_period_d.h" +#include "nodes/bitmapset.h" + +/* ---------------- + * pg_period definition. cpp turns this into + * typedef struct FormData_pg_period + * ---------------- + */ +CATALOG(pg_period,8000,PeriodRelationId) +{ + Oid oid; /* OID of the period */ + NameData pername; /* name of period */ + Oid perrelid; /* OID of relation containing this period */ + int16 perstart; /* column for start value */ + int16 perend; /* column for end value */ + int16 perrange; /* column for range value */ + Oid perconstraint; /* OID of (start < end) constraint */ +} FormData_pg_period; + +/* ---------------- + * Form_pg_period corresponds to a pointer to a tuple with + * the format of pg_period relation. + * ---------------- + */ +typedef FormData_pg_period *Form_pg_period; + +DECLARE_UNIQUE_INDEX_PKEY(pg_period_oid_index, 8001, PeriodObjectIndexId, pg_period, btree(oid oid_ops)); +DECLARE_UNIQUE_INDEX(pg_period_perrelid_pername_index, 8002, PeriodRelidNameIndexId, pg_period, btree(perrelid oid_ops, pername name_ops)); + +MAKE_SYSCACHE(PERIODOID, pg_period_oid_index, 32); +MAKE_SYSCACHE(PERIODNAME, pg_period_perrelid_pername_index, 32); + +extern void RemovePeriodById(Oid periodId); + +extern Oid get_relation_period_oid(Oid relid, const char *pername, bool missing_ok); +extern Bitmapset *get_period_attnos(Oid relid); + +#endif /* PG_PERIOD_H */ diff --git a/src/include/catalog/pg_range.h b/src/include/catalog/pg_range.h index 3bba6162782..89e60d6cbf1 100644 --- a/src/include/catalog/pg_range.h +++ b/src/include/catalog/pg_range.h @@ -59,8 +59,10 @@ typedef FormData_pg_range *Form_pg_range; DECLARE_UNIQUE_INDEX_PKEY(pg_range_rngtypid_index, 3542, RangeTypidIndexId, pg_range, btree(rngtypid oid_ops)); DECLARE_UNIQUE_INDEX(pg_range_rngmultitypid_index, 2228, RangeMultirangeTypidIndexId, pg_range, btree(rngmultitypid oid_ops)); +DECLARE_UNIQUE_INDEX(pg_range_rngsubtype_rngtypid_index, 8003, RangeSubTypidTypidIndexId, pg_range, btree(rngsubtype oid_ops, rngtypid oid_ops)); MAKE_SYSCACHE(RANGETYPE, pg_range_rngtypid_index, 4); +MAKE_SYSCACHE(RANGESUBTYPE, pg_range_rngsubtype_rngtypid_index, 4); MAKE_SYSCACHE(RANGEMULTIRANGE, pg_range_rngmultitypid_index, 4); /* diff --git a/src/include/commands/tablecmds.h b/src/include/commands/tablecmds.h index e9b0fab0767..bf8d6e33183 100644 --- a/src/include/commands/tablecmds.h +++ b/src/include/commands/tablecmds.h @@ -41,7 +41,8 @@ extern LOCKMODE AlterTableGetLockLevel(List *cmds); extern void ATExecChangeOwner(Oid relationOid, Oid newOwnerId, bool recursing, LOCKMODE lockmode); -extern void AlterTableInternal(Oid relid, List *cmds, bool recurse); +extern void AlterTableInternal(Oid relid, List *cmds, bool recurse, + struct AlterTableUtilityContext *context); extern Oid AlterTableMoveAll(AlterTableMoveAllStmt *stmt); @@ -107,5 +108,6 @@ extern void RangeVarCallbackOwnsRelation(const RangeVar *relation, Oid relId, Oid oldRelId, void *arg); extern bool PartConstraintImpliedByRelConstraint(Relation scanrel, List *partConstraint); +extern Oid choose_rangetype_for_period(PeriodDef *period); #endif /* TABLECMDS_H */ diff --git a/src/include/nodes/execnodes.h b/src/include/nodes/execnodes.h index 77c9469139b..a0fb56d63fd 100644 --- a/src/include/nodes/execnodes.h +++ b/src/include/nodes/execnodes.h @@ -465,6 +465,10 @@ typedef struct ForPortionOfState char *fp_rangeName; /* the column named in FOR PORTION OF */ Oid fp_rangeType; /* the type of the FOR PORTION OF expression */ int fp_rangeAttno; /* the attno of the range column */ + int fp_periodStartAttno; /* the attno of the PERIOD start + * column (or 0 for a range) */ + int fp_periodEndAttno; /* the attno of the PERIOD end column (or + * 0 for a range) */ Datum fp_targetRange; /* the range/multirange from FOR PORTION OF */ TypeCacheEntry *fp_leftoverstypcache; /* type cache entry of the range */ TupleTableSlot *fp_Existing; /* slot to store old tuple */ diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h index a09e9d9ba6a..3785be6f85d 100644 --- a/src/include/nodes/parsenodes.h +++ b/src/include/nodes/parsenodes.h @@ -2370,6 +2370,7 @@ typedef enum ObjectType OBJECT_OPERATOR, OBJECT_OPFAMILY, OBJECT_PARAMETER_ACL, + OBJECT_PERIOD, OBJECT_POLICY, OBJECT_PROCEDURE, OBJECT_PUBLICATION, @@ -2457,6 +2458,8 @@ typedef enum AlterTableType AT_ValidateConstraint, /* validate constraint */ AT_AddIndexConstraint, /* add constraint using existing index */ AT_DropConstraint, /* drop constraint */ + AT_AddPeriod, /* ADD PERIOD */ + AT_DropPeriod, /* DROP PERIOD */ AT_ReAddComment, /* internal to commands/tablecmds.c */ AT_AlterColumnType, /* alter column type */ AT_AlterColumnGenericOptions, /* alter column OPTIONS (...) */ @@ -2756,11 +2759,12 @@ typedef struct VariableShowStmt /* ---------------------- * Create Table Statement * - * NOTE: in the raw gram.y output, ColumnDef and Constraint nodes are - * intermixed in tableElts, and constraints and nnconstraints are NIL. After + * NOTE: in the raw gram.y output, ColumnDef, PeriodDef, and Constraint nodes are + * intermixed in tableElts; constraints, nnconstraints, and periods are NIL. After * parse analysis, tableElts contains just ColumnDefs, nnconstraints contains - * Constraint nodes of CONSTR_NOTNULL type from various sources, and - * constraints contains just CONSTR_CHECK Constraint nodes. + * Constraint nodes of CONSTR_NOTNULL type from various sources, constraints + * contains just CONSTR_CHECK Constraint nodes, and periods contains just + * PeriodDefs. * ---------------------- */ @@ -2769,6 +2773,7 @@ typedef struct CreateStmt NodeTag type; RangeVar *relation; /* relation to create */ List *tableElts; /* column definitions (list of ColumnDef) */ + List *periods; /* periods (list of PeriodDef nodes) */ List *inhRelations; /* relations to inherit from (list of * RangeVar) */ PartitionBoundSpec *partbound; /* FOR VALUES clause */ @@ -2783,6 +2788,31 @@ typedef struct CreateStmt bool if_not_exists; /* just do nothing if it already exists? */ } CreateStmt; + +/* ---------- + * Definitions for periods in CreateStmt + * ---------- + */ + +typedef struct PeriodDef +{ + NodeTag type; + Oid oid; /* period oid, once it's transformed */ + char *periodname; /* period name */ + char *startcolname; /* name of start column */ + char *endcolname; /* name of end column */ + AttrNumber startattnum; /* attnum of the start column */ + AttrNumber endattnum; /* attnum of the end column */ + AttrNumber rngattnum; /* attnum of the GENERATED range column */ + List *options; /* options from WITH clause */ + char *constraintname; /* name of the CHECK constraint */ + char *rangetypename; /* name of the range type */ + Oid coltypid; /* the start/end col type */ + Oid rngtypid; /* the range type to use */ + bool colexists; /* use an existing GENERATED column */ + int location; /* token location, or -1 if unknown */ +} PeriodDef; + /* ---------- * Definitions for constraints in CreateStmt * @@ -3509,6 +3539,7 @@ typedef struct IndexStmt List *indexParams; /* columns to index: a list of IndexElem */ List *indexIncludingParams; /* additional columns to index: a list * of IndexElem */ + PeriodDef *period; /* The period included in the index */ List *options; /* WITH clause options: a list of DefElem */ Node *whereClause; /* qualification (partial-index predicate) */ List *excludeOpNames; /* exclusion operator names, or NIL if none */ diff --git a/src/include/nodes/primnodes.h b/src/include/nodes/primnodes.h index fbbcd77dd84..93d06efe0ce 100644 --- a/src/include/nodes/primnodes.h +++ b/src/include/nodes/primnodes.h @@ -2403,6 +2403,8 @@ typedef struct ForPortionOfExpr { NodeTag type; Var *rangeVar; /* Range column */ + Var *startVar; /* Start column if PERIOD */ + Var *endVar; /* End column if PERIOD */ char *range_name; /* Range name */ Node *targetRange; /* FOR PORTION OF bounds as a range */ Oid rangeType; /* type of targetRange */ diff --git a/src/include/parser/kwlist.h b/src/include/parser/kwlist.h index a8def3a386c..cfc2f68ca8a 100644 --- a/src/include/parser/kwlist.h +++ b/src/include/parser/kwlist.h @@ -341,7 +341,7 @@ PG_KEYWORD("partition", PARTITION, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("passing", PASSING, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("password", PASSWORD, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("path", PATH, UNRESERVED_KEYWORD, BARE_LABEL) -PG_KEYWORD("period", PERIOD, UNRESERVED_KEYWORD, BARE_LABEL) +PG_KEYWORD("period", PERIOD, RESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("placing", PLACING, RESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("plan", PLAN, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("plans", PLANS, UNRESERVED_KEYWORD, BARE_LABEL) diff --git a/src/include/parser/parse_utilcmd.h b/src/include/parser/parse_utilcmd.h index 4965fac4495..d0d23af3d41 100644 --- a/src/include/parser/parse_utilcmd.h +++ b/src/include/parser/parse_utilcmd.h @@ -40,5 +40,6 @@ extern IndexStmt *generateClonedIndexStmt(RangeVar *heapRel, Relation source_idx, const AttrMap *attmap, Oid *constraintOid); +extern void transformPeriodOptions(PeriodDef *period); #endif /* PARSE_UTILCMD_H */ diff --git a/src/include/utils/lsyscache.h b/src/include/utils/lsyscache.h index 5b50ef230ab..eddca997efb 100644 --- a/src/include/utils/lsyscache.h +++ b/src/include/utils/lsyscache.h @@ -98,6 +98,8 @@ extern Oid get_atttype(Oid relid, AttrNumber attnum); extern void get_atttypetypmodcoll(Oid relid, AttrNumber attnum, Oid *typid, int32 *typmod, Oid *collid); extern Datum get_attoptions(Oid relid, int16 attnum); +extern char *get_periodname(Oid periodid, bool missing_ok); +extern Oid get_period_oid(Oid relid, const char *periodname, bool missing_ok); extern Oid get_cast_oid(Oid sourcetypeid, Oid targettypeid, bool missing_ok); extern char *get_collation_name(Oid colloid); extern bool get_collation_isdeterministic(Oid colloid); @@ -203,6 +205,7 @@ extern Oid get_range_subtype(Oid rangeOid); extern Oid get_range_collation(Oid rangeOid); extern Oid get_range_multirange(Oid rangeOid); extern Oid get_multirange_range(Oid multirangeOid); +extern Oid get_subtype_range(Oid subtypeOid); extern Oid get_index_column_opclass(Oid index_oid, int attno); extern bool get_index_isreplident(Oid index_oid); extern bool get_index_isvalid(Oid index_oid); diff --git a/src/include/utils/period.h b/src/include/utils/period.h new file mode 100644 index 00000000000..d02c17036fd --- /dev/null +++ b/src/include/utils/period.h @@ -0,0 +1,21 @@ +/*------------------------------------------------------------------------- + * + * period.h + * support for Postgres periods. + * + * + * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * src/include/utils/period.h + * + *------------------------------------------------------------------------- + */ +#ifndef PERIOD_H +#define PERIOD_H + +#include "executor/tuptable.h" + +extern Datum period_to_range(TupleTableSlot *slot, int startattno, int endattno, Oid rangetype); + +#endif /* PERIOD_H */ diff --git a/src/test/modules/test_ddl_deparse/test_ddl_deparse.c b/src/test/modules/test_ddl_deparse/test_ddl_deparse.c index 193669f2bc1..9ed429fdadd 100644 --- a/src/test/modules/test_ddl_deparse/test_ddl_deparse.c +++ b/src/test/modules/test_ddl_deparse/test_ddl_deparse.c @@ -179,6 +179,12 @@ get_altertable_subcmdinfo(PG_FUNCTION_ARGS) case AT_DropConstraint: strtype = "DROP CONSTRAINT"; break; + case AT_AddPeriod: + strtype = "ADD PERIOD"; + break; + case AT_DropPeriod: + strtype = "DROP PERIOD"; + break; case AT_ReAddComment: strtype = "(re) ADD COMMENT"; break; diff --git a/src/test/regress/expected/for_portion_of.out b/src/test/regress/expected/for_portion_of.out index 52fb044d2e5..c0d0de7e810 100644 --- a/src/test/regress/expected/for_portion_of.out +++ b/src/test/regress/expected/for_portion_of.out @@ -1100,6 +1100,64 @@ SELECT * FROM for_portion_of_test2 ORDER BY id, valid_at; [3,4) | {[2018-01-01,)} | three (6 rows) +DROP TABLE for_portion_of_test2; +-- Test with PERIODs +CREATE TABLE for_portion_of_test2 ( + id int4range NOT NULL, + valid_from date, + valid_til date, + name text NOT NULL, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT for_portion_of_test2_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +); +INSERT INTO for_portion_of_test2 +VALUES +('[1,2)', '2018-01-02', '2018-02-03', 'one'), +('[1,2)', '2018-02-04', '2018-03-03', 'one'), +('[1,2)', '2018-03-03', '2018-04-04', 'one'), +('[2,3)', '2018-01-01', '2018-05-01', 'two'), +('[3,4)', '2018-01-01', null, 'three'); +; +UPDATE for_portion_of_test2 +FOR PORTION OF valid_at FROM '2018-01-10' TO '2018-02-10' +SET name = 'one^1' +WHERE id = '[1,2)'; +DELETE FROM for_portion_of_test2 +FOR PORTION OF valid_at FROM '2018-01-15' TO '2018-02-15' +WHERE id = '[2,3)'; +-- Setting the start column fails: +UPDATE for_portion_of_test2 +FOR PORTION OF valid_at FROM '2018-01-10' TO '2018-02-10' +SET valid_from = '2018-02-01' +WHERE id = '[1,2)'; +ERROR: multiple assignments to same column "valid_from" +-- Setting the end column fails: +UPDATE for_portion_of_test2 +FOR PORTION OF valid_at FROM '2018-01-10' TO '2018-02-10' +SET valid_til = '2018-02-01' +WHERE id = '[1,2)'; +ERROR: multiple assignments to same column "valid_til" +-- Setting the generated column fails: +UPDATE for_portion_of_test2 +FOR PORTION OF valid_at FROM '2018-01-10' TO '2018-02-10' +SET valid_at = '[2018-02-01,2018-02-03)' +WHERE id = '[1,2)'; +ERROR: can't directly assign to "valid_at" in a FOR PORTION OF update +LINE 3: SET valid_at = '[2018-02-01,2018-02-03)' + ^ +SELECT * FROM for_portion_of_test2 ORDER BY id, valid_at; + id | valid_from | valid_til | name +-------+------------+------------+------- + [1,2) | 2018-01-02 | 2018-01-10 | one + [1,2) | 2018-01-10 | 2018-02-03 | one^1 + [1,2) | 2018-02-04 | 2018-02-10 | one^1 + [1,2) | 2018-02-10 | 2018-03-03 | one + [1,2) | 2018-03-03 | 2018-04-04 | one + [2,3) | 2018-01-01 | 2018-01-15 | two + [2,3) | 2018-02-15 | 2018-05-01 | two + [3,4) | 2018-01-01 | | three +(8 rows) + DROP TABLE for_portion_of_test2; -- Test with a custom range type CREATE TYPE mydaterange AS range(subtype=date); diff --git a/src/test/regress/expected/periods.out b/src/test/regress/expected/periods.out new file mode 100644 index 00000000000..d429097600a --- /dev/null +++ b/src/test/regress/expected/periods.out @@ -0,0 +1,318 @@ +/* System periods are not implemented */ +create table pt (id integer, ds date, de date, period for system_time (ds, de)); +ERROR: PERIOD FOR SYSTEM_TIME is not supported +LINE 2: create table pt (id integer, ds date, de date, period for sy... + ^ +/* Periods must specify actual columns */ +create table pt (id integer, ds date, de date, period for p (bogus, de)); +ERROR: column "bogus" of relation "pt" does not exist +create table pt (id integer, ds date, de date, period for p (ds, bogus)); +ERROR: column "bogus" of relation "pt" does not exist +/* Data types must match exactly */ +create table pt (id integer, ds date, de timestamp, period for p (ds, de)); +ERROR: start and end columns of period must be of same type +create table pt (id integer, ds text collate "C", de text collate "POSIX", period for p (ds, de)); +ERROR: start and end columns of period must have same collation +/* Periods must have a default BTree operator class */ +create table pt (id integer, ds xml, de xml, period for p (ds, de)); +ERROR: no range type for xml found for period p +HINT: You can define a custom range type with CREATE TYPE +/* Period and column names are in the same namespace */ +create table pt (id integer, ds date, de date, period for ctid (ds, de)); +ERROR: period name "ctid" conflicts with a system column name +create table pt (id integer, ds date, de date, period for id (ds, de)); +ERROR: period name "id" conflicts with a column name +/* Period name can't be given more than once */ +create table pt (id integer, ds date, de date, period for p (ds, de), period for p (ds, de)); +ERROR: period name "p" specified more than once +/* Period can't use the same colum for start and end*/ +create table pt (id integer, ds date, de date, period for p (ds, ds)); +ERROR: column "ds" can't be the start and end column for period "p" +/* Now make one that works */ +create table pt (id integer, ds date, de date, period for p (ds, de)); +/* Its generated column looks good */ +select attname, atttypid::regtype, attnotnull, attgenerated from pg_attribute where attrelid = 'pt'::regclass and attname = 'p'; + attname | atttypid | attnotnull | attgenerated +---------+-----------+------------+-------------- + p | daterange | t | s +(1 row) + +select conname, contype from pg_constraint where conrelid = 'pt'::regclass order by conname; + conname | contype +---------------+--------- + pt_p_check | c + pt_p_not_null | n +(2 rows) + +/* It appears in the information_schema */ +select * from information_schema.periods; + table_catalog | table_schema | table_name | period_name | start_column_name | end_column_name +---------------+--------------+------------+-------------+-------------------+----------------- + regression | public | pt | p | ds | de +(1 row) + +/* SELECT * excludes the PERIOD */ +insert into pt values (1, '2000-01-01', '2001-01-01'); +select * from pt; + id | ds | de +----+------------+------------ + 1 | 01-01-2000 | 01-01-2001 +(1 row) + +/* You can get it if you want */ +select *, p from pt; + id | ds | de | p +----+------------+------------+------------------------- + 1 | 01-01-2000 | 01-01-2001 | [01-01-2000,01-01-2001) +(1 row) + +/* You can comment on it */ +comment on period pt.p is 'test comment'; +select obj_description((select oid from pg_period where perrelid = 'pt'::regclass and pername = 'p'), 'pg_period'); + obj_description +----------------- + test comment +(1 row) + +/* Two are okay */ +create table pt2 (id integer, ds date, de date, period for p1 (ds, de), period for p2 (ds, de)); +drop table pt2; +/* Skip creating GENERATED column: works */ +create table pt2 (id integer, ds date, de date, p daterange not null generated always as (daterange(ds, de)) stored, period for p (ds, de) with (colexists = true)); +\d pt2 + Table "public.pt2" + Column | Type | Collation | Nullable | Default +--------+---------+-----------+----------+--------- + id | integer | | | + ds | date | | | + de | date | | | +Periods: + p (ds, de) +Check constraints: + "pt2_p_check" CHECK (ds < de) + +drop table pt2; +/* Skip creating GENERATED column: fails because the col isn't there */ +create table pt2 (id integer, ds date, de date, period for p (ds, de) with (colexists = true)); +ERROR: No column found with name p +/* Skip creating GENERATED column: fails because the option has an invalid value */ +create table pt2 (id integer, ds date, de date, period for p (ds, de) with (colexists = 'whatever')); +ERROR: colexists requires a Boolean value +/* Skip creating GENERATED column: fails because the column is not NOT NULL */ +create table pt2 (id integer, ds date, de date, p daterange generated always as (daterange(ds, de)) stored, period for p (ds, de) with (colexists = true)); +ERROR: Period p uses a generated column that allows nulls +/* Skip creating GENERATED column: fails because the column is not GENERATED */ +create table pt2 (id integer, ds date, de date, p daterange not null, period for p (ds, de) with (colexists = true)); +ERROR: Period p uses a non-generated column +/* Skip creating GENERATED column: fails because the column is GENERATED but with the wrong expression */ +-- TODO: +-- create table pt2 (id integer, ds date, de date, p daterange not null generated always as (daterange(de, ds)) stored, period for p (ds, de) with (colexists = true)); +/* Skip creating GENERATED column: fails because the column is the wrong type */ +create table pt2 (id integer, ds date, de date, p tsrange not null generated always as (tsrange(ds, de)) stored, period for p (ds, de) with (colexists = true)); +ERROR: Period p uses a generated column with the wrong type +/* Skip creating GENERATED column: fails because the column is inherited */ +create table pt2parent (id integer, ds date, de date, p daterange not null generated always as (daterange(ds, de)) stored); +create table pt2 (period for p (ds, de) with (colexists = true)) inherits (pt2parent); +ERROR: Inheriting is not supported when a table has a PERIOD +drop table pt2parent; +/* + * ALTER TABLE tests + */ +alter table pt drop period for p; +alter table pt add period for system_time (ds, de); +ERROR: PERIOD FOR SYSTEM_TIME is not supported +alter table pt add period for p (ds, de); +/* Its generated column looks good */ +select attname, atttypid::regtype, attnotnull, attgenerated from pg_attribute where attrelid = 'pt'::regclass and attname = 'p'; + attname | atttypid | attnotnull | attgenerated +---------+-----------+------------+-------------- + p | daterange | t | s +(1 row) + +select conname, contype from pg_constraint where conrelid = 'pt'::regclass order by conname; + conname | contype +---------------+--------- + pt_p_check | c + pt_p_not_null | n +(2 rows) + +/* Adding a second one */ +create table pt2 (id integer, ds date, de date, period for p1 (ds, de)); +alter table pt2 add period for p2 (ds, de); +drop table pt2; +/* Can't drop its columns */ +alter table pt drop column ds; +ERROR: cannot drop column ds of table pt because other objects depend on it +DETAIL: period p on table pt depends on column ds of table pt +HINT: Use DROP ... CASCADE to drop the dependent objects too. +alter table pt drop column de; +ERROR: cannot drop column de of table pt because other objects depend on it +DETAIL: period p on table pt depends on column de of table pt +HINT: Use DROP ... CASCADE to drop the dependent objects too. +/* Can't change the data types */ +alter table pt alter column ds type timestamp; +ERROR: cannot alter type of a column used by a period +DETAIL: period p on table pt depends on column "ds" +alter table pt alter column ds type timestamp; +ERROR: cannot alter type of a column used by a period +DETAIL: period p on table pt depends on column "ds" +/* column/period namespace conflicts */ +alter table pt add column p integer; +ERROR: column name "p" conflicts with a period name +alter table pt rename column id to p; +ERROR: column name "p" conflicts with a period name +alter table pt add period for tableoid (ds, de); +ERROR: period name "tableoid" conflicts with a system column name +alter table pt add period for "........pg.dropped.4........" (ds, de); +ERROR: period name "........pg.dropped.4........" conflicts with a column name +/* adding columns and the period at the same time */ +create table pt2 (id integer); +alter table pt2 add column ds date, add column de date, add period for p (ds, de); +drop table pt2; +/* Ambiguous range types raise an error */ +create type mydaterange as range(subtype=date); +create table pt2 (id int, ds date, de date, period for p (ds, de)); +ERROR: ambiguous range for type date +/* You can give an explicit range type */ +create table pt2 (id int, ds date, de date, period for p (ds, de) with (rangetype = 'mydaterange')); +drop type mydaterange; +ERROR: cannot drop type mydaterange because other objects depend on it +DETAIL: period p on table pt2 depends on type mydaterange +HINT: Use DROP ... CASCADE to drop the dependent objects too. +drop type mydaterange cascade; +NOTICE: drop cascades to period p on table pt2 +drop table pt2; +create table pt2 (id int, ds date, de date, period for p (ds, de) with (rangetype = 'daterange')); +/* Range type is not found */ +create table pt3 (id int, ds date, de date, period for p (ds, de) with (rangetype = 'notarange')); +ERROR: Range type notarange not found +/* Range type is the wrong type */ +create table pt3 (id int, ds date, de date, period for p (ds, de) with (rangetype = 'tstzrange')); +ERROR: Range type tstzrange does not match column type date +drop table pt2; +/* Period can't use the same colum for start and end*/ +create table pt2 (id integer, ds date, de date); +alter table pt2 add period for p (ds, ds); +ERROR: column "ds" can't be the start and end column for period "p" +drop table pt2; +/* Skip creating GENERATED column: works */ +create table pt2 (id integer, ds date, de date, p daterange not null generated always as (daterange(ds, de)) stored); +alter table pt2 add period for p (ds, de) with (colexists = true); +\d pt2 + Table "public.pt2" + Column | Type | Collation | Nullable | Default +--------+---------+-----------+----------+--------- + id | integer | | | + ds | date | | | + de | date | | | +Periods: + p (ds, de) +Check constraints: + "pt2_p_check" CHECK (ds < de) + +drop table pt2; +/* Skip creating GENERATED column: fails because the col isn't there */ +create table pt2 (id integer, ds date, de date); +alter table pt2 add period for p (ds, de) with (colexists = true); +ERROR: column "p" of relation "pt2" does not exist +drop table pt2; +/* Skip creating GENERATED column: fails because the option has an invalid value */ +create table pt2 (id integer, ds date, de date, p daterange not null generated always as (daterange(ds, de)) stored); +alter table pt2 add period for p (ds, de) with (colexists = 'whatever'); +ERROR: colexists requires a Boolean value +drop table pt2; +/* Skip creating GENERATED column: fails because the column is not NOT NULL */ +create table pt2 (id integer, ds date, de date, p daterange generated always as (daterange(ds, de)) stored); +alter table pt2 add period for p (ds, de) with (colexists = true); +ERROR: Period p uses a generated column that allows nulls +drop table pt2; +/* Skip creating GENERATED column: fails because the column is not GENERATED */ +create table pt2 (id integer, ds date, de date, p daterange not null); +alter table pt2 add period for p (ds, de) with (colexists = true); +ERROR: Period p uses a non-generated column +drop table pt2; +/* Skip creating GENERATED column: fails because the column is GENERATED but with the wrong expression */ +-- TODO: +-- create table pt2 (id integer, ds date, de date, p daterange not null generated always as (daterange(de, ds)) stored); +-- alter table pt2 add period for p (ds, de) with (colexists = true); +/* Skip creating GENERATED column: fails because the column is the wrong type */ +create table pt2 (id integer, ds date, de date, p tsrange not null generated always as (tsrange(ds, de)) stored); +alter table pt2 add period for p (ds, de) with (colexists = true); +ERROR: Period p uses a generated column with the wrong type +drop table pt2; +/* Skip creating GENERATED column: fails because the column is inherited */ +create table pt2parent (id integer, ds date, de date, p daterange not null generated always as (daterange(ds, de)) stored); +create table pt2 () inherits (pt2parent); +alter table pt2 add period for p (ds, de) with (colexists = true); +ERROR: Period p uses a generated column that is inherited +drop table pt2; +drop table pt2parent; +/* CREATE TABLE (LIKE ...) */ +/* Periods are not copied by LIKE, so their columns aren't either */ +create table pt2 (like pt); +\d pt2 + Table "public.pt2" + Column | Type | Collation | Nullable | Default +--------+---------+-----------+----------+--------- + id | integer | | | + ds | date | | | + de | date | | | + +drop table pt2; +/* Can add a period referring to LIKE'd columns */ +create table not_p (id integer, ds date, de date); +create table pt2 (like not_p, period for p (ds, de)); +\d pt2 + Table "public.pt2" + Column | Type | Collation | Nullable | Default +--------+---------+-----------+----------+--------- + id | integer | | | + ds | date | | | + de | date | | | +Periods: + p (ds, de) +Check constraints: + "pt2_p_check" CHECK (ds < de) + +drop table pt2; +/* Can add a period with the same name */ +create table pt2 (like pt, period for p (ds, de)); +\d pt2 + Table "public.pt2" + Column | Type | Collation | Nullable | Default +--------+---------+-----------+----------+--------- + id | integer | | | + ds | date | | | + de | date | | | +Periods: + p (ds, de) +Check constraints: + "pt2_p_check" CHECK (ds < de) + +drop table pt2; +/* Can add a period with a different name */ +create table pt2 (like pt, period for p2 (ds, de)); +\d pt2 + Table "public.pt2" + Column | Type | Collation | Nullable | Default +--------+---------+-----------+----------+--------- + id | integer | | | + ds | date | | | + de | date | | | +Periods: + p2 (ds, de) +Check constraints: + "pt2_p2_check" CHECK (ds < de) + +drop table pt2; +/* Can't add a period whose name conflicts with a LIKE'd column */ +create table pt2 (like pt, period for id (ds, de)); +ERROR: period name "id" conflicts with a column name +/* CREATE TALBE INHERITS */ +/* Can't inherit from a table with a period */ +create table pt2 (name text) inherits (pt); +ERROR: Inheriting from a table with a PERIOD is not supported +/* Can't inherit with a period */ +create table pt2 (d2s date, d2e date, period for p (d2s, d2e)) inherits (not_p); +ERROR: Inheriting is not supported when a table has a PERIOD +drop table not_p; diff --git a/src/test/regress/expected/privileges.out b/src/test/regress/expected/privileges.out index 9ff8e7fb363..4f577218cdf 100644 --- a/src/test/regress/expected/privileges.out +++ b/src/test/regress/expected/privileges.out @@ -1137,6 +1137,35 @@ SET SESSION AUTHORIZATION regress_priv_user3; UPDATE t1 FOR PORTION OF valid_at FROM '2000-01-01' TO '2001-01-01' SET c1 = '[2,3)'; SET SESSION AUTHORIZATION regress_priv_user1; DROP TABLE t1; +-- test column-level privileges on the PERIOD used in FOR PORTION OF +SET SESSION AUTHORIZATION regress_priv_user1; +CREATE TABLE t1 ( + c1 int4range, + valid_from timestamp, + valid_til timestamp, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT t1pk PRIMARY KEY (c1, valid_at WITHOUT OVERLAPS) +); +GRANT SELECT ON t1 TO regress_priv_user2; +GRANT SELECT ON t1 TO regress_priv_user3; +GRANT SELECT ON t1 TO regress_priv_user4; +GRANT SELECT ON t1 TO regress_priv_user5; +GRANT UPDATE (c1) ON t1 TO regress_priv_user2; +GRANT UPDATE (c1, valid_at) ON t1 TO regress_priv_user3; +GRANT UPDATE (c1, valid_from, valid_til) ON t1 TO regress_priv_user4; +GRANT UPDATE (c1, valid_from, valid_til, valid_at) ON t1 TO regress_priv_user5; +SET SESSION AUTHORIZATION regress_priv_user2; +UPDATE t1 FOR PORTION OF valid_at FROM '2000-01-01' TO '2001-01-01' SET c1 = '[2,3)'; +ERROR: permission denied for table t1 +SET SESSION AUTHORIZATION regress_priv_user3; +UPDATE t1 FOR PORTION OF valid_at FROM '2000-01-01' TO '2001-01-01' SET c1 = '[2,3)'; +ERROR: permission denied for table t1 +SET SESSION AUTHORIZATION regress_priv_user4; +UPDATE t1 FOR PORTION OF valid_at FROM '2000-01-01' TO '2001-01-01' SET c1 = '[2,3)'; +SET SESSION AUTHORIZATION regress_priv_user5; +UPDATE t1 FOR PORTION OF valid_at FROM '2000-01-01' TO '2001-01-01' SET c1 = '[2,3)'; +SET SESSION AUTHORIZATION regress_priv_user1; +DROP TABLE t1; -- test column-level privileges when involved with DELETE SET SESSION AUTHORIZATION regress_priv_user1; ALTER TABLE atest6 ADD COLUMN three integer; diff --git a/src/test/regress/expected/without_overlaps.out b/src/test/regress/expected/without_overlaps.out index 4d9f25ac405..e545d0e33d4 100644 --- a/src/test/regress/expected/without_overlaps.out +++ b/src/test/regress/expected/without_overlaps.out @@ -153,6 +153,76 @@ SELECT pg_get_indexdef(conindid, 0, true) FROM pg_constraint WHERE conname = 'te CREATE UNIQUE INDEX temporal_rng2_pk ON temporal_rng2 USING gist (id1, id2, valid_at) (1 row) +-- PK with one column plus a PERIOD: +CREATE TABLE temporal_per ( + id int4range, + valid_from date, + valid_til date, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +); +\d temporal_per + Table "public.temporal_per" + Column | Type | Collation | Nullable | Default +------------+-----------+-----------+----------+--------- + id | int4range | | not null | + valid_from | date | | | + valid_til | date | | | +Periods: + valid_at (valid_from, valid_til) +Indexes: + "temporal_per_pk" PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +Check constraints: + "temporal_per_valid_at_check" CHECK (valid_from < valid_til) + +SELECT pg_get_constraintdef(oid) FROM pg_constraint WHERE conname = 'temporal_per_pk'; + pg_get_constraintdef +--------------------------------------------- + PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +(1 row) + +SELECT pg_get_indexdef(conindid, 0, true) FROM pg_constraint WHERE conname = 'temporal_per_pk'; + pg_get_indexdef +------------------------------------------------------------------------------- + CREATE UNIQUE INDEX temporal_per_pk ON temporal_per USING gist (id, valid_at) +(1 row) + +-- PK with two columns plus a PERIOD: +CREATE TABLE temporal_per2 ( + id1 int4range, + id2 int4range, + valid_from date, + valid_til date, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_per2_pk PRIMARY KEY (id1, id2, valid_at WITHOUT OVERLAPS) +); +\d temporal_per2 + Table "public.temporal_per2" + Column | Type | Collation | Nullable | Default +------------+-----------+-----------+----------+--------- + id1 | int4range | | not null | + id2 | int4range | | not null | + valid_from | date | | | + valid_til | date | | | +Periods: + valid_at (valid_from, valid_til) +Indexes: + "temporal_per2_pk" PRIMARY KEY (id1, id2, valid_at WITHOUT OVERLAPS) +Check constraints: + "temporal_per2_valid_at_check" CHECK (valid_from < valid_til) + +SELECT pg_get_constraintdef(oid) FROM pg_constraint WHERE conname = 'temporal_per2_pk'; + pg_get_constraintdef +--------------------------------------------------- + PRIMARY KEY (id1, id2, valid_at WITHOUT OVERLAPS) +(1 row) + +SELECT pg_get_indexdef(conindid, 0, true) FROM pg_constraint WHERE conname = 'temporal_per2_pk'; + pg_get_indexdef +--------------------------------------------------------------------------------------- + CREATE UNIQUE INDEX temporal_per2_pk ON temporal_per2 USING gist (id1, id2, valid_at) +(1 row) + -- PK with a custom range type: CREATE TYPE textrange2 AS range (subtype=text, collation="C"); CREATE TABLE temporal_rng3 ( @@ -300,6 +370,78 @@ SELECT pg_get_indexdef(conindid, 0, true) FROM pg_constraint WHERE conname = 'te (1 row) DROP TABLE temporal_rng3; +-- UNIQUE with one column plus a PERIOD: +CREATE TABLE temporal_per3 ( + id int4range, + valid_from date, + valid_til date, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_per3_uq UNIQUE (id, valid_at WITHOUT OVERLAPS) +); +\d temporal_per3 + Table "public.temporal_per3" + Column | Type | Collation | Nullable | Default +------------+-----------+-----------+----------+--------- + id | int4range | | | + valid_from | date | | | + valid_til | date | | | +Periods: + valid_at (valid_from, valid_til) +Indexes: + "temporal_per3_uq" UNIQUE (id, valid_at WITHOUT OVERLAPS) +Check constraints: + "temporal_per3_valid_at_check" CHECK (valid_from < valid_til) + +SELECT pg_get_constraintdef(oid) FROM pg_constraint WHERE conname = 'temporal_per3_uq'; + pg_get_constraintdef +---------------------------------------- + UNIQUE (id, valid_at WITHOUT OVERLAPS) +(1 row) + +SELECT pg_get_indexdef(conindid, 0, true) FROM pg_constraint WHERE conname = 'temporal_per3_uq'; + pg_get_indexdef +--------------------------------------------------------------------------------- + CREATE UNIQUE INDEX temporal_per3_uq ON temporal_per3 USING gist (id, valid_at) +(1 row) + +DROP TABLE temporal_per3; +-- UNIQUE with two columns plus a PERIOD: +CREATE TABLE temporal_per3 ( + id1 int4range, + id2 int4range, + valid_from date, + valid_til date, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_per3_uq UNIQUE (id1, id2, valid_at WITHOUT OVERLAPS) +); +\d temporal_per3 + Table "public.temporal_per3" + Column | Type | Collation | Nullable | Default +------------+-----------+-----------+----------+--------- + id1 | int4range | | | + id2 | int4range | | | + valid_from | date | | | + valid_til | date | | | +Periods: + valid_at (valid_from, valid_til) +Indexes: + "temporal_per3_uq" UNIQUE (id1, id2, valid_at WITHOUT OVERLAPS) +Check constraints: + "temporal_per3_valid_at_check" CHECK (valid_from < valid_til) + +SELECT pg_get_constraintdef(oid) FROM pg_constraint WHERE conname = 'temporal_per3_uq'; + pg_get_constraintdef +---------------------------------------------- + UNIQUE (id1, id2, valid_at WITHOUT OVERLAPS) +(1 row) + +SELECT pg_get_indexdef(conindid, 0, true) FROM pg_constraint WHERE conname = 'temporal_per3_uq'; + pg_get_indexdef +--------------------------------------------------------------------------------------- + CREATE UNIQUE INDEX temporal_per3_uq ON temporal_per3 USING gist (id1, id2, valid_at) +(1 row) + +DROP TABLE temporal_per3; -- UNIQUE with a custom range type: CREATE TYPE textrange2 AS range (subtype=text, collation="C"); CREATE TABLE temporal_rng3 ( @@ -377,6 +519,106 @@ ALTER TABLE temporal3 ADD CONSTRAINT temporal3_uq UNIQUE (id, valid_at WITHOUT OVERLAPS); DROP TABLE temporal3; +-- PRIMARY KEY with PERIOD already there +CREATE TABLE temporal3 ( + id int4range, + valid_from date, + valid_til date, + PERIOD FOR valid_at (valid_from, valid_til) +); +ALTER TABLE temporal3 + ADD CONSTRAINT temporal3_pk + PRIMARY KEY (id, valid_at WITHOUT OVERLAPS); +\d temporal3 + Table "public.temporal3" + Column | Type | Collation | Nullable | Default +------------+-----------+-----------+----------+--------- + id | int4range | | not null | + valid_from | date | | | + valid_til | date | | | +Periods: + valid_at (valid_from, valid_til) +Indexes: + "temporal3_pk" PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +Check constraints: + "temporal3_valid_at_check" CHECK (valid_from < valid_til) + +DROP TABLE temporal3; +-- PRIMARY KEY with PERIOD too +CREATE TABLE temporal3 ( + id int4range, + valid_from date, + valid_til date +); +ALTER TABLE temporal3 + ADD PERIOD FOR valid_at (valid_from, valid_til), + ADD CONSTRAINT temporal3_pk + PRIMARY KEY (id, valid_at WITHOUT OVERLAPS); +\d temporal3 + Table "public.temporal3" + Column | Type | Collation | Nullable | Default +------------+-----------+-----------+----------+--------- + id | int4range | | not null | + valid_from | date | | | + valid_til | date | | | +Periods: + valid_at (valid_from, valid_til) +Indexes: + "temporal3_pk" PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +Check constraints: + "temporal3_valid_at_check" CHECK (valid_from < valid_til) + +DROP TABLE temporal3; +-- UNIQUE with PERIOD already there +CREATE TABLE temporal3 ( + id int4range, + valid_from date, + valid_til date, + PERIOD FOR valid_at (valid_from, valid_til) +); +ALTER TABLE temporal3 + ADD CONSTRAINT temporal3_uq + UNIQUE (id, valid_at WITHOUT OVERLAPS); +\d temporal3 + Table "public.temporal3" + Column | Type | Collation | Nullable | Default +------------+-----------+-----------+----------+--------- + id | int4range | | | + valid_from | date | | | + valid_til | date | | | +Periods: + valid_at (valid_from, valid_til) +Indexes: + "temporal3_uq" UNIQUE (id, valid_at WITHOUT OVERLAPS) +Check constraints: + "temporal3_valid_at_check" CHECK (valid_from < valid_til) + +DROP TABLE temporal3; +-- UNIQUE with PERIOD too +CREATE TABLE temporal3 ( + id int4range, + valid_from date, + valid_til date +); +ALTER TABLE temporal3 + ADD PERIOD FOR valid_at (valid_from, valid_til), + ADD CONSTRAINT temporal3_uq + UNIQUE (id, valid_at WITHOUT OVERLAPS); +\d temporal3 + Table "public.temporal3" + Column | Type | Collation | Nullable | Default +------------+-----------+-----------+----------+--------- + id | int4range | | | + valid_from | date | | | + valid_til | date | | | +Periods: + valid_at (valid_from, valid_til) +Indexes: + "temporal3_uq" UNIQUE (id, valid_at WITHOUT OVERLAPS) +Check constraints: + "temporal3_valid_at_check" CHECK (valid_from < valid_til) + +DROP TABLE temporal3; -- -- range PK: test with existing rows -- @@ -874,6 +1116,284 @@ SELECT * FROM temporal_mltrng3 ORDER BY id, valid_at; DROP TABLE temporal_mltrng3; -- +-- PERIOD PK: test with existing rows +-- +ALTER TABLE temporal_per DROP CONSTRAINT temporal_per_pk; +-- okay: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2018-01-02', '2018-02-03'); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2018-03-03', '2018-04-04'); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[2,3)', '2018-01-01', '2018-01-05'); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[3,4)', '2018-01-01', NULL); +ALTER TABLE temporal_per ADD CONSTRAINT temporal_per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS); +ALTER TABLE temporal_per DROP CONSTRAINT temporal_per_pk; +-- should fail: +BEGIN; + INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2018-01-01', '2018-01-05'); + ALTER TABLE temporal_per ADD CONSTRAINT temporal_per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS); +ERROR: could not create exclusion constraint "temporal_per_pk" +DETAIL: Key (id, valid_at)=([1,2), [2018-01-02,2018-02-03)) conflicts with key (id, valid_at)=([1,2), [2018-01-01,2018-01-05)). +ROLLBACK; +-- rejects empty: +BEGIN; + ALTER TABLE temporal_per DROP PERIOD FOR valid_at; + INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[3,4)', '2020-01-01', '2020-01-01'); + ALTER TABLE temporal_per ADD PERIOD FOR valid_at (valid_from, valid_til); +ERROR: check constraint "temporal_per_valid_at_check" of relation "temporal_per" is violated by some row +ROLLBACK; +ALTER TABLE temporal_per ADD CONSTRAINT temporal_per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS); +DELETE FROM temporal_per; +-- +-- PERIOD PK: test inserts +-- +-- okay: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2018-01-02', '2018-02-03'); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2018-03-03', '2018-04-04'); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[2,3)', '2018-01-01', '2018-01-05'); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[3,4)', '2018-01-01', NULL); +-- should fail: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2018-01-01', '2018-01-05'); +ERROR: conflicting key value violates exclusion constraint "temporal_per_pk" +DETAIL: Key (id, valid_at)=([1,2), [2018-01-01,2018-01-05)) conflicts with existing key (id, valid_at)=([1,2), [2018-01-02,2018-02-03)). +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES (NULL, '2018-01-01', '2018-01-05'); +ERROR: null value in column "id" of relation "temporal_per" violates not-null constraint +DETAIL: Failing row contains (null, 2018-01-01, 2018-01-05, [2018-01-01,2018-01-05)). +-- rejects empty: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[3,4)', '2020-01-01', '2020-01-01'); +ERROR: new row for relation "temporal_per" violates check constraint "temporal_per_valid_at_check" +DETAIL: Failing row contains ([3,4), 2020-01-01, 2020-01-01, empty). +SELECT * FROM temporal_per ORDER BY id, valid_at; + id | valid_from | valid_til +-------+------------+------------ + [1,2) | 2018-01-02 | 2018-02-03 + [1,2) | 2018-03-03 | 2018-04-04 + [2,3) | 2018-01-01 | 2018-01-05 + [3,4) | 2018-01-01 | +(4 rows) + +-- +-- PERIOD PK: test updates +-- +-- update the scalar part +UPDATE temporal_per +SET id = '[11,12)' +WHERE id = '[1,2)' +AND valid_at @> '2018-01-15'::date; +-- update the PERIOD bounds +UPDATE temporal_per +SET valid_from = '2020-01-01', + valid_til = '2021-01-01' +WHERE id = '[11,12)' +AND valid_at @> '2018-01-15'::date; +-- update both at once +UPDATE temporal_per +SET id = '[21,22)', + valid_from = '2018-01-02', + valid_til = '2018-02-03' +WHERE id = '[11,12)' +AND valid_at @> '2020-01-15'::date; +SELECT * FROM temporal_per ORDER BY id, valid_at; + id | valid_from | valid_til +---------+------------+------------ + [1,2) | 2018-03-03 | 2018-04-04 + [2,3) | 2018-01-01 | 2018-01-05 + [3,4) | 2018-01-01 | + [21,22) | 2018-01-02 | 2018-02-03 +(4 rows) + +-- should fail: +UPDATE temporal_per +SET id = '[1,2)', + valid_from = '2018-03-05', + valid_til = '2018-05-05' +WHERE id = '[21,22)'; +ERROR: conflicting key value violates exclusion constraint "temporal_per_pk" +DETAIL: Key (id, valid_at)=([1,2), [2018-03-05,2018-05-05)) conflicts with existing key (id, valid_at)=([1,2), [2018-03-03,2018-04-04)). +-- set the scalar part to NULL +UPDATE temporal_per +SET id = NULL, + valid_from = '2018-03-05', + valid_til = '2018-05-05' +WHERE id = '[21,22)'; +ERROR: null value in column "id" of relation "temporal_per" violates not-null constraint +DETAIL: Failing row contains (null, 2018-03-05, 2018-05-05, [2018-03-05,2018-05-05)). +-- set a PERIOD bound to NULL (okay) +UPDATE temporal_per +SET id = '[1,2)', + valid_from = '2018-04-04', + valid_til = NULL +WHERE id = '[21,22)'; +-- rejects empty: +UPDATE temporal_per +SET id = '[21,22)', + valid_from = '2020-01-01', + valid_til = '2020-01-01' +WHERE id = '[1,2)'; +ERROR: new row for relation "temporal_per" violates check constraint "temporal_per_valid_at_check" +DETAIL: Failing row contains ([21,22), 2020-01-01, 2020-01-01, empty). +SELECT * FROM temporal_per ORDER BY id, valid_at; + id | valid_from | valid_til +-------+------------+------------ + [1,2) | 2018-03-03 | 2018-04-04 + [1,2) | 2018-04-04 | + [2,3) | 2018-01-01 | 2018-01-05 + [3,4) | 2018-01-01 | +(4 rows) + +-- +-- PERIOD UQ: test with existing rows +-- +CREATE TABLE temporal_per3 ( + id int4range, + valid_from date, + valid_til date, + PERIOD FOR valid_at (valid_from, valid_til) +); +-- okay: +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2018-01-02', '2018-02-03'); +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2018-03-03', '2018-04-04'); +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[2,3)', '2018-01-01', '2018-01-05'); +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[3,4)', '2018-01-01', NULL); +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES (NULL, '2018-01-01', '2018-01-05'); +ALTER TABLE temporal_per3 ADD CONSTRAINT temporal_per3_uq UNIQUE (id, valid_at WITHOUT OVERLAPS); +ALTER TABLE temporal_per3 DROP CONSTRAINT temporal_per3_uq; +-- should fail: +BEGIN; + INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2018-01-01', '2018-01-05'); + ALTER TABLE temporal_per3 ADD CONSTRAINT temporal_per3_uq UNIQUE (id, valid_at WITHOUT OVERLAPS); +ERROR: could not create exclusion constraint "temporal_per3_uq" +DETAIL: Key (id, valid_at)=([1,2), [2018-01-02,2018-02-03)) conflicts with key (id, valid_at)=([1,2), [2018-01-01,2018-01-05)). +ROLLBACK; +-- rejects empty: +BEGIN; + ALTER TABLE temporal_per3 DROP PERIOD FOR valid_at; + INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[3,4)', '2020-01-01', '2020-01-01'); + ALTER TABLE temporal_per3 ADD PERIOD FOR valid_at (valid_from, valid_til); +ERROR: check constraint "temporal_per3_valid_at_check" of relation "temporal_per3" is violated by some row +ROLLBACK; +ALTER TABLE temporal_per3 ADD CONSTRAINT temporal_per3_uq UNIQUE (id, valid_at WITHOUT OVERLAPS); +DELETE FROM temporal_per3; +-- +-- PERIOD UQ: test inserts +-- +-- okay: +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2018-01-02', '2018-02-03'); +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2018-03-03', '2018-04-04'); +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[2,3)', '2018-01-01', '2018-01-05'); +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[3,4)', '2018-01-01', NULL); +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES (NULL, '2018-01-01', '2018-01-05'); +-- should fail: +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2018-01-01', '2018-01-05'); +ERROR: conflicting key value violates exclusion constraint "temporal_per3_uq" +DETAIL: Key (id, valid_at)=([1,2), [2018-01-01,2018-01-05)) conflicts with existing key (id, valid_at)=([1,2), [2018-01-02,2018-02-03)). +-- rejects empty: +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[3,4)', '2020-01-01', '2020-01-01'); +ERROR: new row for relation "temporal_per3" violates check constraint "temporal_per3_valid_at_check" +DETAIL: Failing row contains ([3,4), 2020-01-01, 2020-01-01, empty). +SELECT * FROM temporal_per3 ORDER BY id, valid_at; + id | valid_from | valid_til +-------+------------+------------ + [1,2) | 2018-01-02 | 2018-02-03 + [1,2) | 2018-03-03 | 2018-04-04 + [2,3) | 2018-01-01 | 2018-01-05 + [3,4) | 2018-01-01 | + | 2018-01-01 | 2018-01-05 +(5 rows) + +-- +-- PERIOD UQ: test updates +-- +-- update the scalar part +UPDATE temporal_per3 +SET id = '[11,12)' +WHERE id = '[1,2)' +AND valid_at @> '2018-01-15'::date; +-- update the PERIOD bounds +UPDATE temporal_per3 +SET valid_from = '2020-01-01', + valid_til = '2021-01-01' +WHERE id = '[11,12)' +AND valid_at @> '2018-01-15'::date; +-- update both at once +UPDATE temporal_per3 +SET id = '[21,22)', + valid_from = '2018-01-02', + valid_til = '2018-02-03' +WHERE id = '[11,12)' +AND valid_at @> '2020-01-15'::date; +SELECT * FROM temporal_per3 ORDER BY id, valid_at; + id | valid_from | valid_til +---------+------------+------------ + [1,2) | 2018-03-03 | 2018-04-04 + [2,3) | 2018-01-01 | 2018-01-05 + [3,4) | 2018-01-01 | + [21,22) | 2018-01-02 | 2018-02-03 + | 2018-01-01 | 2018-01-05 +(5 rows) + +-- should fail: +UPDATE temporal_per3 +SET id = '[1,2)', + valid_from = '2018-03-05', + valid_til = '2018-05-05' +WHERE id = '[21,22)'; +ERROR: conflicting key value violates exclusion constraint "temporal_per3_uq" +DETAIL: Key (id, valid_at)=([1,2), [2018-03-05,2018-05-05)) conflicts with existing key (id, valid_at)=([1,2), [2018-03-03,2018-04-04)). +-- set the scalar part to NULL (okay) +UPDATE temporal_per3 +SET id = NULL, + valid_from = '2020-01-01', + valid_til = '2021-01-01' +WHERE id = '[21,22)'; +-- set a PERIOD bound to NULL (okay) +UPDATE temporal_per3 +SET id = '[1,2)', + valid_from = '2018-04-04', + valid_til = NULL +WHERE id IS NULL AND valid_at @> '2020-06-01'::date; +SELECT * FROM temporal_per3 ORDER BY id, valid_at; + id | valid_from | valid_til +-------+------------+------------ + [1,2) | 2018-03-03 | 2018-04-04 + [1,2) | 2018-04-04 | + [2,3) | 2018-01-01 | 2018-01-05 + [3,4) | 2018-01-01 | + | 2018-01-01 | 2018-01-05 +(5 rows) + +-- should fail: +UPDATE temporal_per3 +SET valid_from = '2018-03-01', + valid_til = '2018-05-05' +WHERE id = '[1,2)' AND valid_til IS NULL; +ERROR: conflicting key value violates exclusion constraint "temporal_per3_uq" +DETAIL: Key (id, valid_at)=([1,2), [2018-03-01,2018-05-05)) conflicts with existing key (id, valid_at)=([1,2), [2018-03-03,2018-04-04)). +-- rejects empty: +UPDATE temporal_per3 +SET valid_from = '2020-01-01', + valid_til = '2020-01-01' +WHERE id = '[1,2)' AND valid_til IS NULL; +ERROR: new row for relation "temporal_per3" violates check constraint "temporal_per3_valid_at_check" +DETAIL: Failing row contains ([1,2), 2020-01-01, 2020-01-01, empty). +-- still rejects empty when scalar part is NULL: +UPDATE temporal_per3 +SET id = NULL, + valid_from = '2020-01-01', + valid_til = '2020-01-01' +WHERE id = '[1,2)' AND valid_til IS NULL; +ERROR: new row for relation "temporal_per3" violates check constraint "temporal_per3_valid_at_check" +DETAIL: Failing row contains (null, 2020-01-01, 2020-01-01, empty). +SELECT * FROM temporal_per3 ORDER BY id, valid_at; + id | valid_from | valid_til +-------+------------+------------ + [1,2) | 2018-03-03 | 2018-04-04 + [1,2) | 2018-04-04 | + [2,3) | 2018-01-01 | 2018-01-05 + [3,4) | 2018-01-01 | + | 2018-01-01 | 2018-01-05 +(5 rows) + +DROP TABLE temporal_per3; +-- -- test a range with both a PK and a UNIQUE constraint -- CREATE TABLE temporal3 ( @@ -933,6 +1453,91 @@ ERROR: column "valid_at" is in a primary key ALTER TABLE temporal3 ALTER COLUMN valid_at TYPE tstzrange USING tstzrange(lower(valid_at), upper(valid_at)); ALTER TABLE temporal3 RENAME COLUMN valid_at TO valid_thru; ALTER TABLE temporal3 DROP COLUMN valid_thru; +\d temporal3 + Table "public.temporal3" + Column | Type | Collation | Nullable | Default +--------+-----------+-----------+----------+--------- + id | int4range | | not null | + +DROP TABLE temporal3; +CREATE TABLE temporal3 ( + id int4range, + valid_from date, + valid_til date, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal3_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +); +\d temporal3 + Table "public.temporal3" + Column | Type | Collation | Nullable | Default +------------+-----------+-----------+----------+--------- + id | int4range | | not null | + valid_from | date | | | + valid_til | date | | | +Periods: + valid_at (valid_from, valid_til) +Indexes: + "temporal3_pk" PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +Check constraints: + "temporal3_valid_at_check" CHECK (valid_from < valid_til) + +SELECT attnotnull FROM pg_attribute WHERE attrelid = 'temporal3'::regclass AND attname = 'valid_at'; + attnotnull +------------ + t +(1 row) + +ALTER TABLE temporal3 ALTER COLUMN valid_at DROP NOT NULL; +ERROR: column "valid_at" is in a primary key +ALTER TABLE temporal3 ALTER COLUMN valid_at TYPE tstzrange USING tstzrange(lower(valid_at), upper(valid_at)); +ERROR: cannot specify USING when altering type of generated column +LINE 1: ALTER TABLE temporal3 ALTER COLUMN valid_at TYPE tstzrange U... + ^ +DETAIL: Column "valid_at" is a generated column. +ALTER TABLE temporal3 DROP COLUMN valid_at; +ERROR: cannot drop column valid_at of table temporal3 because period valid_at on table temporal3 requires it +HINT: You can drop period valid_at on table temporal3 instead. +ALTER TABLE temporal3 DROP PERIOD FOR valid_at; +\d temporal3 + Table "public.temporal3" + Column | Type | Collation | Nullable | Default +------------+-----------+-----------+----------+--------- + id | int4range | | not null | + valid_from | date | | | + valid_til | date | | | + +DROP TABLE temporal3; +-- Same results if we add the PERIOD and PK from ALTER TABLE: +CREATE TABLE temporal3 ( + id int4range, + valid_from date, + valid_til date +); +ALTER TABLE temporal3 + ADD PERIOD FOR valid_at (valid_from, valid_til), + ADD CONSTRAINT temporal3_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS); +\d temporal3 + Table "public.temporal3" + Column | Type | Collation | Nullable | Default +------------+-----------+-----------+----------+--------- + id | int4range | | not null | + valid_from | date | | | + valid_til | date | | | +Periods: + valid_at (valid_from, valid_til) +Indexes: + "temporal3_pk" PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +Check constraints: + "temporal3_valid_at_check" CHECK (valid_from < valid_til) + +SELECT attnotnull FROM pg_attribute WHERE attrelid = 'temporal3'::regclass AND attname = 'valid_at'; + attnotnull +------------ + t +(1 row) + +ALTER TABLE temporal3 ALTER COLUMN valid_at DROP NOT NULL; +ERROR: column "valid_at" is in a primary key DROP TABLE temporal3; -- -- test PARTITION BY for ranges @@ -1414,32 +2019,236 @@ SELECT * FROM temporal_mltrng3 ORDER BY id, valid_at; DROP TABLE temporal_mltrng3; -- --- test FK dependencies --- --- can't drop a range referenced by an FK, unless with CASCADE -CREATE TABLE temporal3 ( - id int4range, - valid_at daterange, - CONSTRAINT temporal3_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) -); -CREATE TABLE temporal_fk_rng2rng ( - id int4range, - valid_at daterange, - parent_id int4range, - CONSTRAINT temporal_fk_rng2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), - CONSTRAINT temporal_fk_rng2rng_fk FOREIGN KEY (parent_id, PERIOD valid_at) - REFERENCES temporal3 (id, PERIOD valid_at) -); -ALTER TABLE temporal3 DROP COLUMN valid_at; -ERROR: cannot drop column valid_at of table temporal3 because other objects depend on it -DETAIL: constraint temporal_fk_rng2rng_fk on table temporal_fk_rng2rng depends on column valid_at of table temporal3 -HINT: Use DROP ... CASCADE to drop the dependent objects too. -ALTER TABLE temporal3 DROP COLUMN valid_at CASCADE; -NOTICE: drop cascades to constraint temporal_fk_rng2rng_fk on table temporal_fk_rng2rng -DROP TABLE temporal_fk_rng2rng; -DROP TABLE temporal3; +-- ON CONFLICT: PERIODs -- --- test FOREIGN KEY, range references range +TRUNCATE temporal_per; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2000-01-01', '2010-01-01'); +-- with a conflict +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2005-01-01', '2006-01-01') ON CONFLICT DO NOTHING; +-- id matches but no conflict +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2010-01-01', '2020-01-01') ON CONFLICT DO NOTHING; +-- date matches but no conflict +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[2,3)', '2005-01-01', '2006-01-01') ON CONFLICT DO NOTHING; +SELECT * FROM temporal_per ORDER BY id, valid_from, valid_til; + id | valid_from | valid_til +-------+------------+------------ + [1,2) | 2000-01-01 | 2010-01-01 + [1,2) | 2010-01-01 | 2020-01-01 + [2,3) | 2005-01-01 | 2006-01-01 +(3 rows) + +TRUNCATE temporal_per; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2000-01-01', '2010-01-01'); +-- with a conflict +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2005-01-01', '2006-01-01') ON CONFLICT (id, valid_at) DO NOTHING; +ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification +-- id matches but no conflict +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2010-01-01', '2020-01-01') ON CONFLICT (id, valid_at) DO NOTHING; +ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification +-- date matches but no conflict +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[2,3)', '2005-01-01', '2006-01-01') ON CONFLICT (id, valid_at) DO NOTHING; +ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification +SELECT * FROM temporal_per ORDER BY id, valid_from, valid_til; + id | valid_from | valid_til +-------+------------+------------ + [1,2) | 2000-01-01 | 2010-01-01 +(1 row) + +TRUNCATE temporal_per; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2000-01-01', '2010-01-01'); +-- with a conflict +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2005-01-01', '2006-01-01') ON CONFLICT ON CONSTRAINT temporal_per_pk DO NOTHING; +-- id matches but no conflict +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2010-01-01', '2020-01-01') ON CONFLICT ON CONSTRAINT temporal_per_pk DO NOTHING; +-- date matches but no conflict +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[2,3)', '2005-01-01', '2006-01-01') ON CONFLICT ON CONSTRAINT temporal_per_pk DO NOTHING; +SELECT * FROM temporal_per ORDER BY id, valid_from, valid_til; + id | valid_from | valid_til +-------+------------+------------ + [1,2) | 2000-01-01 | 2010-01-01 + [1,2) | 2010-01-01 | 2020-01-01 + [2,3) | 2005-01-01 | 2006-01-01 +(3 rows) + +TRUNCATE temporal_per; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2000-01-01', '2010-01-01'); +-- with a conflict +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2005-01-01', '2006-01-01') ON CONFLICT (id, valid_at) DO UPDATE SET id = EXCLUDED.id + '[2,3)'; +ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification +-- id matches but no conflict +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2010-01-01', '2020-01-01') ON CONFLICT (id, valid_at) DO UPDATE SET id = EXCLUDED.id + '[3,4)'; +ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification +-- date matches but no conflict +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[2,3)', '2005-01-01', '2006-01-01') ON CONFLICT (id, valid_at) DO UPDATE SET id = EXCLUDED.id + '[4,5)'; +ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification +SELECT * FROM temporal_per ORDER BY id, valid_from, valid_til; + id | valid_from | valid_til +-------+------------+------------ + [1,2) | 2000-01-01 | 2010-01-01 +(1 row) + +TRUNCATE temporal_per; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2000-01-01', '2010-01-01'); +-- with a conflict +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2005-01-01', '2006-01-01') ON CONFLICT ON CONSTRAINT temporal_per_pk DO UPDATE SET id = EXCLUDED.id + '[2,3)'; +ERROR: ON CONFLICT DO UPDATE not supported with exclusion constraints +-- id matches but no conflict +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2010-01-01', '2020-01-01') ON CONFLICT ON CONSTRAINT temporal_per_pk DO UPDATE SET id = EXCLUDED.id + '[3,4)'; +ERROR: ON CONFLICT DO UPDATE not supported with exclusion constraints +-- date matches but no conflict +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[2,3)', '2005-01-01', '2006-01-01') ON CONFLICT ON CONSTRAINT temporal_per_pk DO UPDATE SET id = EXCLUDED.id + '[4,5)'; +ERROR: ON CONFLICT DO UPDATE not supported with exclusion constraints +SELECT * FROM temporal_per ORDER BY id, valid_from, valid_til; + id | valid_from | valid_til +-------+------------+------------ + [1,2) | 2000-01-01 | 2010-01-01 +(1 row) + +-- with a UNIQUE constraint: +CREATE TABLE temporal_per3 ( + id int4range, + valid_from date, + valid_til date, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_per3_uq UNIQUE (id, valid_at WITHOUT OVERLAPS) +); +TRUNCATE temporal_per3; +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2000-01-01', '2010-01-01'); +-- with a conflict +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2005-01-01', '2006-01-01') ON CONFLICT DO NOTHING; +-- id matches but no conflict +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2010-01-01', '2020-01-01') ON CONFLICT DO NOTHING; +-- date matches but no conflict +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[2,3)', '2005-01-01', '2006-01-01') ON CONFLICT DO NOTHING; +SELECT * FROM temporal_per3 ORDER BY id, valid_from, valid_til; + id | valid_from | valid_til +-------+------------+------------ + [1,2) | 2000-01-01 | 2010-01-01 + [1,2) | 2010-01-01 | 2020-01-01 + [2,3) | 2005-01-01 | 2006-01-01 +(3 rows) + +TRUNCATE temporal_per3; +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2000-01-01', '2010-01-01'); +-- with a conflict +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2005-01-01', '2006-01-01') ON CONFLICT (id, valid_at) DO NOTHING; +ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification +-- id matches but no conflict +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2010-01-01', '2020-01-01') ON CONFLICT (id, valid_at) DO NOTHING; +ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification +-- date matches but no conflict +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[2,3)', '2005-01-01', '2006-01-01') ON CONFLICT (id, valid_at) DO NOTHING; +ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification +SELECT * FROM temporal_per3 ORDER BY id, valid_from, valid_til; + id | valid_from | valid_til +-------+------------+------------ + [1,2) | 2000-01-01 | 2010-01-01 +(1 row) + +TRUNCATE temporal_per3; +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2000-01-01', '2010-01-01'); +-- with a conflict +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2005-01-01', '2006-01-01') ON CONFLICT ON CONSTRAINT temporal_per3_uq DO NOTHING; +-- id matches but no conflict +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2010-01-01', '2020-01-01') ON CONFLICT ON CONSTRAINT temporal_per3_uq DO NOTHING; +-- date matches but no conflict +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[2,3)', '2005-01-01', '2006-01-01') ON CONFLICT ON CONSTRAINT temporal_per3_uq DO NOTHING; +SELECT * FROM temporal_per3 ORDER BY id, valid_from, valid_til; + id | valid_from | valid_til +-------+------------+------------ + [1,2) | 2000-01-01 | 2010-01-01 + [1,2) | 2010-01-01 | 2020-01-01 + [2,3) | 2005-01-01 | 2006-01-01 +(3 rows) + +TRUNCATE temporal_per3; +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2000-01-01', '2010-01-01'); +-- with a conflict +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2005-01-01', '2006-01-01') ON CONFLICT (id, valid_at) DO UPDATE SET id = EXCLUDED.id + '[2,3)'; +ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification +-- id matches but no conflict +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2010-01-01', '2020-01-01') ON CONFLICT (id, valid_at) DO UPDATE SET id = EXCLUDED.id + '[3,4)'; +ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification +-- date matches but no conflict +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[2,3)', '2005-01-01', '2006-01-01') ON CONFLICT (id, valid_at) DO UPDATE SET id = EXCLUDED.id + '[4,5)'; +ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification +SELECT * FROM temporal_per3 ORDER BY id, valid_from, valid_til; + id | valid_from | valid_til +-------+------------+------------ + [1,2) | 2000-01-01 | 2010-01-01 +(1 row) + +TRUNCATE temporal_per3; +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2000-01-01', '2010-01-01'); +-- with a conflict +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2005-01-01', '2006-01-01') ON CONFLICT ON CONSTRAINT temporal_per3_uq DO UPDATE SET id = EXCLUDED.id + '[2,3)'; +ERROR: ON CONFLICT DO UPDATE not supported with exclusion constraints +-- id matches but no conflict +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2010-01-01', '2020-01-01') ON CONFLICT ON CONSTRAINT temporal_per3_uq DO UPDATE SET id = EXCLUDED.id + '[3,4)'; +ERROR: ON CONFLICT DO UPDATE not supported with exclusion constraints +-- date matches but no conflict +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[2,3)', '2005-01-01', '2006-01-01') ON CONFLICT ON CONSTRAINT temporal_per3_uq DO UPDATE SET id = EXCLUDED.id + '[4,5)'; +ERROR: ON CONFLICT DO UPDATE not supported with exclusion constraints +SELECT * FROM temporal_per3 ORDER BY id, valid_from, valid_til; + id | valid_from | valid_til +-------+------------+------------ + [1,2) | 2000-01-01 | 2010-01-01 +(1 row) + +DROP TABLE temporal_per3; +-- +-- test FK dependencies +-- +-- can't drop a range referenced by an FK, unless with CASCADE +CREATE TABLE temporal3 ( + id int4range, + valid_at daterange, + CONSTRAINT temporal3_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +); +CREATE TABLE temporal_fk_rng2rng ( + id int4range, + valid_at daterange, + parent_id int4range, + CONSTRAINT temporal_fk_rng2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_rng2rng_fk FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal3 (id, PERIOD valid_at) +); +ALTER TABLE temporal3 DROP COLUMN valid_at; +ERROR: cannot drop column valid_at of table temporal3 because other objects depend on it +DETAIL: constraint temporal_fk_rng2rng_fk on table temporal_fk_rng2rng depends on column valid_at of table temporal3 +HINT: Use DROP ... CASCADE to drop the dependent objects too. +ALTER TABLE temporal3 DROP COLUMN valid_at CASCADE; +NOTICE: drop cascades to constraint temporal_fk_rng2rng_fk on table temporal_fk_rng2rng +DROP TABLE temporal_fk_rng2rng; +DROP TABLE temporal3; +-- can't drop a PERIOD referenced by an FK, unless with CASCADE +CREATE TABLE temporal3 ( + id int4range, + valid_from date, + valid_til date, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal3_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +); +CREATE TABLE temporal_fk_per2per ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2per_fk FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal3 (id, PERIOD valid_at) +); +ALTER TABLE temporal3 DROP PERIOD FOR valid_at; +ERROR: cannot drop period valid_at on table temporal3 because other objects depend on it +DETAIL: constraint temporal_fk_per2per_fk on table temporal_fk_per2per depends on column valid_at of table temporal3 +HINT: Use DROP ... CASCADE to drop the dependent objects too. +ALTER TABLE temporal3 DROP PERIOD FOR valid_at CASCADE; +NOTICE: drop cascades to constraint temporal_fk_per2per_fk on table temporal_fk_per2per +DROP TABLE temporal_fk_per2per; +DROP TABLE temporal3; +-- +-- test FOREIGN KEY, range references range -- -- test table setup DROP TABLE temporal_rng; @@ -1467,6 +2276,18 @@ CREATE TABLE temporal_fk_rng2rng ( CONSTRAINT temporal_fk_rng2rng_fk FOREIGN KEY (parent_id, PERIOD valid_at) REFERENCES temporal_rng (id, PERIOD valid_at) ); +\d temporal_fk_rng2rng + Table "public.temporal_fk_rng2rng" + Column | Type | Collation | Nullable | Default +-----------+-----------+-----------+----------+--------- + id | int4range | | not null | + valid_at | daterange | | not null | + parent_id | int4range | | | +Indexes: + "temporal_fk_rng2rng_pk" PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +Foreign-key constraints: + "temporal_fk_rng2rng_fk" FOREIGN KEY (parent_id, PERIOD valid_at) REFERENCES temporal_rng(id, PERIOD valid_at) + DROP TABLE temporal_fk_rng2rng; -- with mismatched PERIOD columns: -- (parent_id, PERIOD valid_at) REFERENCES (id, valid_at) @@ -3585,83 +4406,3566 @@ SELECT * FROM temporal_fk2_mltrng2mltrng WHERE id = '[200,201)' ORDER BY id, val [200,201) | {[2020-01-01,2021-01-01)} | [8,9) | [8,9) (2 rows) --- FK with a custom range type -CREATE TYPE mydaterange AS range(subtype=date); -CREATE TABLE temporal_rng3 ( +-- +-- test FOREIGN KEY, PERIOD references PERIOD +-- +-- test table setup +DROP TABLE temporal_per; +CREATE TABLE temporal_per ( id int4range, - valid_at mydaterange, - CONSTRAINT temporal_rng3_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) + valid_from date, + valid_til date, + PERIOD FOR valid_at (valid_from, valid_til) ); -CREATE TABLE temporal_fk3_rng2rng ( +ALTER TABLE temporal_per + ADD CONSTRAINT temporal_per_pk + PRIMARY KEY (id, valid_at WITHOUT OVERLAPS); +-- Can't create a FK with a mismatched range type +CREATE TABLE temporal_fk_per2per ( id int4range, - valid_at mydaterange, + valid_from int, + valid_til int, parent_id int4range, - CONSTRAINT temporal_fk3_rng2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), - CONSTRAINT temporal_fk3_rng2rng_fk FOREIGN KEY (parent_id, PERIOD valid_at) - REFERENCES temporal_rng3 (id, PERIOD valid_at) ON DELETE CASCADE + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2per_fk FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per (id, PERIOD valid_at) ); -INSERT INTO temporal_rng3 (id, valid_at) VALUES ('[8,9)', mydaterange('2018-01-01', '2021-01-01')); -INSERT INTO temporal_fk3_rng2rng (id, valid_at, parent_id) VALUES ('[5,6)', mydaterange('2018-01-01', '2021-01-01'), '[8,9)'); -DELETE FROM temporal_rng3 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id = '[8,9)'; -SELECT * FROM temporal_fk3_rng2rng WHERE id = '[5,6)'; - id | valid_at | parent_id --------+-------------------------+----------- - [5,6) | [2018-01-01,2019-01-01) | [8,9) - [5,6) | [2020-01-01,2021-01-01) | [8,9) -(2 rows) +ERROR: foreign key constraint "temporal_fk_per2per_fk" cannot be implemented +DETAIL: Key columns "valid_at" of the referencing table and "valid_at" of the referenced table are of incompatible types: int4range and daterange. +-- works: PERIOD for both referenced and referencing +CREATE TABLE temporal_fk_per2per ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2per_fk FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per (id, PERIOD valid_at) +); +\d temporal_fk_per2per + Table "public.temporal_fk_per2per" + Column | Type | Collation | Nullable | Default +------------+-----------+-----------+----------+--------- + id | int4range | | not null | + valid_from | date | | | + valid_til | date | | | + parent_id | int4range | | | +Periods: + valid_at (valid_from, valid_til) +Indexes: + "temporal_fk_per2per_pk" PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +Check constraints: + "temporal_fk_per2per_valid_at_check" CHECK (valid_from < valid_til) +Foreign-key constraints: + "temporal_fk_per2per_fk" FOREIGN KEY (parent_id, PERIOD valid_at) REFERENCES temporal_per(id, PERIOD valid_at) -DROP TABLE temporal_fk3_rng2rng; -DROP TABLE temporal_rng3; -DROP TYPE mydaterange; --- --- FK between partitioned tables: ranges --- -CREATE TABLE temporal_partitioned_rng ( +DROP TABLE temporal_fk_per2per; +-- with mismatched PERIOD columns: +-- (parent_id, PERIOD valid_at) REFERENCES (id, valid_at) +-- REFERENCES part should specify PERIOD +CREATE TABLE temporal_fk_per2per ( id int4range, - valid_at daterange, - name text, - CONSTRAINT temporal_paritioned_rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) -) PARTITION BY LIST (id); -CREATE TABLE tp1 PARTITION OF temporal_partitioned_rng FOR VALUES IN ('[1,2)', '[3,4)', '[5,6)', '[7,8)', '[9,10)', '[11,12)', '[13,14)', '[15,16)', '[17,18)', '[19,20)', '[21,22)', '[23,24)'); -CREATE TABLE tp2 PARTITION OF temporal_partitioned_rng FOR VALUES IN ('[0,1)', '[2,3)', '[4,5)', '[6,7)', '[8,9)', '[10,11)', '[12,13)', '[14,15)', '[16,17)', '[18,19)', '[20,21)', '[22,23)', '[24,25)'); -INSERT INTO temporal_partitioned_rng (id, valid_at, name) VALUES - ('[1,2)', daterange('2000-01-01', '2000-02-01'), 'one'), - ('[1,2)', daterange('2000-02-01', '2000-03-01'), 'one'), - ('[2,3)', daterange('2000-01-01', '2010-01-01'), 'two'); -CREATE TABLE temporal_partitioned_fk_rng2rng ( + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2per_fk FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per (id, valid_at) +); +ERROR: foreign key uses PERIOD on the referencing table but not the referenced table +-- (parent_id, valid_at) REFERENCES (id, valid_at) +-- both should specify PERIOD: +CREATE TABLE temporal_fk_per2per ( id int4range, - valid_at daterange, + valid_from date, + valid_til date, parent_id int4range, - CONSTRAINT temporal_partitioned_fk_rng2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), - CONSTRAINT temporal_partitioned_fk_rng2rng_fk FOREIGN KEY (parent_id, PERIOD valid_at) - REFERENCES temporal_partitioned_rng (id, PERIOD valid_at) -) PARTITION BY LIST (id); -CREATE TABLE tfkp1 PARTITION OF temporal_partitioned_fk_rng2rng FOR VALUES IN ('[1,2)', '[3,4)', '[5,6)', '[7,8)', '[9,10)', '[11,12)', '[13,14)', '[15,16)', '[17,18)', '[19,20)', '[21,22)', '[23,24)'); -CREATE TABLE tfkp2 PARTITION OF temporal_partitioned_fk_rng2rng FOR VALUES IN ('[0,1)', '[2,3)', '[4,5)', '[6,7)', '[8,9)', '[10,11)', '[12,13)', '[14,15)', '[16,17)', '[18,19)', '[20,21)', '[22,23)', '[24,25)'); --- --- partitioned FK referencing inserts --- -INSERT INTO temporal_partitioned_fk_rng2rng (id, valid_at, parent_id) VALUES - ('[1,2)', daterange('2000-01-01', '2000-02-15'), '[1,2)'), - ('[1,2)', daterange('2001-01-01', '2002-01-01'), '[2,3)'), - ('[2,3)', daterange('2000-01-01', '2000-02-15'), '[1,2)'); --- should fail: -INSERT INTO temporal_partitioned_fk_rng2rng (id, valid_at, parent_id) VALUES - ('[3,4)', daterange('2010-01-01', '2010-02-15'), '[1,2)'); -ERROR: insert or update on table "tfkp1" violates foreign key constraint "temporal_partitioned_fk_rng2rng_fk" -DETAIL: Key (parent_id, valid_at)=([1,2), [2010-01-01,2010-02-15)) is not present in table "temporal_partitioned_rng". -INSERT INTO temporal_partitioned_fk_rng2rng (id, valid_at, parent_id) VALUES - ('[3,4)', daterange('2000-01-01', '2000-02-15'), '[3,4)'); -ERROR: insert or update on table "tfkp1" violates foreign key constraint "temporal_partitioned_fk_rng2rng_fk" -DETAIL: Key (parent_id, valid_at)=([3,4), [2000-01-01,2000-02-15)) is not present in table "temporal_partitioned_rng". + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2per_fk FOREIGN KEY (parent_id, valid_at) + REFERENCES temporal_per (id, valid_at) +); +ERROR: foreign key must use PERIOD when referencing a primary key using WITHOUT OVERLAPS +-- (parent_id, valid_at) REFERENCES (id, PERIOD valid_at) +-- FOREIGN KEY part should specify PERIOD +CREATE TABLE temporal_fk_per2per ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2per_fk FOREIGN KEY (parent_id, valid_at) + REFERENCES temporal_per (id, PERIOD valid_at) +); +ERROR: foreign key uses PERIOD on the referenced table but not the referencing table +-- (parent_id, valid_at) REFERENCES [implicit] +-- FOREIGN KEY part should specify PERIOD +CREATE TABLE temporal_fk_per2per ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2per_fk FOREIGN KEY (parent_id, valid_at) + REFERENCES temporal_per +); +ERROR: foreign key uses PERIOD on the referenced table but not the referencing table +-- (parent_id, PERIOD valid_at) REFERENCES (id) +CREATE TABLE temporal_fk_per2per ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2per_fk FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per (id) +); +ERROR: foreign key uses PERIOD on the referencing table but not the referenced table +-- (parent_id) REFERENCES (id, PERIOD valid_at) +CREATE TABLE temporal_fk_per2per ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2per_fk FOREIGN KEY (parent_id) + REFERENCES temporal_per (id, PERIOD valid_at) +); +ERROR: foreign key uses PERIOD on the referenced table but not the referencing table +-- with inferred PK on the referenced table: +-- (parent_id, PERIOD valid_at) REFERENCES [implicit] +CREATE TABLE temporal_fk_per2per ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2per_fk FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per +); +DROP TABLE temporal_fk_per2per; +-- (parent_id) REFERENCES [implicit] +CREATE TABLE temporal_fk_per2per ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2per_fk FOREIGN KEY (parent_id) + REFERENCES temporal_per +); +ERROR: foreign key uses PERIOD on the referenced table but not the referencing table +-- should fail because of duplicate referenced columns: +CREATE TABLE temporal_fk_per2per ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2per_fk FOREIGN KEY (parent_id, PERIOD parent_id) + REFERENCES temporal_per (id, PERIOD id) +); +ERROR: foreign key referenced-columns list must not contain duplicates +-- Two scalar columns +DROP TABLE temporal_per2; +CREATE TABLE temporal_per2 ( + id1 int4range, + id2 int4range, + valid_from date, + valid_til date, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_per2_pk PRIMARY KEY (id1, id2, valid_at WITHOUT OVERLAPS) +); +CREATE TABLE temporal_fk2_per2per ( + id int4range, + valid_from date, + valid_til date, + parent_id1 int4range, + parent_id2 int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk2_per2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk2_per2per_fk FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_per2 (id1, id2, PERIOD valid_at) +); +\d temporal_fk2_per2per + Table "public.temporal_fk2_per2per" + Column | Type | Collation | Nullable | Default +------------+-----------+-----------+----------+--------- + id | int4range | | not null | + valid_from | date | | | + valid_til | date | | | + parent_id1 | int4range | | | + parent_id2 | int4range | | | +Periods: + valid_at (valid_from, valid_til) +Indexes: + "temporal_fk2_per2per_pk" PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +Check constraints: + "temporal_fk2_per2per_valid_at_check" CHECK (valid_from < valid_til) +Foreign-key constraints: + "temporal_fk2_per2per_fk" FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) REFERENCES temporal_per2(id1, id2, PERIOD valid_at) + +DROP TABLE temporal_fk2_per2per; -- --- partitioned FK referencing updates +-- test ALTER TABLE ADD CONSTRAINT -- -UPDATE temporal_partitioned_fk_rng2rng SET valid_at = daterange('2000-01-01', '2000-02-13') WHERE id = '[2,3)'; --- move a row from the first partition to the second -UPDATE temporal_partitioned_fk_rng2rng SET id = '[4,5)' WHERE id = '[1,2)'; --- move a row from the second partition to the first -UPDATE temporal_partitioned_fk_rng2rng SET id = '[1,2)' WHERE id = '[4,5)'; +CREATE TABLE temporal_fk_per2per ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +); +ALTER TABLE temporal_fk_per2per + ADD CONSTRAINT temporal_fk_per2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per (id, PERIOD valid_at); +-- Two scalar columns: +CREATE TABLE temporal_fk2_per2per ( + id int4range, + valid_from date, + valid_til date, + parent_id1 int4range, + parent_id2 int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk2_per2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +); +ALTER TABLE temporal_fk2_per2per + ADD CONSTRAINT temporal_fk2_per2per_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_per2 (id1, id2, PERIOD valid_at); +\d temporal_fk2_per2per + Table "public.temporal_fk2_per2per" + Column | Type | Collation | Nullable | Default +------------+-----------+-----------+----------+--------- + id | int4range | | not null | + valid_from | date | | | + valid_til | date | | | + parent_id1 | int4range | | | + parent_id2 | int4range | | | +Periods: + valid_at (valid_from, valid_til) +Indexes: + "temporal_fk2_per2per_pk" PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +Check constraints: + "temporal_fk2_per2per_valid_at_check" CHECK (valid_from < valid_til) +Foreign-key constraints: + "temporal_fk2_per2per_fk" FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) REFERENCES temporal_per2(id1, id2, PERIOD valid_at) + +-- with inferred PK on the referenced table, and wrong column type: +ALTER TABLE temporal_fk_per2per + DROP CONSTRAINT temporal_fk_per2per_fk, + DROP PERIOD FOR valid_at, + ALTER COLUMN valid_from TYPE timestamp, + ALTER COLUMN valid_til TYPE timestamp, + ADD PERIOD FOR valid_at (valid_from, valid_til); +ALTER TABLE temporal_fk_per2per + ADD CONSTRAINT temporal_fk_per2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per; +ERROR: foreign key constraint "temporal_fk_per2per_fk" cannot be implemented +DETAIL: Key columns "valid_at" of the referencing table and "valid_at" of the referenced table are of incompatible types: tsrange and daterange. +ALTER TABLE temporal_fk_per2per + DROP PERIOD FOR valid_at, + ALTER COLUMN valid_from TYPE date, + ALTER COLUMN valid_til TYPE date, + ADD PERIOD FOR valid_at (valid_from, valid_til); +-- with inferred PK on the referenced table: +ALTER TABLE temporal_fk_per2per + ADD CONSTRAINT temporal_fk_per2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per; +-- should fail because of duplicate referenced columns: +ALTER TABLE temporal_fk_per2per + ADD CONSTRAINT temporal_fk_per2per_fk2 + FOREIGN KEY (parent_id, PERIOD parent_id) + REFERENCES temporal_per (id, PERIOD id); +ERROR: foreign key referenced-columns list must not contain duplicates +-- +-- test with rows already +-- +DELETE FROM temporal_fk_per2per; +DELETE FROM temporal_per; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES + ('[1,2)', '2018-01-02', '2018-02-03'), + ('[1,2)', '2018-03-03', '2018-04-04'), + ('[2,3)', '2018-01-01', '2018-01-05'), + ('[3,4)', '2018-01-01', NULL); +ALTER TABLE temporal_fk_per2per + DROP CONSTRAINT temporal_fk_per2per_fk; +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES ('[1,2)', '2018-01-02', '2018-02-01', '[1,2)'); +ALTER TABLE temporal_fk_per2per + ADD CONSTRAINT temporal_fk_per2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per; +ALTER TABLE temporal_fk_per2per + DROP CONSTRAINT temporal_fk_per2per_fk; +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES ('[2,3)', '2018-01-02', '2018-04-01', '[1,2)'); +-- should fail: +ALTER TABLE temporal_fk_per2per + ADD CONSTRAINT temporal_fk_per2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per; +ERROR: insert or update on table "temporal_fk_per2per" violates foreign key constraint "temporal_fk_per2per_fk" +DETAIL: Key (parent_id, valid_at)=([1,2), [2018-01-02,2018-04-01)) is not present in table "temporal_per". +-- okay again: +DELETE FROM temporal_fk_per2per; +ALTER TABLE temporal_fk_per2per + ADD CONSTRAINT temporal_fk_per2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per; +-- +-- test pg_get_constraintdef +-- +SELECT pg_get_constraintdef(oid) FROM pg_constraint WHERE conname = 'temporal_fk_per2per_fk'; + pg_get_constraintdef +--------------------------------------------------------------------------------------- + FOREIGN KEY (parent_id, PERIOD valid_at) REFERENCES temporal_per(id, PERIOD valid_at) +(1 row) + +-- +-- test FK referencing inserts +-- +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES ('[1,2)', '2018-01-02', '2018-02-01', '[1,2)'); +-- should fail: +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES ('[2,3)', '2018-01-02', '2018-04-01', '[1,2)'); +ERROR: insert or update on table "temporal_fk_per2per" violates foreign key constraint "temporal_fk_per2per_fk" +DETAIL: Key (parent_id, valid_at)=([1,2), [2018-01-02,2018-04-01)) is not present in table "temporal_per". +-- now it should work: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2018-02-03', '2018-03-03'); +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES ('[2,3)', '2018-01-02', '2018-04-01', '[1,2)'); +-- +-- test FK referencing updates +-- +-- slide the edge across a referenced transition: +UPDATE temporal_fk_per2per SET valid_from = '2018-01-02', valid_til = '2018-02-20' WHERE id = '[1,2)'; +-- should fail: +UPDATE temporal_fk_per2per SET valid_from = '2018-01-02', valid_til = '2018-05-01' WHERE id = '[1,2)'; +ERROR: insert or update on table "temporal_fk_per2per" violates foreign key constraint "temporal_fk_per2per_fk" +DETAIL: Key (parent_id, valid_at)=([1,2), [2018-01-02,2018-05-01)) is not present in table "temporal_per". +UPDATE temporal_fk_per2per SET parent_id = '[8,9)' WHERE id = '[1,2)'; +ERROR: insert or update on table "temporal_fk_per2per" violates foreign key constraint "temporal_fk_per2per_fk" +DETAIL: Key (parent_id, valid_at)=([8,9), [2018-01-02,2018-02-20)) is not present in table "temporal_per". +-- ALTER FK DEFERRABLE +BEGIN; + INSERT INTO temporal_per (id, valid_from, valid_til) VALUES + ('[5,6)', '2018-01-01', '2018-02-01'), + ('[5,6)', '2018-02-01', '2018-03-01'); + INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES + ('[3,4)', '2018-01-05', '2018-01-10', '[5,6)'); + ALTER TABLE temporal_fk_per2per + ALTER CONSTRAINT temporal_fk_per2per_fk + DEFERRABLE INITIALLY DEFERRED; + DELETE FROM temporal_per WHERE id = '[5,6)'; --should not fail yet. +COMMIT; -- should fail here. +ERROR: update or delete on table "temporal_per" violates foreign key constraint "temporal_fk_per2per_fk" on table "temporal_fk_per2per" +DETAIL: Key (id, valid_at)=([5,6), [2018-01-01,2018-02-01)) is still referenced from table "temporal_fk_per2per". +-- +-- test FK referenced updates NO ACTION +-- +TRUNCATE temporal_per, temporal_fk_per2per; +ALTER TABLE temporal_fk_per2per + DROP CONSTRAINT temporal_fk_per2per_fk; +ALTER TABLE temporal_fk_per2per + ADD CONSTRAINT temporal_fk_per2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per + ON UPDATE NO ACTION; +-- a PK update that succeeds because the numeric id isn't referenced: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[5,6)', '2018-01-01', '2018-02-01'); +UPDATE temporal_per SET valid_from = '2016-01-01', valid_til = '2016-02-01' WHERE id = '[5,6)'; +-- a PK update that succeeds even though the numeric id is referenced because the range isn't: +DELETE FROM temporal_per WHERE id = '[5,6)'; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES + ('[5,6)', '2018-01-01', '2018-02-01'), + ('[5,6)', '2018-02-01', '2018-03-01'); +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES + ('[3,4)', '2018-01-05', '2018-01-10', '[5,6)'); +UPDATE temporal_per SET valid_from = '2016-02-01', valid_til = '2016-03-01' +WHERE id = '[5,6)' AND valid_from = '2018-02-01' AND valid_til = '2018-03-01'; +-- A PK update sliding the edge between two referenced rows: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES + ('[6,7)', '2018-01-01', '2018-02-01'), + ('[6,7)', '2018-02-01', '2018-03-01'); +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES + ('[4,5)', '2018-01-15', '2018-02-15', '[6,7)'); +UPDATE temporal_per +SET valid_from = CASE WHEN valid_from = '2018-01-01' THEN '2018-01-01' + WHEN valid_from = '2018-02-01' THEN '2018-01-05' END::date, + valid_til = CASE WHEN valid_from = '2018-01-01' THEN '2018-01-05' + WHEN valid_from = '2018-02-01' THEN '2018-03-01' END::date +WHERE id = '[6,7)'; +-- a PK update shrinking the referenced range but still valid: +-- There are two references: one fulfilled by the first pk row, +-- the other fulfilled by both pk rows combined. +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES + ('[1,2)', '2018-01-01', '2018-03-01'), + ('[1,2)', '2018-03-01', '2018-06-01'); +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES + ('[1,2)', '2018-01-15', '2018-02-01', '[1,2)'), + ('[2,3)', '2018-01-15', '2018-05-01', '[1,2)'); +UPDATE temporal_per SET valid_from = '2018-01-15', valid_til = '2018-03-01' +WHERE id = '[1,2)' AND valid_at @> '2018-01-15'::date; +-- a PK update growing the referenced range is fine: +UPDATE temporal_per SET valid_from = '2018-01-01', valid_til = '2018-03-01' +WHERE id = '[1,2)' AND valid_at @> '2018-01-25'::date; +-- a PK update shrinking the referenced range and changing the id invalidates the whole range: +UPDATE temporal_per SET id = '[2,3)', valid_from = '2018-01-15', valid_til = '2018-03-01' +WHERE id = '[1,2)' AND valid_at @> '2018-01-15'::date; +ERROR: update or delete on table "temporal_per" violates foreign key constraint "temporal_fk_per2per_fk" on table "temporal_fk_per2per" +DETAIL: Key (id, valid_at)=([1,2), [2018-01-01,2018-03-01)) is still referenced from table "temporal_fk_per2per". +-- a PK update changing only the id invalidates the whole range: +UPDATE temporal_per SET id = '[2,3)' +WHERE id = '[1,2)' AND valid_at @> '2018-01-15'::date; +ERROR: update or delete on table "temporal_per" violates foreign key constraint "temporal_fk_per2per_fk" on table "temporal_fk_per2per" +DETAIL: Key (id, valid_at)=([1,2), [2018-01-01,2018-03-01)) is still referenced from table "temporal_fk_per2per". +-- a PK update that loses time from both ends, but is still valid: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES + ('[2,3)', '2018-01-01', '2018-03-01'); +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES + ('[5,6)', '2018-01-15', '2018-02-01', '[2,3)'); +UPDATE temporal_per SET valid_from = '2018-01-15', valid_til = '2018-02-15' +WHERE id = '[2,3)'; +-- a PK update that fails because both are referenced: +UPDATE temporal_per SET valid_from = '2016-01-01', valid_til = '2016-02-01' +WHERE id = '[5,6)' AND valid_from = '2018-01-01' AND valid_til = '2018-02-01'; +ERROR: update or delete on table "temporal_per" violates foreign key constraint "temporal_fk_per2per_fk" on table "temporal_fk_per2per" +DETAIL: Key (id, valid_at)=([5,6), [2018-01-01,2018-02-01)) is still referenced from table "temporal_fk_per2per". +-- a PK update that fails because both are referenced, but not 'til commit: +BEGIN; + ALTER TABLE temporal_fk_per2per + ALTER CONSTRAINT temporal_fk_per2per_fk + DEFERRABLE INITIALLY DEFERRED; + UPDATE temporal_per SET valid_from = '2016-01-01', valid_til = '2016-02-01' + WHERE id = '[5,6)' AND valid_at = daterange('2018-01-01', '2018-02-01'); +COMMIT; +ERROR: update or delete on table "temporal_per" violates foreign key constraint "temporal_fk_per2per_fk" on table "temporal_fk_per2per" +DETAIL: Key (id, valid_at)=([5,6), [2018-01-01,2018-02-01)) is still referenced from table "temporal_fk_per2per". +-- changing the scalar part fails: +UPDATE temporal_per SET id = '[7,8)' +WHERE id = '[5,6)' AND valid_from = '2018-01-01' AND valid_til = '2018-02-01'; +ERROR: update or delete on table "temporal_per" violates foreign key constraint "temporal_fk_per2per_fk" on table "temporal_fk_per2per" +DETAIL: Key (id, valid_at)=([5,6), [2018-01-01,2018-02-01)) is still referenced from table "temporal_fk_per2per". +-- changing an unreferenced part is okay: +UPDATE temporal_per +FOR PORTION OF valid_at FROM '2018-01-02' TO '2018-01-03' +SET id = '[7,8)' +WHERE id = '[5,6)'; +-- changing just a part fails: +UPDATE temporal_per +FOR PORTION OF valid_at FROM '2018-01-05' TO '2018-01-10' +SET id = '[7,8)' +WHERE id = '[5,6)'; +ERROR: update or delete on table "temporal_per" violates foreign key constraint "temporal_fk_per2per_fk" on table "temporal_fk_per2per" +DETAIL: Key (id, valid_at)=([5,6), [2018-01-03,2018-02-01)) is still referenced from table "temporal_fk_per2per". +SELECT * FROM temporal_per WHERE id in ('[5,6)', '[7,8)') ORDER BY id, valid_at; + id | valid_from | valid_til +-------+------------+------------ + [5,6) | 2016-02-01 | 2016-03-01 + [5,6) | 2018-01-01 | 2018-01-02 + [5,6) | 2018-01-03 | 2018-02-01 + [7,8) | 2018-01-02 | 2018-01-03 +(4 rows) + +SELECT * FROM temporal_fk_per2per WHERE id in ('[3,4)') ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +-------+------------+------------+----------- + [3,4) | 2018-01-05 | 2018-01-10 | [5,6) +(1 row) + +-- then delete the objecting FK record and the same PK update succeeds: +DELETE FROM temporal_fk_per2per WHERE id = '[3,4)'; +UPDATE temporal_per SET valid_from = '2016-01-01', valid_til = '2016-02-01' +WHERE id = '[5,6)' AND valid_from = '2018-01-01' AND valid_til = '2018-02-01'; +-- +-- test FK referenced updates RESTRICT +-- +TRUNCATE temporal_per, temporal_fk_per2per; +ALTER TABLE temporal_fk_per2per + DROP CONSTRAINT temporal_fk_per2per_fk; +ALTER TABLE temporal_fk_per2per + ADD CONSTRAINT temporal_fk_per2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per + ON UPDATE RESTRICT; +ERROR: unsupported ON UPDATE action for foreign key constraint using PERIOD +-- +-- test FK referenced deletes NO ACTION +-- +TRUNCATE temporal_per, temporal_fk_per2per; +ALTER TABLE temporal_fk_per2per + ADD CONSTRAINT temporal_fk_per2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per; +-- a PK delete that succeeds because the numeric id isn't referenced: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[5,6)', '2018-01-01', '2018-02-01'); +DELETE FROM temporal_per WHERE id = '[5,6)'; +-- a PK delete that succeeds even though the numeric id is referenced because the range isn't: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES + ('[5,6)', '2018-01-01', '2018-02-01'), + ('[5,6)', '2018-02-01', '2018-03-01'); +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES ('[3,4)', '2018-01-05', '2018-01-10', '[5,6)'); +DELETE FROM temporal_per WHERE id = '[5,6)' AND valid_from = '2018-02-01' AND valid_til = '2018-03-01'; +-- a PK delete that fails because both are referenced: +DELETE FROM temporal_per WHERE id = '[5,6)' AND valid_from = '2018-01-01' AND valid_til = '2018-02-01'; +ERROR: update or delete on table "temporal_per" violates foreign key constraint "temporal_fk_per2per_fk" on table "temporal_fk_per2per" +DETAIL: Key (id, valid_at)=([5,6), [2018-01-01,2018-02-01)) is still referenced from table "temporal_fk_per2per". +-- a PK delete that fails because both are referenced, but not 'til commit: +BEGIN; + ALTER TABLE temporal_fk_per2per + ALTER CONSTRAINT temporal_fk_per2per_fk + DEFERRABLE INITIALLY DEFERRED; + DELETE FROM temporal_per WHERE id = '[5,6)' AND valid_at = daterange('2018-01-01', '2018-02-01'); +COMMIT; +ERROR: update or delete on table "temporal_per" violates foreign key constraint "temporal_fk_per2per_fk" on table "temporal_fk_per2per" +DETAIL: Key (id, valid_at)=([5,6), [2018-01-01,2018-02-01)) is still referenced from table "temporal_fk_per2per". +-- deleting an unreferenced part is okay: +DELETE FROM temporal_per +FOR PORTION OF valid_at FROM '2018-01-02' TO '2018-01-03' +WHERE id = '[5,6)'; +-- deleting just a part fails: +DELETE FROM temporal_per +FOR PORTION OF valid_at FROM '2018-01-05' TO '2018-01-10' +WHERE id = '[5,6)'; +ERROR: update or delete on table "temporal_per" violates foreign key constraint "temporal_fk_per2per_fk" on table "temporal_fk_per2per" +DETAIL: Key (id, valid_at)=([5,6), [2018-01-03,2018-02-01)) is still referenced from table "temporal_fk_per2per". +SELECT * FROM temporal_per WHERE id in ('[5,6)', '[7,8)') ORDER BY id, valid_at; + id | valid_from | valid_til +-------+------------+------------ + [5,6) | 2018-01-01 | 2018-01-02 + [5,6) | 2018-01-03 | 2018-02-01 +(2 rows) + +SELECT * FROM temporal_fk_per2per WHERE id in ('[3,4)') ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +-------+------------+------------+----------- + [3,4) | 2018-01-05 | 2018-01-10 | [5,6) +(1 row) + +-- then delete the objecting FK record and the same PK delete succeeds: +DELETE FROM temporal_fk_per2per WHERE id = '[3,4)'; +DELETE FROM temporal_per WHERE id = '[5,6)' AND valid_from = '2018-01-01' AND valid_til = '2018-02-01'; +-- +-- test FK referenced deletes RESTRICT +-- +TRUNCATE temporal_per, temporal_fk_per2per; +ALTER TABLE temporal_fk_per2per + DROP CONSTRAINT temporal_fk_per2per_fk; +ALTER TABLE temporal_fk_per2per + ADD CONSTRAINT temporal_fk_per2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per + ON DELETE RESTRICT; +ERROR: unsupported ON DELETE action for foreign key constraint using PERIOD +-- +-- per2per test ON UPDATE/DELETE options +-- +-- TOC: +-- referenced updates CASCADE +-- referenced deletes CASCADE +-- referenced updates SET NULL +-- referenced deletes SET NULL +-- referenced updates SET DEFAULT +-- referenced deletes SET DEFAULT +-- referenced updates CASCADE (two scalar cols) +-- referenced deletes CASCADE (two scalar cols) +-- referenced updates SET NULL (two scalar cols) +-- referenced deletes SET NULL (two scalar cols) +-- referenced deletes SET NULL (two scalar cols, SET NULL subset) +-- referenced updates SET DEFAULT (two scalar cols) +-- referenced deletes SET DEFAULT (two scalar cols) +-- referenced deletes SET DEFAULT (two scalar cols, SET DEFAULT subset) +-- +-- test FK referenced updates CASCADE +-- +TRUNCATE temporal_per, temporal_fk_per2per; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)'); +ALTER TABLE temporal_fk_per2per + ADD CONSTRAINT temporal_fk_per2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per + ON DELETE CASCADE ON UPDATE CASCADE; +-- leftovers on both sides: +UPDATE temporal_per FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' SET id = '[7,8)' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +-----------+------------+------------+----------- + [100,101) | 2018-01-01 | 2019-01-01 | [6,7) + [100,101) | 2019-01-01 | 2020-01-01 | [7,8) + [100,101) | 2020-01-01 | 2021-01-01 | [6,7) +(3 rows) + +-- non-FPO update: +UPDATE temporal_per SET id = '[7,8)' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +-----------+------------+------------+----------- + [100,101) | 2018-01-01 | 2019-01-01 | [7,8) + [100,101) | 2019-01-01 | 2020-01-01 | [7,8) + [100,101) | 2020-01-01 | 2021-01-01 | [7,8) +(3 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)'); +UPDATE temporal_per SET id = '[9,10)' WHERE id = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk_per2per WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +-----------+------------+------------+----------- + [200,201) | 2018-01-01 | 2020-01-01 | [9,10) + [200,201) | 2020-01-01 | 2021-01-01 | [8,9) +(2 rows) + +-- +-- test FK referenced deletes CASCADE +-- +TRUNCATE temporal_per, temporal_fk_per2per; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)'); +-- leftovers on both sides: +DELETE FROM temporal_per FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +-----------+------------+------------+----------- + [100,101) | 2018-01-01 | 2019-01-01 | [6,7) + [100,101) | 2020-01-01 | 2021-01-01 | [6,7) +(2 rows) + +-- non-FPO delete: +DELETE FROM temporal_per WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +----+------------+-----------+----------- +(0 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)'); +DELETE FROM temporal_per WHERE id = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk_per2per WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +-----------+------------+------------+----------- + [200,201) | 2020-01-01 | 2021-01-01 | [8,9) +(1 row) + +-- +-- test FK referenced updates SET NULL +-- +TRUNCATE temporal_per, temporal_fk_per2per; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)'); +ALTER TABLE temporal_fk_per2per + DROP CONSTRAINT temporal_fk_per2per_fk, + ADD CONSTRAINT temporal_fk_per2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per + ON DELETE SET NULL ON UPDATE SET NULL; +-- leftovers on both sides: +UPDATE temporal_per FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' SET id = '[7,8)' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +-----------+------------+------------+----------- + [100,101) | 2018-01-01 | 2019-01-01 | [6,7) + [100,101) | 2019-01-01 | 2020-01-01 | + [100,101) | 2020-01-01 | 2021-01-01 | [6,7) +(3 rows) + +-- non-FPO update: +UPDATE temporal_per SET id = '[7,8)' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +-----------+------------+------------+----------- + [100,101) | 2018-01-01 | 2019-01-01 | + [100,101) | 2019-01-01 | 2020-01-01 | + [100,101) | 2020-01-01 | 2021-01-01 | +(3 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)'); +UPDATE temporal_per SET id = '[9,10)' WHERE id = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk_per2per WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +-----------+------------+------------+----------- + [200,201) | 2018-01-01 | 2020-01-01 | + [200,201) | 2020-01-01 | 2021-01-01 | [8,9) +(2 rows) + +-- +-- test FK referenced deletes SET NULL +-- +TRUNCATE temporal_per, temporal_fk_per2per; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)'); +-- leftovers on both sides: +DELETE FROM temporal_per FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +-----------+------------+------------+----------- + [100,101) | 2018-01-01 | 2019-01-01 | [6,7) + [100,101) | 2019-01-01 | 2020-01-01 | + [100,101) | 2020-01-01 | 2021-01-01 | [6,7) +(3 rows) + +-- non-FPO delete: +DELETE FROM temporal_per WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +-----------+------------+------------+----------- + [100,101) | 2018-01-01 | 2019-01-01 | + [100,101) | 2019-01-01 | 2020-01-01 | + [100,101) | 2020-01-01 | 2021-01-01 | +(3 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)'); +DELETE FROM temporal_per WHERE id = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk_per2per WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +-----------+------------+------------+----------- + [200,201) | 2018-01-01 | 2020-01-01 | + [200,201) | 2020-01-01 | 2021-01-01 | [8,9) +(2 rows) + +-- +-- test FK referenced updates SET DEFAULT +-- +TRUNCATE temporal_per, temporal_fk_per2per; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[-1,-1]', null, null); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)'); +ALTER TABLE temporal_fk_per2per + ALTER COLUMN parent_id SET DEFAULT '[-1,-1]', + DROP CONSTRAINT temporal_fk_per2per_fk, + ADD CONSTRAINT temporal_fk_per2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per + ON DELETE SET DEFAULT ON UPDATE SET DEFAULT; +-- leftovers on both sides: +UPDATE temporal_per FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' SET id = '[7,8)' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +-----------+------------+------------+----------- + [100,101) | 2018-01-01 | 2019-01-01 | [6,7) + [100,101) | 2019-01-01 | 2020-01-01 | [-1,0) + [100,101) | 2020-01-01 | 2021-01-01 | [6,7) +(3 rows) + +-- non-FPO update: +UPDATE temporal_per SET id = '[7,8)' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +-----------+------------+------------+----------- + [100,101) | 2018-01-01 | 2019-01-01 | [-1,0) + [100,101) | 2019-01-01 | 2020-01-01 | [-1,0) + [100,101) | 2020-01-01 | 2021-01-01 | [-1,0) +(3 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)'); +UPDATE temporal_per SET id = '[9,10)' WHERE id = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk_per2per WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +-----------+------------+------------+----------- + [200,201) | 2018-01-01 | 2020-01-01 | [-1,0) + [200,201) | 2020-01-01 | 2021-01-01 | [8,9) +(2 rows) + +-- +-- test FK referenced deletes SET DEFAULT +-- +TRUNCATE temporal_per, temporal_fk_per2per; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[-1,-1]', null, null); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)'); +-- leftovers on both sides: +DELETE FROM temporal_per FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +-----------+------------+------------+----------- + [100,101) | 2018-01-01 | 2019-01-01 | [6,7) + [100,101) | 2019-01-01 | 2020-01-01 | [-1,0) + [100,101) | 2020-01-01 | 2021-01-01 | [6,7) +(3 rows) + +-- non-FPO update: +DELETE FROM temporal_per WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +-----------+------------+------------+----------- + [100,101) | 2018-01-01 | 2019-01-01 | [-1,0) + [100,101) | 2019-01-01 | 2020-01-01 | [-1,0) + [100,101) | 2020-01-01 | 2021-01-01 | [-1,0) +(3 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)'); +DELETE FROM temporal_per WHERE id = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk_per2per WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +-----------+------------+------------+----------- + [200,201) | 2018-01-01 | 2020-01-01 | [-1,0) + [200,201) | 2020-01-01 | 2021-01-01 | [8,9) +(2 rows) + +-- +-- test FK referenced updates CASCADE (two scalar cols) +-- +TRUNCATE temporal_per2, temporal_fk2_per2per; +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[6,7)', '[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_per2per (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)', '[6,7)'); +ALTER TABLE temporal_fk2_per2per + DROP CONSTRAINT temporal_fk2_per2per_fk, + ADD CONSTRAINT temporal_fk2_per2per_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_per2 + ON DELETE CASCADE ON UPDATE CASCADE; +-- leftovers on both sides: +UPDATE temporal_per2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' SET id1 = '[7,8)' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [100,101) | 2018-01-01 | 2019-01-01 | [6,7) | [6,7) + [100,101) | 2019-01-01 | 2020-01-01 | [7,8) | [6,7) + [100,101) | 2020-01-01 | 2021-01-01 | [6,7) | [6,7) +(3 rows) + +-- non-FPO update: +UPDATE temporal_per2 SET id1 = '[7,8)' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [100,101) | 2018-01-01 | 2019-01-01 | [7,8) | [6,7) + [100,101) | 2019-01-01 | 2020-01-01 | [7,8) | [6,7) + [100,101) | 2020-01-01 | 2021-01-01 | [7,8) | [6,7) +(3 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_per2per (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)', '[8,9)'); +UPDATE temporal_per2 SET id1 = '[9,10)' WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_per2per WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [200,201) | 2018-01-01 | 2020-01-01 | [9,10) | [8,9) + [200,201) | 2020-01-01 | 2021-01-01 | [8,9) | [8,9) +(2 rows) + +-- +-- test FK referenced deletes CASCADE (two scalar cols) +-- +TRUNCATE temporal_per2, temporal_fk2_per2per; +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[6,7)', '[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_per2per (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)', '[6,7)'); +-- leftovers on both sides: +DELETE FROM temporal_per2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [100,101) | 2018-01-01 | 2019-01-01 | [6,7) | [6,7) + [100,101) | 2020-01-01 | 2021-01-01 | [6,7) | [6,7) +(2 rows) + +-- non-FPO delete: +DELETE FROM temporal_per2 WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +----+------------+-----------+------------+------------ +(0 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_per2per (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)', '[8,9)'); +DELETE FROM temporal_per2 WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_per2per WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [200,201) | 2020-01-01 | 2021-01-01 | [8,9) | [8,9) +(1 row) + +-- +-- test FK referenced updates SET NULL (two scalar cols) +-- +TRUNCATE temporal_per2, temporal_fk2_per2per; +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[6,7)', '[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_per2per (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)', '[6,7)'); +ALTER TABLE temporal_fk2_per2per + DROP CONSTRAINT temporal_fk2_per2per_fk, + ADD CONSTRAINT temporal_fk2_per2per_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_per2 + ON DELETE SET NULL ON UPDATE SET NULL; +-- leftovers on both sides: +UPDATE temporal_per2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' SET id1 = '[7,8)' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [100,101) | 2018-01-01 | 2019-01-01 | [6,7) | [6,7) + [100,101) | 2019-01-01 | 2020-01-01 | | + [100,101) | 2020-01-01 | 2021-01-01 | [6,7) | [6,7) +(3 rows) + +-- non-FPO update: +UPDATE temporal_per2 SET id1 = '[7,8)' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [100,101) | 2018-01-01 | 2019-01-01 | | + [100,101) | 2019-01-01 | 2020-01-01 | | + [100,101) | 2020-01-01 | 2021-01-01 | | +(3 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_per2per (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)', '[8,9)'); +UPDATE temporal_per2 SET id1 = '[9,10)' WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_per2per WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [200,201) | 2018-01-01 | 2020-01-01 | | + [200,201) | 2020-01-01 | 2021-01-01 | [8,9) | [8,9) +(2 rows) + +-- +-- test FK referenced deletes SET NULL (two scalar cols) +-- +TRUNCATE temporal_per2, temporal_fk2_per2per; +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[6,7)', '[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_per2per (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)', '[6,7)'); +-- leftovers on both sides: +DELETE FROM temporal_per2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [100,101) | 2018-01-01 | 2019-01-01 | [6,7) | [6,7) + [100,101) | 2019-01-01 | 2020-01-01 | | + [100,101) | 2020-01-01 | 2021-01-01 | [6,7) | [6,7) +(3 rows) + +-- non-FPO delete: +DELETE FROM temporal_per2 WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [100,101) | 2018-01-01 | 2019-01-01 | | + [100,101) | 2019-01-01 | 2020-01-01 | | + [100,101) | 2020-01-01 | 2021-01-01 | | +(3 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_per2per (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)', '[8,9)'); +DELETE FROM temporal_per2 WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_per2per WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [200,201) | 2018-01-01 | 2020-01-01 | | + [200,201) | 2020-01-01 | 2021-01-01 | [8,9) | [8,9) +(2 rows) + +-- +-- test FK referenced deletes SET NULL (two scalar cols, SET NULL subset) +-- +TRUNCATE temporal_per2, temporal_fk2_per2per; +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[6,7)', '[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_per2per (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)', '[6,7)'); +-- fails because you can't set the PERIOD column: +ALTER TABLE temporal_fk2_per2per + DROP CONSTRAINT temporal_fk2_per2per_fk, + ADD CONSTRAINT temporal_fk2_per2per_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_per2 + ON DELETE SET NULL (valid_at) ON UPDATE SET NULL; +ERROR: column "valid_at" referenced in ON DELETE SET action cannot be PERIOD +-- ok: +ALTER TABLE temporal_fk2_per2per + DROP CONSTRAINT temporal_fk2_per2per_fk, + ADD CONSTRAINT temporal_fk2_per2per_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_per2 + ON DELETE SET NULL (parent_id1) ON UPDATE SET NULL; +-- leftovers on both sides: +DELETE FROM temporal_per2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [100,101) | 2018-01-01 | 2019-01-01 | [6,7) | [6,7) + [100,101) | 2019-01-01 | 2020-01-01 | | [6,7) + [100,101) | 2020-01-01 | 2021-01-01 | [6,7) | [6,7) +(3 rows) + +-- non-FPO delete: +DELETE FROM temporal_per2 WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [100,101) | 2018-01-01 | 2019-01-01 | | [6,7) + [100,101) | 2019-01-01 | 2020-01-01 | | [6,7) + [100,101) | 2020-01-01 | 2021-01-01 | | [6,7) +(3 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_per2per (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)', '[8,9)'); +DELETE FROM temporal_per2 WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_per2per WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [200,201) | 2018-01-01 | 2020-01-01 | | [8,9) + [200,201) | 2020-01-01 | 2021-01-01 | [8,9) | [8,9) +(2 rows) + +-- +-- test FK referenced updates SET DEFAULT (two scalar cols) +-- +TRUNCATE temporal_per2, temporal_fk2_per2per; +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[-1,-1]', '[-1,-1]', null, null); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[6,7)', '[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_per2per (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)', '[6,7)'); +ALTER TABLE temporal_fk2_per2per + ALTER COLUMN parent_id1 SET DEFAULT '[-1,-1]', + ALTER COLUMN parent_id2 SET DEFAULT '[-1,-1]', + DROP CONSTRAINT temporal_fk2_per2per_fk, + ADD CONSTRAINT temporal_fk2_per2per_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_per2 + ON DELETE SET DEFAULT ON UPDATE SET DEFAULT; +-- leftovers on both sides: +UPDATE temporal_per2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' SET id1 = '[7,8)', id2 = '[7,8)' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [100,101) | 2018-01-01 | 2019-01-01 | [6,7) | [6,7) + [100,101) | 2019-01-01 | 2020-01-01 | [-1,0) | [-1,0) + [100,101) | 2020-01-01 | 2021-01-01 | [6,7) | [6,7) +(3 rows) + +-- non-FPO update: +UPDATE temporal_per2 SET id1 = '[7,8)', id2 = '[7,8)' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [100,101) | 2018-01-01 | 2019-01-01 | [-1,0) | [-1,0) + [100,101) | 2019-01-01 | 2020-01-01 | [-1,0) | [-1,0) + [100,101) | 2020-01-01 | 2021-01-01 | [-1,0) | [-1,0) +(3 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_per2per (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)', '[8,9)'); +UPDATE temporal_per2 SET id1 = '[9,10)', id2 = '[9,10)' WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_per2per WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [200,201) | 2018-01-01 | 2020-01-01 | [-1,0) | [-1,0) + [200,201) | 2020-01-01 | 2021-01-01 | [8,9) | [8,9) +(2 rows) + +-- +-- test FK referenced deletes SET DEFAULT (two scalar cols) +-- +TRUNCATE temporal_per2, temporal_fk2_per2per; +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[-1,-1]', '[-1,-1]', null, null); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[6,7)', '[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_per2per (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)', '[6,7)'); +-- leftovers on both sides: +DELETE FROM temporal_per2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [100,101) | 2018-01-01 | 2019-01-01 | [6,7) | [6,7) + [100,101) | 2019-01-01 | 2020-01-01 | [-1,0) | [-1,0) + [100,101) | 2020-01-01 | 2021-01-01 | [6,7) | [6,7) +(3 rows) + +-- non-FPO update: +DELETE FROM temporal_per2 WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [100,101) | 2018-01-01 | 2019-01-01 | [-1,0) | [-1,0) + [100,101) | 2019-01-01 | 2020-01-01 | [-1,0) | [-1,0) + [100,101) | 2020-01-01 | 2021-01-01 | [-1,0) | [-1,0) +(3 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_per2per (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)', '[8,9)'); +DELETE FROM temporal_per2 WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_per2per WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [200,201) | 2018-01-01 | 2020-01-01 | [-1,0) | [-1,0) + [200,201) | 2020-01-01 | 2021-01-01 | [8,9) | [8,9) +(2 rows) + +-- +-- test FK referenced deletes SET DEFAULT (two scalar cols, SET DEFAULT subset) +-- +TRUNCATE temporal_per2, temporal_fk2_per2per; +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[-1,-1]', '[6,7)', null, null); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[6,7)', '[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_per2per (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)', '[6,7)'); +-- fails because you can't set the PERIOD column: +ALTER TABLE temporal_fk2_per2per + ALTER COLUMN parent_id1 SET DEFAULT '[-1,-1]', + DROP CONSTRAINT temporal_fk2_per2per_fk, + ADD CONSTRAINT temporal_fk2_per2per_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_per2 + ON DELETE SET DEFAULT (valid_at) ON UPDATE SET DEFAULT; +ERROR: column "valid_at" referenced in ON DELETE SET action cannot be PERIOD +-- ok: +ALTER TABLE temporal_fk2_per2per + ALTER COLUMN parent_id1 SET DEFAULT '[-1,-1]', + DROP CONSTRAINT temporal_fk2_per2per_fk, + ADD CONSTRAINT temporal_fk2_per2per_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_per2 + ON DELETE SET DEFAULT (parent_id1) ON UPDATE SET DEFAULT; +-- leftovers on both sides: +DELETE FROM temporal_per2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [100,101) | 2018-01-01 | 2019-01-01 | [6,7) | [6,7) + [100,101) | 2019-01-01 | 2020-01-01 | [-1,0) | [6,7) + [100,101) | 2020-01-01 | 2021-01-01 | [6,7) | [6,7) +(3 rows) + +-- non-FPO update: +DELETE FROM temporal_per2 WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [100,101) | 2018-01-01 | 2019-01-01 | [-1,0) | [6,7) + [100,101) | 2019-01-01 | 2020-01-01 | [-1,0) | [6,7) + [100,101) | 2020-01-01 | 2021-01-01 | [-1,0) | [6,7) +(3 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[-1,-1]', '[8,9)', null, null); +INSERT INTO temporal_fk2_per2per (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)', '[8,9)'); +DELETE FROM temporal_per2 WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_per2per WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [200,201) | 2018-01-01 | 2020-01-01 | [-1,0) | [8,9) + [200,201) | 2020-01-01 | 2021-01-01 | [8,9) | [8,9) +(2 rows) + +-- +-- test FOREIGN KEY, range references PERIOD +-- +-- test table setup +DROP TABLE temporal_per CASCADE; +NOTICE: drop cascades to constraint temporal_fk_per2per_fk on table temporal_fk_per2per +CREATE TABLE temporal_per ( + id int4range, + valid_from date, + valid_til date, + PERIOD FOR valid_at (valid_from, valid_til) +); +ALTER TABLE temporal_per + ADD CONSTRAINT temporal_per_pk + PRIMARY KEY (id, valid_at WITHOUT OVERLAPS); +-- Can't create a FK with a mismatched range type +CREATE TABLE temporal_fk_rng2per ( + id int4range, + valid_at int4range, + parent_id int4range, + CONSTRAINT temporal_fk_rng2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_rng2per_fk FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per (id, PERIOD valid_at) +); +ERROR: foreign key constraint "temporal_fk_rng2per_fk" cannot be implemented +DETAIL: Key columns "valid_at" of the referencing table and "valid_at" of the referenced table are of incompatible types: int4range and daterange. +-- works: PERIOD for referenced, range for referencing +CREATE TABLE temporal_fk_rng2per ( + id int4range, + valid_at daterange, + parent_id int4range, + CONSTRAINT temporal_fk_rng2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_rng2per_fk FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per (id, PERIOD valid_at) +); +\d temporal_fk_rng2per + Table "public.temporal_fk_rng2per" + Column | Type | Collation | Nullable | Default +-----------+-----------+-----------+----------+--------- + id | int4range | | not null | + valid_at | daterange | | not null | + parent_id | int4range | | | +Indexes: + "temporal_fk_rng2per_pk" PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +Foreign-key constraints: + "temporal_fk_rng2per_fk" FOREIGN KEY (parent_id, PERIOD valid_at) REFERENCES temporal_per(id, PERIOD valid_at) + +DROP TABLE temporal_fk_rng2per; +-- with mismatched PERIOD columns: +-- (parent_id, PERIOD valid_at) REFERENCES (id, valid_at) +-- REFERENCES part should specify PERIOD +CREATE TABLE temporal_fk_rng2per ( + id int4range, + valid_at daterange, + parent_id int4range, + CONSTRAINT temporal_fk_rng2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_rng2per_fk FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per (id, valid_at) +); +ERROR: foreign key uses PERIOD on the referencing table but not the referenced table +-- (parent_id, valid_at) REFERENCES (id, valid_at) +-- both should specify PERIOD: +CREATE TABLE temporal_fk_rng2per ( + id int4range, + valid_at daterange, + parent_id int4range, + CONSTRAINT temporal_fk_rng2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_rng2per_fk FOREIGN KEY (parent_id, valid_at) + REFERENCES temporal_per (id, valid_at) +); +ERROR: foreign key must use PERIOD when referencing a primary key using WITHOUT OVERLAPS +-- (parent_id, valid_at) REFERENCES (id, PERIOD valid_at) +-- FOREIGN KEY part should specify PERIOD +CREATE TABLE temporal_fk_rng2per ( + id int4range, + valid_at daterange, + parent_id int4range, + CONSTRAINT temporal_fk_rng2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_rng2per_fk FOREIGN KEY (parent_id, valid_at) + REFERENCES temporal_per (id, PERIOD valid_at) +); +ERROR: foreign key uses PERIOD on the referenced table but not the referencing table +-- (parent_id, valid_at) REFERENCES [implicit] +-- FOREIGN KEY part should specify PERIOD +CREATE TABLE temporal_fk_rng2per ( + id int4range, + valid_at daterange, + parent_id int4range, + CONSTRAINT temporal_fk_rng2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_rng2per_fk FOREIGN KEY (parent_id, valid_at) + REFERENCES temporal_per +); +ERROR: foreign key uses PERIOD on the referenced table but not the referencing table +-- (parent_id, PERIOD valid_at) REFERENCES (id) +CREATE TABLE temporal_fk_rng2per ( + id int4range, + valid_at daterange, + parent_id int4range, + CONSTRAINT temporal_fk_rng2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_rng2per_fk FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per (id) +); +ERROR: foreign key uses PERIOD on the referencing table but not the referenced table +-- (parent_id) REFERENCES (id, PERIOD valid_at) +CREATE TABLE temporal_fk_rng2per ( + id int4range, + valid_at daterange, + parent_id int4range, + CONSTRAINT temporal_fk_rng2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_rng2per_fk FOREIGN KEY (parent_id) + REFERENCES temporal_per (id, PERIOD valid_at) +); +ERROR: foreign key uses PERIOD on the referenced table but not the referencing table +-- with inferred PK on the referenced table: +-- (parent_id, PERIOD valid_at) REFERENCES [implicit] +CREATE TABLE temporal_fk_rng2per ( + id int4range, + valid_at daterange, + parent_id int4range, + CONSTRAINT temporal_fk_rng2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_rng2per_fk FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per +); +DROP TABLE temporal_fk_rng2per; +-- (parent_id) REFERENCES [implicit] +CREATE TABLE temporal_fk_rng2per ( + id int4range, + valid_at daterange, + parent_id int4range, + CONSTRAINT temporal_fk_rng2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_rng2per_fk FOREIGN KEY (parent_id) + REFERENCES temporal_per +); +ERROR: foreign key uses PERIOD on the referenced table but not the referencing table +-- should fail because of duplicate referenced columns: +CREATE TABLE temporal_fk_rng2per ( + id int4range, + valid_at daterange, + parent_id int4range, + CONSTRAINT temporal_fk_rng2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_rng2per_fk FOREIGN KEY (parent_id, PERIOD parent_id) + REFERENCES temporal_per (id, PERIOD id) +); +ERROR: foreign key referenced-columns list must not contain duplicates +-- Two scalar columns +DROP TABLE temporal_per2 CASCADE; +NOTICE: drop cascades to constraint temporal_fk2_per2per_fk on table temporal_fk2_per2per +CREATE TABLE temporal_per2 ( + id1 int4range, + id2 int4range, + valid_from date, + valid_til date, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_per2_pk PRIMARY KEY (id1, id2, valid_at WITHOUT OVERLAPS) +); +CREATE TABLE temporal_fk2_rng2per ( + id int4range, + valid_at daterange, + parent_id1 int4range, + parent_id2 int4range, + CONSTRAINT temporal_fk2_rng2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk2_rng2per_fk FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_per2 (id1, id2, PERIOD valid_at) +); +\d temporal_fk2_rng2per + Table "public.temporal_fk2_rng2per" + Column | Type | Collation | Nullable | Default +------------+-----------+-----------+----------+--------- + id | int4range | | not null | + valid_at | daterange | | not null | + parent_id1 | int4range | | | + parent_id2 | int4range | | | +Indexes: + "temporal_fk2_rng2per_pk" PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +Foreign-key constraints: + "temporal_fk2_rng2per_fk" FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) REFERENCES temporal_per2(id1, id2, PERIOD valid_at) + +DROP TABLE temporal_fk2_rng2per; +-- +-- test ALTER TABLE ADD CONSTRAINT +-- +CREATE TABLE temporal_fk_rng2per ( + id int4range, + valid_at daterange, + parent_id int4range, + CONSTRAINT temporal_fk_rng2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +); +ALTER TABLE temporal_fk_rng2per + ADD CONSTRAINT temporal_fk_rng2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per (id, PERIOD valid_at); +-- Two scalar columns: +CREATE TABLE temporal_fk2_rng2per ( + id int4range, + valid_at daterange, + parent_id1 int4range, + parent_id2 int4range, + CONSTRAINT temporal_fk2_rng2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +); +ALTER TABLE temporal_fk2_rng2per + ADD CONSTRAINT temporal_fk2_rng2per_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_per2 (id1, id2, PERIOD valid_at); +\d temporal_fk2_rng2per + Table "public.temporal_fk2_rng2per" + Column | Type | Collation | Nullable | Default +------------+-----------+-----------+----------+--------- + id | int4range | | not null | + valid_at | daterange | | not null | + parent_id1 | int4range | | | + parent_id2 | int4range | | | +Indexes: + "temporal_fk2_rng2per_pk" PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +Foreign-key constraints: + "temporal_fk2_rng2per_fk" FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) REFERENCES temporal_per2(id1, id2, PERIOD valid_at) + +-- with inferred PK on the referenced table, and wrong column type: +ALTER TABLE temporal_fk_rng2per + DROP CONSTRAINT temporal_fk_rng2per_fk, + ALTER COLUMN valid_at TYPE tsrange USING tsrange(lower(valid_at), upper(valid_at)); +ALTER TABLE temporal_fk_rng2per + ADD CONSTRAINT temporal_fk_rng2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per; +ERROR: foreign key constraint "temporal_fk_rng2per_fk" cannot be implemented +DETAIL: Key columns "valid_at" of the referencing table and "valid_at" of the referenced table are of incompatible types: tsrange and daterange. +ALTER TABLE temporal_fk_rng2per + ALTER COLUMN valid_at TYPE daterange USING daterange(lower(valid_at)::date, upper(valid_at)::date); +-- with inferred PK on the referenced table: +ALTER TABLE temporal_fk_rng2per + ADD CONSTRAINT temporal_fk_rng2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per; +-- should fail because of duplicate referenced columns: +ALTER TABLE temporal_fk_rng2per + ADD CONSTRAINT temporal_fk_rng2per_fk2 + FOREIGN KEY (parent_id, PERIOD parent_id) + REFERENCES temporal_per (id, PERIOD id); +ERROR: foreign key referenced-columns list must not contain duplicates +-- +-- test with rows already +-- +DELETE FROM temporal_fk_rng2per; +DELETE FROM temporal_per; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES + ('[1,2)', '2018-01-02', '2018-02-03'), + ('[1,2)', '2018-03-03', '2018-04-04'), + ('[2,3)', '2018-01-01', '2018-01-05'), + ('[3,4)', '2018-01-01', NULL); +ALTER TABLE temporal_fk_rng2per + DROP CONSTRAINT temporal_fk_rng2per_fk; +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES ('[1,2)', '[2018-01-02,2018-02-01)', '[1,2)'); +ALTER TABLE temporal_fk_rng2per + ADD CONSTRAINT temporal_fk_rng2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per; +ALTER TABLE temporal_fk_rng2per + DROP CONSTRAINT temporal_fk_rng2per_fk; +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES ('[2,3)', '[2018-01-02,2018-04-01)', '[1,2)'); +-- should fail: +ALTER TABLE temporal_fk_rng2per + ADD CONSTRAINT temporal_fk_rng2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per; +ERROR: insert or update on table "temporal_fk_rng2per" violates foreign key constraint "temporal_fk_rng2per_fk" +DETAIL: Key (parent_id, valid_at)=([1,2), [2018-01-02,2018-04-01)) is not present in table "temporal_per". +-- okay again: +DELETE FROM temporal_fk_rng2per; +ALTER TABLE temporal_fk_rng2per + ADD CONSTRAINT temporal_fk_rng2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per; +-- +-- test pg_get_constraintdef +-- +SELECT pg_get_constraintdef(oid) FROM pg_constraint WHERE conname = 'temporal_fk_rng2per_fk'; + pg_get_constraintdef +--------------------------------------------------------------------------------------- + FOREIGN KEY (parent_id, PERIOD valid_at) REFERENCES temporal_per(id, PERIOD valid_at) +(1 row) + +-- +-- test FK referencing inserts +-- +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES ('[1,2)', '[2018-01-02,2018-02-01)', '[1,2)'); +-- should fail: +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES ('[2,3)', '[2018-01-02,2018-04-01)', '[1,2)'); +ERROR: insert or update on table "temporal_fk_rng2per" violates foreign key constraint "temporal_fk_rng2per_fk" +DETAIL: Key (parent_id, valid_at)=([1,2), [2018-01-02,2018-04-01)) is not present in table "temporal_per". +-- now it should work: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2018-02-03', '2018-03-03'); +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES ('[2,3)', '[2018-01-02,2018-04-01)', '[1,2)'); +-- +-- test FK referencing updates +-- +-- slide the edge across a referenced transition: +UPDATE temporal_fk_rng2per SET valid_at = '[2018-01-02,2018-02-20)' WHERE id = '[1,2)'; +-- should fail: +UPDATE temporal_fk_rng2per SET valid_at = '[2018-01-02,2018-05-01)' WHERE id = '[1,2)'; +ERROR: insert or update on table "temporal_fk_rng2per" violates foreign key constraint "temporal_fk_rng2per_fk" +DETAIL: Key (parent_id, valid_at)=([1,2), [2018-01-02,2018-05-01)) is not present in table "temporal_per". +UPDATE temporal_fk_rng2per SET parent_id = '[8,9)' WHERE id = '[1,2)'; +ERROR: insert or update on table "temporal_fk_rng2per" violates foreign key constraint "temporal_fk_rng2per_fk" +DETAIL: Key (parent_id, valid_at)=([8,9), [2018-01-02,2018-02-20)) is not present in table "temporal_per". +-- ALTER FK DEFERRABLE +BEGIN; + INSERT INTO temporal_per (id, valid_from, valid_til) VALUES + ('[5,6)', '2018-01-01', '2018-02-01'), + ('[5,6)', '2018-02-01', '2018-03-01'); + INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES + ('[3,4)', '[2018-01-05,2018-01-10)', '[5,6)'); + ALTER TABLE temporal_fk_rng2per + ALTER CONSTRAINT temporal_fk_rng2per_fk + DEFERRABLE INITIALLY DEFERRED; + DELETE FROM temporal_per WHERE id = '[5,6)'; --should not fail yet. +COMMIT; -- should fail here. +ERROR: update or delete on table "temporal_per" violates foreign key constraint "temporal_fk_rng2per_fk" on table "temporal_fk_rng2per" +DETAIL: Key (id, valid_at)=([5,6), [2018-01-01,2018-02-01)) is still referenced from table "temporal_fk_rng2per". +-- +-- test FK referenced updates NO ACTION +-- +TRUNCATE temporal_per, temporal_fk_rng2per; +ALTER TABLE temporal_fk_rng2per + DROP CONSTRAINT temporal_fk_rng2per_fk; +ALTER TABLE temporal_fk_rng2per + ADD CONSTRAINT temporal_fk_rng2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per + ON UPDATE NO ACTION; +-- a PK update that succeeds because the numeric id isn't referenced: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[5,6)', '2018-01-01', '2018-02-01'); +UPDATE temporal_per SET valid_from = '2016-01-01', valid_til = '2016-02-01' WHERE id = '[5,6)'; +-- a PK update that succeeds even though the numeric id is referenced because the range isn't: +DELETE FROM temporal_per WHERE id = '[5,6)'; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES + ('[5,6)', '2018-01-01', '2018-02-01'), + ('[5,6)', '2018-02-01', '2018-03-01'); +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES + ('[3,4)', '[2018-01-05,2018-01-10)', '[5,6)'); +UPDATE temporal_per SET valid_from = '2016-02-01', valid_til = '2016-03-01' +WHERE id = '[5,6)' AND valid_from = '2018-02-01' AND valid_til = '2018-03-01'; +-- A PK update sliding the edge between two referenced rows: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES + ('[6,7)', '2018-01-01', '2018-02-01'), + ('[6,7)', '2018-02-01', '2018-03-01'); +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES + ('[4,5)', '[2018-01-15,2018-02-15)', '[6,7)'); +UPDATE temporal_per +SET valid_from = CASE WHEN valid_from = '2018-01-01' THEN '2018-01-01' + WHEN valid_from = '2018-02-01' THEN '2018-01-05' END::date, + valid_til = CASE WHEN valid_from = '2018-01-01' THEN '2018-01-05' + WHEN valid_from = '2018-02-01' THEN '2018-03-01' END::date +WHERE id = '[6,7)'; +-- a PK update shrinking the referenced range but still valid: +-- There are two references: one fulfilled by the first pk row, +-- the other fulfilled by both pk rows combined. +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES + ('[1,2)', '2018-01-01', '2018-03-01'), + ('[1,2)', '2018-03-01', '2018-06-01'); +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES + ('[1,2)', '[2018-01-15,2018-02-01)', '[1,2)'), + ('[2,3)', '[2018-01-15,2018-05-01)', '[1,2)'); +UPDATE temporal_per SET valid_from = '2018-01-15', valid_til = '2018-03-01' +WHERE id = '[1,2)' AND valid_at @> '2018-01-15'::date; +-- a PK update growing the referenced range is fine: +UPDATE temporal_per SET valid_from = '2018-01-01', valid_til = '2018-03-01' +WHERE id = '[1,2)' AND valid_at @> '2018-01-25'::date; +-- a PK update shrinking the referenced range and changing the id invalidates the whole range: +UPDATE temporal_per SET id = '[2,3)', valid_from = '2018-01-15', valid_til = '2018-03-01' +WHERE id = '[1,2)' AND valid_at @> '2018-01-15'::date; +ERROR: update or delete on table "temporal_per" violates foreign key constraint "temporal_fk_rng2per_fk" on table "temporal_fk_rng2per" +DETAIL: Key (id, valid_at)=([1,2), [2018-01-01,2018-03-01)) is still referenced from table "temporal_fk_rng2per". +-- a PK update changing only the id invalidates the whole range: +UPDATE temporal_per SET id = '[2,3)' +WHERE id = '[1,2)' AND valid_at @> '2018-01-15'::date; +ERROR: update or delete on table "temporal_per" violates foreign key constraint "temporal_fk_rng2per_fk" on table "temporal_fk_rng2per" +DETAIL: Key (id, valid_at)=([1,2), [2018-01-01,2018-03-01)) is still referenced from table "temporal_fk_rng2per". +-- a PK update that loses time from both ends, but is still valid: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES + ('[2,3)', '2018-01-01', '2018-03-01'); +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES + ('[5,6)', '[2018-01-15,2018-02-01)', '[2,3)'); +UPDATE temporal_per SET valid_from = '2018-01-15', valid_til = '2018-02-15' +WHERE id = '[2,3)'; +-- a PK update that fails because both are referenced: +UPDATE temporal_per SET valid_from = '2016-01-01', valid_til = '2016-02-01' +WHERE id = '[5,6)' AND valid_from = '2018-01-01' AND valid_til = '2018-02-01'; +ERROR: update or delete on table "temporal_per" violates foreign key constraint "temporal_fk_rng2per_fk" on table "temporal_fk_rng2per" +DETAIL: Key (id, valid_at)=([5,6), [2018-01-01,2018-02-01)) is still referenced from table "temporal_fk_rng2per". +-- a PK update that fails because both are referenced, but not 'til commit: +BEGIN; + ALTER TABLE temporal_fk_rng2per + ALTER CONSTRAINT temporal_fk_rng2per_fk + DEFERRABLE INITIALLY DEFERRED; + UPDATE temporal_per SET valid_from = '2016-01-01', valid_til = '2016-02-01' + WHERE id = '[5,6)' AND valid_at = daterange('2018-01-01', '2018-02-01'); +COMMIT; +ERROR: update or delete on table "temporal_per" violates foreign key constraint "temporal_fk_rng2per_fk" on table "temporal_fk_rng2per" +DETAIL: Key (id, valid_at)=([5,6), [2018-01-01,2018-02-01)) is still referenced from table "temporal_fk_rng2per". +-- changing the scalar part fails: +UPDATE temporal_per SET id = '[7,8)' +WHERE id = '[5,6)' AND valid_from = '2018-01-01' AND valid_til = '2018-02-01'; +ERROR: update or delete on table "temporal_per" violates foreign key constraint "temporal_fk_rng2per_fk" on table "temporal_fk_rng2per" +DETAIL: Key (id, valid_at)=([5,6), [2018-01-01,2018-02-01)) is still referenced from table "temporal_fk_rng2per". +-- changing an unreferenced part is okay: +UPDATE temporal_per +FOR PORTION OF valid_at FROM '2018-01-02' TO '2018-01-03' +SET id = '[7,8)' +WHERE id = '[5,6)'; +-- changing just a part fails: +UPDATE temporal_per +FOR PORTION OF valid_at FROM '2018-01-05' TO '2018-01-10' +SET id = '[7,8)' +WHERE id = '[5,6)'; +ERROR: update or delete on table "temporal_per" violates foreign key constraint "temporal_fk_rng2per_fk" on table "temporal_fk_rng2per" +DETAIL: Key (id, valid_at)=([5,6), [2018-01-03,2018-02-01)) is still referenced from table "temporal_fk_rng2per". +SELECT * FROM temporal_per WHERE id in ('[5,6)', '[7,8)') ORDER BY id, valid_at; + id | valid_from | valid_til +-------+------------+------------ + [5,6) | 2016-02-01 | 2016-03-01 + [5,6) | 2018-01-01 | 2018-01-02 + [5,6) | 2018-01-03 | 2018-02-01 + [7,8) | 2018-01-02 | 2018-01-03 +(4 rows) + +SELECT * FROM temporal_fk_rng2per WHERE id in ('[3,4)') ORDER BY id, valid_at; + id | valid_at | parent_id +-------+-------------------------+----------- + [3,4) | [2018-01-05,2018-01-10) | [5,6) +(1 row) + +-- then delete the objecting FK record and the same PK update succeeds: +DELETE FROM temporal_fk_rng2per WHERE id = '[3,4)'; +UPDATE temporal_per SET valid_from = '2016-01-01', valid_til = '2016-02-01' +WHERE id = '[5,6)' AND valid_at = '[2018-01-01,2018-02-01)'; +-- +-- test FK referenced updates RESTRICT +-- +TRUNCATE temporal_per, temporal_fk_rng2per; +ALTER TABLE temporal_fk_rng2per + DROP CONSTRAINT temporal_fk_rng2per_fk; +ALTER TABLE temporal_fk_rng2per + ADD CONSTRAINT temporal_fk_rng2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per + ON UPDATE RESTRICT; +ERROR: unsupported ON UPDATE action for foreign key constraint using PERIOD +-- +-- test FK referenced deletes NO ACTION +-- +TRUNCATE temporal_per, temporal_fk_rng2per; +ALTER TABLE temporal_fk_rng2per + ADD CONSTRAINT temporal_fk_rng2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per; +-- a PK delete that succeeds because the numeric id isn't referenced: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[5,6)', '2018-01-01', '2018-02-01'); +DELETE FROM temporal_per WHERE id = '[5,6)'; +-- a PK delete that succeeds even though the numeric id is referenced because the range isn't: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES + ('[5,6)', '2018-01-01', '2018-02-01'), + ('[5,6)', '2018-02-01', '2018-03-01'); +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES ('[3,4)', '[2018-01-05,2018-01-10)', '[5,6)'); +DELETE FROM temporal_per WHERE id = '[5,6)' AND valid_from = '2018-02-01' AND valid_til = '2018-03-01'; +-- a PK delete that fails because both are referenced: +DELETE FROM temporal_per WHERE id = '[5,6)' AND valid_from = '2018-01-01' AND valid_til = '2018-02-01'; +ERROR: update or delete on table "temporal_per" violates foreign key constraint "temporal_fk_rng2per_fk" on table "temporal_fk_rng2per" +DETAIL: Key (id, valid_at)=([5,6), [2018-01-01,2018-02-01)) is still referenced from table "temporal_fk_rng2per". +-- a PK delete that fails because both are referenced, but not 'til commit: +BEGIN; + ALTER TABLE temporal_fk_rng2per + ALTER CONSTRAINT temporal_fk_rng2per_fk + DEFERRABLE INITIALLY DEFERRED; + DELETE FROM temporal_per WHERE id = '[5,6)' AND valid_at = daterange('2018-01-01', '2018-02-01'); +COMMIT; +ERROR: update or delete on table "temporal_per" violates foreign key constraint "temporal_fk_rng2per_fk" on table "temporal_fk_rng2per" +DETAIL: Key (id, valid_at)=([5,6), [2018-01-01,2018-02-01)) is still referenced from table "temporal_fk_rng2per". +-- deleting an unreferenced part is okay: +DELETE FROM temporal_per +FOR PORTION OF valid_at FROM '2018-01-02' TO '2018-01-03' +WHERE id = '[5,6)'; +-- deleting just a part fails: +DELETE FROM temporal_per +FOR PORTION OF valid_at FROM '2018-01-05' TO '2018-01-10' +WHERE id = '[5,6)'; +ERROR: update or delete on table "temporal_per" violates foreign key constraint "temporal_fk_rng2per_fk" on table "temporal_fk_rng2per" +DETAIL: Key (id, valid_at)=([5,6), [2018-01-03,2018-02-01)) is still referenced from table "temporal_fk_rng2per". +SELECT * FROM temporal_per WHERE id in ('[5,6)', '[7,8)') ORDER BY id, valid_at; + id | valid_from | valid_til +-------+------------+------------ + [5,6) | 2018-01-01 | 2018-01-02 + [5,6) | 2018-01-03 | 2018-02-01 +(2 rows) + +SELECT * FROM temporal_fk_rng2per WHERE id in ('[3,4)') ORDER BY id, valid_at; + id | valid_at | parent_id +-------+-------------------------+----------- + [3,4) | [2018-01-05,2018-01-10) | [5,6) +(1 row) + +-- then delete the objecting FK record and the same PK delete succeeds: +DELETE FROM temporal_fk_rng2per WHERE id = '[3,4)'; +DELETE FROM temporal_per WHERE id = '[5,6)' AND valid_from = '2018-01-01' AND valid_til = '2018-02-01'; +-- +-- test FK referenced deletes RESTRICT +-- +TRUNCATE temporal_per, temporal_fk_rng2per; +ALTER TABLE temporal_fk_rng2per + DROP CONSTRAINT temporal_fk_rng2per_fk; +ALTER TABLE temporal_fk_rng2per + ADD CONSTRAINT temporal_fk_rng2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per + ON DELETE RESTRICT; +ERROR: unsupported ON DELETE action for foreign key constraint using PERIOD +-- +-- rng2per test ON UPDATE/DELETE options +-- +-- TOC: +-- referenced updates CASCADE +-- referenced deletes CASCADE +-- referenced updates SET NULL +-- referenced deletes SET NULL +-- referenced updates SET DEFAULT +-- referenced deletes SET DEFAULT +-- referenced updates CASCADE (two scalar cols) +-- referenced deletes CASCADE (two scalar cols) +-- referenced updates SET NULL (two scalar cols) +-- referenced deletes SET NULL (two scalar cols) +-- referenced deletes SET NULL (two scalar cols, SET NULL subset) +-- referenced updates SET DEFAULT (two scalar cols) +-- referenced deletes SET DEFAULT (two scalar cols) +-- referenced deletes SET DEFAULT (two scalar cols, SET DEFAULT subset) +-- +-- test FK referenced updates CASCADE +-- +TRUNCATE temporal_per, temporal_fk_rng2per; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES ('[100,100]', '[2018-01-01,2021-01-01)', '[6,7)'); +ALTER TABLE temporal_fk_rng2per + ADD CONSTRAINT temporal_fk_rng2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per + ON DELETE CASCADE ON UPDATE CASCADE; +-- leftovers on both sides: +UPDATE temporal_per FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' SET id = '[7,8)' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_at | parent_id +-----------+-------------------------+----------- + [100,101) | [2018-01-01,2019-01-01) | [6,7) + [100,101) | [2019-01-01,2020-01-01) | [7,8) + [100,101) | [2020-01-01,2021-01-01) | [6,7) +(3 rows) + +-- non-FPO update: +UPDATE temporal_per SET id = '[7,8)' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_at | parent_id +-----------+-------------------------+----------- + [100,101) | [2018-01-01,2019-01-01) | [7,8) + [100,101) | [2019-01-01,2020-01-01) | [7,8) + [100,101) | [2020-01-01,2021-01-01) | [7,8) +(3 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES ('[200,200]', '[2018-01-01,2021-01-01)', '[8,9)'); +UPDATE temporal_per SET id = '[9,10)' WHERE id = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk_rng2per WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_at | parent_id +-----------+-------------------------+----------- + [200,201) | [2018-01-01,2020-01-01) | [9,10) + [200,201) | [2020-01-01,2021-01-01) | [8,9) +(2 rows) + +-- +-- test FK referenced deletes CASCADE +-- +TRUNCATE temporal_per, temporal_fk_rng2per; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES ('[100,100]', '[2018-01-01,2021-01-01)', '[6,7)'); +-- leftovers on both sides: +DELETE FROM temporal_per FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_at | parent_id +-----------+-------------------------+----------- + [100,101) | [2018-01-01,2019-01-01) | [6,7) + [100,101) | [2020-01-01,2021-01-01) | [6,7) +(2 rows) + +-- non-FPO delete: +DELETE FROM temporal_per WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_at | parent_id +----+----------+----------- +(0 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES ('[200,200]', '[2018-01-01,2021-01-01)', '[8,9)'); +DELETE FROM temporal_per WHERE id = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk_rng2per WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_at | parent_id +-----------+-------------------------+----------- + [200,201) | [2020-01-01,2021-01-01) | [8,9) +(1 row) + +-- +-- test FK referenced updates SET NULL +-- +TRUNCATE temporal_per, temporal_fk_rng2per; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES ('[100,100]', '[2018-01-01,2021-01-01)', '[6,7)'); +ALTER TABLE temporal_fk_rng2per + DROP CONSTRAINT temporal_fk_rng2per_fk, + ADD CONSTRAINT temporal_fk_rng2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per + ON DELETE SET NULL ON UPDATE SET NULL; +-- leftovers on both sides: +UPDATE temporal_per FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' SET id = '[7,8)' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_at | parent_id +-----------+-------------------------+----------- + [100,101) | [2018-01-01,2019-01-01) | [6,7) + [100,101) | [2019-01-01,2020-01-01) | + [100,101) | [2020-01-01,2021-01-01) | [6,7) +(3 rows) + +-- non-FPO update: +UPDATE temporal_per SET id = '[7,8)' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_at | parent_id +-----------+-------------------------+----------- + [100,101) | [2018-01-01,2019-01-01) | + [100,101) | [2019-01-01,2020-01-01) | + [100,101) | [2020-01-01,2021-01-01) | +(3 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES ('[200,200]', '[2018-01-01,2021-01-01)', '[8,9)'); +UPDATE temporal_per SET id = '[9,10)' WHERE id = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk_rng2per WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_at | parent_id +-----------+-------------------------+----------- + [200,201) | [2018-01-01,2020-01-01) | + [200,201) | [2020-01-01,2021-01-01) | [8,9) +(2 rows) + +-- +-- test FK referenced deletes SET NULL +-- +TRUNCATE temporal_per, temporal_fk_rng2per; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES ('[100,100]', '[2018-01-01,2021-01-01)', '[6,7)'); +-- leftovers on both sides: +DELETE FROM temporal_per FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_at | parent_id +-----------+-------------------------+----------- + [100,101) | [2018-01-01,2019-01-01) | [6,7) + [100,101) | [2019-01-01,2020-01-01) | + [100,101) | [2020-01-01,2021-01-01) | [6,7) +(3 rows) + +-- non-FPO delete: +DELETE FROM temporal_per WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_at | parent_id +-----------+-------------------------+----------- + [100,101) | [2018-01-01,2019-01-01) | + [100,101) | [2019-01-01,2020-01-01) | + [100,101) | [2020-01-01,2021-01-01) | +(3 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES ('[200,200]', '[2018-01-01,2021-01-01)', '[8,9)'); +DELETE FROM temporal_per WHERE id = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk_rng2per WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_at | parent_id +-----------+-------------------------+----------- + [200,201) | [2018-01-01,2020-01-01) | + [200,201) | [2020-01-01,2021-01-01) | [8,9) +(2 rows) + +-- +-- test FK referenced updates SET DEFAULT +-- +TRUNCATE temporal_per, temporal_fk_rng2per; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[-1,-1]', null, null); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES ('[100,100]', '[2018-01-01,2021-01-01)', '[6,7)'); +ALTER TABLE temporal_fk_rng2per + ALTER COLUMN parent_id SET DEFAULT '[-1,-1]', + DROP CONSTRAINT temporal_fk_rng2per_fk, + ADD CONSTRAINT temporal_fk_rng2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per + ON DELETE SET DEFAULT ON UPDATE SET DEFAULT; +-- leftovers on both sides: +UPDATE temporal_per FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' SET id = '[7,8)' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_at | parent_id +-----------+-------------------------+----------- + [100,101) | [2018-01-01,2019-01-01) | [6,7) + [100,101) | [2019-01-01,2020-01-01) | [-1,0) + [100,101) | [2020-01-01,2021-01-01) | [6,7) +(3 rows) + +-- non-FPO update: +UPDATE temporal_per SET id = '[7,8)' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_at | parent_id +-----------+-------------------------+----------- + [100,101) | [2018-01-01,2019-01-01) | [-1,0) + [100,101) | [2019-01-01,2020-01-01) | [-1,0) + [100,101) | [2020-01-01,2021-01-01) | [-1,0) +(3 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES ('[200,200]', '[2018-01-01,2021-01-01)', '[8,9)'); +UPDATE temporal_per SET id = '[9,10)' WHERE id = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk_rng2per WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_at | parent_id +-----------+-------------------------+----------- + [200,201) | [2018-01-01,2020-01-01) | [-1,0) + [200,201) | [2020-01-01,2021-01-01) | [8,9) +(2 rows) + +-- +-- test FK referenced deletes SET DEFAULT +-- +TRUNCATE temporal_per, temporal_fk_rng2per; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[-1,-1]', null, null); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES ('[100,100]', '[2018-01-01,2021-01-01)', '[6,7)'); +-- leftovers on both sides: +DELETE FROM temporal_per FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_at | parent_id +-----------+-------------------------+----------- + [100,101) | [2018-01-01,2019-01-01) | [6,7) + [100,101) | [2019-01-01,2020-01-01) | [-1,0) + [100,101) | [2020-01-01,2021-01-01) | [6,7) +(3 rows) + +-- non-FPO update: +DELETE FROM temporal_per WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_at | parent_id +-----------+-------------------------+----------- + [100,101) | [2018-01-01,2019-01-01) | [-1,0) + [100,101) | [2019-01-01,2020-01-01) | [-1,0) + [100,101) | [2020-01-01,2021-01-01) | [-1,0) +(3 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES ('[200,200]', '[2018-01-01,2021-01-01)', '[8,9)'); +DELETE FROM temporal_per WHERE id = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk_rng2per WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_at | parent_id +-----------+-------------------------+----------- + [200,201) | [2018-01-01,2020-01-01) | [-1,0) + [200,201) | [2020-01-01,2021-01-01) | [8,9) +(2 rows) + +-- +-- test FK referenced updates CASCADE (two scalar cols) +-- +TRUNCATE temporal_per2, temporal_fk2_rng2per; +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[6,7)', '[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_rng2per (id, valid_at, parent_id1, parent_id2) VALUES ('[100,100]', '[2018-01-01,2021-01-01)', '[6,7)', '[6,7)'); +ALTER TABLE temporal_fk2_rng2per + DROP CONSTRAINT temporal_fk2_rng2per_fk, + ADD CONSTRAINT temporal_fk2_rng2per_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_per2 + ON DELETE CASCADE ON UPDATE CASCADE; +-- leftovers on both sides: +UPDATE temporal_per2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' SET id1 = '[7,8)' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_at | parent_id1 | parent_id2 +-----------+-------------------------+------------+------------ + [100,101) | [2018-01-01,2019-01-01) | [6,7) | [6,7) + [100,101) | [2019-01-01,2020-01-01) | [7,8) | [6,7) + [100,101) | [2020-01-01,2021-01-01) | [6,7) | [6,7) +(3 rows) + +-- non-FPO update: +UPDATE temporal_per2 SET id1 = '[7,8)' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_at | parent_id1 | parent_id2 +-----------+-------------------------+------------+------------ + [100,101) | [2018-01-01,2019-01-01) | [7,8) | [6,7) + [100,101) | [2019-01-01,2020-01-01) | [7,8) | [6,7) + [100,101) | [2020-01-01,2021-01-01) | [7,8) | [6,7) +(3 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_rng2per (id, valid_at, parent_id1, parent_id2) VALUES ('[200,200]', '[2018-01-01,2021-01-01)', '[8,9)', '[8,9)'); +UPDATE temporal_per2 SET id1 = '[9,10)' WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_at | parent_id1 | parent_id2 +-----------+-------------------------+------------+------------ + [200,201) | [2018-01-01,2020-01-01) | [9,10) | [8,9) + [200,201) | [2020-01-01,2021-01-01) | [8,9) | [8,9) +(2 rows) + +-- +-- test FK referenced deletes CASCADE (two scalar cols) +-- +TRUNCATE temporal_per2, temporal_fk2_rng2per; +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[6,7)', '[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_rng2per (id, valid_at, parent_id1, parent_id2) VALUES ('[100,100]', '[2018-01-01,2021-01-01)', '[6,7)', '[6,7)'); +-- leftovers on both sides: +DELETE FROM temporal_per2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_at | parent_id1 | parent_id2 +-----------+-------------------------+------------+------------ + [100,101) | [2018-01-01,2019-01-01) | [6,7) | [6,7) + [100,101) | [2020-01-01,2021-01-01) | [6,7) | [6,7) +(2 rows) + +-- non-FPO delete: +DELETE FROM temporal_per2 WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_at | parent_id1 | parent_id2 +----+----------+------------+------------ +(0 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_rng2per (id, valid_at, parent_id1, parent_id2) VALUES ('[200,200]', '[2018-01-01,2021-01-01)', '[8,9)', '[8,9)'); +DELETE FROM temporal_per2 WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_at | parent_id1 | parent_id2 +-----------+-------------------------+------------+------------ + [200,201) | [2020-01-01,2021-01-01) | [8,9) | [8,9) +(1 row) + +-- +-- test FK referenced updates SET NULL (two scalar cols) +-- +TRUNCATE temporal_per2, temporal_fk2_rng2per; +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[6,7)', '[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_rng2per (id, valid_at, parent_id1, parent_id2) VALUES ('[100,100]', '[2018-01-01,2021-01-01)', '[6,7)', '[6,7)'); +ALTER TABLE temporal_fk2_rng2per + DROP CONSTRAINT temporal_fk2_rng2per_fk, + ADD CONSTRAINT temporal_fk2_rng2per_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_per2 + ON DELETE SET NULL ON UPDATE SET NULL; +-- leftovers on both sides: +UPDATE temporal_per2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' SET id1 = '[7,8)' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_at | parent_id1 | parent_id2 +-----------+-------------------------+------------+------------ + [100,101) | [2018-01-01,2019-01-01) | [6,7) | [6,7) + [100,101) | [2019-01-01,2020-01-01) | | + [100,101) | [2020-01-01,2021-01-01) | [6,7) | [6,7) +(3 rows) + +-- non-FPO update: +UPDATE temporal_per2 SET id1 = '[7,8)' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_at | parent_id1 | parent_id2 +-----------+-------------------------+------------+------------ + [100,101) | [2018-01-01,2019-01-01) | | + [100,101) | [2019-01-01,2020-01-01) | | + [100,101) | [2020-01-01,2021-01-01) | | +(3 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_rng2per (id, valid_at, parent_id1, parent_id2) VALUES ('[200,200]', '[2018-01-01,2021-01-01)', '[8,9)', '[8,9)'); +UPDATE temporal_per2 SET id1 = '[9,10)' WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_at | parent_id1 | parent_id2 +-----------+-------------------------+------------+------------ + [200,201) | [2018-01-01,2020-01-01) | | + [200,201) | [2020-01-01,2021-01-01) | [8,9) | [8,9) +(2 rows) + +-- +-- test FK referenced deletes SET NULL (two scalar cols) +-- +TRUNCATE temporal_per2, temporal_fk2_rng2per; +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[6,7)', '[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_rng2per (id, valid_at, parent_id1, parent_id2) VALUES ('[100,100]', '[2018-01-01,2021-01-01)', '[6,7)', '[6,7)'); +-- leftovers on both sides: +DELETE FROM temporal_per2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_at | parent_id1 | parent_id2 +-----------+-------------------------+------------+------------ + [100,101) | [2018-01-01,2019-01-01) | [6,7) | [6,7) + [100,101) | [2019-01-01,2020-01-01) | | + [100,101) | [2020-01-01,2021-01-01) | [6,7) | [6,7) +(3 rows) + +-- non-FPO delete: +DELETE FROM temporal_per2 WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_at | parent_id1 | parent_id2 +-----------+-------------------------+------------+------------ + [100,101) | [2018-01-01,2019-01-01) | | + [100,101) | [2019-01-01,2020-01-01) | | + [100,101) | [2020-01-01,2021-01-01) | | +(3 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_rng2per (id, valid_at, parent_id1, parent_id2) VALUES ('[200,200]', '[2018-01-01,2021-01-01)', '[8,9)', '[8,9)'); +DELETE FROM temporal_per2 WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_at | parent_id1 | parent_id2 +-----------+-------------------------+------------+------------ + [200,201) | [2018-01-01,2020-01-01) | | + [200,201) | [2020-01-01,2021-01-01) | [8,9) | [8,9) +(2 rows) + +-- +-- test FK referenced deletes SET NULL (two scalar cols, SET NULL subset) +-- +TRUNCATE temporal_per2, temporal_fk2_rng2per; +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[6,7)', '[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_rng2per (id, valid_at, parent_id1, parent_id2) VALUES ('[100,100]', '[2018-01-01,2021-01-01)', '[6,7)', '[6,7)'); +-- fails because you can't set the PERIOD column: +ALTER TABLE temporal_fk2_rng2per + DROP CONSTRAINT temporal_fk2_rng2per_fk, + ADD CONSTRAINT temporal_fk2_rng2per_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_per2 + ON DELETE SET NULL (valid_at) ON UPDATE SET NULL; +ERROR: column "valid_at" referenced in ON DELETE SET action cannot be PERIOD +-- ok: +ALTER TABLE temporal_fk2_rng2per + DROP CONSTRAINT temporal_fk2_rng2per_fk, + ADD CONSTRAINT temporal_fk2_rng2per_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_per2 + ON DELETE SET NULL (parent_id1) ON UPDATE SET NULL; +-- leftovers on both sides: +DELETE FROM temporal_per2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_at | parent_id1 | parent_id2 +-----------+-------------------------+------------+------------ + [100,101) | [2018-01-01,2019-01-01) | [6,7) | [6,7) + [100,101) | [2019-01-01,2020-01-01) | | [6,7) + [100,101) | [2020-01-01,2021-01-01) | [6,7) | [6,7) +(3 rows) + +-- non-FPO delete: +DELETE FROM temporal_per2 WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_at | parent_id1 | parent_id2 +-----------+-------------------------+------------+------------ + [100,101) | [2018-01-01,2019-01-01) | | [6,7) + [100,101) | [2019-01-01,2020-01-01) | | [6,7) + [100,101) | [2020-01-01,2021-01-01) | | [6,7) +(3 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_rng2per (id, valid_at, parent_id1, parent_id2) VALUES ('[200,200]', '[2018-01-01,2021-01-01)', '[8,9)', '[8,9)'); +DELETE FROM temporal_per2 WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_at | parent_id1 | parent_id2 +-----------+-------------------------+------------+------------ + [200,201) | [2018-01-01,2020-01-01) | | [8,9) + [200,201) | [2020-01-01,2021-01-01) | [8,9) | [8,9) +(2 rows) + +-- +-- test FK referenced updates SET DEFAULT (two scalar cols) +-- +TRUNCATE temporal_per2, temporal_fk2_rng2per; +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[-1,-1]', '[-1,-1]', null, null); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[6,7)', '[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_rng2per (id, valid_at, parent_id1, parent_id2) VALUES ('[100,100]', '[2018-01-01,2021-01-01)', '[6,7)', '[6,7)'); +ALTER TABLE temporal_fk2_rng2per + ALTER COLUMN parent_id1 SET DEFAULT '[-1,-1]', + ALTER COLUMN parent_id2 SET DEFAULT '[-1,-1]', + DROP CONSTRAINT temporal_fk2_rng2per_fk, + ADD CONSTRAINT temporal_fk2_rng2per_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_per2 + ON DELETE SET DEFAULT ON UPDATE SET DEFAULT; +-- leftovers on both sides: +UPDATE temporal_per2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' SET id1 = '[7,8)', id2 = '[7,8)' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_at | parent_id1 | parent_id2 +-----------+-------------------------+------------+------------ + [100,101) | [2018-01-01,2019-01-01) | [6,7) | [6,7) + [100,101) | [2019-01-01,2020-01-01) | [-1,0) | [-1,0) + [100,101) | [2020-01-01,2021-01-01) | [6,7) | [6,7) +(3 rows) + +-- non-FPO update: +UPDATE temporal_per2 SET id1 = '[7,8)', id2 = '[7,8)' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_at | parent_id1 | parent_id2 +-----------+-------------------------+------------+------------ + [100,101) | [2018-01-01,2019-01-01) | [-1,0) | [-1,0) + [100,101) | [2019-01-01,2020-01-01) | [-1,0) | [-1,0) + [100,101) | [2020-01-01,2021-01-01) | [-1,0) | [-1,0) +(3 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_rng2per (id, valid_at, parent_id1, parent_id2) VALUES ('[200,200]', '[2018-01-01,2021-01-01)', '[8,9)', '[8,9)'); +UPDATE temporal_per2 SET id1 = '[9,10)', id2 = '[9,10)' WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_at | parent_id1 | parent_id2 +-----------+-------------------------+------------+------------ + [200,201) | [2018-01-01,2020-01-01) | [-1,0) | [-1,0) + [200,201) | [2020-01-01,2021-01-01) | [8,9) | [8,9) +(2 rows) + +-- +-- test FK referenced deletes SET DEFAULT (two scalar cols) +-- +TRUNCATE temporal_per2, temporal_fk2_rng2per; +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[-1,-1]', '[-1,-1]', null, null); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[6,7)', '[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_rng2per (id, valid_at, parent_id1, parent_id2) VALUES ('[100,100]', '[2018-01-01,2021-01-01)', '[6,7)', '[6,7)'); +-- leftovers on both sides: +DELETE FROM temporal_per2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_at | parent_id1 | parent_id2 +-----------+-------------------------+------------+------------ + [100,101) | [2018-01-01,2019-01-01) | [6,7) | [6,7) + [100,101) | [2019-01-01,2020-01-01) | [-1,0) | [-1,0) + [100,101) | [2020-01-01,2021-01-01) | [6,7) | [6,7) +(3 rows) + +-- non-FPO update: +DELETE FROM temporal_per2 WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_at | parent_id1 | parent_id2 +-----------+-------------------------+------------+------------ + [100,101) | [2018-01-01,2019-01-01) | [-1,0) | [-1,0) + [100,101) | [2019-01-01,2020-01-01) | [-1,0) | [-1,0) + [100,101) | [2020-01-01,2021-01-01) | [-1,0) | [-1,0) +(3 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_rng2per (id, valid_at, parent_id1, parent_id2) VALUES ('[200,200]', '[2018-01-01,2021-01-01)', '[8,9)', '[8,9)'); +DELETE FROM temporal_per2 WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_at | parent_id1 | parent_id2 +-----------+-------------------------+------------+------------ + [200,201) | [2018-01-01,2020-01-01) | [-1,0) | [-1,0) + [200,201) | [2020-01-01,2021-01-01) | [8,9) | [8,9) +(2 rows) + +-- +-- test FK referenced deletes SET DEFAULT (two scalar cols, SET DEFAULT subset) +-- +TRUNCATE temporal_per2, temporal_fk2_rng2per; +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[-1,-1]', '[6,7)', null, null); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[6,7)', '[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_rng2per (id, valid_at, parent_id1, parent_id2) VALUES ('[100,100]', '[2018-01-01,2021-01-01)', '[6,7)', '[6,7)'); +-- fails because you can't set the PERIOD column: +ALTER TABLE temporal_fk2_rng2per + ALTER COLUMN parent_id1 SET DEFAULT '[-1,-1]', + DROP CONSTRAINT temporal_fk2_rng2per_fk, + ADD CONSTRAINT temporal_fk2_rng2per_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_per2 + ON DELETE SET DEFAULT (valid_at) ON UPDATE SET DEFAULT; +ERROR: column "valid_at" referenced in ON DELETE SET action cannot be PERIOD +-- ok: +ALTER TABLE temporal_fk2_rng2per + ALTER COLUMN parent_id1 SET DEFAULT '[-1,-1]', + DROP CONSTRAINT temporal_fk2_rng2per_fk, + ADD CONSTRAINT temporal_fk2_rng2per_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_per2 + ON DELETE SET DEFAULT (parent_id1) ON UPDATE SET DEFAULT; +-- leftovers on both sides: +DELETE FROM temporal_per2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_at | parent_id1 | parent_id2 +-----------+-------------------------+------------+------------ + [100,101) | [2018-01-01,2019-01-01) | [6,7) | [6,7) + [100,101) | [2019-01-01,2020-01-01) | [-1,0) | [6,7) + [100,101) | [2020-01-01,2021-01-01) | [6,7) | [6,7) +(3 rows) + +-- non-FPO update: +DELETE FROM temporal_per2 WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_at | parent_id1 | parent_id2 +-----------+-------------------------+------------+------------ + [100,101) | [2018-01-01,2019-01-01) | [-1,0) | [6,7) + [100,101) | [2019-01-01,2020-01-01) | [-1,0) | [6,7) + [100,101) | [2020-01-01,2021-01-01) | [-1,0) | [6,7) +(3 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[-1,-1]', '[8,9)', null, null); +INSERT INTO temporal_fk2_rng2per (id, valid_at, parent_id1, parent_id2) VALUES ('[200,200]', '[2018-01-01,2021-01-01)', '[8,9)', '[8,9)'); +DELETE FROM temporal_per2 WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_at | parent_id1 | parent_id2 +-----------+-------------------------+------------+------------ + [200,201) | [2018-01-01,2020-01-01) | [-1,0) | [8,9) + [200,201) | [2020-01-01,2021-01-01) | [8,9) | [8,9) +(2 rows) + +-- +-- test FOREIGN KEY, PERIOD references range +-- +-- test table setup +DROP TABLE temporal_rng CASCADE; +NOTICE: drop cascades to constraint temporal_fk_rng2rng_fk on table temporal_fk_rng2rng +CREATE TABLE temporal_rng ( + id int4range, + valid_at daterange +); +ALTER TABLE temporal_rng + ADD CONSTRAINT temporal_rng_pk + PRIMARY KEY (id, valid_at WITHOUT OVERLAPS); +-- Can't create a FK with a mismatched range type +CREATE TABLE temporal_fk_per2rng ( + id int4range, + valid_from int, + valid_til int, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2rng_fk FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_rng (id, PERIOD valid_at) +); +ERROR: foreign key constraint "temporal_fk_per2rng_fk" cannot be implemented +DETAIL: Key columns "valid_at" of the referencing table and "valid_at" of the referenced table are of incompatible types: int4range and daterange. +-- works: range for referenced, PERIOD for referencing +CREATE TABLE temporal_fk_per2rng ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2rng_fk FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_rng (id, PERIOD valid_at) +); +\d temporal_fk_per2rng + Table "public.temporal_fk_per2rng" + Column | Type | Collation | Nullable | Default +------------+-----------+-----------+----------+--------- + id | int4range | | not null | + valid_from | date | | | + valid_til | date | | | + parent_id | int4range | | | +Periods: + valid_at (valid_from, valid_til) +Indexes: + "temporal_fk_per2rng_pk" PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +Check constraints: + "temporal_fk_per2rng_valid_at_check" CHECK (valid_from < valid_til) +Foreign-key constraints: + "temporal_fk_per2rng_fk" FOREIGN KEY (parent_id, PERIOD valid_at) REFERENCES temporal_rng(id, PERIOD valid_at) + +DROP TABLE temporal_fk_per2rng; +-- with mismatched PERIOD columns: +-- (parent_id, PERIOD valid_at) REFERENCES (id, valid_at) +-- REFERENCES part should specify PERIOD +CREATE TABLE temporal_fk_per2rng ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2rng_fk FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_rng (id, valid_at) +); +ERROR: foreign key uses PERIOD on the referencing table but not the referenced table +-- (parent_id, valid_at) REFERENCES (id, valid_at) +-- both should specify PERIOD: +CREATE TABLE temporal_fk_per2rng ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2rng_fk FOREIGN KEY (parent_id, valid_at) + REFERENCES temporal_rng (id, valid_at) +); +ERROR: foreign key must use PERIOD when referencing a primary key using WITHOUT OVERLAPS +-- (parent_id, valid_at) REFERENCES (id, PERIOD valid_at) +-- FOREIGN KEY part should specify PERIOD +CREATE TABLE temporal_fk_per2rng ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2rng_fk FOREIGN KEY (parent_id, valid_at) + REFERENCES temporal_rng (id, PERIOD valid_at) +); +ERROR: foreign key uses PERIOD on the referenced table but not the referencing table +-- (parent_id, valid_at) REFERENCES [implicit] +-- FOREIGN KEY part should specify PERIOD +CREATE TABLE temporal_fk_per2rng ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2rng_fk FOREIGN KEY (parent_id, valid_at) + REFERENCES temporal_rng +); +ERROR: foreign key uses PERIOD on the referenced table but not the referencing table +-- (parent_id, PERIOD valid_at) REFERENCES (id) +CREATE TABLE temporal_fk_per2rng ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2rng_fk FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_rng (id) +); +ERROR: foreign key uses PERIOD on the referencing table but not the referenced table +-- (parent_id) REFERENCES (id, PERIOD valid_at) +CREATE TABLE temporal_fk_per2rng ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2rng_fk FOREIGN KEY (parent_id) + REFERENCES temporal_rng (id, PERIOD valid_at) +); +ERROR: foreign key uses PERIOD on the referenced table but not the referencing table +-- with inferred PK on the referenced table: +-- (parent_id, PERIOD valid_at) REFERENCES [implicit] +CREATE TABLE temporal_fk_per2rng ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2rng_fk FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_rng +); +DROP TABLE temporal_fk_per2rng; +-- (parent_id) REFERENCES [implicit] +CREATE TABLE temporal_fk_per2rng ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2rng_fk FOREIGN KEY (parent_id) + REFERENCES temporal_rng +); +ERROR: foreign key uses PERIOD on the referenced table but not the referencing table +-- should fail because of duplicate referenced columns: +CREATE TABLE temporal_fk_per2rng ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2rng_fk FOREIGN KEY (parent_id, PERIOD parent_id) + REFERENCES temporal_rng (id, PERIOD id) +); +ERROR: foreign key referenced-columns list must not contain duplicates +-- Two scalar columns +DROP TABLE temporal_rng2 CASCADE; +NOTICE: drop cascades to constraint temporal_fk2_rng2rng_fk on table temporal_fk2_rng2rng +CREATE TABLE temporal_rng2 ( + id1 int4range, + id2 int4range, + valid_at daterange, + CONSTRAINT temporal_rng2_pk PRIMARY KEY (id1, id2, valid_at WITHOUT OVERLAPS) +); +CREATE TABLE temporal_fk2_per2rng ( + id int4range, + valid_from date, + valid_til date, + parent_id1 int4range, + parent_id2 int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk2_per2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk2_per2rng_fk FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_rng2 (id1, id2, PERIOD valid_at) +); +\d temporal_fk2_per2rng + Table "public.temporal_fk2_per2rng" + Column | Type | Collation | Nullable | Default +------------+-----------+-----------+----------+--------- + id | int4range | | not null | + valid_from | date | | | + valid_til | date | | | + parent_id1 | int4range | | | + parent_id2 | int4range | | | +Periods: + valid_at (valid_from, valid_til) +Indexes: + "temporal_fk2_per2rng_pk" PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +Check constraints: + "temporal_fk2_per2rng_valid_at_check" CHECK (valid_from < valid_til) +Foreign-key constraints: + "temporal_fk2_per2rng_fk" FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) REFERENCES temporal_rng2(id1, id2, PERIOD valid_at) + +DROP TABLE temporal_fk2_per2rng; +-- +-- test ALTER TABLE ADD CONSTRAINT +-- +CREATE TABLE temporal_fk_per2rng ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +); +ALTER TABLE temporal_fk_per2rng + ADD CONSTRAINT temporal_fk_per2rng_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_rng (id, PERIOD valid_at); +-- Two scalar columns: +CREATE TABLE temporal_fk2_per2rng ( + id int4range, + valid_from date, + valid_til date, + parent_id1 int4range, + parent_id2 int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk2_per2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +); +ALTER TABLE temporal_fk2_per2rng + ADD CONSTRAINT temporal_fk2_per2rng_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_rng2 (id1, id2, PERIOD valid_at); +\d temporal_fk2_per2rng + Table "public.temporal_fk2_per2rng" + Column | Type | Collation | Nullable | Default +------------+-----------+-----------+----------+--------- + id | int4range | | not null | + valid_from | date | | | + valid_til | date | | | + parent_id1 | int4range | | | + parent_id2 | int4range | | | +Periods: + valid_at (valid_from, valid_til) +Indexes: + "temporal_fk2_per2rng_pk" PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +Check constraints: + "temporal_fk2_per2rng_valid_at_check" CHECK (valid_from < valid_til) +Foreign-key constraints: + "temporal_fk2_per2rng_fk" FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) REFERENCES temporal_rng2(id1, id2, PERIOD valid_at) + +-- with inferred PK on the referenced table, and wrong column type: +ALTER TABLE temporal_fk_per2rng + DROP CONSTRAINT temporal_fk_per2rng_fk, + DROP PERIOD FOR valid_at, + ALTER COLUMN valid_from TYPE timestamp, + ALTER COLUMN valid_til TYPE timestamp, + ADD PERIOD FOR valid_at (valid_from, valid_til); +ALTER TABLE temporal_fk_per2rng + ADD CONSTRAINT temporal_fk_per2rng_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_rng; +ERROR: foreign key constraint "temporal_fk_per2rng_fk" cannot be implemented +DETAIL: Key columns "valid_at" of the referencing table and "valid_at" of the referenced table are of incompatible types: tsrange and daterange. +ALTER TABLE temporal_fk_per2rng + DROP PERIOD FOR valid_at, + ALTER COLUMN valid_from TYPE date, + ALTER COLUMN valid_til TYPE date, + ADD PERIOD FOR valid_at (valid_from, valid_til); +-- with inferred PK on the referenced table: +ALTER TABLE temporal_fk_per2rng + ADD CONSTRAINT temporal_fk_per2rng_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_rng; +-- should fail because of duplicate referenced columns: +ALTER TABLE temporal_fk_per2rng + ADD CONSTRAINT temporal_fk_per2rng_fk2 + FOREIGN KEY (parent_id, PERIOD parent_id) + REFERENCES temporal_rng (id, PERIOD id); +ERROR: foreign key referenced-columns list must not contain duplicates +-- +-- test with rows already +-- +DELETE FROM temporal_fk_per2rng; +DELETE FROM temporal_rng; +INSERT INTO temporal_rng (id, valid_at) VALUES + ('[1,2)', '[2018-01-02,2018-02-03)'), + ('[1,2)', '[2018-03-03,2018-04-04)'), + ('[2,3)', '[2018-01-01,2018-01-05)'), + ('[3,4)', '[2018-01-01,)'); +ALTER TABLE temporal_fk_per2rng + DROP CONSTRAINT temporal_fk_per2rng_fk; +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES ('[1,2)', '2018-01-02', '2018-02-01', '[1,2)'); +ALTER TABLE temporal_fk_per2rng + ADD CONSTRAINT temporal_fk_per2rng_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_rng; +ALTER TABLE temporal_fk_per2rng + DROP CONSTRAINT temporal_fk_per2rng_fk; +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES ('[2,3)', '2018-01-02', '2018-04-01', '[1,2)'); +-- should fail: +ALTER TABLE temporal_fk_per2rng + ADD CONSTRAINT temporal_fk_per2rng_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_rng; +ERROR: insert or update on table "temporal_fk_per2rng" violates foreign key constraint "temporal_fk_per2rng_fk" +DETAIL: Key (parent_id, valid_at)=([1,2), [2018-01-02,2018-04-01)) is not present in table "temporal_rng". +-- okay again: +DELETE FROM temporal_fk_per2rng; +ALTER TABLE temporal_fk_per2rng + ADD CONSTRAINT temporal_fk_per2rng_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_rng; +-- +-- test pg_get_constraintdef +-- +SELECT pg_get_constraintdef(oid) FROM pg_constraint WHERE conname = 'temporal_fk_per2rng_fk'; + pg_get_constraintdef +--------------------------------------------------------------------------------------- + FOREIGN KEY (parent_id, PERIOD valid_at) REFERENCES temporal_rng(id, PERIOD valid_at) +(1 row) + +-- +-- test FK referencing inserts +-- +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES ('[1,2)', '2018-01-02', '2018-02-01', '[1,2)'); +-- should fail: +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES ('[2,3)', '2018-01-02', '2018-04-01', '[1,2)'); +ERROR: insert or update on table "temporal_fk_per2rng" violates foreign key constraint "temporal_fk_per2rng_fk" +DETAIL: Key (parent_id, valid_at)=([1,2), [2018-01-02,2018-04-01)) is not present in table "temporal_rng". +-- now it should work: +INSERT INTO temporal_rng (id, valid_at) VALUES ('[1,2)', '[2018-02-03,2018-03-03)'); +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES ('[2,3)', '2018-01-02', '2018-04-01', '[1,2)'); +-- +-- test FK referencing updates +-- +-- slide the edge across a referenced transition: +UPDATE temporal_fk_per2rng SET valid_from = '2018-01-02', valid_til = '2018-02-20' WHERE id = '[1,2)'; +-- should fail: +UPDATE temporal_fk_per2rng SET valid_from = '2018-01-02', valid_til = '2018-05-01' WHERE id = '[1,2)'; +ERROR: insert or update on table "temporal_fk_per2rng" violates foreign key constraint "temporal_fk_per2rng_fk" +DETAIL: Key (parent_id, valid_at)=([1,2), [2018-01-02,2018-05-01)) is not present in table "temporal_rng". +UPDATE temporal_fk_per2rng SET parent_id = '[8,9)' WHERE id = '[1,2)'; +ERROR: insert or update on table "temporal_fk_per2rng" violates foreign key constraint "temporal_fk_per2rng_fk" +DETAIL: Key (parent_id, valid_at)=([8,9), [2018-01-02,2018-02-20)) is not present in table "temporal_rng". +-- ALTER FK DEFERRABLE +BEGIN; + INSERT INTO temporal_rng (id, valid_at) VALUES + ('[5,6)', '[2018-01-01,2018-02-01)'), + ('[5,6)', '[2018-02-01,2018-03-01)'); + INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES + ('[3,4)', '2018-01-05', '2018-01-10', '[5,6)'); + ALTER TABLE temporal_fk_per2rng + ALTER CONSTRAINT temporal_fk_per2rng_fk + DEFERRABLE INITIALLY DEFERRED; + DELETE FROM temporal_rng WHERE id = '[5,6)'; --should not fail yet. +COMMIT; -- should fail here. +ERROR: update or delete on table "temporal_rng" violates foreign key constraint "temporal_fk_per2rng_fk" on table "temporal_fk_per2rng" +DETAIL: Key (id, valid_at)=([5,6), [2018-01-01,2018-02-01)) is still referenced from table "temporal_fk_per2rng". +-- +-- test FK referenced updates NO ACTION +-- +TRUNCATE temporal_rng, temporal_fk_per2rng; +ALTER TABLE temporal_fk_per2rng + DROP CONSTRAINT temporal_fk_per2rng_fk; +ALTER TABLE temporal_fk_per2rng + ADD CONSTRAINT temporal_fk_per2rng_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_rng + ON UPDATE NO ACTION; +-- a PK update that succeeds because the numeric id isn't referenced: +INSERT INTO temporal_rng (id, valid_at) VALUES ('[5,6)', '[2018-01-01,2018-02-01)'); +UPDATE temporal_rng SET valid_at = '[2016-01-01,2016-02-01)' WHERE id = '[5,6)'; +-- a PK update that succeeds even though the numeric id is referenced because the range isn't: +DELETE FROM temporal_rng WHERE id = '[5,6)'; +INSERT INTO temporal_rng (id, valid_at) VALUES + ('[5,6)', '[2018-01-01,2018-02-01)'), + ('[5,6)', '[2018-02-01,2018-03-01)'); +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES + ('[3,4)', '2018-01-05', '2018-01-10', '[5,6)'); +UPDATE temporal_rng SET valid_at = '[2016-02-01,2016-03-01)' +WHERE id = '[5,6)' AND valid_at = '[2018-02-01,2018-03-01)'; +-- A PK update sliding the edge between two referenced rows: +INSERT INTO temporal_rng (id, valid_at) VALUES + ('[6,7)', '[2018-01-01,2018-02-01)'), + ('[6,7)', '[2018-02-01,2018-03-01)'); +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES + ('[4,5)', '2018-01-15', '2018-02-15', '[6,7)'); +UPDATE temporal_rng +SET valid_at = CASE WHEN lower(valid_at) = '2018-01-01' THEN daterange('2018-01-01', '2018-01-05') + WHEN lower(valid_at) = '2018-02-01' THEN daterange('2018-01-05', '2018-03-01') END +WHERE id = '[6,7)'; +-- a PK update shrinking the referenced range but still valid: +-- There are two references: one fulfilled by the first pk row, +-- the other fulfilled by both pk rows combined. +INSERT INTO temporal_rng (id, valid_at) VALUES + ('[1,2)', '[2018-01-01,2018-03-01)'), + ('[1,2)', '[2018-03-01,2018-06-01)'); +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES + ('[1,2)', '2018-01-15', '2018-02-01', '[1,2)'), + ('[2,3)', '2018-01-15', '2018-05-01', '[1,2)'); +UPDATE temporal_rng SET valid_at = '[2018-01-15,2018-03-01)' +WHERE id = '[1,2)' AND valid_at @> '2018-01-15'::date; +-- a PK update growing the referenced range is fine: +UPDATE temporal_rng SET valid_at = '[2018-01-01,2018-03-01)' +WHERE id = '[1,2)' AND valid_at @> '2018-01-25'::date; +-- a PK update shrinking the referenced range and changing the id invalidates the whole range: +UPDATE temporal_rng SET id = '[2,3)', valid_at = '[2018-01-15,2018-03-01)' +WHERE id = '[1,2)' AND valid_at @> '2018-01-15'::date; +ERROR: update or delete on table "temporal_rng" violates foreign key constraint "temporal_fk_per2rng_fk" on table "temporal_fk_per2rng" +DETAIL: Key (id, valid_at)=([1,2), [2018-01-01,2018-03-01)) is still referenced from table "temporal_fk_per2rng". +-- a PK update changing only the id invalidates the whole range: +UPDATE temporal_rng SET id = '[2,3)' +WHERE id = '[1,2)' AND valid_at @> '2018-01-15'::date; +ERROR: update or delete on table "temporal_rng" violates foreign key constraint "temporal_fk_per2rng_fk" on table "temporal_fk_per2rng" +DETAIL: Key (id, valid_at)=([1,2), [2018-01-01,2018-03-01)) is still referenced from table "temporal_fk_per2rng". +-- a PK update that loses time from both ends, but is still valid: +INSERT INTO temporal_rng (id, valid_at) VALUES + ('[2,3)', '[2018-01-01,2018-03-01)'); +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES + ('[5,6)', '2018-01-15', '2018-02-01', '[2,3)'); +UPDATE temporal_rng SET valid_at = '[2018-01-15,2018-02-15)' +WHERE id = '[2,3)'; +-- a PK update that fails because both are referenced: +UPDATE temporal_rng SET valid_at = '[2016-01-01,2016-02-01)' +WHERE id = '[5,6)' AND valid_at = '[2018-01-01,2018-02-01)'; +ERROR: update or delete on table "temporal_rng" violates foreign key constraint "temporal_fk_per2rng_fk" on table "temporal_fk_per2rng" +DETAIL: Key (id, valid_at)=([5,6), [2018-01-01,2018-02-01)) is still referenced from table "temporal_fk_per2rng". +-- a PK update that fails because both are referenced, but not 'til commit: +BEGIN; + ALTER TABLE temporal_fk_per2rng + ALTER CONSTRAINT temporal_fk_per2rng_fk + DEFERRABLE INITIALLY DEFERRED; + UPDATE temporal_rng SET valid_at = '[2016-01-01,2016-02-01)' + WHERE id = '[5,6)' AND valid_at = daterange('2018-01-01', '2018-02-01'); +COMMIT; +ERROR: update or delete on table "temporal_rng" violates foreign key constraint "temporal_fk_per2rng_fk" on table "temporal_fk_per2rng" +DETAIL: Key (id, valid_at)=([5,6), [2018-01-01,2018-02-01)) is still referenced from table "temporal_fk_per2rng". +-- changing the scalar part fails: +UPDATE temporal_rng SET id = '[7,8)' +WHERE id = '[5,6)' AND valid_at = '[2018-01-01,2018-02-01)'; +ERROR: update or delete on table "temporal_rng" violates foreign key constraint "temporal_fk_per2rng_fk" on table "temporal_fk_per2rng" +DETAIL: Key (id, valid_at)=([5,6), [2018-01-01,2018-02-01)) is still referenced from table "temporal_fk_per2rng". +-- changing an unreferenced part is okay: +UPDATE temporal_rng +FOR PORTION OF valid_at FROM '2018-01-02' TO '2018-01-03' +SET id = '[7,8)' +WHERE id = '[5,6)'; +-- changing just a part fails: +UPDATE temporal_rng +FOR PORTION OF valid_at FROM '2018-01-05' TO '2018-01-10' +SET id = '[7,8)' +WHERE id = '[5,6)'; +ERROR: update or delete on table "temporal_rng" violates foreign key constraint "temporal_fk_per2rng_fk" on table "temporal_fk_per2rng" +DETAIL: Key (id, valid_at)=([5,6), [2018-01-03,2018-02-01)) is still referenced from table "temporal_fk_per2rng". +SELECT * FROM temporal_rng WHERE id in ('[5,6)', '[7,8)') ORDER BY id, valid_at; + id | valid_at +-------+------------------------- + [5,6) | [2016-02-01,2016-03-01) + [5,6) | [2018-01-01,2018-01-02) + [5,6) | [2018-01-03,2018-02-01) + [7,8) | [2018-01-02,2018-01-03) +(4 rows) + +SELECT * FROM temporal_fk_per2rng WHERE id in ('[3,4)') ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +-------+------------+------------+----------- + [3,4) | 2018-01-05 | 2018-01-10 | [5,6) +(1 row) + +-- then delete the objecting FK record and the same PK update succeeds: +DELETE FROM temporal_fk_per2rng WHERE id = '[3,4)'; +UPDATE temporal_rng SET valid_at = '[2016-01-01,2016-02-01)' +WHERE id = '[5,6)' AND valid_at = '[2018-01-01,2018-02-01)'; +-- +-- test FK referenced updates RESTRICT +-- +TRUNCATE temporal_rng, temporal_fk_per2rng; +ALTER TABLE temporal_fk_per2rng + DROP CONSTRAINT temporal_fk_per2rng_fk; +ALTER TABLE temporal_fk_per2rng + ADD CONSTRAINT temporal_fk_per2rng_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_rng + ON UPDATE RESTRICT; +ERROR: unsupported ON UPDATE action for foreign key constraint using PERIOD +-- +-- test FK referenced deletes NO ACTION +-- +TRUNCATE temporal_rng, temporal_fk_per2rng; +ALTER TABLE temporal_fk_per2rng + ADD CONSTRAINT temporal_fk_per2rng_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_rng; +-- a PK delete that succeeds because the numeric id isn't referenced: +INSERT INTO temporal_rng (id, valid_at) VALUES ('[5,6)', '[2018-01-01,2018-02-01)'); +DELETE FROM temporal_rng WHERE id = '[5,6)'; +-- a PK delete that succeeds even though the numeric id is referenced because the range isn't: +INSERT INTO temporal_rng (id, valid_at) VALUES + ('[5,6)', '[2018-01-01,2018-02-01)'), + ('[5,6)', '[2018-02-01,2018-03-01)'); +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES ('[3,4)', '2018-01-05', '2018-01-10', '[5,6)'); +DELETE FROM temporal_rng WHERE id = '[5,6)' AND valid_at = '[2018-02-01,2018-03-01)'; +-- a PK delete that fails because both are referenced: +DELETE FROM temporal_rng WHERE id = '[5,6)' AND valid_at = '[2018-01-01,2018-02-01)'; +ERROR: update or delete on table "temporal_rng" violates foreign key constraint "temporal_fk_per2rng_fk" on table "temporal_fk_per2rng" +DETAIL: Key (id, valid_at)=([5,6), [2018-01-01,2018-02-01)) is still referenced from table "temporal_fk_per2rng". +-- a PK delete that fails because both are referenced, but not 'til commit: +BEGIN; + ALTER TABLE temporal_fk_per2rng + ALTER CONSTRAINT temporal_fk_per2rng_fk + DEFERRABLE INITIALLY DEFERRED; + DELETE FROM temporal_rng WHERE id = '[5,6)' AND valid_at = daterange('2018-01-01', '2018-02-01'); +COMMIT; +ERROR: update or delete on table "temporal_rng" violates foreign key constraint "temporal_fk_per2rng_fk" on table "temporal_fk_per2rng" +DETAIL: Key (id, valid_at)=([5,6), [2018-01-01,2018-02-01)) is still referenced from table "temporal_fk_per2rng". +-- deleting an unreferenced part is okay: +DELETE FROM temporal_rng +FOR PORTION OF valid_at FROM '2018-01-02' TO '2018-01-03' +WHERE id = '[5,6)'; +-- deleting just a part fails: +DELETE FROM temporal_rng +FOR PORTION OF valid_at FROM '2018-01-05' TO '2018-01-10' +WHERE id = '[5,6)'; +ERROR: update or delete on table "temporal_rng" violates foreign key constraint "temporal_fk_per2rng_fk" on table "temporal_fk_per2rng" +DETAIL: Key (id, valid_at)=([5,6), [2018-01-03,2018-02-01)) is still referenced from table "temporal_fk_per2rng". +SELECT * FROM temporal_rng WHERE id in ('[5,6)', '[7,8)') ORDER BY id, valid_at; + id | valid_at +-------+------------------------- + [5,6) | [2018-01-01,2018-01-02) + [5,6) | [2018-01-03,2018-02-01) +(2 rows) + +SELECT * FROM temporal_fk_per2rng WHERE id in ('[3,4)') ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +-------+------------+------------+----------- + [3,4) | 2018-01-05 | 2018-01-10 | [5,6) +(1 row) + +-- then delete the objecting FK record and the same PK delete succeeds: +DELETE FROM temporal_fk_per2rng WHERE id = '[3,4)'; +DELETE FROM temporal_rng WHERE id = '[5,6)' AND valid_at = '[2018-01-01,2018-02-01)'; +-- +-- test FK referenced deletes RESTRICT +-- +TRUNCATE temporal_rng, temporal_fk_per2rng; +ALTER TABLE temporal_fk_per2rng + DROP CONSTRAINT temporal_fk_per2rng_fk; +ALTER TABLE temporal_fk_per2rng + ADD CONSTRAINT temporal_fk_per2rng_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_rng + ON DELETE RESTRICT; +ERROR: unsupported ON DELETE action for foreign key constraint using PERIOD +-- +-- per2rng test ON UPDATE/DELETE options +-- +-- TOC: +-- referenced updates CASCADE +-- referenced deletes CASCADE +-- referenced updates SET NULL +-- referenced deletes SET NULL +-- referenced updates SET DEFAULT +-- referenced deletes SET DEFAULT +-- referenced updates CASCADE (two scalar cols) +-- referenced deletes CASCADE (two scalar cols) +-- referenced updates SET NULL (two scalar cols) +-- referenced deletes SET NULL (two scalar cols) +-- referenced deletes SET NULL (two scalar cols, SET NULL subset) +-- referenced updates SET DEFAULT (two scalar cols) +-- referenced deletes SET DEFAULT (two scalar cols) +-- referenced deletes SET DEFAULT (two scalar cols, SET DEFAULT subset) +-- +-- test FK referenced updates CASCADE +-- +TRUNCATE temporal_rng, temporal_fk_per2rng; +INSERT INTO temporal_rng (id, valid_at) VALUES ('[6,7)', '[2018-01-01,2021-01-01)'); +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)'); +ALTER TABLE temporal_fk_per2rng + ADD CONSTRAINT temporal_fk_per2rng_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_rng + ON DELETE CASCADE ON UPDATE CASCADE; +-- leftovers on both sides: +UPDATE temporal_rng FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' SET id = '[7,8)' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +-----------+------------+------------+----------- + [100,101) | 2018-01-01 | 2019-01-01 | [6,7) + [100,101) | 2019-01-01 | 2020-01-01 | [7,8) + [100,101) | 2020-01-01 | 2021-01-01 | [6,7) +(3 rows) + +-- non-FPO update: +UPDATE temporal_rng SET id = '[7,8)' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +-----------+------------+------------+----------- + [100,101) | 2018-01-01 | 2019-01-01 | [7,8) + [100,101) | 2019-01-01 | 2020-01-01 | [7,8) + [100,101) | 2020-01-01 | 2021-01-01 | [7,8) +(3 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_rng (id, valid_at) VALUES ('[8,9)', '[2018-01-01,2020-01-01)'); +INSERT INTO temporal_rng (id, valid_at) VALUES ('[8,9)', '[2020-01-01,2021-01-01)'); +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)'); +UPDATE temporal_rng SET id = '[9,10)' WHERE id = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk_per2rng WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +-----------+------------+------------+----------- + [200,201) | 2018-01-01 | 2020-01-01 | [9,10) + [200,201) | 2020-01-01 | 2021-01-01 | [8,9) +(2 rows) + +-- +-- test FK referenced deletes CASCADE +-- +TRUNCATE temporal_rng, temporal_fk_per2rng; +INSERT INTO temporal_rng (id, valid_at) VALUES ('[6,7)', '[2018-01-01,2021-01-01)'); +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)'); +-- leftovers on both sides: +DELETE FROM temporal_rng FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +-----------+------------+------------+----------- + [100,101) | 2018-01-01 | 2019-01-01 | [6,7) + [100,101) | 2020-01-01 | 2021-01-01 | [6,7) +(2 rows) + +-- non-FPO delete: +DELETE FROM temporal_rng WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +----+------------+-----------+----------- +(0 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_rng (id, valid_at) VALUES ('[8,9)', '[2018-01-01,2020-01-01)'); +INSERT INTO temporal_rng (id, valid_at) VALUES ('[8,9)', '[2020-01-01,2021-01-01)'); +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)'); +DELETE FROM temporal_rng WHERE id = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk_per2rng WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +-----------+------------+------------+----------- + [200,201) | 2020-01-01 | 2021-01-01 | [8,9) +(1 row) + +-- +-- test FK referenced updates SET NULL +-- +TRUNCATE temporal_rng, temporal_fk_per2rng; +INSERT INTO temporal_rng (id, valid_at) VALUES ('[6,7)', '[2018-01-01,2021-01-01)'); +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)'); +ALTER TABLE temporal_fk_per2rng + DROP CONSTRAINT temporal_fk_per2rng_fk, + ADD CONSTRAINT temporal_fk_per2rng_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_rng + ON DELETE SET NULL ON UPDATE SET NULL; +-- leftovers on both sides: +UPDATE temporal_rng FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' SET id = '[7,8)' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +-----------+------------+------------+----------- + [100,101) | 2018-01-01 | 2019-01-01 | [6,7) + [100,101) | 2019-01-01 | 2020-01-01 | + [100,101) | 2020-01-01 | 2021-01-01 | [6,7) +(3 rows) + +-- non-FPO update: +UPDATE temporal_rng SET id = '[7,8)' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +-----------+------------+------------+----------- + [100,101) | 2018-01-01 | 2019-01-01 | + [100,101) | 2019-01-01 | 2020-01-01 | + [100,101) | 2020-01-01 | 2021-01-01 | +(3 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_rng (id, valid_at) VALUES ('[8,9)', '[2018-01-01,2020-01-01)'); +INSERT INTO temporal_rng (id, valid_at) VALUES ('[8,9)', '[2020-01-01,2021-01-01)'); +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)'); +UPDATE temporal_rng SET id = '[9,10)' WHERE id = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk_per2rng WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +-----------+------------+------------+----------- + [200,201) | 2018-01-01 | 2020-01-01 | + [200,201) | 2020-01-01 | 2021-01-01 | [8,9) +(2 rows) + +-- +-- test FK referenced deletes SET NULL +-- +TRUNCATE temporal_rng, temporal_fk_per2rng; +INSERT INTO temporal_rng (id, valid_at) VALUES ('[6,7)', '[2018-01-01,2021-01-01)'); +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)'); +-- leftovers on both sides: +DELETE FROM temporal_rng FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +-----------+------------+------------+----------- + [100,101) | 2018-01-01 | 2019-01-01 | [6,7) + [100,101) | 2019-01-01 | 2020-01-01 | + [100,101) | 2020-01-01 | 2021-01-01 | [6,7) +(3 rows) + +-- non-FPO delete: +DELETE FROM temporal_rng WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +-----------+------------+------------+----------- + [100,101) | 2018-01-01 | 2019-01-01 | + [100,101) | 2019-01-01 | 2020-01-01 | + [100,101) | 2020-01-01 | 2021-01-01 | +(3 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_rng (id, valid_at) VALUES ('[8,9)', '[2018-01-01,2020-01-01)'); +INSERT INTO temporal_rng (id, valid_at) VALUES ('[8,9)', '[2020-01-01,2021-01-01)'); +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)'); +DELETE FROM temporal_rng WHERE id = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk_per2rng WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +-----------+------------+------------+----------- + [200,201) | 2018-01-01 | 2020-01-01 | + [200,201) | 2020-01-01 | 2021-01-01 | [8,9) +(2 rows) + +-- +-- test FK referenced updates SET DEFAULT +-- +TRUNCATE temporal_rng, temporal_fk_per2rng; +INSERT INTO temporal_rng (id, valid_at) VALUES ('[-1,-1]', '(,)'); +INSERT INTO temporal_rng (id, valid_at) VALUES ('[6,7)', '[2018-01-01,2021-01-01)'); +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)'); +ALTER TABLE temporal_fk_per2rng + ALTER COLUMN parent_id SET DEFAULT '[-1,-1]', + DROP CONSTRAINT temporal_fk_per2rng_fk, + ADD CONSTRAINT temporal_fk_per2rng_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_rng + ON DELETE SET DEFAULT ON UPDATE SET DEFAULT; +-- leftovers on both sides: +UPDATE temporal_rng FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' SET id = '[7,8)' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +-----------+------------+------------+----------- + [100,101) | 2018-01-01 | 2019-01-01 | [6,7) + [100,101) | 2019-01-01 | 2020-01-01 | [-1,0) + [100,101) | 2020-01-01 | 2021-01-01 | [6,7) +(3 rows) + +-- non-FPO update: +UPDATE temporal_rng SET id = '[7,8)' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +-----------+------------+------------+----------- + [100,101) | 2018-01-01 | 2019-01-01 | [-1,0) + [100,101) | 2019-01-01 | 2020-01-01 | [-1,0) + [100,101) | 2020-01-01 | 2021-01-01 | [-1,0) +(3 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_rng (id, valid_at) VALUES ('[8,9)', '[2018-01-01,2020-01-01)'); +INSERT INTO temporal_rng (id, valid_at) VALUES ('[8,9)', '[2020-01-01,2021-01-01)'); +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)'); +UPDATE temporal_rng SET id = '[9,10)' WHERE id = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk_per2rng WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +-----------+------------+------------+----------- + [200,201) | 2018-01-01 | 2020-01-01 | [-1,0) + [200,201) | 2020-01-01 | 2021-01-01 | [8,9) +(2 rows) + +-- +-- test FK referenced deletes SET DEFAULT +-- +TRUNCATE temporal_rng, temporal_fk_per2rng; +INSERT INTO temporal_rng (id, valid_at) VALUES ('[-1,-1]', '(,)'); +INSERT INTO temporal_rng (id, valid_at) VALUES ('[6,7)', '[2018-01-01,2021-01-01)'); +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)'); +-- leftovers on both sides: +DELETE FROM temporal_rng FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +-----------+------------+------------+----------- + [100,101) | 2018-01-01 | 2019-01-01 | [6,7) + [100,101) | 2019-01-01 | 2020-01-01 | [-1,0) + [100,101) | 2020-01-01 | 2021-01-01 | [6,7) +(3 rows) + +-- non-FPO update: +DELETE FROM temporal_rng WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +-----------+------------+------------+----------- + [100,101) | 2018-01-01 | 2019-01-01 | [-1,0) + [100,101) | 2019-01-01 | 2020-01-01 | [-1,0) + [100,101) | 2020-01-01 | 2021-01-01 | [-1,0) +(3 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_rng (id, valid_at) VALUES ('[8,9)', '[2018-01-01,2020-01-01)'); +INSERT INTO temporal_rng (id, valid_at) VALUES ('[8,9)', '[2020-01-01,2021-01-01)'); +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)'); +DELETE FROM temporal_rng WHERE id = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk_per2rng WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id +-----------+------------+------------+----------- + [200,201) | 2018-01-01 | 2020-01-01 | [-1,0) + [200,201) | 2020-01-01 | 2021-01-01 | [8,9) +(2 rows) + +-- +-- test FK referenced updates CASCADE (two scalar cols) +-- +TRUNCATE temporal_rng2, temporal_fk2_per2rng; +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[6,7)', '[6,7)', '[2018-01-01,2021-01-01)'); +INSERT INTO temporal_fk2_per2rng (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)', '[6,7)'); +ALTER TABLE temporal_fk2_per2rng + DROP CONSTRAINT temporal_fk2_per2rng_fk, + ADD CONSTRAINT temporal_fk2_per2rng_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_rng2 + ON DELETE CASCADE ON UPDATE CASCADE; +-- leftovers on both sides: +UPDATE temporal_rng2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' SET id1 = '[7,8)' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [100,101) | 2018-01-01 | 2019-01-01 | [6,7) | [6,7) + [100,101) | 2019-01-01 | 2020-01-01 | [7,8) | [6,7) + [100,101) | 2020-01-01 | 2021-01-01 | [6,7) | [6,7) +(3 rows) + +-- non-FPO update: +UPDATE temporal_rng2 SET id1 = '[7,8)' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [100,101) | 2018-01-01 | 2019-01-01 | [7,8) | [6,7) + [100,101) | 2019-01-01 | 2020-01-01 | [7,8) | [6,7) + [100,101) | 2020-01-01 | 2021-01-01 | [7,8) | [6,7) +(3 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[8,9)', '[8,9)', '[2018-01-01,2020-01-01)'); +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[8,9)', '[8,9)', '[2020-01-01,2021-01-01)'); +INSERT INTO temporal_fk2_per2rng (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)', '[8,9)'); +UPDATE temporal_rng2 SET id1 = '[9,10)' WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [200,201) | 2018-01-01 | 2020-01-01 | [9,10) | [8,9) + [200,201) | 2020-01-01 | 2021-01-01 | [8,9) | [8,9) +(2 rows) + +-- +-- test FK referenced deletes CASCADE (two scalar cols) +-- +TRUNCATE temporal_rng2, temporal_fk2_per2rng; +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[6,7)', '[6,7)', '[2018-01-01,2021-01-01)'); +INSERT INTO temporal_fk2_per2rng (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)', '[6,7)'); +-- leftovers on both sides: +DELETE FROM temporal_rng2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [100,101) | 2018-01-01 | 2019-01-01 | [6,7) | [6,7) + [100,101) | 2020-01-01 | 2021-01-01 | [6,7) | [6,7) +(2 rows) + +-- non-FPO delete: +DELETE FROM temporal_rng2 WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +----+------------+-----------+------------+------------ +(0 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[8,9)', '[8,9)', '[2018-01-01,2020-01-01)'); +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[8,9)', '[8,9)', '[2020-01-01,2021-01-01)'); +INSERT INTO temporal_fk2_per2rng (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)', '[8,9)'); +DELETE FROM temporal_rng2 WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [200,201) | 2020-01-01 | 2021-01-01 | [8,9) | [8,9) +(1 row) + +-- +-- test FK referenced updates SET NULL (two scalar cols) +-- +TRUNCATE temporal_rng2, temporal_fk2_per2rng; +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[6,7)', '[6,7)', '[2018-01-01,2021-01-01)'); +INSERT INTO temporal_fk2_per2rng (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)', '[6,7)'); +ALTER TABLE temporal_fk2_per2rng + DROP CONSTRAINT temporal_fk2_per2rng_fk, + ADD CONSTRAINT temporal_fk2_per2rng_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_rng2 + ON DELETE SET NULL ON UPDATE SET NULL; +-- leftovers on both sides: +UPDATE temporal_rng2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' SET id1 = '[7,8)' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [100,101) | 2018-01-01 | 2019-01-01 | [6,7) | [6,7) + [100,101) | 2019-01-01 | 2020-01-01 | | + [100,101) | 2020-01-01 | 2021-01-01 | [6,7) | [6,7) +(3 rows) + +-- non-FPO update: +UPDATE temporal_rng2 SET id1 = '[7,8)' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [100,101) | 2018-01-01 | 2019-01-01 | | + [100,101) | 2019-01-01 | 2020-01-01 | | + [100,101) | 2020-01-01 | 2021-01-01 | | +(3 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[8,9)', '[8,9)', '[2018-01-01,2020-01-01)'); +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[8,9)', '[8,9)', '[2020-01-01,2021-01-01)'); +INSERT INTO temporal_fk2_per2rng (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)', '[8,9)'); +UPDATE temporal_rng2 SET id1 = '[9,10)' WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [200,201) | 2018-01-01 | 2020-01-01 | | + [200,201) | 2020-01-01 | 2021-01-01 | [8,9) | [8,9) +(2 rows) + +-- +-- test FK referenced deletes SET NULL (two scalar cols) +-- +TRUNCATE temporal_rng2, temporal_fk2_per2rng; +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[6,7)', '[6,7)', '[2018-01-01,2021-01-01)'); +INSERT INTO temporal_fk2_per2rng (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)', '[6,7)'); +-- leftovers on both sides: +DELETE FROM temporal_rng2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [100,101) | 2018-01-01 | 2019-01-01 | [6,7) | [6,7) + [100,101) | 2019-01-01 | 2020-01-01 | | + [100,101) | 2020-01-01 | 2021-01-01 | [6,7) | [6,7) +(3 rows) + +-- non-FPO delete: +DELETE FROM temporal_rng2 WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [100,101) | 2018-01-01 | 2019-01-01 | | + [100,101) | 2019-01-01 | 2020-01-01 | | + [100,101) | 2020-01-01 | 2021-01-01 | | +(3 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[8,9)', '[8,9)', '[2018-01-01,2020-01-01)'); +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[8,9)', '[8,9)', '[2020-01-01,2021-01-01)'); +INSERT INTO temporal_fk2_per2rng (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)', '[8,9)'); +DELETE FROM temporal_rng2 WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [200,201) | 2018-01-01 | 2020-01-01 | | + [200,201) | 2020-01-01 | 2021-01-01 | [8,9) | [8,9) +(2 rows) + +-- +-- test FK referenced deletes SET NULL (two scalar cols, SET NULL subset) +-- +TRUNCATE temporal_rng2, temporal_fk2_per2rng; +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[6,7)', '[6,7)', '[2018-01-01,2021-01-01)'); +INSERT INTO temporal_fk2_per2rng (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)', '[6,7)'); +-- fails because you can't set the PERIOD column: +ALTER TABLE temporal_fk2_per2rng + DROP CONSTRAINT temporal_fk2_per2rng_fk, + ADD CONSTRAINT temporal_fk2_per2rng_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_rng2 + ON DELETE SET NULL (valid_at) ON UPDATE SET NULL; +ERROR: column "valid_at" referenced in ON DELETE SET action cannot be PERIOD +-- ok: +ALTER TABLE temporal_fk2_per2rng + DROP CONSTRAINT temporal_fk2_per2rng_fk, + ADD CONSTRAINT temporal_fk2_per2rng_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_rng2 + ON DELETE SET NULL (parent_id1) ON UPDATE SET NULL; +-- leftovers on both sides: +DELETE FROM temporal_rng2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [100,101) | 2018-01-01 | 2019-01-01 | [6,7) | [6,7) + [100,101) | 2019-01-01 | 2020-01-01 | | [6,7) + [100,101) | 2020-01-01 | 2021-01-01 | [6,7) | [6,7) +(3 rows) + +-- non-FPO delete: +DELETE FROM temporal_rng2 WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [100,101) | 2018-01-01 | 2019-01-01 | | [6,7) + [100,101) | 2019-01-01 | 2020-01-01 | | [6,7) + [100,101) | 2020-01-01 | 2021-01-01 | | [6,7) +(3 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[8,9)', '[8,9)', '[2018-01-01,2020-01-01)'); +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[8,9)', '[8,9)', '[2020-01-01,2021-01-01)'); +INSERT INTO temporal_fk2_per2rng (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)', '[8,9)'); +DELETE FROM temporal_rng2 WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [200,201) | 2018-01-01 | 2020-01-01 | | [8,9) + [200,201) | 2020-01-01 | 2021-01-01 | [8,9) | [8,9) +(2 rows) + +-- +-- test FK referenced updates SET DEFAULT (two scalar cols) +-- +TRUNCATE temporal_rng2, temporal_fk2_per2rng; +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[-1,-1]', '[-1,-1]', '(,)'); +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[6,7)', '[6,7)', '[2018-01-01,2021-01-01)'); +INSERT INTO temporal_fk2_per2rng (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)', '[6,7)'); +ALTER TABLE temporal_fk2_per2rng + ALTER COLUMN parent_id1 SET DEFAULT '[-1,-1]', + ALTER COLUMN parent_id2 SET DEFAULT '[-1,-1]', + DROP CONSTRAINT temporal_fk2_per2rng_fk, + ADD CONSTRAINT temporal_fk2_per2rng_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_rng2 + ON DELETE SET DEFAULT ON UPDATE SET DEFAULT; +-- leftovers on both sides: +UPDATE temporal_rng2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' SET id1 = '[7,8)', id2 = '[7,8)' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [100,101) | 2018-01-01 | 2019-01-01 | [6,7) | [6,7) + [100,101) | 2019-01-01 | 2020-01-01 | [-1,0) | [-1,0) + [100,101) | 2020-01-01 | 2021-01-01 | [6,7) | [6,7) +(3 rows) + +-- non-FPO update: +UPDATE temporal_rng2 SET id1 = '[7,8)', id2 = '[7,8)' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [100,101) | 2018-01-01 | 2019-01-01 | [-1,0) | [-1,0) + [100,101) | 2019-01-01 | 2020-01-01 | [-1,0) | [-1,0) + [100,101) | 2020-01-01 | 2021-01-01 | [-1,0) | [-1,0) +(3 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[8,9)', '[8,9)', '[2018-01-01,2020-01-01)'); +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[8,9)', '[8,9)', '[2020-01-01,2021-01-01)'); +INSERT INTO temporal_fk2_per2rng (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)', '[8,9)'); +UPDATE temporal_rng2 SET id1 = '[9,10)', id2 = '[9,10)' WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [200,201) | 2018-01-01 | 2020-01-01 | [-1,0) | [-1,0) + [200,201) | 2020-01-01 | 2021-01-01 | [8,9) | [8,9) +(2 rows) + +-- +-- test FK referenced deletes SET DEFAULT (two scalar cols) +-- +TRUNCATE temporal_rng2, temporal_fk2_per2rng; +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[-1,-1]', '[-1,-1]', '(,)'); +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[6,7)', '[6,7)', '[2018-01-01,2021-01-01)'); +INSERT INTO temporal_fk2_per2rng (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)', '[6,7)'); +-- leftovers on both sides: +DELETE FROM temporal_rng2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [100,101) | 2018-01-01 | 2019-01-01 | [6,7) | [6,7) + [100,101) | 2019-01-01 | 2020-01-01 | [-1,0) | [-1,0) + [100,101) | 2020-01-01 | 2021-01-01 | [6,7) | [6,7) +(3 rows) + +-- non-FPO update: +DELETE FROM temporal_rng2 WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [100,101) | 2018-01-01 | 2019-01-01 | [-1,0) | [-1,0) + [100,101) | 2019-01-01 | 2020-01-01 | [-1,0) | [-1,0) + [100,101) | 2020-01-01 | 2021-01-01 | [-1,0) | [-1,0) +(3 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[8,9)', '[8,9)', '[2018-01-01,2020-01-01)'); +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[8,9)', '[8,9)', '[2020-01-01,2021-01-01)'); +INSERT INTO temporal_fk2_per2rng (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)', '[8,9)'); +DELETE FROM temporal_rng2 WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [200,201) | 2018-01-01 | 2020-01-01 | [-1,0) | [-1,0) + [200,201) | 2020-01-01 | 2021-01-01 | [8,9) | [8,9) +(2 rows) + +-- +-- test FK referenced deletes SET DEFAULT (two scalar cols, SET DEFAULT subset) +-- +TRUNCATE temporal_rng2, temporal_fk2_per2rng; +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[-1,-1]', '[6,7)', '(,)'); +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[6,7)', '[6,7)', '[2018-01-01,2021-01-01)'); +INSERT INTO temporal_fk2_per2rng (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)', '[6,7)'); +-- fails because you can't set the PERIOD column: +ALTER TABLE temporal_fk2_per2rng + ALTER COLUMN parent_id1 SET DEFAULT '[-1,-1]', + DROP CONSTRAINT temporal_fk2_per2rng_fk, + ADD CONSTRAINT temporal_fk2_per2rng_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_rng2 + ON DELETE SET DEFAULT (valid_at) ON UPDATE SET DEFAULT; +ERROR: column "valid_at" referenced in ON DELETE SET action cannot be PERIOD +-- ok: +ALTER TABLE temporal_fk2_per2rng + ALTER COLUMN parent_id1 SET DEFAULT '[-1,-1]', + DROP CONSTRAINT temporal_fk2_per2rng_fk, + ADD CONSTRAINT temporal_fk2_per2rng_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_rng2 + ON DELETE SET DEFAULT (parent_id1) ON UPDATE SET DEFAULT; +-- leftovers on both sides: +DELETE FROM temporal_rng2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [100,101) | 2018-01-01 | 2019-01-01 | [6,7) | [6,7) + [100,101) | 2019-01-01 | 2020-01-01 | [-1,0) | [6,7) + [100,101) | 2020-01-01 | 2021-01-01 | [6,7) | [6,7) +(3 rows) + +-- non-FPO update: +DELETE FROM temporal_rng2 WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [100,101) | 2018-01-01 | 2019-01-01 | [-1,0) | [6,7) + [100,101) | 2019-01-01 | 2020-01-01 | [-1,0) | [6,7) + [100,101) | 2020-01-01 | 2021-01-01 | [-1,0) | [6,7) +(3 rows) + +-- FK across two referenced rows: +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[8,9)', '[8,9)', '[2018-01-01,2020-01-01)'); +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[8,9)', '[8,9)', '[2020-01-01,2021-01-01)'); +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[-1,-1]', '[8,9)', '(,)'); +INSERT INTO temporal_fk2_per2rng (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)', '[8,9)'); +DELETE FROM temporal_rng2 WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[200,200]' ORDER BY id, valid_at; + id | valid_from | valid_til | parent_id1 | parent_id2 +-----------+------------+------------+------------+------------ + [200,201) | 2018-01-01 | 2020-01-01 | [-1,0) | [8,9) + [200,201) | 2020-01-01 | 2021-01-01 | [8,9) | [8,9) +(2 rows) + +-- FK with a custom range type +CREATE TYPE mydaterange AS range(subtype=date); +CREATE TABLE temporal_rng3 ( + id int4range, + valid_at mydaterange, + CONSTRAINT temporal_rng3_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +); +CREATE TABLE temporal_fk3_rng2rng ( + id int4range, + valid_at mydaterange, + parent_id int4range, + CONSTRAINT temporal_fk3_rng2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk3_rng2rng_fk FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_rng3 (id, PERIOD valid_at) ON DELETE CASCADE +); +INSERT INTO temporal_rng3 (id, valid_at) VALUES ('[8,9)', mydaterange('2018-01-01', '2021-01-01')); +INSERT INTO temporal_fk3_rng2rng (id, valid_at, parent_id) VALUES ('[5,6)', mydaterange('2018-01-01', '2021-01-01'), '[8,9)'); +DELETE FROM temporal_rng3 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id = '[8,9)'; +SELECT * FROM temporal_fk3_rng2rng WHERE id = '[5,6)'; + id | valid_at | parent_id +-------+-------------------------+----------- + [5,6) | [2018-01-01,2019-01-01) | [8,9) + [5,6) | [2020-01-01,2021-01-01) | [8,9) +(2 rows) + +DROP TABLE temporal_fk3_rng2rng; +DROP TABLE temporal_rng3; +DROP TYPE mydaterange; +-- +-- FK between partitioned tables: ranges +-- +CREATE TABLE temporal_partitioned_rng ( + id int4range, + valid_at daterange, + name text, + CONSTRAINT temporal_paritioned_rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +) PARTITION BY LIST (id); +CREATE TABLE tp1 PARTITION OF temporal_partitioned_rng FOR VALUES IN ('[1,2)', '[3,4)', '[5,6)', '[7,8)', '[9,10)', '[11,12)', '[13,14)', '[15,16)', '[17,18)', '[19,20)', '[21,22)', '[23,24)'); +CREATE TABLE tp2 PARTITION OF temporal_partitioned_rng FOR VALUES IN ('[0,1)', '[2,3)', '[4,5)', '[6,7)', '[8,9)', '[10,11)', '[12,13)', '[14,15)', '[16,17)', '[18,19)', '[20,21)', '[22,23)', '[24,25)'); +INSERT INTO temporal_partitioned_rng (id, valid_at, name) VALUES + ('[1,2)', daterange('2000-01-01', '2000-02-01'), 'one'), + ('[1,2)', daterange('2000-02-01', '2000-03-01'), 'one'), + ('[2,3)', daterange('2000-01-01', '2010-01-01'), 'two'); +CREATE TABLE temporal_partitioned_fk_rng2rng ( + id int4range, + valid_at daterange, + parent_id int4range, + CONSTRAINT temporal_partitioned_fk_rng2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_partitioned_fk_rng2rng_fk FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_partitioned_rng (id, PERIOD valid_at) +) PARTITION BY LIST (id); +CREATE TABLE tfkp1 PARTITION OF temporal_partitioned_fk_rng2rng FOR VALUES IN ('[1,2)', '[3,4)', '[5,6)', '[7,8)', '[9,10)', '[11,12)', '[13,14)', '[15,16)', '[17,18)', '[19,20)', '[21,22)', '[23,24)'); +CREATE TABLE tfkp2 PARTITION OF temporal_partitioned_fk_rng2rng FOR VALUES IN ('[0,1)', '[2,3)', '[4,5)', '[6,7)', '[8,9)', '[10,11)', '[12,13)', '[14,15)', '[16,17)', '[18,19)', '[20,21)', '[22,23)', '[24,25)'); +-- +-- partitioned FK referencing inserts +-- +INSERT INTO temporal_partitioned_fk_rng2rng (id, valid_at, parent_id) VALUES + ('[1,2)', daterange('2000-01-01', '2000-02-15'), '[1,2)'), + ('[1,2)', daterange('2001-01-01', '2002-01-01'), '[2,3)'), + ('[2,3)', daterange('2000-01-01', '2000-02-15'), '[1,2)'); +-- should fail: +INSERT INTO temporal_partitioned_fk_rng2rng (id, valid_at, parent_id) VALUES + ('[3,4)', daterange('2010-01-01', '2010-02-15'), '[1,2)'); +ERROR: insert or update on table "tfkp1" violates foreign key constraint "temporal_partitioned_fk_rng2rng_fk" +DETAIL: Key (parent_id, valid_at)=([1,2), [2010-01-01,2010-02-15)) is not present in table "temporal_partitioned_rng". +INSERT INTO temporal_partitioned_fk_rng2rng (id, valid_at, parent_id) VALUES + ('[3,4)', daterange('2000-01-01', '2000-02-15'), '[3,4)'); +ERROR: insert or update on table "tfkp1" violates foreign key constraint "temporal_partitioned_fk_rng2rng_fk" +DETAIL: Key (parent_id, valid_at)=([3,4), [2000-01-01,2000-02-15)) is not present in table "temporal_partitioned_rng". +-- +-- partitioned FK referencing updates +-- +UPDATE temporal_partitioned_fk_rng2rng SET valid_at = daterange('2000-01-01', '2000-02-13') WHERE id = '[2,3)'; +-- move a row from the first partition to the second +UPDATE temporal_partitioned_fk_rng2rng SET id = '[4,5)' WHERE id = '[1,2)'; +-- move a row from the second partition to the first +UPDATE temporal_partitioned_fk_rng2rng SET id = '[1,2)' WHERE id = '[4,5)'; -- should fail: UPDATE temporal_partitioned_fk_rng2rng SET valid_at = daterange('2000-01-01', '2000-04-01') WHERE id = '[1,2)'; ERROR: conflicting key value violates exclusion constraint "tfkp1_pkey" @@ -4220,4 +8524,18 @@ SELECT * FROM temporal_partitioned_fk_mltrng2mltrng WHERE id = '[15,16)'; DROP TABLE temporal_partitioned_fk_mltrng2mltrng; DROP TABLE temporal_partitioned_mltrng; +-- +-- FK between partitioned tables: PERIODs +-- +CREATE TABLE temporal_partitioned_per ( + id int4range, + valid_from date, + valid_til date, + name text, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_paritioned_rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +) PARTITION BY LIST (id); +CREATE TABLE tp1 PARTITION OF temporal_partitioned_per FOR VALUES IN ('[1,2)', '[3,4)', '[5,6)', '[7,8)', '[9,10)', '[11,12)', '[13,14)', '[15,16)', '[17,18)', '[19,20)', '[21,22)', '[23,24)'); +ERROR: Inheriting from a table with a PERIOD is not supported +DROP TABLE temporal_partitioned_per; RESET datestyle; diff --git a/src/test/regress/parallel_schedule b/src/test/regress/parallel_schedule index 84435026217..60308d42bb7 100644 --- a/src/test/regress/parallel_schedule +++ b/src/test/regress/parallel_schedule @@ -43,7 +43,7 @@ test: copy copyselect copydml copyencoding insert insert_conflict # Note: many of the tests in later groups depend on create_index # ---------- test: create_function_c create_misc create_operator create_procedure create_table create_type create_schema -test: create_index create_index_spgist create_view index_including index_including_gist +test: create_index create_index_spgist create_view index_including index_including_gist periods # ---------- # Another group of parallel tests diff --git a/src/test/regress/sql/for_portion_of.sql b/src/test/regress/sql/for_portion_of.sql index 7493cc4c233..0b3361a1420 100644 --- a/src/test/regress/sql/for_portion_of.sql +++ b/src/test/regress/sql/for_portion_of.sql @@ -801,6 +801,56 @@ SELECT * FROM for_portion_of_test2 ORDER BY id, valid_at; DROP TABLE for_portion_of_test2; +-- Test with PERIODs + +CREATE TABLE for_portion_of_test2 ( + id int4range NOT NULL, + valid_from date, + valid_til date, + name text NOT NULL, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT for_portion_of_test2_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +); +INSERT INTO for_portion_of_test2 +VALUES +('[1,2)', '2018-01-02', '2018-02-03', 'one'), +('[1,2)', '2018-02-04', '2018-03-03', 'one'), +('[1,2)', '2018-03-03', '2018-04-04', 'one'), +('[2,3)', '2018-01-01', '2018-05-01', 'two'), +('[3,4)', '2018-01-01', null, 'three'); +; + +UPDATE for_portion_of_test2 +FOR PORTION OF valid_at FROM '2018-01-10' TO '2018-02-10' +SET name = 'one^1' +WHERE id = '[1,2)'; + +DELETE FROM for_portion_of_test2 +FOR PORTION OF valid_at FROM '2018-01-15' TO '2018-02-15' +WHERE id = '[2,3)'; + +-- Setting the start column fails: +UPDATE for_portion_of_test2 +FOR PORTION OF valid_at FROM '2018-01-10' TO '2018-02-10' +SET valid_from = '2018-02-01' +WHERE id = '[1,2)'; + +-- Setting the end column fails: +UPDATE for_portion_of_test2 +FOR PORTION OF valid_at FROM '2018-01-10' TO '2018-02-10' +SET valid_til = '2018-02-01' +WHERE id = '[1,2)'; + +-- Setting the generated column fails: +UPDATE for_portion_of_test2 +FOR PORTION OF valid_at FROM '2018-01-10' TO '2018-02-10' +SET valid_at = '[2018-02-01,2018-02-03)' +WHERE id = '[1,2)'; + +SELECT * FROM for_portion_of_test2 ORDER BY id, valid_at; + +DROP TABLE for_portion_of_test2; + -- Test with a custom range type CREATE TYPE mydaterange AS range(subtype=date); diff --git a/src/test/regress/sql/periods.sql b/src/test/regress/sql/periods.sql new file mode 100644 index 00000000000..04a30110dec --- /dev/null +++ b/src/test/regress/sql/periods.sql @@ -0,0 +1,198 @@ +/* System periods are not implemented */ +create table pt (id integer, ds date, de date, period for system_time (ds, de)); + +/* Periods must specify actual columns */ +create table pt (id integer, ds date, de date, period for p (bogus, de)); +create table pt (id integer, ds date, de date, period for p (ds, bogus)); + +/* Data types must match exactly */ +create table pt (id integer, ds date, de timestamp, period for p (ds, de)); +create table pt (id integer, ds text collate "C", de text collate "POSIX", period for p (ds, de)); + +/* Periods must have a default BTree operator class */ +create table pt (id integer, ds xml, de xml, period for p (ds, de)); + +/* Period and column names are in the same namespace */ +create table pt (id integer, ds date, de date, period for ctid (ds, de)); +create table pt (id integer, ds date, de date, period for id (ds, de)); + +/* Period name can't be given more than once */ +create table pt (id integer, ds date, de date, period for p (ds, de), period for p (ds, de)); + +/* Period can't use the same colum for start and end*/ +create table pt (id integer, ds date, de date, period for p (ds, ds)); + +/* Now make one that works */ +create table pt (id integer, ds date, de date, period for p (ds, de)); +/* Its generated column looks good */ +select attname, atttypid::regtype, attnotnull, attgenerated from pg_attribute where attrelid = 'pt'::regclass and attname = 'p'; +select conname, contype from pg_constraint where conrelid = 'pt'::regclass order by conname; +/* It appears in the information_schema */ +select * from information_schema.periods; + +/* SELECT * excludes the PERIOD */ +insert into pt values (1, '2000-01-01', '2001-01-01'); +select * from pt; + +/* You can get it if you want */ +select *, p from pt; + +/* You can comment on it */ +comment on period pt.p is 'test comment'; +select obj_description((select oid from pg_period where perrelid = 'pt'::regclass and pername = 'p'), 'pg_period'); + +/* Two are okay */ +create table pt2 (id integer, ds date, de date, period for p1 (ds, de), period for p2 (ds, de)); +drop table pt2; + +/* Skip creating GENERATED column: works */ +create table pt2 (id integer, ds date, de date, p daterange not null generated always as (daterange(ds, de)) stored, period for p (ds, de) with (colexists = true)); +\d pt2 +drop table pt2; +/* Skip creating GENERATED column: fails because the col isn't there */ +create table pt2 (id integer, ds date, de date, period for p (ds, de) with (colexists = true)); +/* Skip creating GENERATED column: fails because the option has an invalid value */ +create table pt2 (id integer, ds date, de date, period for p (ds, de) with (colexists = 'whatever')); +/* Skip creating GENERATED column: fails because the column is not NOT NULL */ +create table pt2 (id integer, ds date, de date, p daterange generated always as (daterange(ds, de)) stored, period for p (ds, de) with (colexists = true)); +/* Skip creating GENERATED column: fails because the column is not GENERATED */ +create table pt2 (id integer, ds date, de date, p daterange not null, period for p (ds, de) with (colexists = true)); +/* Skip creating GENERATED column: fails because the column is GENERATED but with the wrong expression */ +-- TODO: +-- create table pt2 (id integer, ds date, de date, p daterange not null generated always as (daterange(de, ds)) stored, period for p (ds, de) with (colexists = true)); +/* Skip creating GENERATED column: fails because the column is the wrong type */ +create table pt2 (id integer, ds date, de date, p tsrange not null generated always as (tsrange(ds, de)) stored, period for p (ds, de) with (colexists = true)); +/* Skip creating GENERATED column: fails because the column is inherited */ +create table pt2parent (id integer, ds date, de date, p daterange not null generated always as (daterange(ds, de)) stored); +create table pt2 (period for p (ds, de) with (colexists = true)) inherits (pt2parent); +drop table pt2parent; + + +/* + * ALTER TABLE tests + */ +alter table pt drop period for p; +alter table pt add period for system_time (ds, de); +alter table pt add period for p (ds, de); +/* Its generated column looks good */ +select attname, atttypid::regtype, attnotnull, attgenerated from pg_attribute where attrelid = 'pt'::regclass and attname = 'p'; +select conname, contype from pg_constraint where conrelid = 'pt'::regclass order by conname; + +/* Adding a second one */ +create table pt2 (id integer, ds date, de date, period for p1 (ds, de)); +alter table pt2 add period for p2 (ds, de); +drop table pt2; + +/* Can't drop its columns */ +alter table pt drop column ds; +alter table pt drop column de; + +/* Can't change the data types */ +alter table pt alter column ds type timestamp; +alter table pt alter column ds type timestamp; + +/* column/period namespace conflicts */ +alter table pt add column p integer; +alter table pt rename column id to p; +alter table pt add period for tableoid (ds, de); +alter table pt add period for "........pg.dropped.4........" (ds, de); + +/* adding columns and the period at the same time */ +create table pt2 (id integer); +alter table pt2 add column ds date, add column de date, add period for p (ds, de); +drop table pt2; + +/* Ambiguous range types raise an error */ +create type mydaterange as range(subtype=date); +create table pt2 (id int, ds date, de date, period for p (ds, de)); + +/* You can give an explicit range type */ +create table pt2 (id int, ds date, de date, period for p (ds, de) with (rangetype = 'mydaterange')); +drop type mydaterange; +drop type mydaterange cascade; +drop table pt2; +create table pt2 (id int, ds date, de date, period for p (ds, de) with (rangetype = 'daterange')); + +/* Range type is not found */ +create table pt3 (id int, ds date, de date, period for p (ds, de) with (rangetype = 'notarange')); + +/* Range type is the wrong type */ +create table pt3 (id int, ds date, de date, period for p (ds, de) with (rangetype = 'tstzrange')); +drop table pt2; + +/* Period can't use the same colum for start and end*/ +create table pt2 (id integer, ds date, de date); +alter table pt2 add period for p (ds, ds); +drop table pt2; + +/* Skip creating GENERATED column: works */ +create table pt2 (id integer, ds date, de date, p daterange not null generated always as (daterange(ds, de)) stored); +alter table pt2 add period for p (ds, de) with (colexists = true); +\d pt2 +drop table pt2; +/* Skip creating GENERATED column: fails because the col isn't there */ +create table pt2 (id integer, ds date, de date); +alter table pt2 add period for p (ds, de) with (colexists = true); +drop table pt2; +/* Skip creating GENERATED column: fails because the option has an invalid value */ +create table pt2 (id integer, ds date, de date, p daterange not null generated always as (daterange(ds, de)) stored); +alter table pt2 add period for p (ds, de) with (colexists = 'whatever'); +drop table pt2; +/* Skip creating GENERATED column: fails because the column is not NOT NULL */ +create table pt2 (id integer, ds date, de date, p daterange generated always as (daterange(ds, de)) stored); +alter table pt2 add period for p (ds, de) with (colexists = true); +drop table pt2; +/* Skip creating GENERATED column: fails because the column is not GENERATED */ +create table pt2 (id integer, ds date, de date, p daterange not null); +alter table pt2 add period for p (ds, de) with (colexists = true); +drop table pt2; +/* Skip creating GENERATED column: fails because the column is GENERATED but with the wrong expression */ +-- TODO: +-- create table pt2 (id integer, ds date, de date, p daterange not null generated always as (daterange(de, ds)) stored); +-- alter table pt2 add period for p (ds, de) with (colexists = true); +/* Skip creating GENERATED column: fails because the column is the wrong type */ +create table pt2 (id integer, ds date, de date, p tsrange not null generated always as (tsrange(ds, de)) stored); +alter table pt2 add period for p (ds, de) with (colexists = true); +drop table pt2; +/* Skip creating GENERATED column: fails because the column is inherited */ +create table pt2parent (id integer, ds date, de date, p daterange not null generated always as (daterange(ds, de)) stored); +create table pt2 () inherits (pt2parent); +alter table pt2 add period for p (ds, de) with (colexists = true); +drop table pt2; +drop table pt2parent; + +/* CREATE TABLE (LIKE ...) */ + +/* Periods are not copied by LIKE, so their columns aren't either */ +create table pt2 (like pt); +\d pt2 +drop table pt2; + +/* Can add a period referring to LIKE'd columns */ +create table not_p (id integer, ds date, de date); +create table pt2 (like not_p, period for p (ds, de)); +\d pt2 +drop table pt2; + +/* Can add a period with the same name */ +create table pt2 (like pt, period for p (ds, de)); +\d pt2 +drop table pt2; + +/* Can add a period with a different name */ +create table pt2 (like pt, period for p2 (ds, de)); +\d pt2 +drop table pt2; + +/* Can't add a period whose name conflicts with a LIKE'd column */ +create table pt2 (like pt, period for id (ds, de)); + +/* CREATE TALBE INHERITS */ + +/* Can't inherit from a table with a period */ +create table pt2 (name text) inherits (pt); + +/* Can't inherit with a period */ +create table pt2 (d2s date, d2e date, period for p (d2s, d2e)) inherits (not_p); + +drop table not_p; diff --git a/src/test/regress/sql/privileges.sql b/src/test/regress/sql/privileges.sql index ae57f233314..c36b1d34af5 100644 --- a/src/test/regress/sql/privileges.sql +++ b/src/test/regress/sql/privileges.sql @@ -783,6 +783,34 @@ UPDATE t1 FOR PORTION OF valid_at FROM '2000-01-01' TO '2001-01-01' SET c1 = '[2 SET SESSION AUTHORIZATION regress_priv_user1; DROP TABLE t1; +-- test column-level privileges on the PERIOD used in FOR PORTION OF +SET SESSION AUTHORIZATION regress_priv_user1; +CREATE TABLE t1 ( + c1 int4range, + valid_from timestamp, + valid_til timestamp, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT t1pk PRIMARY KEY (c1, valid_at WITHOUT OVERLAPS) +); +GRANT SELECT ON t1 TO regress_priv_user2; +GRANT SELECT ON t1 TO regress_priv_user3; +GRANT SELECT ON t1 TO regress_priv_user4; +GRANT SELECT ON t1 TO regress_priv_user5; +GRANT UPDATE (c1) ON t1 TO regress_priv_user2; +GRANT UPDATE (c1, valid_at) ON t1 TO regress_priv_user3; +GRANT UPDATE (c1, valid_from, valid_til) ON t1 TO regress_priv_user4; +GRANT UPDATE (c1, valid_from, valid_til, valid_at) ON t1 TO regress_priv_user5; +SET SESSION AUTHORIZATION regress_priv_user2; +UPDATE t1 FOR PORTION OF valid_at FROM '2000-01-01' TO '2001-01-01' SET c1 = '[2,3)'; +SET SESSION AUTHORIZATION regress_priv_user3; +UPDATE t1 FOR PORTION OF valid_at FROM '2000-01-01' TO '2001-01-01' SET c1 = '[2,3)'; +SET SESSION AUTHORIZATION regress_priv_user4; +UPDATE t1 FOR PORTION OF valid_at FROM '2000-01-01' TO '2001-01-01' SET c1 = '[2,3)'; +SET SESSION AUTHORIZATION regress_priv_user5; +UPDATE t1 FOR PORTION OF valid_at FROM '2000-01-01' TO '2001-01-01' SET c1 = '[2,3)'; +SET SESSION AUTHORIZATION regress_priv_user1; +DROP TABLE t1; + -- test column-level privileges when involved with DELETE SET SESSION AUTHORIZATION regress_priv_user1; ALTER TABLE atest6 ADD COLUMN three integer; diff --git a/src/test/regress/sql/without_overlaps.sql b/src/test/regress/sql/without_overlaps.sql index 224ddef8430..188041e1d07 100644 --- a/src/test/regress/sql/without_overlaps.sql +++ b/src/test/regress/sql/without_overlaps.sql @@ -91,6 +91,31 @@ CREATE TABLE temporal_rng2 ( SELECT pg_get_constraintdef(oid) FROM pg_constraint WHERE conname = 'temporal_rng2_pk'; SELECT pg_get_indexdef(conindid, 0, true) FROM pg_constraint WHERE conname = 'temporal_rng2_pk'; +-- PK with one column plus a PERIOD: +CREATE TABLE temporal_per ( + id int4range, + valid_from date, + valid_til date, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +); +\d temporal_per +SELECT pg_get_constraintdef(oid) FROM pg_constraint WHERE conname = 'temporal_per_pk'; +SELECT pg_get_indexdef(conindid, 0, true) FROM pg_constraint WHERE conname = 'temporal_per_pk'; + +-- PK with two columns plus a PERIOD: +CREATE TABLE temporal_per2 ( + id1 int4range, + id2 int4range, + valid_from date, + valid_til date, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_per2_pk PRIMARY KEY (id1, id2, valid_at WITHOUT OVERLAPS) +); +\d temporal_per2 +SELECT pg_get_constraintdef(oid) FROM pg_constraint WHERE conname = 'temporal_per2_pk'; +SELECT pg_get_indexdef(conindid, 0, true) FROM pg_constraint WHERE conname = 'temporal_per2_pk'; + -- PK with a custom range type: CREATE TYPE textrange2 AS range (subtype=text, collation="C"); CREATE TABLE temporal_rng3 ( @@ -169,6 +194,33 @@ SELECT pg_get_constraintdef(oid) FROM pg_constraint WHERE conname = 'temporal_rn SELECT pg_get_indexdef(conindid, 0, true) FROM pg_constraint WHERE conname = 'temporal_rng3_uq'; DROP TABLE temporal_rng3; +-- UNIQUE with one column plus a PERIOD: +CREATE TABLE temporal_per3 ( + id int4range, + valid_from date, + valid_til date, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_per3_uq UNIQUE (id, valid_at WITHOUT OVERLAPS) +); +\d temporal_per3 +SELECT pg_get_constraintdef(oid) FROM pg_constraint WHERE conname = 'temporal_per3_uq'; +SELECT pg_get_indexdef(conindid, 0, true) FROM pg_constraint WHERE conname = 'temporal_per3_uq'; +DROP TABLE temporal_per3; + +-- UNIQUE with two columns plus a PERIOD: +CREATE TABLE temporal_per3 ( + id1 int4range, + id2 int4range, + valid_from date, + valid_til date, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_per3_uq UNIQUE (id1, id2, valid_at WITHOUT OVERLAPS) +); +\d temporal_per3 +SELECT pg_get_constraintdef(oid) FROM pg_constraint WHERE conname = 'temporal_per3_uq'; +SELECT pg_get_indexdef(conindid, 0, true) FROM pg_constraint WHERE conname = 'temporal_per3_uq'; +DROP TABLE temporal_per3; + -- UNIQUE with a custom range type: CREATE TYPE textrange2 AS range (subtype=text, collation="C"); CREATE TABLE temporal_rng3 ( @@ -245,6 +297,58 @@ ALTER TABLE temporal3 UNIQUE (id, valid_at WITHOUT OVERLAPS); DROP TABLE temporal3; +-- PRIMARY KEY with PERIOD already there +CREATE TABLE temporal3 ( + id int4range, + valid_from date, + valid_til date, + PERIOD FOR valid_at (valid_from, valid_til) +); +ALTER TABLE temporal3 + ADD CONSTRAINT temporal3_pk + PRIMARY KEY (id, valid_at WITHOUT OVERLAPS); +\d temporal3 +DROP TABLE temporal3; + +-- PRIMARY KEY with PERIOD too +CREATE TABLE temporal3 ( + id int4range, + valid_from date, + valid_til date +); +ALTER TABLE temporal3 + ADD PERIOD FOR valid_at (valid_from, valid_til), + ADD CONSTRAINT temporal3_pk + PRIMARY KEY (id, valid_at WITHOUT OVERLAPS); +\d temporal3 +DROP TABLE temporal3; + +-- UNIQUE with PERIOD already there +CREATE TABLE temporal3 ( + id int4range, + valid_from date, + valid_til date, + PERIOD FOR valid_at (valid_from, valid_til) +); +ALTER TABLE temporal3 + ADD CONSTRAINT temporal3_uq + UNIQUE (id, valid_at WITHOUT OVERLAPS); +\d temporal3 +DROP TABLE temporal3; + +-- UNIQUE with PERIOD too +CREATE TABLE temporal3 ( + id int4range, + valid_from date, + valid_til date +); +ALTER TABLE temporal3 + ADD PERIOD FOR valid_at (valid_from, valid_til), + ADD CONSTRAINT temporal3_uq + UNIQUE (id, valid_at WITHOUT OVERLAPS); +\d temporal3 +DROP TABLE temporal3; + -- -- range PK: test with existing rows -- @@ -615,6 +719,212 @@ WHERE id = '[1,2)' AND valid_at IS NULL; SELECT * FROM temporal_mltrng3 ORDER BY id, valid_at; DROP TABLE temporal_mltrng3; +-- +-- PERIOD PK: test with existing rows +-- + +ALTER TABLE temporal_per DROP CONSTRAINT temporal_per_pk; + +-- okay: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2018-01-02', '2018-02-03'); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2018-03-03', '2018-04-04'); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[2,3)', '2018-01-01', '2018-01-05'); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[3,4)', '2018-01-01', NULL); +ALTER TABLE temporal_per ADD CONSTRAINT temporal_per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS); +ALTER TABLE temporal_per DROP CONSTRAINT temporal_per_pk; + +-- should fail: +BEGIN; + INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2018-01-01', '2018-01-05'); + ALTER TABLE temporal_per ADD CONSTRAINT temporal_per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS); +ROLLBACK; +-- rejects empty: +BEGIN; + ALTER TABLE temporal_per DROP PERIOD FOR valid_at; + INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[3,4)', '2020-01-01', '2020-01-01'); + ALTER TABLE temporal_per ADD PERIOD FOR valid_at (valid_from, valid_til); +ROLLBACK; +ALTER TABLE temporal_per ADD CONSTRAINT temporal_per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS); +DELETE FROM temporal_per; + +-- +-- PERIOD PK: test inserts +-- + +-- okay: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2018-01-02', '2018-02-03'); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2018-03-03', '2018-04-04'); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[2,3)', '2018-01-01', '2018-01-05'); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[3,4)', '2018-01-01', NULL); + +-- should fail: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2018-01-01', '2018-01-05'); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES (NULL, '2018-01-01', '2018-01-05'); +-- rejects empty: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[3,4)', '2020-01-01', '2020-01-01'); +SELECT * FROM temporal_per ORDER BY id, valid_at; + +-- +-- PERIOD PK: test updates +-- + +-- update the scalar part +UPDATE temporal_per +SET id = '[11,12)' +WHERE id = '[1,2)' +AND valid_at @> '2018-01-15'::date; +-- update the PERIOD bounds +UPDATE temporal_per +SET valid_from = '2020-01-01', + valid_til = '2021-01-01' +WHERE id = '[11,12)' +AND valid_at @> '2018-01-15'::date; +-- update both at once +UPDATE temporal_per +SET id = '[21,22)', + valid_from = '2018-01-02', + valid_til = '2018-02-03' +WHERE id = '[11,12)' +AND valid_at @> '2020-01-15'::date; +SELECT * FROM temporal_per ORDER BY id, valid_at; +-- should fail: +UPDATE temporal_per +SET id = '[1,2)', + valid_from = '2018-03-05', + valid_til = '2018-05-05' +WHERE id = '[21,22)'; +-- set the scalar part to NULL +UPDATE temporal_per +SET id = NULL, + valid_from = '2018-03-05', + valid_til = '2018-05-05' +WHERE id = '[21,22)'; +-- set a PERIOD bound to NULL (okay) +UPDATE temporal_per +SET id = '[1,2)', + valid_from = '2018-04-04', + valid_til = NULL +WHERE id = '[21,22)'; +-- rejects empty: +UPDATE temporal_per +SET id = '[21,22)', + valid_from = '2020-01-01', + valid_til = '2020-01-01' +WHERE id = '[1,2)'; +SELECT * FROM temporal_per ORDER BY id, valid_at; + +-- +-- PERIOD UQ: test with existing rows +-- + +CREATE TABLE temporal_per3 ( + id int4range, + valid_from date, + valid_til date, + PERIOD FOR valid_at (valid_from, valid_til) +); + +-- okay: +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2018-01-02', '2018-02-03'); +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2018-03-03', '2018-04-04'); +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[2,3)', '2018-01-01', '2018-01-05'); +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[3,4)', '2018-01-01', NULL); +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES (NULL, '2018-01-01', '2018-01-05'); +ALTER TABLE temporal_per3 ADD CONSTRAINT temporal_per3_uq UNIQUE (id, valid_at WITHOUT OVERLAPS); +ALTER TABLE temporal_per3 DROP CONSTRAINT temporal_per3_uq; + +-- should fail: +BEGIN; + INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2018-01-01', '2018-01-05'); + ALTER TABLE temporal_per3 ADD CONSTRAINT temporal_per3_uq UNIQUE (id, valid_at WITHOUT OVERLAPS); +ROLLBACK; +-- rejects empty: +BEGIN; + ALTER TABLE temporal_per3 DROP PERIOD FOR valid_at; + INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[3,4)', '2020-01-01', '2020-01-01'); + ALTER TABLE temporal_per3 ADD PERIOD FOR valid_at (valid_from, valid_til); +ROLLBACK; +ALTER TABLE temporal_per3 ADD CONSTRAINT temporal_per3_uq UNIQUE (id, valid_at WITHOUT OVERLAPS); +DELETE FROM temporal_per3; + +-- +-- PERIOD UQ: test inserts +-- + +-- okay: +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2018-01-02', '2018-02-03'); +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2018-03-03', '2018-04-04'); +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[2,3)', '2018-01-01', '2018-01-05'); +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[3,4)', '2018-01-01', NULL); +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES (NULL, '2018-01-01', '2018-01-05'); + +-- should fail: +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2018-01-01', '2018-01-05'); +-- rejects empty: +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[3,4)', '2020-01-01', '2020-01-01'); +SELECT * FROM temporal_per3 ORDER BY id, valid_at; + +-- +-- PERIOD UQ: test updates +-- + +-- update the scalar part +UPDATE temporal_per3 +SET id = '[11,12)' +WHERE id = '[1,2)' +AND valid_at @> '2018-01-15'::date; +-- update the PERIOD bounds +UPDATE temporal_per3 +SET valid_from = '2020-01-01', + valid_til = '2021-01-01' +WHERE id = '[11,12)' +AND valid_at @> '2018-01-15'::date; +-- update both at once +UPDATE temporal_per3 +SET id = '[21,22)', + valid_from = '2018-01-02', + valid_til = '2018-02-03' +WHERE id = '[11,12)' +AND valid_at @> '2020-01-15'::date; +SELECT * FROM temporal_per3 ORDER BY id, valid_at; +-- should fail: +UPDATE temporal_per3 +SET id = '[1,2)', + valid_from = '2018-03-05', + valid_til = '2018-05-05' +WHERE id = '[21,22)'; +-- set the scalar part to NULL (okay) +UPDATE temporal_per3 +SET id = NULL, + valid_from = '2020-01-01', + valid_til = '2021-01-01' +WHERE id = '[21,22)'; +-- set a PERIOD bound to NULL (okay) +UPDATE temporal_per3 +SET id = '[1,2)', + valid_from = '2018-04-04', + valid_til = NULL +WHERE id IS NULL AND valid_at @> '2020-06-01'::date; +SELECT * FROM temporal_per3 ORDER BY id, valid_at; +-- should fail: +UPDATE temporal_per3 +SET valid_from = '2018-03-01', + valid_til = '2018-05-05' +WHERE id = '[1,2)' AND valid_til IS NULL; +-- rejects empty: +UPDATE temporal_per3 +SET valid_from = '2020-01-01', + valid_til = '2020-01-01' +WHERE id = '[1,2)' AND valid_til IS NULL; +-- still rejects empty when scalar part is NULL: +UPDATE temporal_per3 +SET id = NULL, + valid_from = '2020-01-01', + valid_til = '2020-01-01' +WHERE id = '[1,2)' AND valid_til IS NULL; +SELECT * FROM temporal_per3 ORDER BY id, valid_at; +DROP TABLE temporal_per3; + -- -- test a range with both a PK and a UNIQUE constraint -- @@ -662,6 +972,37 @@ ALTER TABLE temporal3 ALTER COLUMN valid_at DROP NOT NULL; ALTER TABLE temporal3 ALTER COLUMN valid_at TYPE tstzrange USING tstzrange(lower(valid_at), upper(valid_at)); ALTER TABLE temporal3 RENAME COLUMN valid_at TO valid_thru; ALTER TABLE temporal3 DROP COLUMN valid_thru; +\d temporal3 +DROP TABLE temporal3; + +CREATE TABLE temporal3 ( + id int4range, + valid_from date, + valid_til date, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal3_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +); +\d temporal3 +SELECT attnotnull FROM pg_attribute WHERE attrelid = 'temporal3'::regclass AND attname = 'valid_at'; +ALTER TABLE temporal3 ALTER COLUMN valid_at DROP NOT NULL; +ALTER TABLE temporal3 ALTER COLUMN valid_at TYPE tstzrange USING tstzrange(lower(valid_at), upper(valid_at)); +ALTER TABLE temporal3 DROP COLUMN valid_at; +ALTER TABLE temporal3 DROP PERIOD FOR valid_at; +\d temporal3 +DROP TABLE temporal3; + +-- Same results if we add the PERIOD and PK from ALTER TABLE: +CREATE TABLE temporal3 ( + id int4range, + valid_from date, + valid_til date +); +ALTER TABLE temporal3 + ADD PERIOD FOR valid_at (valid_from, valid_til), + ADD CONSTRAINT temporal3_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS); +\d temporal3 +SELECT attnotnull FROM pg_attribute WHERE attrelid = 'temporal3'::regclass AND attname = 'valid_at'; +ALTER TABLE temporal3 ALTER COLUMN valid_at DROP NOT NULL; DROP TABLE temporal3; -- @@ -963,6 +1304,121 @@ SELECT * FROM temporal_mltrng3 ORDER BY id, valid_at; DROP TABLE temporal_mltrng3; +-- +-- ON CONFLICT: PERIODs +-- + +TRUNCATE temporal_per; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2000-01-01', '2010-01-01'); +-- with a conflict +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2005-01-01', '2006-01-01') ON CONFLICT DO NOTHING; +-- id matches but no conflict +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2010-01-01', '2020-01-01') ON CONFLICT DO NOTHING; +-- date matches but no conflict +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[2,3)', '2005-01-01', '2006-01-01') ON CONFLICT DO NOTHING; +SELECT * FROM temporal_per ORDER BY id, valid_from, valid_til; + +TRUNCATE temporal_per; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2000-01-01', '2010-01-01'); +-- with a conflict +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2005-01-01', '2006-01-01') ON CONFLICT (id, valid_at) DO NOTHING; +-- id matches but no conflict +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2010-01-01', '2020-01-01') ON CONFLICT (id, valid_at) DO NOTHING; +-- date matches but no conflict +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[2,3)', '2005-01-01', '2006-01-01') ON CONFLICT (id, valid_at) DO NOTHING; +SELECT * FROM temporal_per ORDER BY id, valid_from, valid_til; + +TRUNCATE temporal_per; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2000-01-01', '2010-01-01'); +-- with a conflict +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2005-01-01', '2006-01-01') ON CONFLICT ON CONSTRAINT temporal_per_pk DO NOTHING; +-- id matches but no conflict +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2010-01-01', '2020-01-01') ON CONFLICT ON CONSTRAINT temporal_per_pk DO NOTHING; +-- date matches but no conflict +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[2,3)', '2005-01-01', '2006-01-01') ON CONFLICT ON CONSTRAINT temporal_per_pk DO NOTHING; +SELECT * FROM temporal_per ORDER BY id, valid_from, valid_til; + +TRUNCATE temporal_per; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2000-01-01', '2010-01-01'); +-- with a conflict +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2005-01-01', '2006-01-01') ON CONFLICT (id, valid_at) DO UPDATE SET id = EXCLUDED.id + '[2,3)'; +-- id matches but no conflict +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2010-01-01', '2020-01-01') ON CONFLICT (id, valid_at) DO UPDATE SET id = EXCLUDED.id + '[3,4)'; +-- date matches but no conflict +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[2,3)', '2005-01-01', '2006-01-01') ON CONFLICT (id, valid_at) DO UPDATE SET id = EXCLUDED.id + '[4,5)'; +SELECT * FROM temporal_per ORDER BY id, valid_from, valid_til; + +TRUNCATE temporal_per; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2000-01-01', '2010-01-01'); +-- with a conflict +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2005-01-01', '2006-01-01') ON CONFLICT ON CONSTRAINT temporal_per_pk DO UPDATE SET id = EXCLUDED.id + '[2,3)'; +-- id matches but no conflict +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2010-01-01', '2020-01-01') ON CONFLICT ON CONSTRAINT temporal_per_pk DO UPDATE SET id = EXCLUDED.id + '[3,4)'; +-- date matches but no conflict +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[2,3)', '2005-01-01', '2006-01-01') ON CONFLICT ON CONSTRAINT temporal_per_pk DO UPDATE SET id = EXCLUDED.id + '[4,5)'; +SELECT * FROM temporal_per ORDER BY id, valid_from, valid_til; + +-- with a UNIQUE constraint: + +CREATE TABLE temporal_per3 ( + id int4range, + valid_from date, + valid_til date, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_per3_uq UNIQUE (id, valid_at WITHOUT OVERLAPS) +); +TRUNCATE temporal_per3; +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2000-01-01', '2010-01-01'); +-- with a conflict +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2005-01-01', '2006-01-01') ON CONFLICT DO NOTHING; +-- id matches but no conflict +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2010-01-01', '2020-01-01') ON CONFLICT DO NOTHING; +-- date matches but no conflict +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[2,3)', '2005-01-01', '2006-01-01') ON CONFLICT DO NOTHING; +SELECT * FROM temporal_per3 ORDER BY id, valid_from, valid_til; + +TRUNCATE temporal_per3; +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2000-01-01', '2010-01-01'); +-- with a conflict +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2005-01-01', '2006-01-01') ON CONFLICT (id, valid_at) DO NOTHING; +-- id matches but no conflict +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2010-01-01', '2020-01-01') ON CONFLICT (id, valid_at) DO NOTHING; +-- date matches but no conflict +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[2,3)', '2005-01-01', '2006-01-01') ON CONFLICT (id, valid_at) DO NOTHING; +SELECT * FROM temporal_per3 ORDER BY id, valid_from, valid_til; + +TRUNCATE temporal_per3; +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2000-01-01', '2010-01-01'); +-- with a conflict +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2005-01-01', '2006-01-01') ON CONFLICT ON CONSTRAINT temporal_per3_uq DO NOTHING; +-- id matches but no conflict +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2010-01-01', '2020-01-01') ON CONFLICT ON CONSTRAINT temporal_per3_uq DO NOTHING; +-- date matches but no conflict +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[2,3)', '2005-01-01', '2006-01-01') ON CONFLICT ON CONSTRAINT temporal_per3_uq DO NOTHING; +SELECT * FROM temporal_per3 ORDER BY id, valid_from, valid_til; + +TRUNCATE temporal_per3; +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2000-01-01', '2010-01-01'); +-- with a conflict +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2005-01-01', '2006-01-01') ON CONFLICT (id, valid_at) DO UPDATE SET id = EXCLUDED.id + '[2,3)'; +-- id matches but no conflict +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2010-01-01', '2020-01-01') ON CONFLICT (id, valid_at) DO UPDATE SET id = EXCLUDED.id + '[3,4)'; +-- date matches but no conflict +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[2,3)', '2005-01-01', '2006-01-01') ON CONFLICT (id, valid_at) DO UPDATE SET id = EXCLUDED.id + '[4,5)'; +SELECT * FROM temporal_per3 ORDER BY id, valid_from, valid_til; + +TRUNCATE temporal_per3; +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2000-01-01', '2010-01-01'); +-- with a conflict +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2005-01-01', '2006-01-01') ON CONFLICT ON CONSTRAINT temporal_per3_uq DO UPDATE SET id = EXCLUDED.id + '[2,3)'; +-- id matches but no conflict +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[1,2)', '2010-01-01', '2020-01-01') ON CONFLICT ON CONSTRAINT temporal_per3_uq DO UPDATE SET id = EXCLUDED.id + '[3,4)'; +-- date matches but no conflict +INSERT INTO temporal_per3 (id, valid_from, valid_til) VALUES ('[2,3)', '2005-01-01', '2006-01-01') ON CONFLICT ON CONSTRAINT temporal_per3_uq DO UPDATE SET id = EXCLUDED.id + '[4,5)'; +SELECT * FROM temporal_per3 ORDER BY id, valid_from, valid_til; + +DROP TABLE temporal_per3; + -- -- test FK dependencies -- @@ -986,6 +1442,29 @@ ALTER TABLE temporal3 DROP COLUMN valid_at CASCADE; DROP TABLE temporal_fk_rng2rng; DROP TABLE temporal3; +-- can't drop a PERIOD referenced by an FK, unless with CASCADE +CREATE TABLE temporal3 ( + id int4range, + valid_from date, + valid_til date, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal3_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +); +CREATE TABLE temporal_fk_per2per ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2per_fk FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal3 (id, PERIOD valid_at) +); +ALTER TABLE temporal3 DROP PERIOD FOR valid_at; +ALTER TABLE temporal3 DROP PERIOD FOR valid_at CASCADE; +DROP TABLE temporal_fk_per2per; +DROP TABLE temporal3; + -- -- test FOREIGN KEY, range references range -- @@ -1016,6 +1495,7 @@ CREATE TABLE temporal_fk_rng2rng ( CONSTRAINT temporal_fk_rng2rng_fk FOREIGN KEY (parent_id, PERIOD valid_at) REFERENCES temporal_rng (id, PERIOD valid_at) ); +\d temporal_fk_rng2rng DROP TABLE temporal_fk_rng2rng; -- with mismatched PERIOD columns: @@ -2567,47 +3047,2555 @@ INSERT INTO temporal_fk2_mltrng2mltrng (id, valid_at, parent_id1, parent_id2) VA DELETE FROM temporal_mltrng2 WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; SELECT * FROM temporal_fk2_mltrng2mltrng WHERE id = '[200,201)' ORDER BY id, valid_at; --- FK with a custom range type +-- +-- test FOREIGN KEY, PERIOD references PERIOD +-- -CREATE TYPE mydaterange AS range(subtype=date); +-- test table setup +DROP TABLE temporal_per; +CREATE TABLE temporal_per ( + id int4range, + valid_from date, + valid_til date, + PERIOD FOR valid_at (valid_from, valid_til) +); +ALTER TABLE temporal_per + ADD CONSTRAINT temporal_per_pk + PRIMARY KEY (id, valid_at WITHOUT OVERLAPS); -CREATE TABLE temporal_rng3 ( +-- Can't create a FK with a mismatched range type +CREATE TABLE temporal_fk_per2per ( id int4range, - valid_at mydaterange, - CONSTRAINT temporal_rng3_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) + valid_from int, + valid_til int, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2per_fk FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per (id, PERIOD valid_at) ); -CREATE TABLE temporal_fk3_rng2rng ( + +-- works: PERIOD for both referenced and referencing +CREATE TABLE temporal_fk_per2per ( id int4range, - valid_at mydaterange, + valid_from date, + valid_til date, parent_id int4range, - CONSTRAINT temporal_fk3_rng2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), - CONSTRAINT temporal_fk3_rng2rng_fk FOREIGN KEY (parent_id, PERIOD valid_at) - REFERENCES temporal_rng3 (id, PERIOD valid_at) ON DELETE CASCADE + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2per_fk FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per (id, PERIOD valid_at) ); -INSERT INTO temporal_rng3 (id, valid_at) VALUES ('[8,9)', mydaterange('2018-01-01', '2021-01-01')); -INSERT INTO temporal_fk3_rng2rng (id, valid_at, parent_id) VALUES ('[5,6)', mydaterange('2018-01-01', '2021-01-01'), '[8,9)'); -DELETE FROM temporal_rng3 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id = '[8,9)'; -SELECT * FROM temporal_fk3_rng2rng WHERE id = '[5,6)'; +\d temporal_fk_per2per -DROP TABLE temporal_fk3_rng2rng; -DROP TABLE temporal_rng3; -DROP TYPE mydaterange; +DROP TABLE temporal_fk_per2per; --- --- FK between partitioned tables: ranges --- +-- with mismatched PERIOD columns: -CREATE TABLE temporal_partitioned_rng ( +-- (parent_id, PERIOD valid_at) REFERENCES (id, valid_at) +-- REFERENCES part should specify PERIOD +CREATE TABLE temporal_fk_per2per ( id int4range, - valid_at daterange, - name text, - CONSTRAINT temporal_paritioned_rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) -) PARTITION BY LIST (id); -CREATE TABLE tp1 PARTITION OF temporal_partitioned_rng FOR VALUES IN ('[1,2)', '[3,4)', '[5,6)', '[7,8)', '[9,10)', '[11,12)', '[13,14)', '[15,16)', '[17,18)', '[19,20)', '[21,22)', '[23,24)'); -CREATE TABLE tp2 PARTITION OF temporal_partitioned_rng FOR VALUES IN ('[0,1)', '[2,3)', '[4,5)', '[6,7)', '[8,9)', '[10,11)', '[12,13)', '[14,15)', '[16,17)', '[18,19)', '[20,21)', '[22,23)', '[24,25)'); -INSERT INTO temporal_partitioned_rng (id, valid_at, name) VALUES - ('[1,2)', daterange('2000-01-01', '2000-02-01'), 'one'), - ('[1,2)', daterange('2000-02-01', '2000-03-01'), 'one'), + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2per_fk FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per (id, valid_at) +); +-- (parent_id, valid_at) REFERENCES (id, valid_at) +-- both should specify PERIOD: +CREATE TABLE temporal_fk_per2per ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2per_fk FOREIGN KEY (parent_id, valid_at) + REFERENCES temporal_per (id, valid_at) +); +-- (parent_id, valid_at) REFERENCES (id, PERIOD valid_at) +-- FOREIGN KEY part should specify PERIOD +CREATE TABLE temporal_fk_per2per ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2per_fk FOREIGN KEY (parent_id, valid_at) + REFERENCES temporal_per (id, PERIOD valid_at) +); +-- (parent_id, valid_at) REFERENCES [implicit] +-- FOREIGN KEY part should specify PERIOD +CREATE TABLE temporal_fk_per2per ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2per_fk FOREIGN KEY (parent_id, valid_at) + REFERENCES temporal_per +); +-- (parent_id, PERIOD valid_at) REFERENCES (id) +CREATE TABLE temporal_fk_per2per ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2per_fk FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per (id) +); +-- (parent_id) REFERENCES (id, PERIOD valid_at) +CREATE TABLE temporal_fk_per2per ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2per_fk FOREIGN KEY (parent_id) + REFERENCES temporal_per (id, PERIOD valid_at) +); +-- with inferred PK on the referenced table: +-- (parent_id, PERIOD valid_at) REFERENCES [implicit] +CREATE TABLE temporal_fk_per2per ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2per_fk FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per +); +DROP TABLE temporal_fk_per2per; +-- (parent_id) REFERENCES [implicit] +CREATE TABLE temporal_fk_per2per ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2per_fk FOREIGN KEY (parent_id) + REFERENCES temporal_per +); + +-- should fail because of duplicate referenced columns: +CREATE TABLE temporal_fk_per2per ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2per_fk FOREIGN KEY (parent_id, PERIOD parent_id) + REFERENCES temporal_per (id, PERIOD id) +); + +-- Two scalar columns +DROP TABLE temporal_per2; +CREATE TABLE temporal_per2 ( + id1 int4range, + id2 int4range, + valid_from date, + valid_til date, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_per2_pk PRIMARY KEY (id1, id2, valid_at WITHOUT OVERLAPS) +); + +CREATE TABLE temporal_fk2_per2per ( + id int4range, + valid_from date, + valid_til date, + parent_id1 int4range, + parent_id2 int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk2_per2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk2_per2per_fk FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_per2 (id1, id2, PERIOD valid_at) +); +\d temporal_fk2_per2per +DROP TABLE temporal_fk2_per2per; + +-- +-- test ALTER TABLE ADD CONSTRAINT +-- + +CREATE TABLE temporal_fk_per2per ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +); +ALTER TABLE temporal_fk_per2per + ADD CONSTRAINT temporal_fk_per2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per (id, PERIOD valid_at); +-- Two scalar columns: +CREATE TABLE temporal_fk2_per2per ( + id int4range, + valid_from date, + valid_til date, + parent_id1 int4range, + parent_id2 int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk2_per2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +); +ALTER TABLE temporal_fk2_per2per + ADD CONSTRAINT temporal_fk2_per2per_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_per2 (id1, id2, PERIOD valid_at); +\d temporal_fk2_per2per + +-- with inferred PK on the referenced table, and wrong column type: +ALTER TABLE temporal_fk_per2per + DROP CONSTRAINT temporal_fk_per2per_fk, + DROP PERIOD FOR valid_at, + ALTER COLUMN valid_from TYPE timestamp, + ALTER COLUMN valid_til TYPE timestamp, + ADD PERIOD FOR valid_at (valid_from, valid_til); +ALTER TABLE temporal_fk_per2per + ADD CONSTRAINT temporal_fk_per2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per; +ALTER TABLE temporal_fk_per2per + DROP PERIOD FOR valid_at, + ALTER COLUMN valid_from TYPE date, + ALTER COLUMN valid_til TYPE date, + ADD PERIOD FOR valid_at (valid_from, valid_til); + +-- with inferred PK on the referenced table: +ALTER TABLE temporal_fk_per2per + ADD CONSTRAINT temporal_fk_per2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per; + +-- should fail because of duplicate referenced columns: +ALTER TABLE temporal_fk_per2per + ADD CONSTRAINT temporal_fk_per2per_fk2 + FOREIGN KEY (parent_id, PERIOD parent_id) + REFERENCES temporal_per (id, PERIOD id); + +-- +-- test with rows already +-- + +DELETE FROM temporal_fk_per2per; +DELETE FROM temporal_per; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES + ('[1,2)', '2018-01-02', '2018-02-03'), + ('[1,2)', '2018-03-03', '2018-04-04'), + ('[2,3)', '2018-01-01', '2018-01-05'), + ('[3,4)', '2018-01-01', NULL); + +ALTER TABLE temporal_fk_per2per + DROP CONSTRAINT temporal_fk_per2per_fk; +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES ('[1,2)', '2018-01-02', '2018-02-01', '[1,2)'); +ALTER TABLE temporal_fk_per2per + ADD CONSTRAINT temporal_fk_per2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per; +ALTER TABLE temporal_fk_per2per + DROP CONSTRAINT temporal_fk_per2per_fk; +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES ('[2,3)', '2018-01-02', '2018-04-01', '[1,2)'); +-- should fail: +ALTER TABLE temporal_fk_per2per + ADD CONSTRAINT temporal_fk_per2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per; +-- okay again: +DELETE FROM temporal_fk_per2per; +ALTER TABLE temporal_fk_per2per + ADD CONSTRAINT temporal_fk_per2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per; + +-- +-- test pg_get_constraintdef +-- + +SELECT pg_get_constraintdef(oid) FROM pg_constraint WHERE conname = 'temporal_fk_per2per_fk'; + +-- +-- test FK referencing inserts +-- + +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES ('[1,2)', '2018-01-02', '2018-02-01', '[1,2)'); +-- should fail: +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES ('[2,3)', '2018-01-02', '2018-04-01', '[1,2)'); +-- now it should work: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2018-02-03', '2018-03-03'); +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES ('[2,3)', '2018-01-02', '2018-04-01', '[1,2)'); + +-- +-- test FK referencing updates +-- + +-- slide the edge across a referenced transition: +UPDATE temporal_fk_per2per SET valid_from = '2018-01-02', valid_til = '2018-02-20' WHERE id = '[1,2)'; +-- should fail: +UPDATE temporal_fk_per2per SET valid_from = '2018-01-02', valid_til = '2018-05-01' WHERE id = '[1,2)'; +UPDATE temporal_fk_per2per SET parent_id = '[8,9)' WHERE id = '[1,2)'; + +-- ALTER FK DEFERRABLE + +BEGIN; + INSERT INTO temporal_per (id, valid_from, valid_til) VALUES + ('[5,6)', '2018-01-01', '2018-02-01'), + ('[5,6)', '2018-02-01', '2018-03-01'); + INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES + ('[3,4)', '2018-01-05', '2018-01-10', '[5,6)'); + ALTER TABLE temporal_fk_per2per + ALTER CONSTRAINT temporal_fk_per2per_fk + DEFERRABLE INITIALLY DEFERRED; + + DELETE FROM temporal_per WHERE id = '[5,6)'; --should not fail yet. +COMMIT; -- should fail here. + +-- +-- test FK referenced updates NO ACTION +-- + +TRUNCATE temporal_per, temporal_fk_per2per; +ALTER TABLE temporal_fk_per2per + DROP CONSTRAINT temporal_fk_per2per_fk; +ALTER TABLE temporal_fk_per2per + ADD CONSTRAINT temporal_fk_per2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per + ON UPDATE NO ACTION; +-- a PK update that succeeds because the numeric id isn't referenced: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[5,6)', '2018-01-01', '2018-02-01'); +UPDATE temporal_per SET valid_from = '2016-01-01', valid_til = '2016-02-01' WHERE id = '[5,6)'; +-- a PK update that succeeds even though the numeric id is referenced because the range isn't: +DELETE FROM temporal_per WHERE id = '[5,6)'; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES + ('[5,6)', '2018-01-01', '2018-02-01'), + ('[5,6)', '2018-02-01', '2018-03-01'); +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES + ('[3,4)', '2018-01-05', '2018-01-10', '[5,6)'); +UPDATE temporal_per SET valid_from = '2016-02-01', valid_til = '2016-03-01' +WHERE id = '[5,6)' AND valid_from = '2018-02-01' AND valid_til = '2018-03-01'; +-- A PK update sliding the edge between two referenced rows: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES + ('[6,7)', '2018-01-01', '2018-02-01'), + ('[6,7)', '2018-02-01', '2018-03-01'); +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES + ('[4,5)', '2018-01-15', '2018-02-15', '[6,7)'); +UPDATE temporal_per +SET valid_from = CASE WHEN valid_from = '2018-01-01' THEN '2018-01-01' + WHEN valid_from = '2018-02-01' THEN '2018-01-05' END::date, + valid_til = CASE WHEN valid_from = '2018-01-01' THEN '2018-01-05' + WHEN valid_from = '2018-02-01' THEN '2018-03-01' END::date +WHERE id = '[6,7)'; +-- a PK update shrinking the referenced range but still valid: +-- There are two references: one fulfilled by the first pk row, +-- the other fulfilled by both pk rows combined. +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES + ('[1,2)', '2018-01-01', '2018-03-01'), + ('[1,2)', '2018-03-01', '2018-06-01'); +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES + ('[1,2)', '2018-01-15', '2018-02-01', '[1,2)'), + ('[2,3)', '2018-01-15', '2018-05-01', '[1,2)'); +UPDATE temporal_per SET valid_from = '2018-01-15', valid_til = '2018-03-01' +WHERE id = '[1,2)' AND valid_at @> '2018-01-15'::date; +-- a PK update growing the referenced range is fine: +UPDATE temporal_per SET valid_from = '2018-01-01', valid_til = '2018-03-01' +WHERE id = '[1,2)' AND valid_at @> '2018-01-25'::date; +-- a PK update shrinking the referenced range and changing the id invalidates the whole range: +UPDATE temporal_per SET id = '[2,3)', valid_from = '2018-01-15', valid_til = '2018-03-01' +WHERE id = '[1,2)' AND valid_at @> '2018-01-15'::date; +-- a PK update changing only the id invalidates the whole range: +UPDATE temporal_per SET id = '[2,3)' +WHERE id = '[1,2)' AND valid_at @> '2018-01-15'::date; +-- a PK update that loses time from both ends, but is still valid: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES + ('[2,3)', '2018-01-01', '2018-03-01'); +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES + ('[5,6)', '2018-01-15', '2018-02-01', '[2,3)'); +UPDATE temporal_per SET valid_from = '2018-01-15', valid_til = '2018-02-15' +WHERE id = '[2,3)'; +-- a PK update that fails because both are referenced: +UPDATE temporal_per SET valid_from = '2016-01-01', valid_til = '2016-02-01' +WHERE id = '[5,6)' AND valid_from = '2018-01-01' AND valid_til = '2018-02-01'; +-- a PK update that fails because both are referenced, but not 'til commit: +BEGIN; + ALTER TABLE temporal_fk_per2per + ALTER CONSTRAINT temporal_fk_per2per_fk + DEFERRABLE INITIALLY DEFERRED; + + UPDATE temporal_per SET valid_from = '2016-01-01', valid_til = '2016-02-01' + WHERE id = '[5,6)' AND valid_at = daterange('2018-01-01', '2018-02-01'); +COMMIT; +-- changing the scalar part fails: +UPDATE temporal_per SET id = '[7,8)' +WHERE id = '[5,6)' AND valid_from = '2018-01-01' AND valid_til = '2018-02-01'; +-- changing an unreferenced part is okay: +UPDATE temporal_per +FOR PORTION OF valid_at FROM '2018-01-02' TO '2018-01-03' +SET id = '[7,8)' +WHERE id = '[5,6)'; +-- changing just a part fails: +UPDATE temporal_per +FOR PORTION OF valid_at FROM '2018-01-05' TO '2018-01-10' +SET id = '[7,8)' +WHERE id = '[5,6)'; +SELECT * FROM temporal_per WHERE id in ('[5,6)', '[7,8)') ORDER BY id, valid_at; +SELECT * FROM temporal_fk_per2per WHERE id in ('[3,4)') ORDER BY id, valid_at; +-- then delete the objecting FK record and the same PK update succeeds: +DELETE FROM temporal_fk_per2per WHERE id = '[3,4)'; +UPDATE temporal_per SET valid_from = '2016-01-01', valid_til = '2016-02-01' +WHERE id = '[5,6)' AND valid_from = '2018-01-01' AND valid_til = '2018-02-01'; + +-- +-- test FK referenced updates RESTRICT +-- + +TRUNCATE temporal_per, temporal_fk_per2per; +ALTER TABLE temporal_fk_per2per + DROP CONSTRAINT temporal_fk_per2per_fk; +ALTER TABLE temporal_fk_per2per + ADD CONSTRAINT temporal_fk_per2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per + ON UPDATE RESTRICT; + +-- +-- test FK referenced deletes NO ACTION +-- + +TRUNCATE temporal_per, temporal_fk_per2per; +ALTER TABLE temporal_fk_per2per + ADD CONSTRAINT temporal_fk_per2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per; +-- a PK delete that succeeds because the numeric id isn't referenced: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[5,6)', '2018-01-01', '2018-02-01'); +DELETE FROM temporal_per WHERE id = '[5,6)'; +-- a PK delete that succeeds even though the numeric id is referenced because the range isn't: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES + ('[5,6)', '2018-01-01', '2018-02-01'), + ('[5,6)', '2018-02-01', '2018-03-01'); +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES ('[3,4)', '2018-01-05', '2018-01-10', '[5,6)'); +DELETE FROM temporal_per WHERE id = '[5,6)' AND valid_from = '2018-02-01' AND valid_til = '2018-03-01'; +-- a PK delete that fails because both are referenced: +DELETE FROM temporal_per WHERE id = '[5,6)' AND valid_from = '2018-01-01' AND valid_til = '2018-02-01'; +-- a PK delete that fails because both are referenced, but not 'til commit: +BEGIN; + ALTER TABLE temporal_fk_per2per + ALTER CONSTRAINT temporal_fk_per2per_fk + DEFERRABLE INITIALLY DEFERRED; + + DELETE FROM temporal_per WHERE id = '[5,6)' AND valid_at = daterange('2018-01-01', '2018-02-01'); +COMMIT; +-- deleting an unreferenced part is okay: +DELETE FROM temporal_per +FOR PORTION OF valid_at FROM '2018-01-02' TO '2018-01-03' +WHERE id = '[5,6)'; +-- deleting just a part fails: +DELETE FROM temporal_per +FOR PORTION OF valid_at FROM '2018-01-05' TO '2018-01-10' +WHERE id = '[5,6)'; +SELECT * FROM temporal_per WHERE id in ('[5,6)', '[7,8)') ORDER BY id, valid_at; +SELECT * FROM temporal_fk_per2per WHERE id in ('[3,4)') ORDER BY id, valid_at; +-- then delete the objecting FK record and the same PK delete succeeds: +DELETE FROM temporal_fk_per2per WHERE id = '[3,4)'; +DELETE FROM temporal_per WHERE id = '[5,6)' AND valid_from = '2018-01-01' AND valid_til = '2018-02-01'; + +-- +-- test FK referenced deletes RESTRICT +-- + +TRUNCATE temporal_per, temporal_fk_per2per; +ALTER TABLE temporal_fk_per2per + DROP CONSTRAINT temporal_fk_per2per_fk; +ALTER TABLE temporal_fk_per2per + ADD CONSTRAINT temporal_fk_per2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per + ON DELETE RESTRICT; + +-- +-- per2per test ON UPDATE/DELETE options +-- +-- TOC: +-- referenced updates CASCADE +-- referenced deletes CASCADE +-- referenced updates SET NULL +-- referenced deletes SET NULL +-- referenced updates SET DEFAULT +-- referenced deletes SET DEFAULT +-- referenced updates CASCADE (two scalar cols) +-- referenced deletes CASCADE (two scalar cols) +-- referenced updates SET NULL (two scalar cols) +-- referenced deletes SET NULL (two scalar cols) +-- referenced deletes SET NULL (two scalar cols, SET NULL subset) +-- referenced updates SET DEFAULT (two scalar cols) +-- referenced deletes SET DEFAULT (two scalar cols) +-- referenced deletes SET DEFAULT (two scalar cols, SET DEFAULT subset) + +-- +-- test FK referenced updates CASCADE +-- + +TRUNCATE temporal_per, temporal_fk_per2per; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)'); +ALTER TABLE temporal_fk_per2per + ADD CONSTRAINT temporal_fk_per2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per + ON DELETE CASCADE ON UPDATE CASCADE; +-- leftovers on both sides: +UPDATE temporal_per FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' SET id = '[7,8)' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO update: +UPDATE temporal_per SET id = '[7,8)' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)'); +UPDATE temporal_per SET id = '[9,10)' WHERE id = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk_per2per WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced deletes CASCADE +-- + +TRUNCATE temporal_per, temporal_fk_per2per; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)'); +-- leftovers on both sides: +DELETE FROM temporal_per FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO delete: +DELETE FROM temporal_per WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)'); +DELETE FROM temporal_per WHERE id = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk_per2per WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced updates SET NULL +-- + +TRUNCATE temporal_per, temporal_fk_per2per; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)'); +ALTER TABLE temporal_fk_per2per + DROP CONSTRAINT temporal_fk_per2per_fk, + ADD CONSTRAINT temporal_fk_per2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per + ON DELETE SET NULL ON UPDATE SET NULL; +-- leftovers on both sides: +UPDATE temporal_per FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' SET id = '[7,8)' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO update: +UPDATE temporal_per SET id = '[7,8)' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)'); +UPDATE temporal_per SET id = '[9,10)' WHERE id = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk_per2per WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced deletes SET NULL +-- + +TRUNCATE temporal_per, temporal_fk_per2per; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)'); +-- leftovers on both sides: +DELETE FROM temporal_per FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO delete: +DELETE FROM temporal_per WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)'); +DELETE FROM temporal_per WHERE id = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk_per2per WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced updates SET DEFAULT +-- + +TRUNCATE temporal_per, temporal_fk_per2per; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[-1,-1]', null, null); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)'); +ALTER TABLE temporal_fk_per2per + ALTER COLUMN parent_id SET DEFAULT '[-1,-1]', + DROP CONSTRAINT temporal_fk_per2per_fk, + ADD CONSTRAINT temporal_fk_per2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per + ON DELETE SET DEFAULT ON UPDATE SET DEFAULT; +-- leftovers on both sides: +UPDATE temporal_per FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' SET id = '[7,8)' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO update: +UPDATE temporal_per SET id = '[7,8)' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)'); +UPDATE temporal_per SET id = '[9,10)' WHERE id = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk_per2per WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced deletes SET DEFAULT +-- + +TRUNCATE temporal_per, temporal_fk_per2per; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[-1,-1]', null, null); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)'); +-- leftovers on both sides: +DELETE FROM temporal_per FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO update: +DELETE FROM temporal_per WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk_per2per (id, valid_from, valid_til, parent_id) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)'); +DELETE FROM temporal_per WHERE id = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk_per2per WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced updates CASCADE (two scalar cols) +-- + +TRUNCATE temporal_per2, temporal_fk2_per2per; +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[6,7)', '[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_per2per (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)', '[6,7)'); +ALTER TABLE temporal_fk2_per2per + DROP CONSTRAINT temporal_fk2_per2per_fk, + ADD CONSTRAINT temporal_fk2_per2per_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_per2 + ON DELETE CASCADE ON UPDATE CASCADE; +-- leftovers on both sides: +UPDATE temporal_per2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' SET id1 = '[7,8)' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO update: +UPDATE temporal_per2 SET id1 = '[7,8)' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_per2per (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)', '[8,9)'); +UPDATE temporal_per2 SET id1 = '[9,10)' WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_per2per WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced deletes CASCADE (two scalar cols) +-- + +TRUNCATE temporal_per2, temporal_fk2_per2per; +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[6,7)', '[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_per2per (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)', '[6,7)'); +-- leftovers on both sides: +DELETE FROM temporal_per2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO delete: +DELETE FROM temporal_per2 WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_per2per (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)', '[8,9)'); +DELETE FROM temporal_per2 WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_per2per WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced updates SET NULL (two scalar cols) +-- + +TRUNCATE temporal_per2, temporal_fk2_per2per; +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[6,7)', '[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_per2per (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)', '[6,7)'); +ALTER TABLE temporal_fk2_per2per + DROP CONSTRAINT temporal_fk2_per2per_fk, + ADD CONSTRAINT temporal_fk2_per2per_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_per2 + ON DELETE SET NULL ON UPDATE SET NULL; +-- leftovers on both sides: +UPDATE temporal_per2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' SET id1 = '[7,8)' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO update: +UPDATE temporal_per2 SET id1 = '[7,8)' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_per2per (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)', '[8,9)'); +UPDATE temporal_per2 SET id1 = '[9,10)' WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_per2per WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced deletes SET NULL (two scalar cols) +-- + +TRUNCATE temporal_per2, temporal_fk2_per2per; +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[6,7)', '[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_per2per (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)', '[6,7)'); +-- leftovers on both sides: +DELETE FROM temporal_per2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO delete: +DELETE FROM temporal_per2 WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_per2per (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)', '[8,9)'); +DELETE FROM temporal_per2 WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_per2per WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced deletes SET NULL (two scalar cols, SET NULL subset) +-- + +TRUNCATE temporal_per2, temporal_fk2_per2per; +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[6,7)', '[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_per2per (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)', '[6,7)'); +-- fails because you can't set the PERIOD column: +ALTER TABLE temporal_fk2_per2per + DROP CONSTRAINT temporal_fk2_per2per_fk, + ADD CONSTRAINT temporal_fk2_per2per_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_per2 + ON DELETE SET NULL (valid_at) ON UPDATE SET NULL; +-- ok: +ALTER TABLE temporal_fk2_per2per + DROP CONSTRAINT temporal_fk2_per2per_fk, + ADD CONSTRAINT temporal_fk2_per2per_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_per2 + ON DELETE SET NULL (parent_id1) ON UPDATE SET NULL; +-- leftovers on both sides: +DELETE FROM temporal_per2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO delete: +DELETE FROM temporal_per2 WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_per2per (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)', '[8,9)'); +DELETE FROM temporal_per2 WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_per2per WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced updates SET DEFAULT (two scalar cols) +-- + +TRUNCATE temporal_per2, temporal_fk2_per2per; +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[-1,-1]', '[-1,-1]', null, null); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[6,7)', '[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_per2per (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)', '[6,7)'); +ALTER TABLE temporal_fk2_per2per + ALTER COLUMN parent_id1 SET DEFAULT '[-1,-1]', + ALTER COLUMN parent_id2 SET DEFAULT '[-1,-1]', + DROP CONSTRAINT temporal_fk2_per2per_fk, + ADD CONSTRAINT temporal_fk2_per2per_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_per2 + ON DELETE SET DEFAULT ON UPDATE SET DEFAULT; +-- leftovers on both sides: +UPDATE temporal_per2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' SET id1 = '[7,8)', id2 = '[7,8)' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO update: +UPDATE temporal_per2 SET id1 = '[7,8)', id2 = '[7,8)' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_per2per (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)', '[8,9)'); +UPDATE temporal_per2 SET id1 = '[9,10)', id2 = '[9,10)' WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_per2per WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced deletes SET DEFAULT (two scalar cols) +-- + +TRUNCATE temporal_per2, temporal_fk2_per2per; +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[-1,-1]', '[-1,-1]', null, null); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[6,7)', '[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_per2per (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)', '[6,7)'); +-- leftovers on both sides: +DELETE FROM temporal_per2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO update: +DELETE FROM temporal_per2 WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_per2per (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)', '[8,9)'); +DELETE FROM temporal_per2 WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_per2per WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced deletes SET DEFAULT (two scalar cols, SET DEFAULT subset) +-- + +TRUNCATE temporal_per2, temporal_fk2_per2per; +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[-1,-1]', '[6,7)', null, null); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[6,7)', '[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_per2per (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)', '[6,7)'); +-- fails because you can't set the PERIOD column: +ALTER TABLE temporal_fk2_per2per + ALTER COLUMN parent_id1 SET DEFAULT '[-1,-1]', + DROP CONSTRAINT temporal_fk2_per2per_fk, + ADD CONSTRAINT temporal_fk2_per2per_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_per2 + ON DELETE SET DEFAULT (valid_at) ON UPDATE SET DEFAULT; +-- ok: +ALTER TABLE temporal_fk2_per2per + ALTER COLUMN parent_id1 SET DEFAULT '[-1,-1]', + DROP CONSTRAINT temporal_fk2_per2per_fk, + ADD CONSTRAINT temporal_fk2_per2per_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_per2 + ON DELETE SET DEFAULT (parent_id1) ON UPDATE SET DEFAULT; +-- leftovers on both sides: +DELETE FROM temporal_per2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO update: +DELETE FROM temporal_per2 WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[-1,-1]', '[8,9)', null, null); +INSERT INTO temporal_fk2_per2per (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)', '[8,9)'); +DELETE FROM temporal_per2 WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_per2per WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FOREIGN KEY, range references PERIOD +-- + +-- test table setup +DROP TABLE temporal_per CASCADE; +CREATE TABLE temporal_per ( + id int4range, + valid_from date, + valid_til date, + PERIOD FOR valid_at (valid_from, valid_til) +); +ALTER TABLE temporal_per + ADD CONSTRAINT temporal_per_pk + PRIMARY KEY (id, valid_at WITHOUT OVERLAPS); + +-- Can't create a FK with a mismatched range type +CREATE TABLE temporal_fk_rng2per ( + id int4range, + valid_at int4range, + parent_id int4range, + CONSTRAINT temporal_fk_rng2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_rng2per_fk FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per (id, PERIOD valid_at) +); + +-- works: PERIOD for referenced, range for referencing +CREATE TABLE temporal_fk_rng2per ( + id int4range, + valid_at daterange, + parent_id int4range, + CONSTRAINT temporal_fk_rng2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_rng2per_fk FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per (id, PERIOD valid_at) +); +\d temporal_fk_rng2per +DROP TABLE temporal_fk_rng2per; + +-- with mismatched PERIOD columns: + +-- (parent_id, PERIOD valid_at) REFERENCES (id, valid_at) +-- REFERENCES part should specify PERIOD +CREATE TABLE temporal_fk_rng2per ( + id int4range, + valid_at daterange, + parent_id int4range, + CONSTRAINT temporal_fk_rng2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_rng2per_fk FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per (id, valid_at) +); +-- (parent_id, valid_at) REFERENCES (id, valid_at) +-- both should specify PERIOD: +CREATE TABLE temporal_fk_rng2per ( + id int4range, + valid_at daterange, + parent_id int4range, + CONSTRAINT temporal_fk_rng2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_rng2per_fk FOREIGN KEY (parent_id, valid_at) + REFERENCES temporal_per (id, valid_at) +); +-- (parent_id, valid_at) REFERENCES (id, PERIOD valid_at) +-- FOREIGN KEY part should specify PERIOD +CREATE TABLE temporal_fk_rng2per ( + id int4range, + valid_at daterange, + parent_id int4range, + CONSTRAINT temporal_fk_rng2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_rng2per_fk FOREIGN KEY (parent_id, valid_at) + REFERENCES temporal_per (id, PERIOD valid_at) +); +-- (parent_id, valid_at) REFERENCES [implicit] +-- FOREIGN KEY part should specify PERIOD +CREATE TABLE temporal_fk_rng2per ( + id int4range, + valid_at daterange, + parent_id int4range, + CONSTRAINT temporal_fk_rng2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_rng2per_fk FOREIGN KEY (parent_id, valid_at) + REFERENCES temporal_per +); +-- (parent_id, PERIOD valid_at) REFERENCES (id) +CREATE TABLE temporal_fk_rng2per ( + id int4range, + valid_at daterange, + parent_id int4range, + CONSTRAINT temporal_fk_rng2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_rng2per_fk FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per (id) +); +-- (parent_id) REFERENCES (id, PERIOD valid_at) +CREATE TABLE temporal_fk_rng2per ( + id int4range, + valid_at daterange, + parent_id int4range, + CONSTRAINT temporal_fk_rng2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_rng2per_fk FOREIGN KEY (parent_id) + REFERENCES temporal_per (id, PERIOD valid_at) +); +-- with inferred PK on the referenced table: +-- (parent_id, PERIOD valid_at) REFERENCES [implicit] +CREATE TABLE temporal_fk_rng2per ( + id int4range, + valid_at daterange, + parent_id int4range, + CONSTRAINT temporal_fk_rng2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_rng2per_fk FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per +); +DROP TABLE temporal_fk_rng2per; +-- (parent_id) REFERENCES [implicit] +CREATE TABLE temporal_fk_rng2per ( + id int4range, + valid_at daterange, + parent_id int4range, + CONSTRAINT temporal_fk_rng2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_rng2per_fk FOREIGN KEY (parent_id) + REFERENCES temporal_per +); + +-- should fail because of duplicate referenced columns: +CREATE TABLE temporal_fk_rng2per ( + id int4range, + valid_at daterange, + parent_id int4range, + CONSTRAINT temporal_fk_rng2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_rng2per_fk FOREIGN KEY (parent_id, PERIOD parent_id) + REFERENCES temporal_per (id, PERIOD id) +); + +-- Two scalar columns +DROP TABLE temporal_per2 CASCADE; +CREATE TABLE temporal_per2 ( + id1 int4range, + id2 int4range, + valid_from date, + valid_til date, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_per2_pk PRIMARY KEY (id1, id2, valid_at WITHOUT OVERLAPS) +); + +CREATE TABLE temporal_fk2_rng2per ( + id int4range, + valid_at daterange, + parent_id1 int4range, + parent_id2 int4range, + CONSTRAINT temporal_fk2_rng2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk2_rng2per_fk FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_per2 (id1, id2, PERIOD valid_at) +); +\d temporal_fk2_rng2per +DROP TABLE temporal_fk2_rng2per; + +-- +-- test ALTER TABLE ADD CONSTRAINT +-- + +CREATE TABLE temporal_fk_rng2per ( + id int4range, + valid_at daterange, + parent_id int4range, + CONSTRAINT temporal_fk_rng2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +); +ALTER TABLE temporal_fk_rng2per + ADD CONSTRAINT temporal_fk_rng2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per (id, PERIOD valid_at); +-- Two scalar columns: +CREATE TABLE temporal_fk2_rng2per ( + id int4range, + valid_at daterange, + parent_id1 int4range, + parent_id2 int4range, + CONSTRAINT temporal_fk2_rng2per_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +); +ALTER TABLE temporal_fk2_rng2per + ADD CONSTRAINT temporal_fk2_rng2per_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_per2 (id1, id2, PERIOD valid_at); +\d temporal_fk2_rng2per + +-- with inferred PK on the referenced table, and wrong column type: +ALTER TABLE temporal_fk_rng2per + DROP CONSTRAINT temporal_fk_rng2per_fk, + ALTER COLUMN valid_at TYPE tsrange USING tsrange(lower(valid_at), upper(valid_at)); +ALTER TABLE temporal_fk_rng2per + ADD CONSTRAINT temporal_fk_rng2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per; +ALTER TABLE temporal_fk_rng2per + ALTER COLUMN valid_at TYPE daterange USING daterange(lower(valid_at)::date, upper(valid_at)::date); + +-- with inferred PK on the referenced table: +ALTER TABLE temporal_fk_rng2per + ADD CONSTRAINT temporal_fk_rng2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per; + +-- should fail because of duplicate referenced columns: +ALTER TABLE temporal_fk_rng2per + ADD CONSTRAINT temporal_fk_rng2per_fk2 + FOREIGN KEY (parent_id, PERIOD parent_id) + REFERENCES temporal_per (id, PERIOD id); + +-- +-- test with rows already +-- + +DELETE FROM temporal_fk_rng2per; +DELETE FROM temporal_per; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES + ('[1,2)', '2018-01-02', '2018-02-03'), + ('[1,2)', '2018-03-03', '2018-04-04'), + ('[2,3)', '2018-01-01', '2018-01-05'), + ('[3,4)', '2018-01-01', NULL); + +ALTER TABLE temporal_fk_rng2per + DROP CONSTRAINT temporal_fk_rng2per_fk; +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES ('[1,2)', '[2018-01-02,2018-02-01)', '[1,2)'); +ALTER TABLE temporal_fk_rng2per + ADD CONSTRAINT temporal_fk_rng2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per; +ALTER TABLE temporal_fk_rng2per + DROP CONSTRAINT temporal_fk_rng2per_fk; +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES ('[2,3)', '[2018-01-02,2018-04-01)', '[1,2)'); +-- should fail: +ALTER TABLE temporal_fk_rng2per + ADD CONSTRAINT temporal_fk_rng2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per; +-- okay again: +DELETE FROM temporal_fk_rng2per; +ALTER TABLE temporal_fk_rng2per + ADD CONSTRAINT temporal_fk_rng2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per; + +-- +-- test pg_get_constraintdef +-- + +SELECT pg_get_constraintdef(oid) FROM pg_constraint WHERE conname = 'temporal_fk_rng2per_fk'; + +-- +-- test FK referencing inserts +-- + +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES ('[1,2)', '[2018-01-02,2018-02-01)', '[1,2)'); +-- should fail: +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES ('[2,3)', '[2018-01-02,2018-04-01)', '[1,2)'); +-- now it should work: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[1,2)', '2018-02-03', '2018-03-03'); +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES ('[2,3)', '[2018-01-02,2018-04-01)', '[1,2)'); + +-- +-- test FK referencing updates +-- + +-- slide the edge across a referenced transition: +UPDATE temporal_fk_rng2per SET valid_at = '[2018-01-02,2018-02-20)' WHERE id = '[1,2)'; +-- should fail: +UPDATE temporal_fk_rng2per SET valid_at = '[2018-01-02,2018-05-01)' WHERE id = '[1,2)'; +UPDATE temporal_fk_rng2per SET parent_id = '[8,9)' WHERE id = '[1,2)'; + +-- ALTER FK DEFERRABLE + +BEGIN; + INSERT INTO temporal_per (id, valid_from, valid_til) VALUES + ('[5,6)', '2018-01-01', '2018-02-01'), + ('[5,6)', '2018-02-01', '2018-03-01'); + INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES + ('[3,4)', '[2018-01-05,2018-01-10)', '[5,6)'); + ALTER TABLE temporal_fk_rng2per + ALTER CONSTRAINT temporal_fk_rng2per_fk + DEFERRABLE INITIALLY DEFERRED; + + DELETE FROM temporal_per WHERE id = '[5,6)'; --should not fail yet. +COMMIT; -- should fail here. + +-- +-- test FK referenced updates NO ACTION +-- + +TRUNCATE temporal_per, temporal_fk_rng2per; +ALTER TABLE temporal_fk_rng2per + DROP CONSTRAINT temporal_fk_rng2per_fk; +ALTER TABLE temporal_fk_rng2per + ADD CONSTRAINT temporal_fk_rng2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per + ON UPDATE NO ACTION; +-- a PK update that succeeds because the numeric id isn't referenced: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[5,6)', '2018-01-01', '2018-02-01'); +UPDATE temporal_per SET valid_from = '2016-01-01', valid_til = '2016-02-01' WHERE id = '[5,6)'; +-- a PK update that succeeds even though the numeric id is referenced because the range isn't: +DELETE FROM temporal_per WHERE id = '[5,6)'; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES + ('[5,6)', '2018-01-01', '2018-02-01'), + ('[5,6)', '2018-02-01', '2018-03-01'); +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES + ('[3,4)', '[2018-01-05,2018-01-10)', '[5,6)'); +UPDATE temporal_per SET valid_from = '2016-02-01', valid_til = '2016-03-01' +WHERE id = '[5,6)' AND valid_from = '2018-02-01' AND valid_til = '2018-03-01'; +-- A PK update sliding the edge between two referenced rows: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES + ('[6,7)', '2018-01-01', '2018-02-01'), + ('[6,7)', '2018-02-01', '2018-03-01'); +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES + ('[4,5)', '[2018-01-15,2018-02-15)', '[6,7)'); +UPDATE temporal_per +SET valid_from = CASE WHEN valid_from = '2018-01-01' THEN '2018-01-01' + WHEN valid_from = '2018-02-01' THEN '2018-01-05' END::date, + valid_til = CASE WHEN valid_from = '2018-01-01' THEN '2018-01-05' + WHEN valid_from = '2018-02-01' THEN '2018-03-01' END::date +WHERE id = '[6,7)'; +-- a PK update shrinking the referenced range but still valid: +-- There are two references: one fulfilled by the first pk row, +-- the other fulfilled by both pk rows combined. +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES + ('[1,2)', '2018-01-01', '2018-03-01'), + ('[1,2)', '2018-03-01', '2018-06-01'); +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES + ('[1,2)', '[2018-01-15,2018-02-01)', '[1,2)'), + ('[2,3)', '[2018-01-15,2018-05-01)', '[1,2)'); +UPDATE temporal_per SET valid_from = '2018-01-15', valid_til = '2018-03-01' +WHERE id = '[1,2)' AND valid_at @> '2018-01-15'::date; +-- a PK update growing the referenced range is fine: +UPDATE temporal_per SET valid_from = '2018-01-01', valid_til = '2018-03-01' +WHERE id = '[1,2)' AND valid_at @> '2018-01-25'::date; +-- a PK update shrinking the referenced range and changing the id invalidates the whole range: +UPDATE temporal_per SET id = '[2,3)', valid_from = '2018-01-15', valid_til = '2018-03-01' +WHERE id = '[1,2)' AND valid_at @> '2018-01-15'::date; +-- a PK update changing only the id invalidates the whole range: +UPDATE temporal_per SET id = '[2,3)' +WHERE id = '[1,2)' AND valid_at @> '2018-01-15'::date; +-- a PK update that loses time from both ends, but is still valid: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES + ('[2,3)', '2018-01-01', '2018-03-01'); +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES + ('[5,6)', '[2018-01-15,2018-02-01)', '[2,3)'); +UPDATE temporal_per SET valid_from = '2018-01-15', valid_til = '2018-02-15' +WHERE id = '[2,3)'; +-- a PK update that fails because both are referenced: +UPDATE temporal_per SET valid_from = '2016-01-01', valid_til = '2016-02-01' +WHERE id = '[5,6)' AND valid_from = '2018-01-01' AND valid_til = '2018-02-01'; +-- a PK update that fails because both are referenced, but not 'til commit: +BEGIN; + ALTER TABLE temporal_fk_rng2per + ALTER CONSTRAINT temporal_fk_rng2per_fk + DEFERRABLE INITIALLY DEFERRED; + + UPDATE temporal_per SET valid_from = '2016-01-01', valid_til = '2016-02-01' + WHERE id = '[5,6)' AND valid_at = daterange('2018-01-01', '2018-02-01'); +COMMIT; +-- changing the scalar part fails: +UPDATE temporal_per SET id = '[7,8)' +WHERE id = '[5,6)' AND valid_from = '2018-01-01' AND valid_til = '2018-02-01'; +-- changing an unreferenced part is okay: +UPDATE temporal_per +FOR PORTION OF valid_at FROM '2018-01-02' TO '2018-01-03' +SET id = '[7,8)' +WHERE id = '[5,6)'; +-- changing just a part fails: +UPDATE temporal_per +FOR PORTION OF valid_at FROM '2018-01-05' TO '2018-01-10' +SET id = '[7,8)' +WHERE id = '[5,6)'; +SELECT * FROM temporal_per WHERE id in ('[5,6)', '[7,8)') ORDER BY id, valid_at; +SELECT * FROM temporal_fk_rng2per WHERE id in ('[3,4)') ORDER BY id, valid_at; +-- then delete the objecting FK record and the same PK update succeeds: +DELETE FROM temporal_fk_rng2per WHERE id = '[3,4)'; +UPDATE temporal_per SET valid_from = '2016-01-01', valid_til = '2016-02-01' +WHERE id = '[5,6)' AND valid_at = '[2018-01-01,2018-02-01)'; + +-- +-- test FK referenced updates RESTRICT +-- + +TRUNCATE temporal_per, temporal_fk_rng2per; +ALTER TABLE temporal_fk_rng2per + DROP CONSTRAINT temporal_fk_rng2per_fk; +ALTER TABLE temporal_fk_rng2per + ADD CONSTRAINT temporal_fk_rng2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per + ON UPDATE RESTRICT; + +-- +-- test FK referenced deletes NO ACTION +-- + +TRUNCATE temporal_per, temporal_fk_rng2per; +ALTER TABLE temporal_fk_rng2per + ADD CONSTRAINT temporal_fk_rng2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per; +-- a PK delete that succeeds because the numeric id isn't referenced: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[5,6)', '2018-01-01', '2018-02-01'); +DELETE FROM temporal_per WHERE id = '[5,6)'; +-- a PK delete that succeeds even though the numeric id is referenced because the range isn't: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES + ('[5,6)', '2018-01-01', '2018-02-01'), + ('[5,6)', '2018-02-01', '2018-03-01'); +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES ('[3,4)', '[2018-01-05,2018-01-10)', '[5,6)'); +DELETE FROM temporal_per WHERE id = '[5,6)' AND valid_from = '2018-02-01' AND valid_til = '2018-03-01'; +-- a PK delete that fails because both are referenced: +DELETE FROM temporal_per WHERE id = '[5,6)' AND valid_from = '2018-01-01' AND valid_til = '2018-02-01'; +-- a PK delete that fails because both are referenced, but not 'til commit: +BEGIN; + ALTER TABLE temporal_fk_rng2per + ALTER CONSTRAINT temporal_fk_rng2per_fk + DEFERRABLE INITIALLY DEFERRED; + + DELETE FROM temporal_per WHERE id = '[5,6)' AND valid_at = daterange('2018-01-01', '2018-02-01'); +COMMIT; +-- deleting an unreferenced part is okay: +DELETE FROM temporal_per +FOR PORTION OF valid_at FROM '2018-01-02' TO '2018-01-03' +WHERE id = '[5,6)'; +-- deleting just a part fails: +DELETE FROM temporal_per +FOR PORTION OF valid_at FROM '2018-01-05' TO '2018-01-10' +WHERE id = '[5,6)'; +SELECT * FROM temporal_per WHERE id in ('[5,6)', '[7,8)') ORDER BY id, valid_at; +SELECT * FROM temporal_fk_rng2per WHERE id in ('[3,4)') ORDER BY id, valid_at; +-- then delete the objecting FK record and the same PK delete succeeds: +DELETE FROM temporal_fk_rng2per WHERE id = '[3,4)'; +DELETE FROM temporal_per WHERE id = '[5,6)' AND valid_from = '2018-01-01' AND valid_til = '2018-02-01'; + +-- +-- test FK referenced deletes RESTRICT +-- + +TRUNCATE temporal_per, temporal_fk_rng2per; +ALTER TABLE temporal_fk_rng2per + DROP CONSTRAINT temporal_fk_rng2per_fk; +ALTER TABLE temporal_fk_rng2per + ADD CONSTRAINT temporal_fk_rng2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per + ON DELETE RESTRICT; + +-- +-- rng2per test ON UPDATE/DELETE options +-- +-- TOC: +-- referenced updates CASCADE +-- referenced deletes CASCADE +-- referenced updates SET NULL +-- referenced deletes SET NULL +-- referenced updates SET DEFAULT +-- referenced deletes SET DEFAULT +-- referenced updates CASCADE (two scalar cols) +-- referenced deletes CASCADE (two scalar cols) +-- referenced updates SET NULL (two scalar cols) +-- referenced deletes SET NULL (two scalar cols) +-- referenced deletes SET NULL (two scalar cols, SET NULL subset) +-- referenced updates SET DEFAULT (two scalar cols) +-- referenced deletes SET DEFAULT (two scalar cols) +-- referenced deletes SET DEFAULT (two scalar cols, SET DEFAULT subset) + +-- +-- test FK referenced updates CASCADE +-- + +TRUNCATE temporal_per, temporal_fk_rng2per; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES ('[100,100]', '[2018-01-01,2021-01-01)', '[6,7)'); +ALTER TABLE temporal_fk_rng2per + ADD CONSTRAINT temporal_fk_rng2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per + ON DELETE CASCADE ON UPDATE CASCADE; +-- leftovers on both sides: +UPDATE temporal_per FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' SET id = '[7,8)' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO update: +UPDATE temporal_per SET id = '[7,8)' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES ('[200,200]', '[2018-01-01,2021-01-01)', '[8,9)'); +UPDATE temporal_per SET id = '[9,10)' WHERE id = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk_rng2per WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced deletes CASCADE +-- + +TRUNCATE temporal_per, temporal_fk_rng2per; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES ('[100,100]', '[2018-01-01,2021-01-01)', '[6,7)'); +-- leftovers on both sides: +DELETE FROM temporal_per FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO delete: +DELETE FROM temporal_per WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES ('[200,200]', '[2018-01-01,2021-01-01)', '[8,9)'); +DELETE FROM temporal_per WHERE id = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk_rng2per WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced updates SET NULL +-- + +TRUNCATE temporal_per, temporal_fk_rng2per; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES ('[100,100]', '[2018-01-01,2021-01-01)', '[6,7)'); +ALTER TABLE temporal_fk_rng2per + DROP CONSTRAINT temporal_fk_rng2per_fk, + ADD CONSTRAINT temporal_fk_rng2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per + ON DELETE SET NULL ON UPDATE SET NULL; +-- leftovers on both sides: +UPDATE temporal_per FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' SET id = '[7,8)' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO update: +UPDATE temporal_per SET id = '[7,8)' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES ('[200,200]', '[2018-01-01,2021-01-01)', '[8,9)'); +UPDATE temporal_per SET id = '[9,10)' WHERE id = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk_rng2per WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced deletes SET NULL +-- + +TRUNCATE temporal_per, temporal_fk_rng2per; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES ('[100,100]', '[2018-01-01,2021-01-01)', '[6,7)'); +-- leftovers on both sides: +DELETE FROM temporal_per FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO delete: +DELETE FROM temporal_per WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES ('[200,200]', '[2018-01-01,2021-01-01)', '[8,9)'); +DELETE FROM temporal_per WHERE id = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk_rng2per WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced updates SET DEFAULT +-- + +TRUNCATE temporal_per, temporal_fk_rng2per; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[-1,-1]', null, null); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES ('[100,100]', '[2018-01-01,2021-01-01)', '[6,7)'); +ALTER TABLE temporal_fk_rng2per + ALTER COLUMN parent_id SET DEFAULT '[-1,-1]', + DROP CONSTRAINT temporal_fk_rng2per_fk, + ADD CONSTRAINT temporal_fk_rng2per_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_per + ON DELETE SET DEFAULT ON UPDATE SET DEFAULT; +-- leftovers on both sides: +UPDATE temporal_per FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' SET id = '[7,8)' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO update: +UPDATE temporal_per SET id = '[7,8)' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES ('[200,200]', '[2018-01-01,2021-01-01)', '[8,9)'); +UPDATE temporal_per SET id = '[9,10)' WHERE id = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk_rng2per WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced deletes SET DEFAULT +-- + +TRUNCATE temporal_per, temporal_fk_rng2per; +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[-1,-1]', null, null); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES ('[100,100]', '[2018-01-01,2021-01-01)', '[6,7)'); +-- leftovers on both sides: +DELETE FROM temporal_per FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO update: +DELETE FROM temporal_per WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per (id, valid_from, valid_til) VALUES ('[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk_rng2per (id, valid_at, parent_id) VALUES ('[200,200]', '[2018-01-01,2021-01-01)', '[8,9)'); +DELETE FROM temporal_per WHERE id = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk_rng2per WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced updates CASCADE (two scalar cols) +-- + +TRUNCATE temporal_per2, temporal_fk2_rng2per; +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[6,7)', '[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_rng2per (id, valid_at, parent_id1, parent_id2) VALUES ('[100,100]', '[2018-01-01,2021-01-01)', '[6,7)', '[6,7)'); +ALTER TABLE temporal_fk2_rng2per + DROP CONSTRAINT temporal_fk2_rng2per_fk, + ADD CONSTRAINT temporal_fk2_rng2per_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_per2 + ON DELETE CASCADE ON UPDATE CASCADE; +-- leftovers on both sides: +UPDATE temporal_per2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' SET id1 = '[7,8)' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO update: +UPDATE temporal_per2 SET id1 = '[7,8)' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_rng2per (id, valid_at, parent_id1, parent_id2) VALUES ('[200,200]', '[2018-01-01,2021-01-01)', '[8,9)', '[8,9)'); +UPDATE temporal_per2 SET id1 = '[9,10)' WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced deletes CASCADE (two scalar cols) +-- + +TRUNCATE temporal_per2, temporal_fk2_rng2per; +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[6,7)', '[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_rng2per (id, valid_at, parent_id1, parent_id2) VALUES ('[100,100]', '[2018-01-01,2021-01-01)', '[6,7)', '[6,7)'); +-- leftovers on both sides: +DELETE FROM temporal_per2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO delete: +DELETE FROM temporal_per2 WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_rng2per (id, valid_at, parent_id1, parent_id2) VALUES ('[200,200]', '[2018-01-01,2021-01-01)', '[8,9)', '[8,9)'); +DELETE FROM temporal_per2 WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced updates SET NULL (two scalar cols) +-- + +TRUNCATE temporal_per2, temporal_fk2_rng2per; +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[6,7)', '[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_rng2per (id, valid_at, parent_id1, parent_id2) VALUES ('[100,100]', '[2018-01-01,2021-01-01)', '[6,7)', '[6,7)'); +ALTER TABLE temporal_fk2_rng2per + DROP CONSTRAINT temporal_fk2_rng2per_fk, + ADD CONSTRAINT temporal_fk2_rng2per_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_per2 + ON DELETE SET NULL ON UPDATE SET NULL; +-- leftovers on both sides: +UPDATE temporal_per2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' SET id1 = '[7,8)' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO update: +UPDATE temporal_per2 SET id1 = '[7,8)' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_rng2per (id, valid_at, parent_id1, parent_id2) VALUES ('[200,200]', '[2018-01-01,2021-01-01)', '[8,9)', '[8,9)'); +UPDATE temporal_per2 SET id1 = '[9,10)' WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced deletes SET NULL (two scalar cols) +-- + +TRUNCATE temporal_per2, temporal_fk2_rng2per; +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[6,7)', '[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_rng2per (id, valid_at, parent_id1, parent_id2) VALUES ('[100,100]', '[2018-01-01,2021-01-01)', '[6,7)', '[6,7)'); +-- leftovers on both sides: +DELETE FROM temporal_per2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO delete: +DELETE FROM temporal_per2 WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_rng2per (id, valid_at, parent_id1, parent_id2) VALUES ('[200,200]', '[2018-01-01,2021-01-01)', '[8,9)', '[8,9)'); +DELETE FROM temporal_per2 WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced deletes SET NULL (two scalar cols, SET NULL subset) +-- + +TRUNCATE temporal_per2, temporal_fk2_rng2per; +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[6,7)', '[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_rng2per (id, valid_at, parent_id1, parent_id2) VALUES ('[100,100]', '[2018-01-01,2021-01-01)', '[6,7)', '[6,7)'); +-- fails because you can't set the PERIOD column: +ALTER TABLE temporal_fk2_rng2per + DROP CONSTRAINT temporal_fk2_rng2per_fk, + ADD CONSTRAINT temporal_fk2_rng2per_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_per2 + ON DELETE SET NULL (valid_at) ON UPDATE SET NULL; +-- ok: +ALTER TABLE temporal_fk2_rng2per + DROP CONSTRAINT temporal_fk2_rng2per_fk, + ADD CONSTRAINT temporal_fk2_rng2per_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_per2 + ON DELETE SET NULL (parent_id1) ON UPDATE SET NULL; +-- leftovers on both sides: +DELETE FROM temporal_per2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO delete: +DELETE FROM temporal_per2 WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_rng2per (id, valid_at, parent_id1, parent_id2) VALUES ('[200,200]', '[2018-01-01,2021-01-01)', '[8,9)', '[8,9)'); +DELETE FROM temporal_per2 WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced updates SET DEFAULT (two scalar cols) +-- + +TRUNCATE temporal_per2, temporal_fk2_rng2per; +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[-1,-1]', '[-1,-1]', null, null); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[6,7)', '[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_rng2per (id, valid_at, parent_id1, parent_id2) VALUES ('[100,100]', '[2018-01-01,2021-01-01)', '[6,7)', '[6,7)'); +ALTER TABLE temporal_fk2_rng2per + ALTER COLUMN parent_id1 SET DEFAULT '[-1,-1]', + ALTER COLUMN parent_id2 SET DEFAULT '[-1,-1]', + DROP CONSTRAINT temporal_fk2_rng2per_fk, + ADD CONSTRAINT temporal_fk2_rng2per_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_per2 + ON DELETE SET DEFAULT ON UPDATE SET DEFAULT; +-- leftovers on both sides: +UPDATE temporal_per2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' SET id1 = '[7,8)', id2 = '[7,8)' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO update: +UPDATE temporal_per2 SET id1 = '[7,8)', id2 = '[7,8)' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_rng2per (id, valid_at, parent_id1, parent_id2) VALUES ('[200,200]', '[2018-01-01,2021-01-01)', '[8,9)', '[8,9)'); +UPDATE temporal_per2 SET id1 = '[9,10)', id2 = '[9,10)' WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced deletes SET DEFAULT (two scalar cols) +-- + +TRUNCATE temporal_per2, temporal_fk2_rng2per; +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[-1,-1]', '[-1,-1]', null, null); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[6,7)', '[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_rng2per (id, valid_at, parent_id1, parent_id2) VALUES ('[100,100]', '[2018-01-01,2021-01-01)', '[6,7)', '[6,7)'); +-- leftovers on both sides: +DELETE FROM temporal_per2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO update: +DELETE FROM temporal_per2 WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_rng2per (id, valid_at, parent_id1, parent_id2) VALUES ('[200,200]', '[2018-01-01,2021-01-01)', '[8,9)', '[8,9)'); +DELETE FROM temporal_per2 WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced deletes SET DEFAULT (two scalar cols, SET DEFAULT subset) +-- + +TRUNCATE temporal_per2, temporal_fk2_rng2per; +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[-1,-1]', '[6,7)', null, null); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[6,7)', '[6,7)', '2018-01-01', '2021-01-01'); +INSERT INTO temporal_fk2_rng2per (id, valid_at, parent_id1, parent_id2) VALUES ('[100,100]', '[2018-01-01,2021-01-01)', '[6,7)', '[6,7)'); +-- fails because you can't set the PERIOD column: +ALTER TABLE temporal_fk2_rng2per + ALTER COLUMN parent_id1 SET DEFAULT '[-1,-1]', + DROP CONSTRAINT temporal_fk2_rng2per_fk, + ADD CONSTRAINT temporal_fk2_rng2per_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_per2 + ON DELETE SET DEFAULT (valid_at) ON UPDATE SET DEFAULT; +-- ok: +ALTER TABLE temporal_fk2_rng2per + ALTER COLUMN parent_id1 SET DEFAULT '[-1,-1]', + DROP CONSTRAINT temporal_fk2_rng2per_fk, + ADD CONSTRAINT temporal_fk2_rng2per_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_per2 + ON DELETE SET DEFAULT (parent_id1) ON UPDATE SET DEFAULT; +-- leftovers on both sides: +DELETE FROM temporal_per2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO update: +DELETE FROM temporal_per2 WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2018-01-01', '2020-01-01'); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[8,9)', '[8,9)', '2020-01-01', '2021-01-01'); +INSERT INTO temporal_per2 (id1, id2, valid_from, valid_til) VALUES ('[-1,-1]', '[8,9)', null, null); +INSERT INTO temporal_fk2_rng2per (id, valid_at, parent_id1, parent_id2) VALUES ('[200,200]', '[2018-01-01,2021-01-01)', '[8,9)', '[8,9)'); +DELETE FROM temporal_per2 WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_rng2per WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FOREIGN KEY, PERIOD references range +-- + +-- test table setup +DROP TABLE temporal_rng CASCADE; +CREATE TABLE temporal_rng ( + id int4range, + valid_at daterange +); +ALTER TABLE temporal_rng + ADD CONSTRAINT temporal_rng_pk + PRIMARY KEY (id, valid_at WITHOUT OVERLAPS); + +-- Can't create a FK with a mismatched range type +CREATE TABLE temporal_fk_per2rng ( + id int4range, + valid_from int, + valid_til int, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2rng_fk FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_rng (id, PERIOD valid_at) +); + +-- works: range for referenced, PERIOD for referencing +CREATE TABLE temporal_fk_per2rng ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2rng_fk FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_rng (id, PERIOD valid_at) +); +\d temporal_fk_per2rng +DROP TABLE temporal_fk_per2rng; + +-- with mismatched PERIOD columns: + +-- (parent_id, PERIOD valid_at) REFERENCES (id, valid_at) +-- REFERENCES part should specify PERIOD +CREATE TABLE temporal_fk_per2rng ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2rng_fk FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_rng (id, valid_at) +); +-- (parent_id, valid_at) REFERENCES (id, valid_at) +-- both should specify PERIOD: +CREATE TABLE temporal_fk_per2rng ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2rng_fk FOREIGN KEY (parent_id, valid_at) + REFERENCES temporal_rng (id, valid_at) +); +-- (parent_id, valid_at) REFERENCES (id, PERIOD valid_at) +-- FOREIGN KEY part should specify PERIOD +CREATE TABLE temporal_fk_per2rng ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2rng_fk FOREIGN KEY (parent_id, valid_at) + REFERENCES temporal_rng (id, PERIOD valid_at) +); +-- (parent_id, valid_at) REFERENCES [implicit] +-- FOREIGN KEY part should specify PERIOD +CREATE TABLE temporal_fk_per2rng ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2rng_fk FOREIGN KEY (parent_id, valid_at) + REFERENCES temporal_rng +); +-- (parent_id, PERIOD valid_at) REFERENCES (id) +CREATE TABLE temporal_fk_per2rng ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2rng_fk FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_rng (id) +); +-- (parent_id) REFERENCES (id, PERIOD valid_at) +CREATE TABLE temporal_fk_per2rng ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2rng_fk FOREIGN KEY (parent_id) + REFERENCES temporal_rng (id, PERIOD valid_at) +); +-- with inferred PK on the referenced table: +-- (parent_id, PERIOD valid_at) REFERENCES [implicit] +CREATE TABLE temporal_fk_per2rng ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2rng_fk FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_rng +); +DROP TABLE temporal_fk_per2rng; +-- (parent_id) REFERENCES [implicit] +CREATE TABLE temporal_fk_per2rng ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2rng_fk FOREIGN KEY (parent_id) + REFERENCES temporal_rng +); + +-- should fail because of duplicate referenced columns: +CREATE TABLE temporal_fk_per2rng ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk_per2rng_fk FOREIGN KEY (parent_id, PERIOD parent_id) + REFERENCES temporal_rng (id, PERIOD id) +); + +-- Two scalar columns +DROP TABLE temporal_rng2 CASCADE; +CREATE TABLE temporal_rng2 ( + id1 int4range, + id2 int4range, + valid_at daterange, + CONSTRAINT temporal_rng2_pk PRIMARY KEY (id1, id2, valid_at WITHOUT OVERLAPS) +); + +CREATE TABLE temporal_fk2_per2rng ( + id int4range, + valid_from date, + valid_til date, + parent_id1 int4range, + parent_id2 int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk2_per2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk2_per2rng_fk FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_rng2 (id1, id2, PERIOD valid_at) +); +\d temporal_fk2_per2rng +DROP TABLE temporal_fk2_per2rng; + +-- +-- test ALTER TABLE ADD CONSTRAINT +-- + +CREATE TABLE temporal_fk_per2rng ( + id int4range, + valid_from date, + valid_til date, + parent_id int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk_per2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +); +ALTER TABLE temporal_fk_per2rng + ADD CONSTRAINT temporal_fk_per2rng_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_rng (id, PERIOD valid_at); +-- Two scalar columns: +CREATE TABLE temporal_fk2_per2rng ( + id int4range, + valid_from date, + valid_til date, + parent_id1 int4range, + parent_id2 int4range, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_fk2_per2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +); +ALTER TABLE temporal_fk2_per2rng + ADD CONSTRAINT temporal_fk2_per2rng_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_rng2 (id1, id2, PERIOD valid_at); +\d temporal_fk2_per2rng + +-- with inferred PK on the referenced table, and wrong column type: +ALTER TABLE temporal_fk_per2rng + DROP CONSTRAINT temporal_fk_per2rng_fk, + DROP PERIOD FOR valid_at, + ALTER COLUMN valid_from TYPE timestamp, + ALTER COLUMN valid_til TYPE timestamp, + ADD PERIOD FOR valid_at (valid_from, valid_til); +ALTER TABLE temporal_fk_per2rng + ADD CONSTRAINT temporal_fk_per2rng_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_rng; +ALTER TABLE temporal_fk_per2rng + DROP PERIOD FOR valid_at, + ALTER COLUMN valid_from TYPE date, + ALTER COLUMN valid_til TYPE date, + ADD PERIOD FOR valid_at (valid_from, valid_til); + +-- with inferred PK on the referenced table: +ALTER TABLE temporal_fk_per2rng + ADD CONSTRAINT temporal_fk_per2rng_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_rng; + +-- should fail because of duplicate referenced columns: +ALTER TABLE temporal_fk_per2rng + ADD CONSTRAINT temporal_fk_per2rng_fk2 + FOREIGN KEY (parent_id, PERIOD parent_id) + REFERENCES temporal_rng (id, PERIOD id); + +-- +-- test with rows already +-- + +DELETE FROM temporal_fk_per2rng; +DELETE FROM temporal_rng; +INSERT INTO temporal_rng (id, valid_at) VALUES + ('[1,2)', '[2018-01-02,2018-02-03)'), + ('[1,2)', '[2018-03-03,2018-04-04)'), + ('[2,3)', '[2018-01-01,2018-01-05)'), + ('[3,4)', '[2018-01-01,)'); + +ALTER TABLE temporal_fk_per2rng + DROP CONSTRAINT temporal_fk_per2rng_fk; +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES ('[1,2)', '2018-01-02', '2018-02-01', '[1,2)'); +ALTER TABLE temporal_fk_per2rng + ADD CONSTRAINT temporal_fk_per2rng_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_rng; +ALTER TABLE temporal_fk_per2rng + DROP CONSTRAINT temporal_fk_per2rng_fk; +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES ('[2,3)', '2018-01-02', '2018-04-01', '[1,2)'); +-- should fail: +ALTER TABLE temporal_fk_per2rng + ADD CONSTRAINT temporal_fk_per2rng_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_rng; +-- okay again: +DELETE FROM temporal_fk_per2rng; +ALTER TABLE temporal_fk_per2rng + ADD CONSTRAINT temporal_fk_per2rng_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_rng; + +-- +-- test pg_get_constraintdef +-- + +SELECT pg_get_constraintdef(oid) FROM pg_constraint WHERE conname = 'temporal_fk_per2rng_fk'; + +-- +-- test FK referencing inserts +-- + +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES ('[1,2)', '2018-01-02', '2018-02-01', '[1,2)'); +-- should fail: +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES ('[2,3)', '2018-01-02', '2018-04-01', '[1,2)'); +-- now it should work: +INSERT INTO temporal_rng (id, valid_at) VALUES ('[1,2)', '[2018-02-03,2018-03-03)'); +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES ('[2,3)', '2018-01-02', '2018-04-01', '[1,2)'); + +-- +-- test FK referencing updates +-- + +-- slide the edge across a referenced transition: +UPDATE temporal_fk_per2rng SET valid_from = '2018-01-02', valid_til = '2018-02-20' WHERE id = '[1,2)'; +-- should fail: +UPDATE temporal_fk_per2rng SET valid_from = '2018-01-02', valid_til = '2018-05-01' WHERE id = '[1,2)'; +UPDATE temporal_fk_per2rng SET parent_id = '[8,9)' WHERE id = '[1,2)'; + +-- ALTER FK DEFERRABLE + +BEGIN; + INSERT INTO temporal_rng (id, valid_at) VALUES + ('[5,6)', '[2018-01-01,2018-02-01)'), + ('[5,6)', '[2018-02-01,2018-03-01)'); + INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES + ('[3,4)', '2018-01-05', '2018-01-10', '[5,6)'); + ALTER TABLE temporal_fk_per2rng + ALTER CONSTRAINT temporal_fk_per2rng_fk + DEFERRABLE INITIALLY DEFERRED; + + DELETE FROM temporal_rng WHERE id = '[5,6)'; --should not fail yet. +COMMIT; -- should fail here. + +-- +-- test FK referenced updates NO ACTION +-- + +TRUNCATE temporal_rng, temporal_fk_per2rng; +ALTER TABLE temporal_fk_per2rng + DROP CONSTRAINT temporal_fk_per2rng_fk; +ALTER TABLE temporal_fk_per2rng + ADD CONSTRAINT temporal_fk_per2rng_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_rng + ON UPDATE NO ACTION; +-- a PK update that succeeds because the numeric id isn't referenced: +INSERT INTO temporal_rng (id, valid_at) VALUES ('[5,6)', '[2018-01-01,2018-02-01)'); +UPDATE temporal_rng SET valid_at = '[2016-01-01,2016-02-01)' WHERE id = '[5,6)'; +-- a PK update that succeeds even though the numeric id is referenced because the range isn't: +DELETE FROM temporal_rng WHERE id = '[5,6)'; +INSERT INTO temporal_rng (id, valid_at) VALUES + ('[5,6)', '[2018-01-01,2018-02-01)'), + ('[5,6)', '[2018-02-01,2018-03-01)'); +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES + ('[3,4)', '2018-01-05', '2018-01-10', '[5,6)'); +UPDATE temporal_rng SET valid_at = '[2016-02-01,2016-03-01)' +WHERE id = '[5,6)' AND valid_at = '[2018-02-01,2018-03-01)'; +-- A PK update sliding the edge between two referenced rows: +INSERT INTO temporal_rng (id, valid_at) VALUES + ('[6,7)', '[2018-01-01,2018-02-01)'), + ('[6,7)', '[2018-02-01,2018-03-01)'); +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES + ('[4,5)', '2018-01-15', '2018-02-15', '[6,7)'); +UPDATE temporal_rng +SET valid_at = CASE WHEN lower(valid_at) = '2018-01-01' THEN daterange('2018-01-01', '2018-01-05') + WHEN lower(valid_at) = '2018-02-01' THEN daterange('2018-01-05', '2018-03-01') END +WHERE id = '[6,7)'; +-- a PK update shrinking the referenced range but still valid: +-- There are two references: one fulfilled by the first pk row, +-- the other fulfilled by both pk rows combined. +INSERT INTO temporal_rng (id, valid_at) VALUES + ('[1,2)', '[2018-01-01,2018-03-01)'), + ('[1,2)', '[2018-03-01,2018-06-01)'); +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES + ('[1,2)', '2018-01-15', '2018-02-01', '[1,2)'), + ('[2,3)', '2018-01-15', '2018-05-01', '[1,2)'); +UPDATE temporal_rng SET valid_at = '[2018-01-15,2018-03-01)' +WHERE id = '[1,2)' AND valid_at @> '2018-01-15'::date; +-- a PK update growing the referenced range is fine: +UPDATE temporal_rng SET valid_at = '[2018-01-01,2018-03-01)' +WHERE id = '[1,2)' AND valid_at @> '2018-01-25'::date; +-- a PK update shrinking the referenced range and changing the id invalidates the whole range: +UPDATE temporal_rng SET id = '[2,3)', valid_at = '[2018-01-15,2018-03-01)' +WHERE id = '[1,2)' AND valid_at @> '2018-01-15'::date; +-- a PK update changing only the id invalidates the whole range: +UPDATE temporal_rng SET id = '[2,3)' +WHERE id = '[1,2)' AND valid_at @> '2018-01-15'::date; +-- a PK update that loses time from both ends, but is still valid: +INSERT INTO temporal_rng (id, valid_at) VALUES + ('[2,3)', '[2018-01-01,2018-03-01)'); +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES + ('[5,6)', '2018-01-15', '2018-02-01', '[2,3)'); +UPDATE temporal_rng SET valid_at = '[2018-01-15,2018-02-15)' +WHERE id = '[2,3)'; +-- a PK update that fails because both are referenced: +UPDATE temporal_rng SET valid_at = '[2016-01-01,2016-02-01)' +WHERE id = '[5,6)' AND valid_at = '[2018-01-01,2018-02-01)'; +-- a PK update that fails because both are referenced, but not 'til commit: +BEGIN; + ALTER TABLE temporal_fk_per2rng + ALTER CONSTRAINT temporal_fk_per2rng_fk + DEFERRABLE INITIALLY DEFERRED; + + UPDATE temporal_rng SET valid_at = '[2016-01-01,2016-02-01)' + WHERE id = '[5,6)' AND valid_at = daterange('2018-01-01', '2018-02-01'); +COMMIT; +-- changing the scalar part fails: +UPDATE temporal_rng SET id = '[7,8)' +WHERE id = '[5,6)' AND valid_at = '[2018-01-01,2018-02-01)'; +-- changing an unreferenced part is okay: +UPDATE temporal_rng +FOR PORTION OF valid_at FROM '2018-01-02' TO '2018-01-03' +SET id = '[7,8)' +WHERE id = '[5,6)'; +-- changing just a part fails: +UPDATE temporal_rng +FOR PORTION OF valid_at FROM '2018-01-05' TO '2018-01-10' +SET id = '[7,8)' +WHERE id = '[5,6)'; +SELECT * FROM temporal_rng WHERE id in ('[5,6)', '[7,8)') ORDER BY id, valid_at; +SELECT * FROM temporal_fk_per2rng WHERE id in ('[3,4)') ORDER BY id, valid_at; +-- then delete the objecting FK record and the same PK update succeeds: +DELETE FROM temporal_fk_per2rng WHERE id = '[3,4)'; +UPDATE temporal_rng SET valid_at = '[2016-01-01,2016-02-01)' +WHERE id = '[5,6)' AND valid_at = '[2018-01-01,2018-02-01)'; + +-- +-- test FK referenced updates RESTRICT +-- + +TRUNCATE temporal_rng, temporal_fk_per2rng; +ALTER TABLE temporal_fk_per2rng + DROP CONSTRAINT temporal_fk_per2rng_fk; +ALTER TABLE temporal_fk_per2rng + ADD CONSTRAINT temporal_fk_per2rng_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_rng + ON UPDATE RESTRICT; + +-- +-- test FK referenced deletes NO ACTION +-- + +TRUNCATE temporal_rng, temporal_fk_per2rng; +ALTER TABLE temporal_fk_per2rng + ADD CONSTRAINT temporal_fk_per2rng_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_rng; +-- a PK delete that succeeds because the numeric id isn't referenced: +INSERT INTO temporal_rng (id, valid_at) VALUES ('[5,6)', '[2018-01-01,2018-02-01)'); +DELETE FROM temporal_rng WHERE id = '[5,6)'; +-- a PK delete that succeeds even though the numeric id is referenced because the range isn't: +INSERT INTO temporal_rng (id, valid_at) VALUES + ('[5,6)', '[2018-01-01,2018-02-01)'), + ('[5,6)', '[2018-02-01,2018-03-01)'); +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES ('[3,4)', '2018-01-05', '2018-01-10', '[5,6)'); +DELETE FROM temporal_rng WHERE id = '[5,6)' AND valid_at = '[2018-02-01,2018-03-01)'; +-- a PK delete that fails because both are referenced: +DELETE FROM temporal_rng WHERE id = '[5,6)' AND valid_at = '[2018-01-01,2018-02-01)'; +-- a PK delete that fails because both are referenced, but not 'til commit: +BEGIN; + ALTER TABLE temporal_fk_per2rng + ALTER CONSTRAINT temporal_fk_per2rng_fk + DEFERRABLE INITIALLY DEFERRED; + + DELETE FROM temporal_rng WHERE id = '[5,6)' AND valid_at = daterange('2018-01-01', '2018-02-01'); +COMMIT; +-- deleting an unreferenced part is okay: +DELETE FROM temporal_rng +FOR PORTION OF valid_at FROM '2018-01-02' TO '2018-01-03' +WHERE id = '[5,6)'; +-- deleting just a part fails: +DELETE FROM temporal_rng +FOR PORTION OF valid_at FROM '2018-01-05' TO '2018-01-10' +WHERE id = '[5,6)'; +SELECT * FROM temporal_rng WHERE id in ('[5,6)', '[7,8)') ORDER BY id, valid_at; +SELECT * FROM temporal_fk_per2rng WHERE id in ('[3,4)') ORDER BY id, valid_at; +-- then delete the objecting FK record and the same PK delete succeeds: +DELETE FROM temporal_fk_per2rng WHERE id = '[3,4)'; +DELETE FROM temporal_rng WHERE id = '[5,6)' AND valid_at = '[2018-01-01,2018-02-01)'; + +-- +-- test FK referenced deletes RESTRICT +-- + +TRUNCATE temporal_rng, temporal_fk_per2rng; +ALTER TABLE temporal_fk_per2rng + DROP CONSTRAINT temporal_fk_per2rng_fk; +ALTER TABLE temporal_fk_per2rng + ADD CONSTRAINT temporal_fk_per2rng_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_rng + ON DELETE RESTRICT; + +-- +-- per2rng test ON UPDATE/DELETE options +-- +-- TOC: +-- referenced updates CASCADE +-- referenced deletes CASCADE +-- referenced updates SET NULL +-- referenced deletes SET NULL +-- referenced updates SET DEFAULT +-- referenced deletes SET DEFAULT +-- referenced updates CASCADE (two scalar cols) +-- referenced deletes CASCADE (two scalar cols) +-- referenced updates SET NULL (two scalar cols) +-- referenced deletes SET NULL (two scalar cols) +-- referenced deletes SET NULL (two scalar cols, SET NULL subset) +-- referenced updates SET DEFAULT (two scalar cols) +-- referenced deletes SET DEFAULT (two scalar cols) +-- referenced deletes SET DEFAULT (two scalar cols, SET DEFAULT subset) + +-- +-- test FK referenced updates CASCADE +-- + +TRUNCATE temporal_rng, temporal_fk_per2rng; +INSERT INTO temporal_rng (id, valid_at) VALUES ('[6,7)', '[2018-01-01,2021-01-01)'); +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)'); +ALTER TABLE temporal_fk_per2rng + ADD CONSTRAINT temporal_fk_per2rng_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_rng + ON DELETE CASCADE ON UPDATE CASCADE; +-- leftovers on both sides: +UPDATE temporal_rng FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' SET id = '[7,8)' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO update: +UPDATE temporal_rng SET id = '[7,8)' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_rng (id, valid_at) VALUES ('[8,9)', '[2018-01-01,2020-01-01)'); +INSERT INTO temporal_rng (id, valid_at) VALUES ('[8,9)', '[2020-01-01,2021-01-01)'); +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)'); +UPDATE temporal_rng SET id = '[9,10)' WHERE id = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk_per2rng WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced deletes CASCADE +-- + +TRUNCATE temporal_rng, temporal_fk_per2rng; +INSERT INTO temporal_rng (id, valid_at) VALUES ('[6,7)', '[2018-01-01,2021-01-01)'); +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)'); +-- leftovers on both sides: +DELETE FROM temporal_rng FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO delete: +DELETE FROM temporal_rng WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_rng (id, valid_at) VALUES ('[8,9)', '[2018-01-01,2020-01-01)'); +INSERT INTO temporal_rng (id, valid_at) VALUES ('[8,9)', '[2020-01-01,2021-01-01)'); +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)'); +DELETE FROM temporal_rng WHERE id = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk_per2rng WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced updates SET NULL +-- + +TRUNCATE temporal_rng, temporal_fk_per2rng; +INSERT INTO temporal_rng (id, valid_at) VALUES ('[6,7)', '[2018-01-01,2021-01-01)'); +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)'); +ALTER TABLE temporal_fk_per2rng + DROP CONSTRAINT temporal_fk_per2rng_fk, + ADD CONSTRAINT temporal_fk_per2rng_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_rng + ON DELETE SET NULL ON UPDATE SET NULL; +-- leftovers on both sides: +UPDATE temporal_rng FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' SET id = '[7,8)' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO update: +UPDATE temporal_rng SET id = '[7,8)' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_rng (id, valid_at) VALUES ('[8,9)', '[2018-01-01,2020-01-01)'); +INSERT INTO temporal_rng (id, valid_at) VALUES ('[8,9)', '[2020-01-01,2021-01-01)'); +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)'); +UPDATE temporal_rng SET id = '[9,10)' WHERE id = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk_per2rng WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced deletes SET NULL +-- + +TRUNCATE temporal_rng, temporal_fk_per2rng; +INSERT INTO temporal_rng (id, valid_at) VALUES ('[6,7)', '[2018-01-01,2021-01-01)'); +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)'); +-- leftovers on both sides: +DELETE FROM temporal_rng FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO delete: +DELETE FROM temporal_rng WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_rng (id, valid_at) VALUES ('[8,9)', '[2018-01-01,2020-01-01)'); +INSERT INTO temporal_rng (id, valid_at) VALUES ('[8,9)', '[2020-01-01,2021-01-01)'); +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)'); +DELETE FROM temporal_rng WHERE id = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk_per2rng WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced updates SET DEFAULT +-- + +TRUNCATE temporal_rng, temporal_fk_per2rng; +INSERT INTO temporal_rng (id, valid_at) VALUES ('[-1,-1]', '(,)'); +INSERT INTO temporal_rng (id, valid_at) VALUES ('[6,7)', '[2018-01-01,2021-01-01)'); +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)'); +ALTER TABLE temporal_fk_per2rng + ALTER COLUMN parent_id SET DEFAULT '[-1,-1]', + DROP CONSTRAINT temporal_fk_per2rng_fk, + ADD CONSTRAINT temporal_fk_per2rng_fk + FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_rng + ON DELETE SET DEFAULT ON UPDATE SET DEFAULT; +-- leftovers on both sides: +UPDATE temporal_rng FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' SET id = '[7,8)' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO update: +UPDATE temporal_rng SET id = '[7,8)' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_rng (id, valid_at) VALUES ('[8,9)', '[2018-01-01,2020-01-01)'); +INSERT INTO temporal_rng (id, valid_at) VALUES ('[8,9)', '[2020-01-01,2021-01-01)'); +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)'); +UPDATE temporal_rng SET id = '[9,10)' WHERE id = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk_per2rng WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced deletes SET DEFAULT +-- + +TRUNCATE temporal_rng, temporal_fk_per2rng; +INSERT INTO temporal_rng (id, valid_at) VALUES ('[-1,-1]', '(,)'); +INSERT INTO temporal_rng (id, valid_at) VALUES ('[6,7)', '[2018-01-01,2021-01-01)'); +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)'); +-- leftovers on both sides: +DELETE FROM temporal_rng FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO update: +DELETE FROM temporal_rng WHERE id = '[6,7)'; +SELECT * FROM temporal_fk_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_rng (id, valid_at) VALUES ('[8,9)', '[2018-01-01,2020-01-01)'); +INSERT INTO temporal_rng (id, valid_at) VALUES ('[8,9)', '[2020-01-01,2021-01-01)'); +INSERT INTO temporal_fk_per2rng (id, valid_from, valid_til, parent_id) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)'); +DELETE FROM temporal_rng WHERE id = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk_per2rng WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced updates CASCADE (two scalar cols) +-- + +TRUNCATE temporal_rng2, temporal_fk2_per2rng; +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[6,7)', '[6,7)', '[2018-01-01,2021-01-01)'); +INSERT INTO temporal_fk2_per2rng (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)', '[6,7)'); +ALTER TABLE temporal_fk2_per2rng + DROP CONSTRAINT temporal_fk2_per2rng_fk, + ADD CONSTRAINT temporal_fk2_per2rng_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_rng2 + ON DELETE CASCADE ON UPDATE CASCADE; +-- leftovers on both sides: +UPDATE temporal_rng2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' SET id1 = '[7,8)' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO update: +UPDATE temporal_rng2 SET id1 = '[7,8)' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[8,9)', '[8,9)', '[2018-01-01,2020-01-01)'); +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[8,9)', '[8,9)', '[2020-01-01,2021-01-01)'); +INSERT INTO temporal_fk2_per2rng (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)', '[8,9)'); +UPDATE temporal_rng2 SET id1 = '[9,10)' WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced deletes CASCADE (two scalar cols) +-- + +TRUNCATE temporal_rng2, temporal_fk2_per2rng; +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[6,7)', '[6,7)', '[2018-01-01,2021-01-01)'); +INSERT INTO temporal_fk2_per2rng (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)', '[6,7)'); +-- leftovers on both sides: +DELETE FROM temporal_rng2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO delete: +DELETE FROM temporal_rng2 WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[8,9)', '[8,9)', '[2018-01-01,2020-01-01)'); +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[8,9)', '[8,9)', '[2020-01-01,2021-01-01)'); +INSERT INTO temporal_fk2_per2rng (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)', '[8,9)'); +DELETE FROM temporal_rng2 WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced updates SET NULL (two scalar cols) +-- + +TRUNCATE temporal_rng2, temporal_fk2_per2rng; +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[6,7)', '[6,7)', '[2018-01-01,2021-01-01)'); +INSERT INTO temporal_fk2_per2rng (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)', '[6,7)'); +ALTER TABLE temporal_fk2_per2rng + DROP CONSTRAINT temporal_fk2_per2rng_fk, + ADD CONSTRAINT temporal_fk2_per2rng_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_rng2 + ON DELETE SET NULL ON UPDATE SET NULL; +-- leftovers on both sides: +UPDATE temporal_rng2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' SET id1 = '[7,8)' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO update: +UPDATE temporal_rng2 SET id1 = '[7,8)' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[8,9)', '[8,9)', '[2018-01-01,2020-01-01)'); +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[8,9)', '[8,9)', '[2020-01-01,2021-01-01)'); +INSERT INTO temporal_fk2_per2rng (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)', '[8,9)'); +UPDATE temporal_rng2 SET id1 = '[9,10)' WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced deletes SET NULL (two scalar cols) +-- + +TRUNCATE temporal_rng2, temporal_fk2_per2rng; +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[6,7)', '[6,7)', '[2018-01-01,2021-01-01)'); +INSERT INTO temporal_fk2_per2rng (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)', '[6,7)'); +-- leftovers on both sides: +DELETE FROM temporal_rng2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO delete: +DELETE FROM temporal_rng2 WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[8,9)', '[8,9)', '[2018-01-01,2020-01-01)'); +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[8,9)', '[8,9)', '[2020-01-01,2021-01-01)'); +INSERT INTO temporal_fk2_per2rng (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)', '[8,9)'); +DELETE FROM temporal_rng2 WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced deletes SET NULL (two scalar cols, SET NULL subset) +-- + +TRUNCATE temporal_rng2, temporal_fk2_per2rng; +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[6,7)', '[6,7)', '[2018-01-01,2021-01-01)'); +INSERT INTO temporal_fk2_per2rng (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)', '[6,7)'); +-- fails because you can't set the PERIOD column: +ALTER TABLE temporal_fk2_per2rng + DROP CONSTRAINT temporal_fk2_per2rng_fk, + ADD CONSTRAINT temporal_fk2_per2rng_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_rng2 + ON DELETE SET NULL (valid_at) ON UPDATE SET NULL; +-- ok: +ALTER TABLE temporal_fk2_per2rng + DROP CONSTRAINT temporal_fk2_per2rng_fk, + ADD CONSTRAINT temporal_fk2_per2rng_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_rng2 + ON DELETE SET NULL (parent_id1) ON UPDATE SET NULL; +-- leftovers on both sides: +DELETE FROM temporal_rng2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO delete: +DELETE FROM temporal_rng2 WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[8,9)', '[8,9)', '[2018-01-01,2020-01-01)'); +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[8,9)', '[8,9)', '[2020-01-01,2021-01-01)'); +INSERT INTO temporal_fk2_per2rng (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)', '[8,9)'); +DELETE FROM temporal_rng2 WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced updates SET DEFAULT (two scalar cols) +-- + +TRUNCATE temporal_rng2, temporal_fk2_per2rng; +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[-1,-1]', '[-1,-1]', '(,)'); +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[6,7)', '[6,7)', '[2018-01-01,2021-01-01)'); +INSERT INTO temporal_fk2_per2rng (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)', '[6,7)'); +ALTER TABLE temporal_fk2_per2rng + ALTER COLUMN parent_id1 SET DEFAULT '[-1,-1]', + ALTER COLUMN parent_id2 SET DEFAULT '[-1,-1]', + DROP CONSTRAINT temporal_fk2_per2rng_fk, + ADD CONSTRAINT temporal_fk2_per2rng_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_rng2 + ON DELETE SET DEFAULT ON UPDATE SET DEFAULT; +-- leftovers on both sides: +UPDATE temporal_rng2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' SET id1 = '[7,8)', id2 = '[7,8)' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO update: +UPDATE temporal_rng2 SET id1 = '[7,8)', id2 = '[7,8)' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[8,9)', '[8,9)', '[2018-01-01,2020-01-01)'); +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[8,9)', '[8,9)', '[2020-01-01,2021-01-01)'); +INSERT INTO temporal_fk2_per2rng (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)', '[8,9)'); +UPDATE temporal_rng2 SET id1 = '[9,10)', id2 = '[9,10)' WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced deletes SET DEFAULT (two scalar cols) +-- + +TRUNCATE temporal_rng2, temporal_fk2_per2rng; +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[-1,-1]', '[-1,-1]', '(,)'); +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[6,7)', '[6,7)', '[2018-01-01,2021-01-01)'); +INSERT INTO temporal_fk2_per2rng (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)', '[6,7)'); +-- leftovers on both sides: +DELETE FROM temporal_rng2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO update: +DELETE FROM temporal_rng2 WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[8,9)', '[8,9)', '[2018-01-01,2020-01-01)'); +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[8,9)', '[8,9)', '[2020-01-01,2021-01-01)'); +INSERT INTO temporal_fk2_per2rng (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)', '[8,9)'); +DELETE FROM temporal_rng2 WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- +-- test FK referenced deletes SET DEFAULT (two scalar cols, SET DEFAULT subset) +-- + +TRUNCATE temporal_rng2, temporal_fk2_per2rng; +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[-1,-1]', '[6,7)', '(,)'); +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[6,7)', '[6,7)', '[2018-01-01,2021-01-01)'); +INSERT INTO temporal_fk2_per2rng (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[100,100]', '2018-01-01', '2021-01-01', '[6,7)', '[6,7)'); +-- fails because you can't set the PERIOD column: +ALTER TABLE temporal_fk2_per2rng + ALTER COLUMN parent_id1 SET DEFAULT '[-1,-1]', + DROP CONSTRAINT temporal_fk2_per2rng_fk, + ADD CONSTRAINT temporal_fk2_per2rng_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_rng2 + ON DELETE SET DEFAULT (valid_at) ON UPDATE SET DEFAULT; +-- ok: +ALTER TABLE temporal_fk2_per2rng + ALTER COLUMN parent_id1 SET DEFAULT '[-1,-1]', + DROP CONSTRAINT temporal_fk2_per2rng_fk, + ADD CONSTRAINT temporal_fk2_per2rng_fk + FOREIGN KEY (parent_id1, parent_id2, PERIOD valid_at) + REFERENCES temporal_rng2 + ON DELETE SET DEFAULT (parent_id1) ON UPDATE SET DEFAULT; +-- leftovers on both sides: +DELETE FROM temporal_rng2 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; +-- non-FPO update: +DELETE FROM temporal_rng2 WHERE id1 = '[6,7)'; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[100,100]' ORDER BY id, valid_at; +-- FK across two referenced rows: +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[8,9)', '[8,9)', '[2018-01-01,2020-01-01)'); +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[8,9)', '[8,9)', '[2020-01-01,2021-01-01)'); +INSERT INTO temporal_rng2 (id1, id2, valid_at) VALUES ('[-1,-1]', '[8,9)', '(,)'); +INSERT INTO temporal_fk2_per2rng (id, valid_from, valid_til, parent_id1, parent_id2) VALUES ('[200,200]', '2018-01-01', '2021-01-01', '[8,9)', '[8,9)'); +DELETE FROM temporal_rng2 WHERE id1 = '[8,9)' AND valid_at @> '2019-01-01'::date; +SELECT * FROM temporal_fk2_per2rng WHERE id = '[200,200]' ORDER BY id, valid_at; + +-- FK with a custom range type + +CREATE TYPE mydaterange AS range(subtype=date); + +CREATE TABLE temporal_rng3 ( + id int4range, + valid_at mydaterange, + CONSTRAINT temporal_rng3_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +); +CREATE TABLE temporal_fk3_rng2rng ( + id int4range, + valid_at mydaterange, + parent_id int4range, + CONSTRAINT temporal_fk3_rng2rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS), + CONSTRAINT temporal_fk3_rng2rng_fk FOREIGN KEY (parent_id, PERIOD valid_at) + REFERENCES temporal_rng3 (id, PERIOD valid_at) ON DELETE CASCADE +); +INSERT INTO temporal_rng3 (id, valid_at) VALUES ('[8,9)', mydaterange('2018-01-01', '2021-01-01')); +INSERT INTO temporal_fk3_rng2rng (id, valid_at, parent_id) VALUES ('[5,6)', mydaterange('2018-01-01', '2021-01-01'), '[8,9)'); +DELETE FROM temporal_rng3 FOR PORTION OF valid_at FROM '2019-01-01' TO '2020-01-01' WHERE id = '[8,9)'; +SELECT * FROM temporal_fk3_rng2rng WHERE id = '[5,6)'; + +DROP TABLE temporal_fk3_rng2rng; +DROP TABLE temporal_rng3; +DROP TYPE mydaterange; + +-- +-- FK between partitioned tables: ranges +-- + +CREATE TABLE temporal_partitioned_rng ( + id int4range, + valid_at daterange, + name text, + CONSTRAINT temporal_paritioned_rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +) PARTITION BY LIST (id); +CREATE TABLE tp1 PARTITION OF temporal_partitioned_rng FOR VALUES IN ('[1,2)', '[3,4)', '[5,6)', '[7,8)', '[9,10)', '[11,12)', '[13,14)', '[15,16)', '[17,18)', '[19,20)', '[21,22)', '[23,24)'); +CREATE TABLE tp2 PARTITION OF temporal_partitioned_rng FOR VALUES IN ('[0,1)', '[2,3)', '[4,5)', '[6,7)', '[8,9)', '[10,11)', '[12,13)', '[14,15)', '[16,17)', '[18,19)', '[20,21)', '[22,23)', '[24,25)'); +INSERT INTO temporal_partitioned_rng (id, valid_at, name) VALUES + ('[1,2)', daterange('2000-01-01', '2000-02-01'), 'one'), + ('[1,2)', daterange('2000-02-01', '2000-03-01'), 'one'), ('[2,3)', daterange('2000-01-01', '2010-01-01'), 'two'); CREATE TABLE temporal_partitioned_fk_rng2rng ( @@ -3007,4 +5995,19 @@ SELECT * FROM temporal_partitioned_fk_mltrng2mltrng WHERE id = '[15,16)'; DROP TABLE temporal_partitioned_fk_mltrng2mltrng; DROP TABLE temporal_partitioned_mltrng; +-- +-- FK between partitioned tables: PERIODs +-- + +CREATE TABLE temporal_partitioned_per ( + id int4range, + valid_from date, + valid_til date, + name text, + PERIOD FOR valid_at (valid_from, valid_til), + CONSTRAINT temporal_paritioned_rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +) PARTITION BY LIST (id); +CREATE TABLE tp1 PARTITION OF temporal_partitioned_per FOR VALUES IN ('[1,2)', '[3,4)', '[5,6)', '[7,8)', '[9,10)', '[11,12)', '[13,14)', '[15,16)', '[17,18)', '[19,20)', '[21,22)', '[23,24)'); +DROP TABLE temporal_partitioned_per; + RESET datestyle; diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list index de6c9bcae49..aaff296dfa1 100644 --- a/src/tools/pgindent/typedefs.list +++ b/src/tools/pgindent/typedefs.list @@ -887,6 +887,7 @@ FormData_pg_opclass FormData_pg_operator FormData_pg_opfamily FormData_pg_partitioned_table +FormData_pg_period FormData_pg_policy FormData_pg_proc FormData_pg_publication @@ -2161,6 +2162,8 @@ PendingUnlinkEntry PendingWrite PendingWriteback PerLockTagEntry +PeriodDef +PeriodInfo PerlInterpreter Perl_ppaddr_t Permutation -- 2.45.0