Add temporal FOREIGN KEY contraints

Add PERIOD clause to foreign key constraint definitions.  This is
supported for range and multirange types.  Temporal foreign keys check
for range containment instead of equality.

This feature matches the behavior of the SQL standard temporal foreign
keys, but it works on PostgreSQL's native ranges instead of SQL's
"periods", which don't exist in PostgreSQL (yet).

Reference actions ON {UPDATE,DELETE} {CASCADE,SET NULL,SET DEFAULT}
are not supported yet.

Author: Paul A. Jungwirth <pj@illuminatedcomputing.com>
Reviewed-by: Peter Eisentraut <peter@eisentraut.org>
Reviewed-by: jian he <jian.universality@gmail.com>
Discussion: https://www.postgresql.org/message-id/flat/CA+renyUApHgSZF9-nd-a0+OPGharLQLO=mDHcY4_qQ0+noCUVg@mail.gmail.com
This commit is contained in:
Peter Eisentraut 2024-03-24 07:37:13 +01:00
parent b1fe8efdf1
commit 34768ee361
16 changed files with 2790 additions and 118 deletions

View File

@ -42,3 +42,51 @@ INSERT INTO temporal_rng VALUES
(1, '[2000-06-01,2001-01-01)'); (1, '[2000-06-01,2001-01-01)');
ERROR: conflicting key value violates exclusion constraint "temporal_rng_pk" 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)). 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".

View File

@ -23,3 +23,31 @@ INSERT INTO temporal_rng VALUES
-- should fail: -- should fail:
INSERT INTO temporal_rng VALUES INSERT INTO temporal_rng VALUES
(1, '[2000-06-01,2001-01-01)'); (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);

View File

@ -2728,7 +2728,8 @@ SCRAM-SHA-256$<replaceable>&lt;iteration count&gt;</replaceable>:<replaceable>&l
</para> </para>
<para> <para>
This constraint is defined with <literal>WITHOUT OVERLAPS</literal> This constraint is defined with <literal>WITHOUT OVERLAPS</literal>
(for primary keys and unique constraints). (for primary keys and unique constraints) or <literal>PERIOD</literal>
(for foreign keys).
</para></entry> </para></entry>
</row> </row>

View File

@ -81,7 +81,7 @@ CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXI
UNIQUE [ NULLS [ NOT ] DISTINCT ] ( <replaceable class="parameter">column_name</replaceable> [, ... ] [, <replaceable class="parameter">column_name</replaceable> WITHOUT OVERLAPS ] ) <replaceable class="parameter">index_parameters</replaceable> | UNIQUE [ NULLS [ NOT ] DISTINCT ] ( <replaceable class="parameter">column_name</replaceable> [, ... ] [, <replaceable class="parameter">column_name</replaceable> WITHOUT OVERLAPS ] ) <replaceable class="parameter">index_parameters</replaceable> |
PRIMARY KEY ( <replaceable class="parameter">column_name</replaceable> [, ... ] [, <replaceable class="parameter">column_name</replaceable> WITHOUT OVERLAPS ] ) <replaceable class="parameter">index_parameters</replaceable> | PRIMARY KEY ( <replaceable class="parameter">column_name</replaceable> [, ... ] [, <replaceable class="parameter">column_name</replaceable> WITHOUT OVERLAPS ] ) <replaceable class="parameter">index_parameters</replaceable> |
EXCLUDE [ USING <replaceable class="parameter">index_method</replaceable> ] ( <replaceable class="parameter">exclude_element</replaceable> WITH <replaceable class="parameter">operator</replaceable> [, ... ] ) <replaceable class="parameter">index_parameters</replaceable> [ WHERE ( <replaceable class="parameter">predicate</replaceable> ) ] | EXCLUDE [ USING <replaceable class="parameter">index_method</replaceable> ] ( <replaceable class="parameter">exclude_element</replaceable> WITH <replaceable class="parameter">operator</replaceable> [, ... ] ) <replaceable class="parameter">index_parameters</replaceable> [ WHERE ( <replaceable class="parameter">predicate</replaceable> ) ] |
FOREIGN KEY ( <replaceable class="parameter">column_name</replaceable> [, ... ] ) REFERENCES <replaceable class="parameter">reftable</replaceable> [ ( <replaceable class="parameter">refcolumn</replaceable> [, ... ] ) ] FOREIGN KEY ( <replaceable class="parameter">column_name</replaceable> [, ... ] [, PERIOD <replaceable class="parameter">column_name</replaceable> ] ) REFERENCES <replaceable class="parameter">reftable</replaceable> [ ( <replaceable class="parameter">refcolumn</replaceable> [, ... ] [, PERIOD <replaceable class="parameter">column_name</replaceable> ] ) ]
[ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE <replaceable [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ] [ ON DELETE <replaceable
class="parameter">referential_action</replaceable> ] [ ON UPDATE <replaceable class="parameter">referential_action</replaceable> ] } class="parameter">referential_action</replaceable> ] [ ON UPDATE <replaceable class="parameter">referential_action</replaceable> ] }
[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
@ -1152,8 +1152,8 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
<varlistentry id="sql-createtable-parms-references"> <varlistentry id="sql-createtable-parms-references">
<term><literal>REFERENCES <replaceable class="parameter">reftable</replaceable> [ ( <replaceable class="parameter">refcolumn</replaceable> ) ] [ MATCH <replaceable class="parameter">matchtype</replaceable> ] [ ON DELETE <replaceable class="parameter">referential_action</replaceable> ] [ ON UPDATE <replaceable class="parameter">referential_action</replaceable> ]</literal> (column constraint)</term> <term><literal>REFERENCES <replaceable class="parameter">reftable</replaceable> [ ( <replaceable class="parameter">refcolumn</replaceable> ) ] [ MATCH <replaceable class="parameter">matchtype</replaceable> ] [ ON DELETE <replaceable class="parameter">referential_action</replaceable> ] [ ON UPDATE <replaceable class="parameter">referential_action</replaceable> ]</literal> (column constraint)</term>
<term><literal>FOREIGN KEY ( <replaceable class="parameter">column_name</replaceable> [, ... ] ) <term><literal>FOREIGN KEY ( <replaceable class="parameter">column_name</replaceable> [, ... ] [, PERIOD <replaceable class="parameter">column_name</replaceable> ] )
REFERENCES <replaceable class="parameter">reftable</replaceable> [ ( <replaceable class="parameter">refcolumn</replaceable> [, ... ] ) ] REFERENCES <replaceable class="parameter">reftable</replaceable> [ ( <replaceable class="parameter">refcolumn</replaceable> [, ... ] [, PERIOD <replaceable class="parameter">column_name</replaceable> ] ) ]
[ MATCH <replaceable class="parameter">matchtype</replaceable> ] [ MATCH <replaceable class="parameter">matchtype</replaceable> ]
[ ON DELETE <replaceable class="parameter">referential_action</replaceable> ] [ ON DELETE <replaceable class="parameter">referential_action</replaceable> ]
[ ON UPDATE <replaceable class="parameter">referential_action</replaceable> ]</literal> [ ON UPDATE <replaceable class="parameter">referential_action</replaceable> ]</literal>
@ -1169,7 +1169,30 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
primary key of the <replaceable class="parameter">reftable</replaceable> primary key of the <replaceable class="parameter">reftable</replaceable>
is used. Otherwise, the <replaceable class="parameter">refcolumn</replaceable> is used. Otherwise, the <replaceable class="parameter">refcolumn</replaceable>
list must refer to the columns of a non-deferrable unique or primary key list must refer to the columns of a non-deferrable unique or primary key
constraint or be the columns of a non-partial unique index. The user constraint or be the columns of a non-partial unique index.
</para>
<para>
If the last column is marked with <literal>PERIOD</literal>, it is
treated in a special way. While the non-<literal>PERIOD</literal>
columns are compared for equality (and there must be at least one of
them), the <literal>PERIOD</literal> column is not. Instead, the
constraint is considered satisfied if the referenced table has matching
records (based on the non-<literal>PERIOD</literal> parts of the key)
whose combined <literal>PERIOD</literal> 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
key or unique constraint declared with <literal>WITHOUT
OVERLAPS</literal>. Finally, if one side of the foreign key uses
<literal>PERIOD</literal>, the other side must too. If the <replaceable
class="parameter">refcolumn</replaceable> list is omitted, the
<literal>WITHOUT OVERLAPS</literal> part of the primary key is treated
as if marked with <literal>PERIOD</literal>.
</para>
<para>
The user
must have <literal>REFERENCES</literal> permission on the referenced must have <literal>REFERENCES</literal> permission on the referenced
table (either the whole table, or the specific referenced columns). The table (either the whole table, or the specific referenced columns). The
addition of a foreign key constraint requires a addition of a foreign key constraint requires a
@ -1243,6 +1266,10 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
values of the referencing column(s) to the new values of the values of the referencing column(s) to the new values of the
referenced columns, respectively. referenced columns, respectively.
</para> </para>
<para>
In a temporal foreign key, this option is not supported.
</para>
</listitem> </listitem>
</varlistentry> </varlistentry>
@ -1254,6 +1281,10 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
referencing columns, to null. A subset of columns can only be referencing columns, to null. A subset of columns can only be
specified for <literal>ON DELETE</literal> actions. specified for <literal>ON DELETE</literal> actions.
</para> </para>
<para>
In a temporal foreign key, this option is not supported.
</para>
</listitem> </listitem>
</varlistentry> </varlistentry>
@ -1267,6 +1298,10 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
(There must be a row in the referenced table matching the default (There must be a row in the referenced table matching the default
values, if they are not null, or the operation will fail.) values, if they are not null, or the operation will fail.)
</para> </para>
<para>
In a temporal foreign key, this option is not supported.
</para>
</listitem> </listitem>
</varlistentry> </varlistentry>
</variablelist> </variablelist>

View File

@ -15,6 +15,7 @@
#include "postgres.h" #include "postgres.h"
#include "access/genam.h" #include "access/genam.h"
#include "access/gist.h"
#include "access/htup_details.h" #include "access/htup_details.h"
#include "access/sysattr.h" #include "access/sysattr.h"
#include "access/table.h" #include "access/table.h"
@ -1649,6 +1650,63 @@ DeconstructFkConstraintRow(HeapTuple tuple, int *numfks,
*numfks = numkeys; *numfks = numkeys;
} }
/*
* FindFkPeriodOpers -
*
* Looks up the operator oids used for the PERIOD part of a temporal foreign key.
* The opclass should be the opclass of that PERIOD element.
* Everything else is an output: containedbyoperoid is the ContainedBy operator for
* types matching the PERIOD element.
* aggedcontainedbyoperoid is also a ContainedBy operator,
* but one whose rhs is a multirange.
* That way foreign keys can compare fkattr <@ range_agg(pkattr).
*/
void
FindFKPeriodOpers(Oid opclass,
Oid *containedbyoperoid,
Oid *aggedcontainedbyoperoid)
{
Oid opfamily = InvalidOid;
Oid opcintype = InvalidOid;
StrategyNumber strat;
/* Make sure we have a range or multirange. */
if (get_opclass_opfamily_and_input_type(opclass, &opfamily, &opcintype))
{
if (opcintype != ANYRANGEOID && opcintype != ANYMULTIRANGEOID)
ereport(ERROR,
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("invalid type for PERIOD part of foreign key"),
errdetail("Only range and multirange are supported."));
}
else
elog(ERROR, "cache lookup failed for opclass %u", opclass);
/*
* Look up the ContainedBy operator whose lhs and rhs are the opclass's
* type. We use this to optimize RI checks: if the new value includes all
* of the old value, then we can treat the attribute as if it didn't
* change, and skip the RI check.
*/
strat = RTContainedByStrategyNumber;
GetOperatorFromWellKnownStrategy(opclass,
InvalidOid,
containedbyoperoid,
&strat);
/*
* Now look up the ContainedBy operator. Its left arg must be the type of
* the column (or rather of the opclass). Its right arg must match the
* return type of the support proc.
*/
strat = RTContainedByStrategyNumber;
GetOperatorFromWellKnownStrategy(opclass,
ANYMULTIRANGEOID,
aggedcontainedbyoperoid,
&strat);
}
/* /*
* Determine whether a relation can be proven functionally dependent on * Determine whether a relation can be proven functionally dependent on
* a set of grouping columns. If so, return true and add the pg_constraint * a set of grouping columns. If so, return true and add the pg_constraint

View File

@ -2185,7 +2185,7 @@ ComputeIndexAttrs(IndexInfo *indexInfo,
strat = RTOverlapStrategyNumber; strat = RTOverlapStrategyNumber;
else else
strat = RTEqualStrategyNumber; strat = RTEqualStrategyNumber;
GetOperatorFromWellKnownStrategy(opclassOids[attn], atttype, GetOperatorFromWellKnownStrategy(opclassOids[attn], InvalidOid,
&opid, &strat); &opid, &strat);
indexInfo->ii_ExclusionOps[attn] = opid; indexInfo->ii_ExclusionOps[attn] = opid;
indexInfo->ii_ExclusionProcs[attn] = get_opcode(opid); indexInfo->ii_ExclusionProcs[attn] = get_opcode(opid);
@ -2425,7 +2425,7 @@ GetDefaultOpClass(Oid type_id, Oid am_id)
* GetOperatorFromWellKnownStrategy * GetOperatorFromWellKnownStrategy
* *
* opclass - the opclass to use * opclass - the opclass to use
* atttype - the type to ask about * rhstype - the type for the right-hand side, or InvalidOid to use the type of the given opclass.
* opid - holds the operator we found * opid - holds the operator we found
* strat - holds the input and output strategy number * strat - holds the input and output strategy number
* *
@ -2438,14 +2438,14 @@ GetDefaultOpClass(Oid type_id, Oid am_id)
* InvalidStrategy. * InvalidStrategy.
*/ */
void void
GetOperatorFromWellKnownStrategy(Oid opclass, Oid atttype, GetOperatorFromWellKnownStrategy(Oid opclass, Oid rhstype,
Oid *opid, StrategyNumber *strat) Oid *opid, StrategyNumber *strat)
{ {
Oid opfamily; Oid opfamily;
Oid opcintype; Oid opcintype;
StrategyNumber instrat = *strat; StrategyNumber instrat = *strat;
Assert(instrat == RTEqualStrategyNumber || instrat == RTOverlapStrategyNumber); Assert(instrat == RTEqualStrategyNumber || instrat == RTOverlapStrategyNumber || instrat == RTContainedByStrategyNumber);
*opid = InvalidOid; *opid = InvalidOid;
@ -2468,16 +2468,21 @@ GetOperatorFromWellKnownStrategy(Oid opclass, Oid atttype,
ereport(ERROR, ereport(ERROR,
errcode(ERRCODE_UNDEFINED_OBJECT), errcode(ERRCODE_UNDEFINED_OBJECT),
instrat == RTEqualStrategyNumber ? instrat == RTEqualStrategyNumber ? errmsg("could not identify an equality operator for type %s", format_type_be(opcintype)) :
errmsg("could not identify an equality operator for type %s", format_type_be(atttype)) : instrat == RTOverlapStrategyNumber ? errmsg("could not identify an overlaps operator for type %s", format_type_be(opcintype)) :
errmsg("could not identify an overlaps operator for type %s", format_type_be(atttype)), instrat == RTContainedByStrategyNumber ? errmsg("could not identify a contained-by operator for type %s", format_type_be(opcintype)) : 0,
errdetail("Could not translate strategy number %d for operator class \"%s\" for access method \"%s\".", errdetail("Could not translate strategy number %d for operator class \"%s\" for access method \"%s\".",
instrat, NameStr(((Form_pg_opclass) GETSTRUCT(tuple))->opcname), "gist")); instrat, NameStr(((Form_pg_opclass) GETSTRUCT(tuple))->opcname), "gist"));
ReleaseSysCache(tuple);
} }
*opid = get_opfamily_member(opfamily, opcintype, opcintype, *strat); /*
* We parameterize rhstype so foreign keys can ask for a <@ operator
* whose rhs matches the aggregate function. For example range_agg
* returns anymultirange.
*/
if (!OidIsValid(rhstype))
rhstype = opcintype;
*opid = get_opfamily_member(opfamily, opcintype, rhstype, *strat);
} }
if (!OidIsValid(*opid)) if (!OidIsValid(*opid))
@ -2490,9 +2495,9 @@ GetOperatorFromWellKnownStrategy(Oid opclass, Oid atttype,
ereport(ERROR, ereport(ERROR,
errcode(ERRCODE_UNDEFINED_OBJECT), errcode(ERRCODE_UNDEFINED_OBJECT),
instrat == RTEqualStrategyNumber ? instrat == RTEqualStrategyNumber ? errmsg("could not identify an equality operator for type %s", format_type_be(opcintype)) :
errmsg("could not identify an equality operator for type %s", format_type_be(atttype)) : instrat == RTOverlapStrategyNumber ? errmsg("could not identify an overlaps operator for type %s", format_type_be(opcintype)) :
errmsg("could not identify an overlaps operator for type %s", format_type_be(atttype)), instrat == RTContainedByStrategyNumber ? errmsg("could not identify a contained-by operator for type %s", format_type_be(opcintype)) : 0,
errdetail("There is no suitable operator in operator family \"%s\" for access method \"%s\".", errdetail("There is no suitable operator in operator family \"%s\" for access method \"%s\".",
NameStr(((Form_pg_opfamily) GETSTRUCT(tuple))->opfname), "gist")); NameStr(((Form_pg_opfamily) GETSTRUCT(tuple))->opfname), "gist"));
} }

View File

@ -16,6 +16,7 @@
#include "access/attmap.h" #include "access/attmap.h"
#include "access/genam.h" #include "access/genam.h"
#include "access/gist.h"
#include "access/heapam.h" #include "access/heapam.h"
#include "access/heapam_xlog.h" #include "access/heapam_xlog.h"
#include "access/multixact.h" #include "access/multixact.h"
@ -209,6 +210,7 @@ typedef struct NewConstraint
ConstrType contype; /* CHECK or FOREIGN */ ConstrType contype; /* CHECK or FOREIGN */
Oid refrelid; /* PK rel, if FOREIGN */ Oid refrelid; /* PK rel, if FOREIGN */
Oid refindid; /* OID of PK's index, if FOREIGN */ Oid refindid; /* OID of PK's index, if FOREIGN */
bool conwithperiod; /* Whether the new FOREIGN KEY uses PERIOD */
Oid conid; /* OID of pg_constraint entry, if FOREIGN */ Oid conid; /* OID of pg_constraint entry, if FOREIGN */
Node *qual; /* Check expr or CONSTR_FOREIGN Constraint */ Node *qual; /* Check expr or CONSTR_FOREIGN Constraint */
ExprState *qualstate; /* Execution state for CHECK expr */ ExprState *qualstate; /* Execution state for CHECK expr */
@ -384,16 +386,17 @@ static int transformColumnNameList(Oid relId, List *colList,
static int transformFkeyGetPrimaryKey(Relation pkrel, Oid *indexOid, static int transformFkeyGetPrimaryKey(Relation pkrel, Oid *indexOid,
List **attnamelist, List **attnamelist,
int16 *attnums, Oid *atttypids, int16 *attnums, Oid *atttypids,
Oid *opclasses); Oid *opclasses, bool *pk_has_without_overlaps);
static Oid transformFkeyCheckAttrs(Relation pkrel, static Oid transformFkeyCheckAttrs(Relation pkrel,
int numattrs, int16 *attnums, int numattrs, int16 *attnums,
Oid *opclasses); bool with_period, Oid *opclasses,
bool *pk_has_without_overlaps);
static void checkFkeyPermissions(Relation rel, int16 *attnums, int natts); static void checkFkeyPermissions(Relation rel, int16 *attnums, int natts);
static CoercionPathType findFkeyCast(Oid targetTypeId, Oid sourceTypeId, static CoercionPathType findFkeyCast(Oid targetTypeId, Oid sourceTypeId,
Oid *funcid); Oid *funcid);
static void validateForeignKeyConstraint(char *conname, static void validateForeignKeyConstraint(char *conname,
Relation rel, Relation pkrel, Relation rel, Relation pkrel,
Oid pkindOid, Oid constraintOid); Oid pkindOid, Oid constraintOid, bool hasperiod);
static void ATController(AlterTableStmt *parsetree, static void ATController(AlterTableStmt *parsetree,
Relation rel, List *cmds, bool recurse, LOCKMODE lockmode, Relation rel, List *cmds, bool recurse, LOCKMODE lockmode,
AlterTableUtilityContext *context); AlterTableUtilityContext *context);
@ -506,7 +509,8 @@ static ObjectAddress addFkRecurseReferenced(List **wqueue, Constraint *fkconstra
Oid *pfeqoperators, Oid *ppeqoperators, Oid *ffeqoperators, Oid *pfeqoperators, Oid *ppeqoperators, Oid *ffeqoperators,
int numfkdelsetcols, int16 *fkdelsetcols, int numfkdelsetcols, int16 *fkdelsetcols,
bool old_check_ok, bool old_check_ok,
Oid parentDelTrigger, Oid parentUpdTrigger); Oid parentDelTrigger, Oid parentUpdTrigger,
bool with_period);
static void validateFkOnDeleteSetColumns(int numfks, const int16 *fkattnums, static void validateFkOnDeleteSetColumns(int numfks, const int16 *fkattnums,
int numfksetcols, const int16 *fksetcolsattnums, int numfksetcols, const int16 *fksetcolsattnums,
List *fksetcols); List *fksetcols);
@ -516,7 +520,9 @@ static void addFkRecurseReferencing(List **wqueue, Constraint *fkconstraint,
Oid *pfeqoperators, Oid *ppeqoperators, Oid *ffeqoperators, Oid *pfeqoperators, Oid *ppeqoperators, Oid *ffeqoperators,
int numfkdelsetcols, int16 *fkdelsetcols, int numfkdelsetcols, int16 *fkdelsetcols,
bool old_check_ok, LOCKMODE lockmode, bool old_check_ok, LOCKMODE lockmode,
Oid parentInsTrigger, Oid parentUpdTrigger); Oid parentInsTrigger, Oid parentUpdTrigger,
bool with_period);
static void CloneForeignKeyConstraints(List **wqueue, Relation parentRel, static void CloneForeignKeyConstraints(List **wqueue, Relation parentRel,
Relation partitionRel); Relation partitionRel);
static void CloneFkReferenced(Relation parentRel, Relation partitionRel); static void CloneFkReferenced(Relation parentRel, Relation partitionRel);
@ -5958,7 +5964,8 @@ ATRewriteTables(AlterTableStmt *parsetree, List **wqueue, LOCKMODE lockmode,
validateForeignKeyConstraint(fkconstraint->conname, rel, refrel, validateForeignKeyConstraint(fkconstraint->conname, rel, refrel,
con->refindid, con->refindid,
con->conid); con->conid,
con->conwithperiod);
/* /*
* No need to mark the constraint row as validated, we did * No need to mark the constraint row as validated, we did
@ -9809,6 +9816,8 @@ ATAddForeignKeyConstraint(List **wqueue, AlteredTableInfo *tab, Relation rel,
Oid ppeqoperators[INDEX_MAX_KEYS] = {0}; Oid ppeqoperators[INDEX_MAX_KEYS] = {0};
Oid ffeqoperators[INDEX_MAX_KEYS] = {0}; Oid ffeqoperators[INDEX_MAX_KEYS] = {0};
int16 fkdelsetcols[INDEX_MAX_KEYS] = {0}; int16 fkdelsetcols[INDEX_MAX_KEYS] = {0};
bool with_period;
bool pk_has_without_overlaps;
int i; int i;
int numfks, int numfks,
numpks, numpks,
@ -9903,6 +9912,11 @@ ATAddForeignKeyConstraint(List **wqueue, AlteredTableInfo *tab, Relation rel,
numfks = transformColumnNameList(RelationGetRelid(rel), numfks = transformColumnNameList(RelationGetRelid(rel),
fkconstraint->fk_attrs, fkconstraint->fk_attrs,
fkattnum, fktypoid); fkattnum, fktypoid);
with_period = fkconstraint->fk_with_period || fkconstraint->pk_with_period;
if (with_period && !fkconstraint->fk_with_period)
ereport(ERROR,
errcode(ERRCODE_INVALID_FOREIGN_KEY),
errmsg("foreign key uses PERIOD on the referenced table but not the referencing table"));
numfkdelsetcols = transformColumnNameList(RelationGetRelid(rel), numfkdelsetcols = transformColumnNameList(RelationGetRelid(rel),
fkconstraint->fk_del_set_cols, fkconstraint->fk_del_set_cols,
@ -9922,18 +9936,40 @@ ATAddForeignKeyConstraint(List **wqueue, AlteredTableInfo *tab, Relation rel,
numpks = transformFkeyGetPrimaryKey(pkrel, &indexOid, numpks = transformFkeyGetPrimaryKey(pkrel, &indexOid,
&fkconstraint->pk_attrs, &fkconstraint->pk_attrs,
pkattnum, pktypoid, pkattnum, pktypoid,
opclasses); opclasses, &pk_has_without_overlaps);
/* If the primary key uses WITHOUT OVERLAPS, the fk must use PERIOD */
if (pk_has_without_overlaps && !fkconstraint->fk_with_period)
ereport(ERROR,
errcode(ERRCODE_INVALID_FOREIGN_KEY),
errmsg("foreign key uses PERIOD on the referenced table but not the referencing table"));
} }
else else
{ {
numpks = transformColumnNameList(RelationGetRelid(pkrel), numpks = transformColumnNameList(RelationGetRelid(pkrel),
fkconstraint->pk_attrs, fkconstraint->pk_attrs,
pkattnum, pktypoid); pkattnum, pktypoid);
/* Since we got pk_attrs, one should be a period. */
if (with_period && !fkconstraint->pk_with_period)
ereport(ERROR,
errcode(ERRCODE_INVALID_FOREIGN_KEY),
errmsg("foreign key uses PERIOD on the referencing table but not the referenced table"));
/* Look for an index matching the column list */ /* Look for an index matching the column list */
indexOid = transformFkeyCheckAttrs(pkrel, numpks, pkattnum, indexOid = transformFkeyCheckAttrs(pkrel, numpks, pkattnum,
opclasses); with_period, opclasses, &pk_has_without_overlaps);
} }
/*
* If the referenced primary key has WITHOUT OVERLAPS, the foreign key
* must use PERIOD.
*/
if (pk_has_without_overlaps && !with_period)
ereport(ERROR,
errcode(ERRCODE_INVALID_FOREIGN_KEY),
errmsg("foreign key must use PERIOD when referencing a primary using WITHOUT OVERLAPS"));
/* /*
* Now we can check permissions. * Now we can check permissions.
*/ */
@ -9967,6 +10003,28 @@ ATAddForeignKeyConstraint(List **wqueue, AlteredTableInfo *tab, Relation rel,
} }
} }
/*
* Some actions are currently unsupported for foreign keys using PERIOD.
*/
if (fkconstraint->fk_with_period)
{
if (fkconstraint->fk_upd_action == FKCONSTR_ACTION_CASCADE ||
fkconstraint->fk_upd_action == FKCONSTR_ACTION_SETNULL ||
fkconstraint->fk_upd_action == FKCONSTR_ACTION_SETDEFAULT)
ereport(ERROR,
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("unsupported %s action for foreign key constraint using PERIOD",
"ON UPDATE"));
if (fkconstraint->fk_del_action == FKCONSTR_ACTION_CASCADE ||
fkconstraint->fk_del_action == FKCONSTR_ACTION_SETNULL ||
fkconstraint->fk_del_action == FKCONSTR_ACTION_SETDEFAULT)
ereport(ERROR,
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("unsupported %s action for foreign key constraint using PERIOD",
"ON DELETE"));
}
/* /*
* Look up the equality operators to use in the constraint. * Look up the equality operators to use in the constraint.
* *
@ -10013,16 +10071,56 @@ ATAddForeignKeyConstraint(List **wqueue, AlteredTableInfo *tab, Relation rel,
opcintype = cla_tup->opcintype; opcintype = cla_tup->opcintype;
ReleaseSysCache(cla_ht); ReleaseSysCache(cla_ht);
/* if (with_period)
* Check it's a btree; currently this can never fail since no other {
* index AMs support unique indexes. If we ever did have other types StrategyNumber rtstrategy;
* of unique indexes, we'd need a way to determine which operator bool for_overlaps = with_period && i == numpks - 1;
* strategy number is equality. (Is it reasonable to insist that
* every such index AM use btree's number for equality?) /*
*/ * GiST indexes are required to support temporal foreign keys
if (amid != BTREE_AM_OID) * because they combine equals and overlaps.
elog(ERROR, "only b-tree indexes are supported for foreign keys"); */
eqstrategy = BTEqualStrategyNumber; if (amid != GIST_AM_OID)
elog(ERROR, "only GiST indexes are supported for temporal foreign keys");
rtstrategy = for_overlaps ? RTOverlapStrategyNumber : RTEqualStrategyNumber;
/*
* An opclass can use whatever strategy numbers it wants, so we
* ask the opclass what number it actually uses instead of our RT*
* constants.
*/
eqstrategy = GistTranslateStratnum(opclasses[i], rtstrategy);
if (eqstrategy == InvalidStrategy)
{
HeapTuple tuple;
tuple = SearchSysCache1(CLAOID, ObjectIdGetDatum(opclasses[i]));
if (!HeapTupleIsValid(tuple))
elog(ERROR, "cache lookup failed for operator class %u", opclasses[i]);
ereport(ERROR,
errcode(ERRCODE_UNDEFINED_OBJECT),
for_overlaps
? errmsg("could not identify an overlaps operator for foreign key")
: errmsg("could not identify an equality operator for foreign key"),
errdetail("Could not translate strategy number %d for operator class \"%s\" for access method \"%s\".",
rtstrategy, NameStr(((Form_pg_opclass) GETSTRUCT(tuple))->opcname), "gist"));
}
}
else
{
/*
* Check it's a btree; currently this can never fail since no
* other index AMs support unique indexes. If we ever did have
* other types of unique indexes, we'd need a way to determine
* which operator strategy number is equality. (We could use
* something like GistTranslateStratnum.)
*/
if (amid != BTREE_AM_OID)
elog(ERROR, "only b-tree indexes are supported for foreign keys");
eqstrategy = BTEqualStrategyNumber;
}
/* /*
* There had better be a primary equality operator for the index. * There had better be a primary equality operator for the index.
@ -10172,6 +10270,22 @@ ATAddForeignKeyConstraint(List **wqueue, AlteredTableInfo *tab, Relation rel,
ffeqoperators[i] = ffeqop; ffeqoperators[i] = ffeqop;
} }
/*
* For FKs with PERIOD we need additional operators to check whether the
* referencing row's range is contained by the aggregated ranges of the
* referenced row(s). For rangetypes and multirangetypes this is
* fk.periodatt <@ range_agg(pk.periodatt). Those are the only types we
* support for now. FKs will look these up at "runtime", but we should
* make sure the lookup works here, even if we don't use the values.
*/
if (with_period)
{
Oid periodoperoid;
Oid aggedperiodoperoid;
FindFKPeriodOpers(opclasses[numpks - 1], &periodoperoid, &aggedperiodoperoid);
}
/* /*
* Create all the constraint and trigger objects, recursing to partitions * Create all the constraint and trigger objects, recursing to partitions
* as necessary. First handle the referenced side. * as necessary. First handle the referenced side.
@ -10188,7 +10302,8 @@ ATAddForeignKeyConstraint(List **wqueue, AlteredTableInfo *tab, Relation rel,
numfkdelsetcols, numfkdelsetcols,
fkdelsetcols, fkdelsetcols,
old_check_ok, old_check_ok,
InvalidOid, InvalidOid); InvalidOid, InvalidOid,
with_period);
/* Now handle the referencing side. */ /* Now handle the referencing side. */
addFkRecurseReferencing(wqueue, fkconstraint, rel, pkrel, addFkRecurseReferencing(wqueue, fkconstraint, rel, pkrel,
@ -10204,7 +10319,8 @@ ATAddForeignKeyConstraint(List **wqueue, AlteredTableInfo *tab, Relation rel,
fkdelsetcols, fkdelsetcols,
old_check_ok, old_check_ok,
lockmode, lockmode,
InvalidOid, InvalidOid); InvalidOid, InvalidOid,
with_period);
/* /*
* Done. Close pk table, but keep lock until we've committed. * Done. Close pk table, but keep lock until we've committed.
@ -10289,7 +10405,8 @@ addFkRecurseReferenced(List **wqueue, Constraint *fkconstraint, Relation rel,
Oid *ppeqoperators, Oid *ffeqoperators, Oid *ppeqoperators, Oid *ffeqoperators,
int numfkdelsetcols, int16 *fkdelsetcols, int numfkdelsetcols, int16 *fkdelsetcols,
bool old_check_ok, bool old_check_ok,
Oid parentDelTrigger, Oid parentUpdTrigger) Oid parentDelTrigger, Oid parentUpdTrigger,
bool with_period)
{ {
ObjectAddress address; ObjectAddress address;
Oid constrOid; Oid constrOid;
@ -10375,7 +10492,7 @@ addFkRecurseReferenced(List **wqueue, Constraint *fkconstraint, Relation rel,
conislocal, /* islocal */ conislocal, /* islocal */
coninhcount, /* inhcount */ coninhcount, /* inhcount */
connoinherit, /* conNoInherit */ connoinherit, /* conNoInherit */
false, /* conPeriod */ with_period, /* conPeriod */
false); /* is_internal */ false); /* is_internal */
ObjectAddressSet(address, ConstraintRelationId, constrOid); ObjectAddressSet(address, ConstraintRelationId, constrOid);
@ -10451,7 +10568,8 @@ addFkRecurseReferenced(List **wqueue, Constraint *fkconstraint, Relation rel,
pfeqoperators, ppeqoperators, ffeqoperators, pfeqoperators, ppeqoperators, ffeqoperators,
numfkdelsetcols, fkdelsetcols, numfkdelsetcols, fkdelsetcols,
old_check_ok, old_check_ok,
deleteTriggerOid, updateTriggerOid); deleteTriggerOid, updateTriggerOid,
with_period);
/* Done -- clean up (but keep the lock) */ /* Done -- clean up (but keep the lock) */
table_close(partRel, NoLock); table_close(partRel, NoLock);
@ -10509,7 +10627,8 @@ addFkRecurseReferencing(List **wqueue, Constraint *fkconstraint, Relation rel,
Oid *pfeqoperators, Oid *ppeqoperators, Oid *ffeqoperators, Oid *pfeqoperators, Oid *ppeqoperators, Oid *ffeqoperators,
int numfkdelsetcols, int16 *fkdelsetcols, int numfkdelsetcols, int16 *fkdelsetcols,
bool old_check_ok, LOCKMODE lockmode, bool old_check_ok, LOCKMODE lockmode,
Oid parentInsTrigger, Oid parentUpdTrigger) Oid parentInsTrigger, Oid parentUpdTrigger,
bool with_period)
{ {
Oid insertTriggerOid, Oid insertTriggerOid,
updateTriggerOid; updateTriggerOid;
@ -10557,6 +10676,7 @@ addFkRecurseReferencing(List **wqueue, Constraint *fkconstraint, Relation rel,
newcon->refrelid = RelationGetRelid(pkrel); newcon->refrelid = RelationGetRelid(pkrel);
newcon->refindid = indexOid; newcon->refindid = indexOid;
newcon->conid = parentConstr; newcon->conid = parentConstr;
newcon->conwithperiod = fkconstraint->fk_with_period;
newcon->qual = (Node *) fkconstraint; newcon->qual = (Node *) fkconstraint;
tab->constraints = lappend(tab->constraints, newcon); tab->constraints = lappend(tab->constraints, newcon);
@ -10674,7 +10794,7 @@ addFkRecurseReferencing(List **wqueue, Constraint *fkconstraint, Relation rel,
false, false,
1, 1,
false, false,
false, /* conPeriod */ with_period, /* conPeriod */
false); false);
/* /*
@ -10705,7 +10825,8 @@ addFkRecurseReferencing(List **wqueue, Constraint *fkconstraint, Relation rel,
old_check_ok, old_check_ok,
lockmode, lockmode,
insertTriggerOid, insertTriggerOid,
updateTriggerOid); updateTriggerOid,
with_period);
table_close(partition, NoLock); table_close(partition, NoLock);
} }
@ -10941,7 +11062,8 @@ CloneFkReferenced(Relation parentRel, Relation partitionRel)
confdelsetcols, confdelsetcols,
true, true,
deleteTriggerOid, deleteTriggerOid,
updateTriggerOid); updateTriggerOid,
constrForm->conperiod);
table_close(fkRel, NoLock); table_close(fkRel, NoLock);
ReleaseSysCache(tuple); ReleaseSysCache(tuple);
@ -11034,6 +11156,7 @@ CloneFkReferencing(List **wqueue, Relation parentRel, Relation partRel)
ListCell *lc; ListCell *lc;
Oid insertTriggerOid, Oid insertTriggerOid,
updateTriggerOid; updateTriggerOid;
bool with_period;
tuple = SearchSysCache1(CONSTROID, ObjectIdGetDatum(parentConstrOid)); tuple = SearchSysCache1(CONSTROID, ObjectIdGetDatum(parentConstrOid));
if (!HeapTupleIsValid(tuple)) if (!HeapTupleIsValid(tuple))
@ -11149,6 +11272,7 @@ CloneFkReferencing(List **wqueue, Relation parentRel, Relation partRel)
fkconstraint->conname = pstrdup(NameStr(constrForm->conname)); fkconstraint->conname = pstrdup(NameStr(constrForm->conname));
indexOid = constrForm->conindid; indexOid = constrForm->conindid;
with_period = constrForm->conperiod;
constrOid = constrOid =
CreateConstraintEntry(fkconstraint->conname, CreateConstraintEntry(fkconstraint->conname,
constrForm->connamespace, constrForm->connamespace,
@ -11180,7 +11304,7 @@ CloneFkReferencing(List **wqueue, Relation parentRel, Relation partRel)
false, /* islocal */ false, /* islocal */
1, /* inhcount */ 1, /* inhcount */
false, /* conNoInherit */ false, /* conNoInherit */
false, /* conPeriod */ with_period, /* conPeriod */
true); true);
/* Set up partition dependencies for the new constraint */ /* Set up partition dependencies for the new constraint */
@ -11214,7 +11338,8 @@ CloneFkReferencing(List **wqueue, Relation parentRel, Relation partRel)
false, /* no old check exists */ false, /* no old check exists */
AccessExclusiveLock, AccessExclusiveLock,
insertTriggerOid, insertTriggerOid,
updateTriggerOid); updateTriggerOid,
with_period);
table_close(pkrel, NoLock); table_close(pkrel, NoLock);
} }
@ -12024,7 +12149,8 @@ transformColumnNameList(Oid relId, List *colList,
* *
* Look up the names, attnums, and types of the primary key attributes * Look up the names, attnums, and types of the primary key attributes
* for the pkrel. Also return the index OID and index opclasses of the * for the pkrel. Also return the index OID and index opclasses of the
* index supporting the primary key. * index supporting the primary key. Also return whether the index has
* WITHOUT OVERLAPS.
* *
* All parameters except pkrel are output parameters. Also, the function * All parameters except pkrel are output parameters. Also, the function
* return value is the number of attributes in the primary key. * return value is the number of attributes in the primary key.
@ -12035,7 +12161,7 @@ static int
transformFkeyGetPrimaryKey(Relation pkrel, Oid *indexOid, transformFkeyGetPrimaryKey(Relation pkrel, Oid *indexOid,
List **attnamelist, List **attnamelist,
int16 *attnums, Oid *atttypids, int16 *attnums, Oid *atttypids,
Oid *opclasses) Oid *opclasses, bool *pk_has_without_overlaps)
{ {
List *indexoidlist; List *indexoidlist;
ListCell *indexoidscan; ListCell *indexoidscan;
@ -12113,6 +12239,8 @@ transformFkeyGetPrimaryKey(Relation pkrel, Oid *indexOid,
makeString(pstrdup(NameStr(*attnumAttName(pkrel, pkattno))))); makeString(pstrdup(NameStr(*attnumAttName(pkrel, pkattno)))));
} }
*pk_has_without_overlaps = indexStruct->indisexclusion;
ReleaseSysCache(indexTuple); ReleaseSysCache(indexTuple);
return i; return i;
@ -12126,14 +12254,16 @@ transformFkeyGetPrimaryKey(Relation pkrel, Oid *indexOid,
* *
* Returns the OID of the unique index supporting the constraint and * Returns the OID of the unique index supporting the constraint and
* populates the caller-provided 'opclasses' array with the opclasses * populates the caller-provided 'opclasses' array with the opclasses
* associated with the index columns. * associated with the index columns. Also sets whether the index
* uses WITHOUT OVERLAPS.
* *
* Raises an ERROR on validation failure. * Raises an ERROR on validation failure.
*/ */
static Oid static Oid
transformFkeyCheckAttrs(Relation pkrel, transformFkeyCheckAttrs(Relation pkrel,
int numattrs, int16 *attnums, int numattrs, int16 *attnums,
Oid *opclasses) bool with_period, Oid *opclasses,
bool *pk_has_without_overlaps)
{ {
Oid indexoid = InvalidOid; Oid indexoid = InvalidOid;
bool found = false; bool found = false;
@ -12180,12 +12310,12 @@ transformFkeyCheckAttrs(Relation pkrel,
indexStruct = (Form_pg_index) GETSTRUCT(indexTuple); indexStruct = (Form_pg_index) GETSTRUCT(indexTuple);
/* /*
* Must have the right number of columns; must be unique and not a * Must have the right number of columns; must be unique (or if
* partial index; forget it if there are any expressions, too. Invalid * temporal then exclusion instead) and not a partial index; forget it
* indexes are out as well. * if there are any expressions, too. Invalid indexes are out as well.
*/ */
if (indexStruct->indnkeyatts == numattrs && if (indexStruct->indnkeyatts == numattrs &&
indexStruct->indisunique && (with_period ? indexStruct->indisexclusion : indexStruct->indisunique) &&
indexStruct->indisvalid && indexStruct->indisvalid &&
heap_attisnull(indexTuple, Anum_pg_index_indpred, NULL) && heap_attisnull(indexTuple, Anum_pg_index_indpred, NULL) &&
heap_attisnull(indexTuple, Anum_pg_index_indexprs, NULL)) heap_attisnull(indexTuple, Anum_pg_index_indexprs, NULL))
@ -12223,6 +12353,13 @@ transformFkeyCheckAttrs(Relation pkrel,
if (!found) if (!found)
break; break;
} }
/* The last attribute in the index must be the PERIOD FK part */
if (found && with_period)
{
int16 periodattnum = attnums[numattrs - 1];
found = (periodattnum == indexStruct->indkey.values[numattrs - 1]);
}
/* /*
* Refuse to use a deferrable unique/primary key. This is per SQL * Refuse to use a deferrable unique/primary key. This is per SQL
@ -12238,6 +12375,10 @@ transformFkeyCheckAttrs(Relation pkrel,
found_deferrable = true; found_deferrable = true;
found = false; found = false;
} }
/* We need to know whether the index has WITHOUT OVERLAPS */
if (found)
*pk_has_without_overlaps = indexStruct->indisexclusion;
} }
ReleaseSysCache(indexTuple); ReleaseSysCache(indexTuple);
if (found) if (found)
@ -12332,7 +12473,8 @@ validateForeignKeyConstraint(char *conname,
Relation rel, Relation rel,
Relation pkrel, Relation pkrel,
Oid pkindOid, Oid pkindOid,
Oid constraintOid) Oid constraintOid,
bool hasperiod)
{ {
TupleTableSlot *slot; TupleTableSlot *slot;
TableScanDesc scan; TableScanDesc scan;
@ -12360,9 +12502,11 @@ validateForeignKeyConstraint(char *conname,
/* /*
* See if we can do it with a single LEFT JOIN query. A false result * See if we can do it with a single LEFT JOIN query. A false result
* indicates we must proceed with the fire-the-trigger method. * indicates we must proceed with the fire-the-trigger method. We can't do
* a LEFT JOIN for temporal FKs yet, but we can once we support temporal
* left joins.
*/ */
if (RI_Initial_Check(&trig, rel, pkrel)) if (!hasperiod && RI_Initial_Check(&trig, rel, pkrel))
return; return;
/* /*
@ -12513,6 +12657,7 @@ createForeignKeyActionTriggers(Relation rel, Oid refRelOid, Constraint *fkconstr
fk_trigger->whenClause = NULL; fk_trigger->whenClause = NULL;
fk_trigger->transitionRels = NIL; fk_trigger->transitionRels = NIL;
fk_trigger->constrrel = NULL; fk_trigger->constrrel = NULL;
switch (fkconstraint->fk_del_action) switch (fkconstraint->fk_del_action)
{ {
case FKCONSTR_ACTION_NOACTION: case FKCONSTR_ACTION_NOACTION:
@ -12573,6 +12718,7 @@ createForeignKeyActionTriggers(Relation rel, Oid refRelOid, Constraint *fkconstr
fk_trigger->whenClause = NULL; fk_trigger->whenClause = NULL;
fk_trigger->transitionRels = NIL; fk_trigger->transitionRels = NIL;
fk_trigger->constrrel = NULL; fk_trigger->constrrel = NULL;
switch (fkconstraint->fk_upd_action) switch (fkconstraint->fk_upd_action)
{ {
case FKCONSTR_ACTION_NOACTION: case FKCONSTR_ACTION_NOACTION:

View File

@ -523,12 +523,13 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
SetResetClause FunctionSetResetClause SetResetClause FunctionSetResetClause
%type <node> TableElement TypedTableElement ConstraintElem TableFuncElement %type <node> TableElement TypedTableElement ConstraintElem TableFuncElement
%type <node> columnDef columnOptions %type <node> columnDef columnOptions optionalPeriodName
%type <defelt> def_elem reloption_elem old_aggr_elem operator_def_elem %type <defelt> def_elem reloption_elem old_aggr_elem operator_def_elem
%type <node> def_arg columnElem where_clause where_or_current_clause %type <node> def_arg columnElem where_clause where_or_current_clause
a_expr b_expr c_expr AexprConst indirection_el opt_slice_bound a_expr b_expr c_expr AexprConst indirection_el opt_slice_bound
columnref in_expr having_clause func_table xmltable array_expr columnref in_expr having_clause func_table xmltable array_expr
OptWhereClause operator_def_arg OptWhereClause operator_def_arg
%type <list> opt_column_and_period_list
%type <list> rowsfrom_item rowsfrom_list opt_col_def_list %type <list> rowsfrom_item rowsfrom_list opt_col_def_list
%type <boolean> opt_ordinality opt_without_overlaps %type <boolean> opt_ordinality opt_without_overlaps
%type <list> ExclusionConstraintList ExclusionConstraintElem %type <list> ExclusionConstraintList ExclusionConstraintElem
@ -755,7 +756,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query);
OVER OVERLAPS OVERLAY OVERRIDING OWNED OWNER OVER OVERLAPS OVERLAY OVERRIDING OWNED OWNER
PARALLEL PARAMETER PARSER PARTIAL PARTITION PASSING PASSWORD PARALLEL PARAMETER PARSER PARTIAL PARTITION PASSING PASSWORD
PLACING PLANS POLICY PERIOD PLACING PLANS POLICY
POSITION PRECEDING PRECISION PRESERVE PREPARE PREPARED PRIMARY POSITION PRECEDING PRECISION PRESERVE PREPARE PREPARED PRIMARY
PRIOR PRIVILEGES PROCEDURAL PROCEDURE PROCEDURES PROGRAM PUBLICATION PRIOR PRIVILEGES PROCEDURAL PROCEDURE PROCEDURES PROGRAM PUBLICATION
@ -4237,21 +4238,31 @@ ConstraintElem:
NULL, yyscanner); NULL, yyscanner);
$$ = (Node *) n; $$ = (Node *) n;
} }
| FOREIGN KEY '(' columnList ')' REFERENCES qualified_name | FOREIGN KEY '(' columnList optionalPeriodName ')' REFERENCES qualified_name
opt_column_list key_match key_actions ConstraintAttributeSpec opt_column_and_period_list key_match key_actions ConstraintAttributeSpec
{ {
Constraint *n = makeNode(Constraint); Constraint *n = makeNode(Constraint);
n->contype = CONSTR_FOREIGN; n->contype = CONSTR_FOREIGN;
n->location = @1; n->location = @1;
n->pktable = $7; n->pktable = $8;
n->fk_attrs = $4; n->fk_attrs = $4;
n->pk_attrs = $8; if ($5)
n->fk_matchtype = $9; {
n->fk_upd_action = ($10)->updateAction->action; n->fk_attrs = lappend(n->fk_attrs, $5);
n->fk_del_action = ($10)->deleteAction->action; n->fk_with_period = true;
n->fk_del_set_cols = ($10)->deleteAction->cols; }
processCASbits($11, @11, "FOREIGN KEY", n->pk_attrs = linitial($9);
if (lsecond($9))
{
n->pk_attrs = lappend(n->pk_attrs, lsecond($9));
n->pk_with_period = true;
}
n->fk_matchtype = $10;
n->fk_upd_action = ($11)->updateAction->action;
n->fk_del_action = ($11)->deleteAction->action;
n->fk_del_set_cols = ($11)->deleteAction->cols;
processCASbits($12, @12, "FOREIGN KEY",
&n->deferrable, &n->initdeferred, &n->deferrable, &n->initdeferred,
&n->skip_validation, NULL, &n->skip_validation, NULL,
yyscanner); yyscanner);
@ -4279,6 +4290,16 @@ columnList:
| columnList ',' columnElem { $$ = lappend($1, $3); } | columnList ',' columnElem { $$ = lappend($1, $3); }
; ;
optionalPeriodName:
',' PERIOD columnElem { $$ = $3; }
| /*EMPTY*/ { $$ = NULL; }
;
opt_column_and_period_list:
'(' columnList optionalPeriodName ')' { $$ = list_make2($2, $3); }
| /*EMPTY*/ { $$ = list_make2(NIL, NULL); }
;
columnElem: ColId columnElem: ColId
{ {
$$ = (Node *) makeString($1); $$ = (Node *) makeString($1);
@ -17491,6 +17512,7 @@ unreserved_keyword:
| PARTITION | PARTITION
| PASSING | PASSING
| PASSWORD | PASSWORD
| PERIOD
| PLANS | PLANS
| POLICY | POLICY
| PRECEDING | PRECEDING
@ -18108,6 +18130,7 @@ bare_label_keyword:
| PARTITION | PARTITION
| PASSING | PASSING
| PASSWORD | PASSWORD
| PERIOD
| PLACING | PLACING
| PLANS | PLANS
| POLICY | POLICY

View File

@ -30,6 +30,7 @@
#include "access/xact.h" #include "access/xact.h"
#include "catalog/pg_collation.h" #include "catalog/pg_collation.h"
#include "catalog/pg_constraint.h" #include "catalog/pg_constraint.h"
#include "catalog/pg_proc.h"
#include "commands/trigger.h" #include "commands/trigger.h"
#include "executor/executor.h" #include "executor/executor.h"
#include "executor/spi.h" #include "executor/spi.h"
@ -45,6 +46,7 @@
#include "utils/inval.h" #include "utils/inval.h"
#include "utils/lsyscache.h" #include "utils/lsyscache.h"
#include "utils/memutils.h" #include "utils/memutils.h"
#include "utils/rangetypes.h"
#include "utils/rel.h" #include "utils/rel.h"
#include "utils/rls.h" #include "utils/rls.h"
#include "utils/ruleutils.h" #include "utils/ruleutils.h"
@ -96,6 +98,9 @@
* *
* Information extracted from an FK pg_constraint entry. This is cached in * Information extracted from an FK pg_constraint entry. This is cached in
* ri_constraint_cache. * ri_constraint_cache.
*
* Note that pf/pp/ff_eq_oprs may hold the overlaps operator instead of equals
* for the PERIOD part of a temporal foreign key.
*/ */
typedef struct RI_ConstraintInfo typedef struct RI_ConstraintInfo
{ {
@ -115,12 +120,15 @@ typedef struct RI_ConstraintInfo
int16 confdelsetcols[RI_MAX_NUMKEYS]; /* attnums of cols to set on int16 confdelsetcols[RI_MAX_NUMKEYS]; /* attnums of cols to set on
* delete */ * delete */
char confmatchtype; /* foreign key's match type */ char confmatchtype; /* foreign key's match type */
bool hasperiod; /* if the foreign key uses PERIOD */
int nkeys; /* number of key columns */ int nkeys; /* number of key columns */
int16 pk_attnums[RI_MAX_NUMKEYS]; /* attnums of referenced cols */ int16 pk_attnums[RI_MAX_NUMKEYS]; /* attnums of referenced cols */
int16 fk_attnums[RI_MAX_NUMKEYS]; /* attnums of referencing cols */ int16 fk_attnums[RI_MAX_NUMKEYS]; /* attnums of referencing cols */
Oid pf_eq_oprs[RI_MAX_NUMKEYS]; /* equality operators (PK = FK) */ Oid pf_eq_oprs[RI_MAX_NUMKEYS]; /* equality operators (PK = FK) */
Oid pp_eq_oprs[RI_MAX_NUMKEYS]; /* equality operators (PK = PK) */ Oid pp_eq_oprs[RI_MAX_NUMKEYS]; /* equality operators (PK = PK) */
Oid ff_eq_oprs[RI_MAX_NUMKEYS]; /* equality operators (FK = FK) */ Oid ff_eq_oprs[RI_MAX_NUMKEYS]; /* equality operators (FK = FK) */
Oid period_contained_by_oper; /* anyrange <@ anyrange */
Oid agged_period_contained_by_oper; /* fkattr <@ range_agg(pkattr) */
dlist_node valid_link; /* Link in list of valid entries */ dlist_node valid_link; /* Link in list of valid entries */
} RI_ConstraintInfo; } RI_ConstraintInfo;
@ -199,8 +207,8 @@ static void ri_BuildQueryKey(RI_QueryKey *key,
int32 constr_queryno); int32 constr_queryno);
static bool ri_KeysEqual(Relation rel, TupleTableSlot *oldslot, TupleTableSlot *newslot, static bool ri_KeysEqual(Relation rel, TupleTableSlot *oldslot, TupleTableSlot *newslot,
const RI_ConstraintInfo *riinfo, bool rel_is_pk); const RI_ConstraintInfo *riinfo, bool rel_is_pk);
static bool ri_AttributesEqual(Oid eq_opr, Oid typeid, static bool ri_CompareWithCast(Oid eq_opr, Oid typeid,
Datum oldvalue, Datum newvalue); Datum lhs, Datum rhs);
static void ri_InitHashTables(void); static void ri_InitHashTables(void);
static void InvalidateConstraintCacheCallBack(Datum arg, int cacheid, uint32 hashvalue); static void InvalidateConstraintCacheCallBack(Datum arg, int cacheid, uint32 hashvalue);
@ -362,14 +370,41 @@ RI_FKey_check(TriggerData *trigdata)
* FOR KEY SHARE OF x * FOR KEY SHARE OF x
* The type id's for the $ parameters are those of the * The type id's for the $ parameters are those of the
* corresponding FK attributes. * corresponding FK attributes.
*
* But for temporal FKs we need to make sure
* the FK's range is completely covered.
* So we use this query instead:
* SELECT 1
* FROM (
* SELECT pkperiodatt AS r
* FROM [ONLY] pktable x
* WHERE pkatt1 = $1 [AND ...]
* AND pkperiodatt && $n
* FOR KEY SHARE OF x
* ) x1
* HAVING $n <@ range_agg(x1.r)
* Note if FOR KEY SHARE ever allows GROUP BY and HAVING
* we can make this a bit simpler.
* ---------- * ----------
*/ */
initStringInfo(&querybuf); initStringInfo(&querybuf);
pk_only = pk_rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE ? pk_only = pk_rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE ?
"" : "ONLY "; "" : "ONLY ";
quoteRelationName(pkrelname, pk_rel); quoteRelationName(pkrelname, pk_rel);
appendStringInfo(&querybuf, "SELECT 1 FROM %s%s x", if (riinfo->hasperiod)
pk_only, pkrelname); {
quoteOneName(attname,
RIAttName(pk_rel, riinfo->pk_attnums[riinfo->nkeys - 1]));
appendStringInfo(&querybuf,
"SELECT 1 FROM (SELECT %s AS r FROM %s%s x",
attname, pk_only, pkrelname);
}
else
{
appendStringInfo(&querybuf, "SELECT 1 FROM %s%s x",
pk_only, pkrelname);
}
querysep = "WHERE"; querysep = "WHERE";
for (int i = 0; i < riinfo->nkeys; i++) for (int i = 0; i < riinfo->nkeys; i++)
{ {
@ -387,6 +422,18 @@ RI_FKey_check(TriggerData *trigdata)
queryoids[i] = fk_type; queryoids[i] = fk_type;
} }
appendStringInfoString(&querybuf, " FOR KEY SHARE OF x"); appendStringInfoString(&querybuf, " FOR KEY SHARE OF x");
if (riinfo->hasperiod)
{
Oid fk_type = RIAttType(fk_rel, riinfo->fk_attnums[riinfo->nkeys - 1]);
appendStringInfo(&querybuf, ") x1 HAVING ");
sprintf(paramname, "$%d", riinfo->nkeys);
ri_GenerateQual(&querybuf, "",
paramname, fk_type,
riinfo->agged_period_contained_by_oper,
"pg_catalog.range_agg", ANYMULTIRANGEOID);
appendStringInfo(&querybuf, "(x1.r)");
}
/* Prepare and save the plan */ /* Prepare and save the plan */
qplan = ri_PlanCheck(querybuf.data, riinfo->nkeys, queryoids, qplan = ri_PlanCheck(querybuf.data, riinfo->nkeys, queryoids,
@ -494,14 +541,39 @@ ri_Check_Pk_Match(Relation pk_rel, Relation fk_rel,
* FOR KEY SHARE OF x * FOR KEY SHARE OF x
* The type id's for the $ parameters are those of the * The type id's for the $ parameters are those of the
* PK attributes themselves. * PK attributes themselves.
* But for temporal FKs we need to make sure
* the FK's range is completely covered.
* So we use this query instead:
* SELECT 1
* FROM (
* SELECT pkperiodatt AS r
* FROM [ONLY] pktable x
* WHERE pkatt1 = $1 [AND ...]
* AND pkperiodatt && $n
* FOR KEY SHARE OF x
* ) x1
* HAVING $n <@ range_agg(x1.r)
* Note if FOR KEY SHARE ever allows GROUP BY and HAVING
* we can make this a bit simpler.
* ---------- * ----------
*/ */
initStringInfo(&querybuf); initStringInfo(&querybuf);
pk_only = pk_rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE ? pk_only = pk_rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE ?
"" : "ONLY "; "" : "ONLY ";
quoteRelationName(pkrelname, pk_rel); quoteRelationName(pkrelname, pk_rel);
appendStringInfo(&querybuf, "SELECT 1 FROM %s%s x", if (riinfo->hasperiod)
pk_only, pkrelname); {
quoteOneName(attname, RIAttName(pk_rel, riinfo->pk_attnums[riinfo->nkeys - 1]));
appendStringInfo(&querybuf,
"SELECT 1 FROM (SELECT %s AS r FROM %s%s x",
attname, pk_only, pkrelname);
}
else
{
appendStringInfo(&querybuf, "SELECT 1 FROM %s%s x",
pk_only, pkrelname);
}
querysep = "WHERE"; querysep = "WHERE";
for (int i = 0; i < riinfo->nkeys; i++) for (int i = 0; i < riinfo->nkeys; i++)
{ {
@ -518,6 +590,18 @@ ri_Check_Pk_Match(Relation pk_rel, Relation fk_rel,
queryoids[i] = pk_type; queryoids[i] = pk_type;
} }
appendStringInfoString(&querybuf, " FOR KEY SHARE OF x"); appendStringInfoString(&querybuf, " FOR KEY SHARE OF x");
if (riinfo->hasperiod)
{
Oid fk_type = RIAttType(fk_rel, riinfo->fk_attnums[riinfo->nkeys - 1]);
appendStringInfo(&querybuf, ") x1 HAVING ");
sprintf(paramname, "$%d", riinfo->nkeys);
ri_GenerateQual(&querybuf, "",
paramname, fk_type,
riinfo->agged_period_contained_by_oper,
"pg_catalog.range_agg", ANYMULTIRANGEOID);
appendStringInfo(&querybuf, "(x1.r)");
}
/* Prepare and save the plan */ /* Prepare and save the plan */
qplan = ri_PlanCheck(querybuf.data, riinfo->nkeys, queryoids, qplan = ri_PlanCheck(querybuf.data, riinfo->nkeys, queryoids,
@ -2162,6 +2246,7 @@ ri_LoadConstraintInfo(Oid constraintOid)
riinfo->confupdtype = conForm->confupdtype; riinfo->confupdtype = conForm->confupdtype;
riinfo->confdeltype = conForm->confdeltype; riinfo->confdeltype = conForm->confdeltype;
riinfo->confmatchtype = conForm->confmatchtype; riinfo->confmatchtype = conForm->confmatchtype;
riinfo->hasperiod = conForm->conperiod;
DeconstructFkConstraintRow(tup, DeconstructFkConstraintRow(tup,
&riinfo->nkeys, &riinfo->nkeys,
@ -2173,6 +2258,20 @@ ri_LoadConstraintInfo(Oid constraintOid)
&riinfo->ndelsetcols, &riinfo->ndelsetcols,
riinfo->confdelsetcols); riinfo->confdelsetcols);
/*
* For temporal FKs, get the operators and functions we need. We ask the
* opclass of the PK element for these. This all gets cached (as does the
* generated plan), so there's no performance issue.
*/
if (riinfo->hasperiod)
{
Oid opclass = get_index_column_opclass(conForm->conindid, riinfo->nkeys);
FindFKPeriodOpers(opclass,
&riinfo->period_contained_by_oper,
&riinfo->agged_period_contained_by_oper);
}
ReleaseSysCache(tup); ReleaseSysCache(tup);
/* /*
@ -2784,7 +2883,10 @@ ri_HashPreparedPlan(RI_QueryKey *key, SPIPlanPtr plan)
/* /*
* ri_KeysEqual - * ri_KeysEqual -
* *
* Check if all key values in OLD and NEW are equal. * Check if all key values in OLD and NEW are "equivalent":
* For normal FKs we check for equality.
* For temporal FKs we check that the PK side is a superset of its old value,
* or the FK side is a subset of its old value.
* *
* Note: at some point we might wish to redefine this as checking for * Note: at some point we might wish to redefine this as checking for
* "IS NOT DISTINCT" rather than "=", that is, allow two nulls to be * "IS NOT DISTINCT" rather than "=", that is, allow two nulls to be
@ -2840,13 +2942,25 @@ ri_KeysEqual(Relation rel, TupleTableSlot *oldslot, TupleTableSlot *newslot,
} }
else else
{ {
Oid eq_opr;
/*
* When comparing the PERIOD columns we can skip the check
* whenever the referencing column stayed equal or shrank, so test
* with the contained-by operator instead.
*/
if (riinfo->hasperiod && i == riinfo->nkeys - 1)
eq_opr = riinfo->period_contained_by_oper;
else
eq_opr = riinfo->ff_eq_oprs[i];
/* /*
* For the FK table, compare with the appropriate equality * For the FK table, compare with the appropriate equality
* operator. Changes that compare equal will still satisfy the * operator. Changes that compare equal will still satisfy the
* constraint after the update. * constraint after the update.
*/ */
if (!ri_AttributesEqual(riinfo->ff_eq_oprs[i], RIAttType(rel, attnums[i]), if (!ri_CompareWithCast(eq_opr, RIAttType(rel, attnums[i]),
oldvalue, newvalue)) newvalue, oldvalue))
return false; return false;
} }
} }
@ -2856,29 +2970,31 @@ ri_KeysEqual(Relation rel, TupleTableSlot *oldslot, TupleTableSlot *newslot,
/* /*
* ri_AttributesEqual - * ri_CompareWithCast -
* *
* Call the appropriate equality comparison operator for two values. * Call the appropriate comparison operator for two values.
* Normally this is equality, but for the PERIOD part of foreign keys
* it is ContainedBy, so the order of lhs vs rhs is significant.
* *
* NB: we have already checked that neither value is null. * NB: we have already checked that neither value is null.
*/ */
static bool static bool
ri_AttributesEqual(Oid eq_opr, Oid typeid, ri_CompareWithCast(Oid eq_opr, Oid typeid,
Datum oldvalue, Datum newvalue) Datum lhs, Datum rhs)
{ {
RI_CompareHashEntry *entry = ri_HashCompareOp(eq_opr, typeid); RI_CompareHashEntry *entry = ri_HashCompareOp(eq_opr, typeid);
/* Do we need to cast the values? */ /* Do we need to cast the values? */
if (OidIsValid(entry->cast_func_finfo.fn_oid)) if (OidIsValid(entry->cast_func_finfo.fn_oid))
{ {
oldvalue = FunctionCall3(&entry->cast_func_finfo, lhs = FunctionCall3(&entry->cast_func_finfo,
oldvalue, lhs,
Int32GetDatum(-1), /* typmod */ Int32GetDatum(-1), /* typmod */
BoolGetDatum(false)); /* implicit coercion */ BoolGetDatum(false)); /* implicit coercion */
newvalue = FunctionCall3(&entry->cast_func_finfo, rhs = FunctionCall3(&entry->cast_func_finfo,
newvalue, rhs,
Int32GetDatum(-1), /* typmod */ Int32GetDatum(-1), /* typmod */
BoolGetDatum(false)); /* implicit coercion */ BoolGetDatum(false)); /* implicit coercion */
} }
/* /*
@ -2892,10 +3008,16 @@ ri_AttributesEqual(Oid eq_opr, Oid typeid,
* open), we'll just use the default collation here, which could lead to * open), we'll just use the default collation here, which could lead to
* some false negatives. All this would break if we ever allow * some false negatives. All this would break if we ever allow
* database-wide collations to be nondeterministic. * database-wide collations to be nondeterministic.
*
* With range/multirangetypes, the collation of the base type is stored as
* part of the rangetype (pg_range.rngcollation), and always used, so
* there is no danger of inconsistency even using a non-equals operator.
* But if we support arbitrary types with PERIOD, we should perhaps just
* always force a re-check.
*/ */
return DatumGetBool(FunctionCall2Coll(&entry->eq_opr_finfo, return DatumGetBool(FunctionCall2Coll(&entry->eq_opr_finfo,
DEFAULT_COLLATION_OID, DEFAULT_COLLATION_OID,
oldvalue, newvalue)); lhs, rhs));
} }
/* /*
@ -2950,7 +3072,7 @@ ri_HashCompareOp(Oid eq_opr, Oid typeid)
* the cast function to get to the operator's input type. * the cast function to get to the operator's input type.
* *
* XXX eventually it would be good to support array-coercion cases * XXX eventually it would be good to support array-coercion cases
* here and in ri_AttributesEqual(). At the moment there is no point * here and in ri_CompareWithCast(). At the moment there is no point
* because cases involving nonidentical array types will be rejected * because cases involving nonidentical array types will be rejected
* at constraint creation time. * at constraint creation time.
* *

View File

@ -338,7 +338,7 @@ static char *pg_get_viewdef_worker(Oid viewoid,
int prettyFlags, int wrapColumn); int prettyFlags, int wrapColumn);
static char *pg_get_triggerdef_worker(Oid trigid, bool pretty); static char *pg_get_triggerdef_worker(Oid trigid, bool pretty);
static int decompile_column_index_array(Datum column_index_array, Oid relId, static int decompile_column_index_array(Datum column_index_array, Oid relId,
StringInfo buf); bool withPeriod, StringInfo buf);
static char *pg_get_ruledef_worker(Oid ruleoid, int prettyFlags); static char *pg_get_ruledef_worker(Oid ruleoid, int prettyFlags);
static char *pg_get_indexdef_worker(Oid indexrelid, int colno, static char *pg_get_indexdef_worker(Oid indexrelid, int colno,
const Oid *excludeOps, const Oid *excludeOps,
@ -2253,7 +2253,8 @@ pg_get_constraintdef_worker(Oid constraintId, bool fullCommand,
val = SysCacheGetAttrNotNull(CONSTROID, tup, val = SysCacheGetAttrNotNull(CONSTROID, tup,
Anum_pg_constraint_conkey); Anum_pg_constraint_conkey);
decompile_column_index_array(val, conForm->conrelid, &buf); /* If it is a temporal foreign key then it uses PERIOD. */
decompile_column_index_array(val, conForm->conrelid, conForm->conperiod, &buf);
/* add foreign relation name */ /* add foreign relation name */
appendStringInfo(&buf, ") REFERENCES %s(", appendStringInfo(&buf, ") REFERENCES %s(",
@ -2264,7 +2265,7 @@ pg_get_constraintdef_worker(Oid constraintId, bool fullCommand,
val = SysCacheGetAttrNotNull(CONSTROID, tup, val = SysCacheGetAttrNotNull(CONSTROID, tup,
Anum_pg_constraint_confkey); Anum_pg_constraint_confkey);
decompile_column_index_array(val, conForm->confrelid, &buf); decompile_column_index_array(val, conForm->confrelid, conForm->conperiod, &buf);
appendStringInfoChar(&buf, ')'); appendStringInfoChar(&buf, ')');
@ -2350,7 +2351,7 @@ pg_get_constraintdef_worker(Oid constraintId, bool fullCommand,
if (!isnull) if (!isnull)
{ {
appendStringInfoString(&buf, " ("); appendStringInfoString(&buf, " (");
decompile_column_index_array(val, conForm->conrelid, &buf); decompile_column_index_array(val, conForm->conrelid, false, &buf);
appendStringInfoChar(&buf, ')'); appendStringInfoChar(&buf, ')');
} }
@ -2385,7 +2386,7 @@ pg_get_constraintdef_worker(Oid constraintId, bool fullCommand,
val = SysCacheGetAttrNotNull(CONSTROID, tup, val = SysCacheGetAttrNotNull(CONSTROID, tup,
Anum_pg_constraint_conkey); Anum_pg_constraint_conkey);
keyatts = decompile_column_index_array(val, conForm->conrelid, &buf); keyatts = decompile_column_index_array(val, conForm->conrelid, false, &buf);
if (conForm->conperiod) if (conForm->conperiod)
appendStringInfoString(&buf, " WITHOUT OVERLAPS"); appendStringInfoString(&buf, " WITHOUT OVERLAPS");
@ -2591,7 +2592,7 @@ pg_get_constraintdef_worker(Oid constraintId, bool fullCommand,
*/ */
static int static int
decompile_column_index_array(Datum column_index_array, Oid relId, decompile_column_index_array(Datum column_index_array, Oid relId,
StringInfo buf) bool withPeriod, StringInfo buf)
{ {
Datum *keys; Datum *keys;
int nKeys; int nKeys;
@ -2610,7 +2611,9 @@ decompile_column_index_array(Datum column_index_array, Oid relId,
if (j == 0) if (j == 0)
appendStringInfoString(buf, quote_identifier(colName)); appendStringInfoString(buf, quote_identifier(colName));
else else
appendStringInfo(buf, ", %s", quote_identifier(colName)); appendStringInfo(buf, ", %s%s",
(withPeriod && j == nKeys - 1) ? "PERIOD " : "",
quote_identifier(colName));
} }
return nKeys; return nKeys;

View File

@ -108,8 +108,8 @@ CATALOG(pg_constraint,2606,ConstraintRelationId)
bool connoinherit; bool connoinherit;
/* /*
* For primary keys and unique constraints, signifies the last column uses * For primary keys, unique constraints, and foreign keys, signifies the
* overlaps instead of equals. * last column uses overlaps instead of equals.
*/ */
bool conperiod; bool conperiod;
@ -127,20 +127,22 @@ CATALOG(pg_constraint,2606,ConstraintRelationId)
int16 confkey[1]; int16 confkey[1];
/* /*
* If a foreign key, the OIDs of the PK = FK equality operators for each * If a foreign key, the OIDs of the PK = FK equality/overlap operators
* column of the constraint * for each column of the constraint
*/ */
Oid conpfeqop[1] BKI_LOOKUP(pg_operator); Oid conpfeqop[1] BKI_LOOKUP(pg_operator);
/* /*
* If a foreign key, the OIDs of the PK = PK equality operators for each * If a foreign key, the OIDs of the PK = PK equality/overlap operators
* column of the constraint (i.e., equality for the referenced columns) * for each column of the constraint (i.e., equality for the referenced
* columns)
*/ */
Oid conppeqop[1] BKI_LOOKUP(pg_operator); Oid conppeqop[1] BKI_LOOKUP(pg_operator);
/* /*
* If a foreign key, the OIDs of the FK = FK equality operators for each * If a foreign key, the OIDs of the FK = FK equality/overlap operators
* column of the constraint (i.e., equality for the referencing columns) * for each column of the constraint (i.e., equality for the referencing
* columns)
*/ */
Oid conffeqop[1] BKI_LOOKUP(pg_operator); Oid conffeqop[1] BKI_LOOKUP(pg_operator);
@ -284,6 +286,9 @@ extern void DeconstructFkConstraintRow(HeapTuple tuple, int *numfks,
AttrNumber *conkey, AttrNumber *confkey, AttrNumber *conkey, AttrNumber *confkey,
Oid *pf_eq_oprs, Oid *pp_eq_oprs, Oid *ff_eq_oprs, Oid *pf_eq_oprs, Oid *pp_eq_oprs, Oid *ff_eq_oprs,
int *num_fk_del_set_cols, AttrNumber *fk_del_set_cols); int *num_fk_del_set_cols, AttrNumber *fk_del_set_cols);
extern void FindFKPeriodOpers(Oid opclass,
Oid *containedbyoperoid,
Oid *aggedcontainedbyoperoid);
extern bool check_functional_grouping(Oid relid, extern bool check_functional_grouping(Oid relid,
Index varno, Index varlevelsup, Index varno, Index varlevelsup,

View File

@ -50,7 +50,7 @@ extern bool CheckIndexCompatible(Oid oldId,
extern Oid GetDefaultOpClass(Oid type_id, Oid am_id); extern Oid GetDefaultOpClass(Oid type_id, Oid am_id);
extern Oid ResolveOpClass(const List *opclass, Oid attrType, extern Oid ResolveOpClass(const List *opclass, Oid attrType,
const char *accessMethodName, Oid accessMethodId); const char *accessMethodName, Oid accessMethodId);
extern void GetOperatorFromWellKnownStrategy(Oid opclass, Oid atttype, extern void GetOperatorFromWellKnownStrategy(Oid opclass, Oid rhstype,
Oid *opid, StrategyNumber *strat); Oid *opid, StrategyNumber *strat);
/* commands/functioncmds.c */ /* commands/functioncmds.c */

View File

@ -2684,6 +2684,8 @@ typedef struct Constraint
RangeVar *pktable; /* Primary key table */ RangeVar *pktable; /* Primary key table */
List *fk_attrs; /* Attributes of foreign key */ List *fk_attrs; /* Attributes of foreign key */
List *pk_attrs; /* Corresponding attrs in PK table */ List *pk_attrs; /* Corresponding attrs in PK table */
bool fk_with_period; /* Last attribute of FK uses PERIOD */
bool pk_with_period; /* Last attribute of PK uses PERIOD */
char fk_matchtype; /* FULL, PARTIAL, SIMPLE */ char fk_matchtype; /* FULL, PARTIAL, SIMPLE */
char fk_upd_action; /* ON UPDATE action */ char fk_upd_action; /* ON UPDATE action */
char fk_del_action; /* ON DELETE action */ char fk_del_action; /* ON DELETE action */

View File

@ -335,6 +335,7 @@ PG_KEYWORD("partial", PARTIAL, UNRESERVED_KEYWORD, BARE_LABEL)
PG_KEYWORD("partition", PARTITION, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("partition", PARTITION, UNRESERVED_KEYWORD, BARE_LABEL)
PG_KEYWORD("passing", PASSING, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("passing", PASSING, UNRESERVED_KEYWORD, BARE_LABEL)
PG_KEYWORD("password", PASSWORD, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("password", PASSWORD, UNRESERVED_KEYWORD, BARE_LABEL)
PG_KEYWORD("period", PERIOD, UNRESERVED_KEYWORD, BARE_LABEL)
PG_KEYWORD("placing", PLACING, RESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("placing", PLACING, RESERVED_KEYWORD, BARE_LABEL)
PG_KEYWORD("plans", PLANS, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("plans", PLANS, UNRESERVED_KEYWORD, BARE_LABEL)
PG_KEYWORD("policy", POLICY, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("policy", POLICY, UNRESERVED_KEYWORD, BARE_LABEL)

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff