mirror of
https://github.com/postgres/postgres.git
synced 2026-01-05 23:38:41 +03:00
Revert temporal primary keys and foreign keys
This feature set did not handle empty ranges correctly, and it's now
too late for PostgreSQL 17 to fix it.
The following commits are reverted:
6db4598fcb Add stratnum GiST support function
46a0cd4cef Add temporal PRIMARY KEY and UNIQUE constraints
86232a49a4 Fix comment on gist_stratnum_btree
030e10ff1a Rename pg_constraint.conwithoutoverlaps to conperiod
a88c800deb Use daterange and YMD in without_overlaps tests instead of tsrange.
5577a71fb0 Use half-open interval notation in without_overlaps tests
34768ee361 Add temporal FOREIGN KEY contraints
482e108cd3 Add test for REPLICA IDENTITY with a temporal key
c3db1f30cb doc: clarify PERIOD and WITHOUT OVERLAPS in CREATE TABLE
144c2ce0cc Fix ON CONFLICT DO NOTHING/UPDATE for temporal indexes
Discussion: https://www.postgresql.org/message-id/d0b64a7a-dfe4-4b84-a906-c7dedfa40a3e@eisentraut.org
This commit is contained in:
@@ -33,14 +33,12 @@ EXTENSION = btree_gist
|
||||
DATA = btree_gist--1.0--1.1.sql \
|
||||
btree_gist--1.1--1.2.sql btree_gist--1.2.sql btree_gist--1.2--1.3.sql \
|
||||
btree_gist--1.3--1.4.sql btree_gist--1.4--1.5.sql \
|
||||
btree_gist--1.5--1.6.sql btree_gist--1.6--1.7.sql \
|
||||
btree_gist--1.7--1.8.sql
|
||||
btree_gist--1.5--1.6.sql btree_gist--1.6--1.7.sql
|
||||
PGFILEDESC = "btree_gist - B-tree equivalent GiST operator classes"
|
||||
|
||||
REGRESS = init int2 int4 int8 float4 float8 cash oid timestamp timestamptz \
|
||||
time timetz date interval macaddr macaddr8 inet cidr text varchar char \
|
||||
bytea bit varbit numeric uuid not_equal enum bool partitions \
|
||||
stratnum without_overlaps
|
||||
bytea bit varbit numeric uuid not_equal enum bool partitions
|
||||
|
||||
SHLIB_LINK += $(filter -lm, $(LIBS))
|
||||
|
||||
|
||||
@@ -1,87 +0,0 @@
|
||||
/* contrib/btree_gist/btree_gist--1.7--1.8.sql */
|
||||
|
||||
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
|
||||
\echo Use "ALTER EXTENSION btree_gist UPDATE TO '1.8'" to load this file. \quit
|
||||
|
||||
CREATE FUNCTION gist_stratnum_btree(smallint)
|
||||
RETURNS smallint
|
||||
AS 'MODULE_PATHNAME'
|
||||
LANGUAGE C IMMUTABLE PARALLEL SAFE STRICT;
|
||||
|
||||
ALTER OPERATOR FAMILY gist_oid_ops USING gist ADD
|
||||
FUNCTION 12 (oid, oid) gist_stratnum_btree (int2) ;
|
||||
|
||||
ALTER OPERATOR FAMILY gist_int2_ops USING gist ADD
|
||||
FUNCTION 12 (int2, int2) gist_stratnum_btree (int2) ;
|
||||
|
||||
ALTER OPERATOR FAMILY gist_int4_ops USING gist ADD
|
||||
FUNCTION 12 (int4, int4) gist_stratnum_btree (int2) ;
|
||||
|
||||
ALTER OPERATOR FAMILY gist_int8_ops USING gist ADD
|
||||
FUNCTION 12 (int8, int8) gist_stratnum_btree (int2) ;
|
||||
|
||||
ALTER OPERATOR FAMILY gist_float4_ops USING gist ADD
|
||||
FUNCTION 12 (float4, float4) gist_stratnum_btree (int2) ;
|
||||
|
||||
ALTER OPERATOR FAMILY gist_float8_ops USING gist ADD
|
||||
FUNCTION 12 (float8, float8) gist_stratnum_btree (int2) ;
|
||||
|
||||
ALTER OPERATOR FAMILY gist_timestamp_ops USING gist ADD
|
||||
FUNCTION 12 (timestamp, timestamp) gist_stratnum_btree (int2) ;
|
||||
|
||||
ALTER OPERATOR FAMILY gist_timestamptz_ops USING gist ADD
|
||||
FUNCTION 12 (timestamptz, timestamptz) gist_stratnum_btree (int2) ;
|
||||
|
||||
ALTER OPERATOR FAMILY gist_time_ops USING gist ADD
|
||||
FUNCTION 12 (time, time) gist_stratnum_btree (int2) ;
|
||||
|
||||
ALTER OPERATOR FAMILY gist_date_ops USING gist ADD
|
||||
FUNCTION 12 (date, date) gist_stratnum_btree (int2) ;
|
||||
|
||||
ALTER OPERATOR FAMILY gist_interval_ops USING gist ADD
|
||||
FUNCTION 12 (interval, interval) gist_stratnum_btree (int2) ;
|
||||
|
||||
ALTER OPERATOR FAMILY gist_cash_ops USING gist ADD
|
||||
FUNCTION 12 (money, money) gist_stratnum_btree (int2) ;
|
||||
|
||||
ALTER OPERATOR FAMILY gist_macaddr_ops USING gist ADD
|
||||
FUNCTION 12 (macaddr, macaddr) gist_stratnum_btree (int2) ;
|
||||
|
||||
ALTER OPERATOR FAMILY gist_text_ops USING gist ADD
|
||||
FUNCTION 12 (text, text) gist_stratnum_btree (int2) ;
|
||||
|
||||
ALTER OPERATOR FAMILY gist_bpchar_ops USING gist ADD
|
||||
FUNCTION 12 (bpchar, bpchar) gist_stratnum_btree (int2) ;
|
||||
|
||||
ALTER OPERATOR FAMILY gist_bytea_ops USING gist ADD
|
||||
FUNCTION 12 (bytea, bytea) gist_stratnum_btree (int2) ;
|
||||
|
||||
ALTER OPERATOR FAMILY gist_numeric_ops USING gist ADD
|
||||
FUNCTION 12 (numeric, numeric) gist_stratnum_btree (int2) ;
|
||||
|
||||
ALTER OPERATOR FAMILY gist_bit_ops USING gist ADD
|
||||
FUNCTION 12 (bit, bit) gist_stratnum_btree (int2) ;
|
||||
|
||||
ALTER OPERATOR FAMILY gist_vbit_ops USING gist ADD
|
||||
FUNCTION 12 (varbit, varbit) gist_stratnum_btree (int2) ;
|
||||
|
||||
ALTER OPERATOR FAMILY gist_inet_ops USING gist ADD
|
||||
FUNCTION 12 (inet, inet) gist_stratnum_btree (int2) ;
|
||||
|
||||
ALTER OPERATOR FAMILY gist_cidr_ops USING gist ADD
|
||||
FUNCTION 12 (cidr, cidr) gist_stratnum_btree (int2) ;
|
||||
|
||||
ALTER OPERATOR FAMILY gist_timetz_ops USING gist ADD
|
||||
FUNCTION 12 (timetz, timetz) gist_stratnum_btree (int2) ;
|
||||
|
||||
ALTER OPERATOR FAMILY gist_uuid_ops USING gist ADD
|
||||
FUNCTION 12 (uuid, uuid) gist_stratnum_btree (int2) ;
|
||||
|
||||
ALTER OPERATOR FAMILY gist_macaddr8_ops USING gist ADD
|
||||
FUNCTION 12 (macaddr8, macaddr8) gist_stratnum_btree (int2) ;
|
||||
|
||||
ALTER OPERATOR FAMILY gist_enum_ops USING gist ADD
|
||||
FUNCTION 12 (anyenum, anyenum) gist_stratnum_btree (int2) ;
|
||||
|
||||
ALTER OPERATOR FAMILY gist_bool_ops USING gist ADD
|
||||
FUNCTION 12 (bool, bool) gist_stratnum_btree (int2) ;
|
||||
@@ -3,7 +3,6 @@
|
||||
*/
|
||||
#include "postgres.h"
|
||||
|
||||
#include "access/stratnum.h"
|
||||
#include "utils/builtins.h"
|
||||
|
||||
PG_MODULE_MAGIC;
|
||||
@@ -11,7 +10,6 @@ PG_MODULE_MAGIC;
|
||||
PG_FUNCTION_INFO_V1(gbt_decompress);
|
||||
PG_FUNCTION_INFO_V1(gbtreekey_in);
|
||||
PG_FUNCTION_INFO_V1(gbtreekey_out);
|
||||
PG_FUNCTION_INFO_V1(gist_stratnum_btree);
|
||||
|
||||
/**************************************************
|
||||
* In/Out for keys
|
||||
@@ -53,28 +51,3 @@ gbt_decompress(PG_FUNCTION_ARGS)
|
||||
{
|
||||
PG_RETURN_POINTER(PG_GETARG_POINTER(0));
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns the btree number for supported operators, otherwise invalid.
|
||||
*/
|
||||
Datum
|
||||
gist_stratnum_btree(PG_FUNCTION_ARGS)
|
||||
{
|
||||
StrategyNumber strat = PG_GETARG_UINT16(0);
|
||||
|
||||
switch (strat)
|
||||
{
|
||||
case RTEqualStrategyNumber:
|
||||
PG_RETURN_UINT16(BTEqualStrategyNumber);
|
||||
case RTLessStrategyNumber:
|
||||
PG_RETURN_UINT16(BTLessStrategyNumber);
|
||||
case RTLessEqualStrategyNumber:
|
||||
PG_RETURN_UINT16(BTLessEqualStrategyNumber);
|
||||
case RTGreaterStrategyNumber:
|
||||
PG_RETURN_UINT16(BTGreaterStrategyNumber);
|
||||
case RTGreaterEqualStrategyNumber:
|
||||
PG_RETURN_UINT16(BTGreaterEqualStrategyNumber);
|
||||
default:
|
||||
PG_RETURN_UINT16(InvalidStrategy);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# btree_gist extension
|
||||
comment = 'support for indexing common datatypes in GiST'
|
||||
default_version = '1.8'
|
||||
default_version = '1.7'
|
||||
module_pathname = '$libdir/btree_gist'
|
||||
relocatable = true
|
||||
trusted = true
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
-- test stratnum support func
|
||||
SELECT gist_stratnum_btree(3::smallint);
|
||||
gist_stratnum_btree
|
||||
---------------------
|
||||
0
|
||||
(1 row)
|
||||
|
||||
SELECT gist_stratnum_btree(18::smallint);
|
||||
gist_stratnum_btree
|
||||
---------------------
|
||||
3
|
||||
(1 row)
|
||||
|
||||
@@ -1,92 +0,0 @@
|
||||
-- Core must test WITHOUT OVERLAPS
|
||||
-- with an int4range + daterange,
|
||||
-- so here we do some simple tests
|
||||
-- to make sure int + daterange works too,
|
||||
-- since that is the expected use-case.
|
||||
CREATE TABLE temporal_rng (
|
||||
id integer,
|
||||
valid_at daterange,
|
||||
CONSTRAINT temporal_rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS)
|
||||
);
|
||||
\d temporal_rng
|
||||
Table "public.temporal_rng"
|
||||
Column | Type | Collation | Nullable | Default
|
||||
----------+-----------+-----------+----------+---------
|
||||
id | integer | | not null |
|
||||
valid_at | daterange | | not null |
|
||||
Indexes:
|
||||
"temporal_rng_pk" PRIMARY KEY (id, valid_at WITHOUT OVERLAPS)
|
||||
|
||||
SELECT pg_get_constraintdef(oid) FROM pg_constraint WHERE conname = 'temporal_rng_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_rng_pk';
|
||||
pg_get_indexdef
|
||||
-------------------------------------------------------------------------------
|
||||
CREATE UNIQUE INDEX temporal_rng_pk ON temporal_rng USING gist (id, valid_at)
|
||||
(1 row)
|
||||
|
||||
INSERT INTO temporal_rng VALUES
|
||||
(1, '[2000-01-01,2001-01-01)');
|
||||
-- same key, doesn't overlap:
|
||||
INSERT INTO temporal_rng VALUES
|
||||
(1, '[2001-01-01,2002-01-01)');
|
||||
-- overlaps but different key:
|
||||
INSERT INTO temporal_rng VALUES
|
||||
(2, '[2000-01-01,2001-01-01)');
|
||||
-- should fail:
|
||||
INSERT INTO temporal_rng VALUES
|
||||
(1, '[2000-06-01,2001-01-01)');
|
||||
ERROR: conflicting key value violates exclusion constraint "temporal_rng_pk"
|
||||
DETAIL: Key (id, valid_at)=(1, [06-01-2000,01-01-2001)) conflicts with existing key (id, valid_at)=(1, [01-01-2000,01-01-2001)).
|
||||
-- Foreign key
|
||||
CREATE TABLE temporal_fk_rng2rng (
|
||||
id integer,
|
||||
valid_at daterange,
|
||||
parent_id integer,
|
||||
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 temporal_rng (id, PERIOD valid_at)
|
||||
);
|
||||
\d temporal_fk_rng2rng
|
||||
Table "public.temporal_fk_rng2rng"
|
||||
Column | Type | Collation | Nullable | Default
|
||||
-----------+-----------+-----------+----------+---------
|
||||
id | integer | | not null |
|
||||
valid_at | daterange | | not null |
|
||||
parent_id | integer | | |
|
||||
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)
|
||||
|
||||
SELECT pg_get_constraintdef(oid) FROM pg_constraint WHERE conname = 'temporal_fk_rng2rng_fk';
|
||||
pg_get_constraintdef
|
||||
---------------------------------------------------------------------------------------
|
||||
FOREIGN KEY (parent_id, PERIOD valid_at) REFERENCES temporal_rng(id, PERIOD valid_at)
|
||||
(1 row)
|
||||
|
||||
-- okay
|
||||
INSERT INTO temporal_fk_rng2rng VALUES
|
||||
(1, '[2000-01-01,2001-01-01)', 1);
|
||||
-- okay spanning two parent records:
|
||||
INSERT INTO temporal_fk_rng2rng VALUES
|
||||
(2, '[2000-01-01,2002-01-01)', 1);
|
||||
-- key is missing
|
||||
INSERT INTO temporal_fk_rng2rng VALUES
|
||||
(3, '[2000-01-01,2001-01-01)', 3);
|
||||
ERROR: insert or update on table "temporal_fk_rng2rng" violates foreign key constraint "temporal_fk_rng2rng_fk"
|
||||
DETAIL: Key (parent_id, valid_at)=(3, [01-01-2000,01-01-2001)) is not present in table "temporal_rng".
|
||||
-- key exist but is outside range
|
||||
INSERT INTO temporal_fk_rng2rng VALUES
|
||||
(4, '[2001-01-01,2002-01-01)', 2);
|
||||
ERROR: insert or update on table "temporal_fk_rng2rng" violates foreign key constraint "temporal_fk_rng2rng_fk"
|
||||
DETAIL: Key (parent_id, valid_at)=(2, [01-01-2001,01-01-2002)) is not present in table "temporal_rng".
|
||||
-- key exist but is partly outside range
|
||||
INSERT INTO temporal_fk_rng2rng VALUES
|
||||
(5, '[2000-01-01,2002-01-01)', 2);
|
||||
ERROR: insert or update on table "temporal_fk_rng2rng" violates foreign key constraint "temporal_fk_rng2rng_fk"
|
||||
DETAIL: Key (parent_id, valid_at)=(2, [01-01-2000,01-01-2002)) is not present in table "temporal_rng".
|
||||
@@ -50,7 +50,6 @@ install_data(
|
||||
'btree_gist--1.4--1.5.sql',
|
||||
'btree_gist--1.5--1.6.sql',
|
||||
'btree_gist--1.6--1.7.sql',
|
||||
'btree_gist--1.7--1.8.sql',
|
||||
kwargs: contrib_data_args,
|
||||
)
|
||||
|
||||
@@ -90,8 +89,6 @@ tests += {
|
||||
'enum',
|
||||
'bool',
|
||||
'partitions',
|
||||
'stratnum',
|
||||
'without_overlaps',
|
||||
],
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
-- test stratnum support func
|
||||
SELECT gist_stratnum_btree(3::smallint);
|
||||
SELECT gist_stratnum_btree(18::smallint);
|
||||
@@ -1,53 +0,0 @@
|
||||
-- Core must test WITHOUT OVERLAPS
|
||||
-- with an int4range + daterange,
|
||||
-- so here we do some simple tests
|
||||
-- to make sure int + daterange works too,
|
||||
-- since that is the expected use-case.
|
||||
CREATE TABLE temporal_rng (
|
||||
id integer,
|
||||
valid_at daterange,
|
||||
CONSTRAINT temporal_rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS)
|
||||
);
|
||||
\d temporal_rng
|
||||
SELECT pg_get_constraintdef(oid) FROM pg_constraint WHERE conname = 'temporal_rng_pk';
|
||||
SELECT pg_get_indexdef(conindid, 0, true) FROM pg_constraint WHERE conname = 'temporal_rng_pk';
|
||||
|
||||
INSERT INTO temporal_rng VALUES
|
||||
(1, '[2000-01-01,2001-01-01)');
|
||||
-- same key, doesn't overlap:
|
||||
INSERT INTO temporal_rng VALUES
|
||||
(1, '[2001-01-01,2002-01-01)');
|
||||
-- overlaps but different key:
|
||||
INSERT INTO temporal_rng VALUES
|
||||
(2, '[2000-01-01,2001-01-01)');
|
||||
-- should fail:
|
||||
INSERT INTO temporal_rng VALUES
|
||||
(1, '[2000-06-01,2001-01-01)');
|
||||
|
||||
-- Foreign key
|
||||
CREATE TABLE temporal_fk_rng2rng (
|
||||
id integer,
|
||||
valid_at daterange,
|
||||
parent_id integer,
|
||||
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 temporal_rng (id, PERIOD valid_at)
|
||||
);
|
||||
\d temporal_fk_rng2rng
|
||||
SELECT pg_get_constraintdef(oid) FROM pg_constraint WHERE conname = 'temporal_fk_rng2rng_fk';
|
||||
|
||||
-- okay
|
||||
INSERT INTO temporal_fk_rng2rng VALUES
|
||||
(1, '[2000-01-01,2001-01-01)', 1);
|
||||
-- okay spanning two parent records:
|
||||
INSERT INTO temporal_fk_rng2rng VALUES
|
||||
(2, '[2000-01-01,2002-01-01)', 1);
|
||||
-- key is missing
|
||||
INSERT INTO temporal_fk_rng2rng VALUES
|
||||
(3, '[2000-01-01,2001-01-01)', 3);
|
||||
-- key exist but is outside range
|
||||
INSERT INTO temporal_fk_rng2rng VALUES
|
||||
(4, '[2001-01-01,2002-01-01)', 2);
|
||||
-- key exist but is partly outside range
|
||||
INSERT INTO temporal_fk_rng2rng VALUES
|
||||
(5, '[2000-01-01,2002-01-01)', 2);
|
||||
Reference in New Issue
Block a user