1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-24 00:23:06 +03:00

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

@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/genam.h"
#include "access/gist.h"
#include "access/htup_details.h"
#include "access/sysattr.h"
#include "access/table.h"
@@ -1649,6 +1650,63 @@ DeconstructFkConstraintRow(HeapTuple tuple, int *numfks,
*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
* a set of grouping columns. If so, return true and add the pg_constraint