pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedTemporal foreign keys
6+ messages / 5 participants
[nested] [flat]
* Temporal foreign keys
@ 2012-02-03 06:58 Matthias <nitrogenycs@googlemail.com>
0 siblings, 2 replies; 6+ messages in thread
From: Matthias @ 2012-02-03 06:58 UTC (permalink / raw)
To: pgsql-general
Hey,
how can I implement temporal foreign keys with postgresql? Is writing
triggers the only way to enforce temporal referential integrity
currently?
-Matthias
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Temporal foreign keys
@ 2012-03-15 21:53 Jeff Davis <pgsql@j-davis.com>
parent: Matthias <nitrogenycs@googlemail.com>
1 sibling, 1 reply; 6+ messages in thread
From: Jeff Davis @ 2012-03-15 21:53 UTC (permalink / raw)
To: Matthias <nitrogenycs@googlemail.com>; +Cc: pgsql-general
On Fri, 2012-02-03 at 07:58 +0100, Matthias wrote:
> Hey,
>
> how can I implement temporal foreign keys with postgresql? Is writing
> triggers the only way to enforce temporal referential integrity
> currently?
Yes, currently that's the only way. Look at CREATE CONSTRAINT TRIGGER.
Regards,
Jeff Davis
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Temporal foreign keys
@ 2012-03-16 14:13 Andreas Kretschmer <akretschmer@spamfence.net>
parent: Jeff Davis <pgsql@j-davis.com>
0 siblings, 1 reply; 6+ messages in thread
From: Andreas Kretschmer @ 2012-03-16 14:13 UTC (permalink / raw)
To: pgsql-general
Jeff Davis <pgsql@j-davis.com> wrote:
> On Fri, 2012-02-03 at 07:58 +0100, Matthias wrote:
> > Hey,
> >
> > how can I implement temporal foreign keys with postgresql? Is writing
> > triggers the only way to enforce temporal referential integrity
> > currently?
>
> Yes, currently that's the only way. Look at CREATE CONSTRAINT TRIGGER.
It works in 9.2devel ;-)
test=# create table x (d daterange primary key);
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "x_pkey"
for table "x"
CREATE TABLE
test=*# create table y (d daterange references x);
CREATE TABLE
test=*# insert into x values ('[2012-01-01,2012-01-10)');
INSERT 0 1
test=*# insert into y values ('[2012-01-01,2012-01-10)');
INSERT 0 1
test=*# insert into y values ('[2012-01-01,2012-01-20)');
ERROR: insert or update on table "y" violates foreign key constraint "y_d_fkey"
DETAIL: Key (d)=([2012-01-01,2012-01-20)) is not present in table "x".
Jeff: thx for YOUR work!
Andreas
--
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect. (Linus Torvalds)
"If I was god, I would recompile penguin with --enable-fly." (unknown)
Kaufbach, Saxony, Germany, Europe. N 51.05082°, E 13.56889°
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Temporal foreign keys
@ 2012-03-16 21:13 Jeff Davis <pgsql@j-davis.com>
parent: Andreas Kretschmer <akretschmer@spamfence.net>
0 siblings, 0 replies; 6+ messages in thread
From: Jeff Davis @ 2012-03-16 21:13 UTC (permalink / raw)
To: Andreas Kretschmer <akretschmer@spamfence.net>; +Cc: pgsql-general
On Fri, 2012-03-16 at 15:13 +0100, Andreas Kretschmer wrote:
> > On Fri, 2012-02-03 at 07:58 +0100, Matthias wrote:
> > > how can I implement temporal foreign keys with postgresql? Is writing
> > > triggers the only way to enforce temporal referential integrity
> > > currently?
> >
> It works in 9.2devel ;-)
>
> test=# create table x (d daterange primary key);
> NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "x_pkey"
> for table "x"
> CREATE TABLE
> test=*# create table y (d daterange references x);
> CREATE TABLE
> test=*# insert into x values ('[2012-01-01,2012-01-10)');
> INSERT 0 1
> test=*# insert into y values ('[2012-01-01,2012-01-10)');
> INSERT 0 1
> test=*# insert into y values ('[2012-01-01,2012-01-20)');
> ERROR: insert or update on table "y" violates foreign key constraint "y_d_fkey"
> DETAIL: Key (d)=([2012-01-01,2012-01-20)) is not present in table "x".
If I understand what he was asking for, it was a kind of "range foreign
key" which means that the following query should succeed:
insert into y values ('[2012-01-02,2012-01-04)');
because that range is contained in a value in the table x.
So it's slightly different semantics than a normal foreign key.
But yes, normal foreign keys (based on equality) work fine over range
types.
Regards,
Jeff Davis
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Temporal foreign keys
@ 2012-03-17 09:58 Simon Riggs <simon@2ndQuadrant.com>
parent: Matthias <nitrogenycs@googlemail.com>
1 sibling, 0 replies; 6+ messages in thread
From: Simon Riggs @ 2012-03-17 09:58 UTC (permalink / raw)
To: Matthias <nitrogenycs@googlemail.com>; +Cc: pgsql-general
On Fri, Feb 3, 2012 at 6:58 AM, Matthias <nitrogenycs@googlemail.com> wrote:
> how can I implement temporal foreign keys with postgresql? Is writing
> triggers the only way to enforce temporal referential integrity
> currently?
I think you need to explain what you want slightly better.
My guess would be you want this
create table x (d daterange primary key);
create table y (e date references x (d));
which is a lookup to show that the date is within a valid date range.
But you may also want this...
create table x (id integer, d daterange, primary key(id, d));
create table y (id integer, xid integer, e date, foreign key (xid,
e) references x (id, d));
which is to locate the valid row within a temporal lookup table.
Neither is possible, as yet.
Or you might want something entirely different?
--
Simon Riggs http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
^ permalink raw reply [nested|flat] 6+ messages in thread
* Temporal Foreign Keys
@ 2017-12-28 18:59 Paul A Jungwirth <pj@illuminatedcomputing.com>
0 siblings, 0 replies; 6+ messages in thread
From: Paul A Jungwirth @ 2017-12-28 18:59 UTC (permalink / raw)
To: pgsql-general
Since there have been a couple threads on the hackers list about
temporal features [1, 2], I thought I'd share an extension I've been
writing for temporal foreign keys:
https://github.com/pjungwir/time_for_keys
There is a big test suite, but right now it is still basically a
proof-of-concept, restricted to single-column-integer+tstzrange,
implemented mostly in plpgsql, with no extra support when a
table/column is dropped/renamed.
I'd like to add support for:
- multi-column foreign keys
- other FK types than integers
- other range types than tstzrange (at least tsrange, but maybe any range)
- better catalog integration: show one constraint instead of four
constraint triggers, properly restrict/cascade when a table/column is
dropped, make the triggers work when a table/column is renamed.
- support ON UPDATE/DELETE CASCADE/SET NULL/SET DEFAULT
- I think I also need to add FOR KEY SHARE locking to get correct
concurrent behavior.
I think all that would be easier if I rewrote the plpgsql parts in C.
I've been reading the code in commands/tablecmds.c,
catalog/pg_constraint.c, utils/adt/ri_triggers.c,
catalog/dependency.c, etc., and I think I'm ready to get started. I
think I'd be doing a lot of copy/paste/adjust from the normal RI code.
But first I wanted to share what I have so far and see if anyone could
offer advice or feedback.
Some other extensions that have been helpful for me to read are:
- https://github.com/arkhipov/temporal_tables
- https://github.com/CartoDB/pg_schema_triggers for detecting
DROP/ALTER commands
Right now this is an extension, but I'd be honored to contribute it to
the core project eventually if folks want that. One issue there is
that (traditional) foreign keys require indexes, and you can't index a
thing+range without btree_gist, another extension.
Oh, sorry if this belongs on the hackers list instead of general. I
wasn't really sure which was best!
Thanks,
Paul
[1] https://www.postgresql.org/message-id/CALNdv1h7TUP24Nro53KecvWB2kwA67p%2BPByDuP6_1GeESTFgSA%40mail.g...
[2] https://www.postgresql.org/message-id/78aadf6b-86d4-21b9-9c2a-51f1efb8a499%40postgrespro.ru
^ permalink raw reply [nested|flat] 6+ messages in thread
end of thread, other threads:[~2017-12-28 18:59 UTC | newest]
Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2012-02-03 06:58 Temporal foreign keys Matthias <nitrogenycs@googlemail.com>
2012-03-15 21:53 ` Jeff Davis <pgsql@j-davis.com>
2012-03-16 14:13 ` Andreas Kretschmer <akretschmer@spamfence.net>
2012-03-16 21:13 ` Jeff Davis <pgsql@j-davis.com>
2012-03-17 09:58 ` Simon Riggs <simon@2ndQuadrant.com>
2017-12-28 18:59 Temporal Foreign Keys Paul A Jungwirth <pj@illuminatedcomputing.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox