timescaledb icon indicating copy to clipboard operation
timescaledb copied to clipboard

Foreign Key to hypertable

Open jvujcic opened this issue 6 years ago • 20 comments

Allow creation of foreign keys (that contain timestamp) to hypertables.

jvujcic avatar Apr 13 '18 08:04 jvujcic

@jvujcic Yes, this is a known issue, but a bit tricky to support since the data in the hypertable is actually split up across a number of subtables (chunks). In theory, you should be able to have a foreign key into a specific chunk.

erimatnor avatar Apr 13 '18 09:04 erimatnor

A little more context: I discussed this issue with @jvujcic at length in Slack, and he understands limitation of handling all types of foreign keys.

However, I think he is especially interested in FKs to existing UNIQUE keys in a hypertable (e.g., whether timestamp is UNIQUE, or a unique composite key on timestamp and some other partitioning key), which should be more architecturally feasible.

mfreed avatar Apr 14 '18 03:04 mfreed

Is this something you guys are planning to tackle? I just noticed this for one of my use cases where I would like to do what the previous poster mentions; refer to existing UNIQUE keys in the hypertable with a foreign key from another hypertable.

njern avatar Jul 11 '18 13:07 njern

@erimatnor Any news on this limitation?

jvujcic avatar Feb 07 '19 17:02 jvujcic

I'm having the same issue while trying to port my DB to make it compatible with timescale. I understand the limitation, I wonder if there are some "desing practices" on how to link to a hypertable.

I have a table for alarms, and another for alarm ack, and i would like to link to what alarm an ack is referenced.

Can I do the link after I remove the FK ? I mean, the FK is just for ensuring integrity, isn't it ?

cercata avatar Feb 15 '19 13:02 cercata

Now that timescaledb is finally available on Azure, we are evaluating it for use with our product. This one is a deal breaker. We want to have data integrity and have the benefits of timescale.

Is dealing with this on the roadmap?

mashaalmemon avatar Mar 24 '19 13:03 mashaalmemon

What I've found strange, is that if you have some tables referencing a timestamp column Y in table X and then call create_hypertable('X', 'Y'), the call succeeds without any error messages and the foreign keys remain.

Then if you try to add a new foreign key constraint on a hypertable, it fails.

Is there a bug here?

Norbo11 avatar May 12 '19 15:05 Norbo11

Seems like Postgres 12 will be able to reference partitioned tables by FKs.

Does this help in any way to implement the same features for hypertables?

kancsuki avatar Jun 20 '19 14:06 kancsuki

Somebody on slack just posted that it is now possible to have FK to unique keys in hypertable. Is this true?

jvujcic avatar Oct 31 '19 09:10 jvujcic

If so how?

utx0 avatar Nov 14 '19 04:11 utx0

Here is a link to slack conversation: https://timescaledb.slack.com/archives/C4GT3N90X/p1572502891281000?thread_ts=1572500608.279900&cid=C4GT3N90X

jvujcic avatar Nov 14 '19 07:11 jvujcic

Here is a link to slack conversation: https://timescaledb.slack.com/archives/C4GT3N90X/p1572502891281000?thread_ts=1572500608.279900&cid=C4GT3N90X

Hi, I tried that example in the current latest release (v2.5.0 over Postgres14 using the docker image timescale/timescaledb:2.5.0-pg14), and it is not working:

demo=# create table logs ( evtime timestamp without time zone not null, device_id int not null, something int not null, primary key(evtime, device_id));
CREATE TABLE
demo=# select create_hypertable('logs', 'evtime');
 create_hypertable 
-------------------
 (1,public,logs,t)
(1 row)

demo=# create table logs_extra (  id bigserial,  evtime timestamp without time zone not null, device_id int not null, "data" jsonb not null , FOREIGN KEY (evtime,device_id) REFERENCES logs(evtime, device_id) ON DELETE RESTRICT );
ERROR:  foreign keys to hypertables are not supported

Version checking, for reference:

demo=# select extname, extversion from pg_extension where extname='timescaledb';
   extname   | extversion 
-------------+------------
 timescaledb | 2.5.0
(1 row)

demo=# select version();
                                                   version                                                    
--------------------------------------------------------------------------------------------------------------
 PostgreSQL 14.0 on x86_64-pc-linux-musl, compiled by gcc (Alpine 10.3.1_git20210424) 10.3.1 20210424, 64-bit
(1 row)
  • I know it is possible to use foreign keys to reference partitioned tables from other tables in Postgres. What is the technical limitation that prevents getting the same behavior in TimescaleDB? I can see that this is now tagged as tech-debt, but I'm curious about the reason.
  • Is this issue planned to be addressed in the short term?

Thank you in advance.

nestormh avatar Nov 26 '21 09:11 nestormh

I just wanted to add a little +1 to this feature request. I've been able to create two hypertables, one with an fkey to the other. This appears to succeed at schema-creation time. But then an insert fails with this message;

"foreign keys to hypertables are not supported"

So I guess they're not really permitted despite the seeming success of the initial schema creation. The above error is thrown from the 'hidden' query that performs an 'ALTER TABLE _timescaledb_internal._hyper_1_1_chunk ADD CONSTRAINT ....'

jvanns avatar Dec 06 '22 17:12 jvanns

+1 on this

I'm a big fan of TimescaleDB so thanks a lot to the whole team for creating such a great product. Is there any comment from the team about this feature request? I (and probably everyone else in this thread) would appreciate a short update @erimatnor

jomatt avatar Dec 06 '23 11:12 jomatt

@jomatt and others, this is something we're currently considering supporting as part of generalizing hypertables to more workloads where foreign keys into a hypertable is required. Can't promise any timeline at this point, but it is definitely on our list of things we'd like to support. Right now it is just a matter of priority compared to other things. Sorry about not being able to say more right now.

erimatnor avatar Dec 06 '23 11:12 erimatnor

Awesome! Just to reiterate what @jomatt said - I'm a massive fan of TimescaleDB too and it is such a welcome and needed enhancement to PostgreSQL so the idea of hypertable generalisation sounds fantastic!

jvanns avatar Dec 06 '23 11:12 jvanns

I ran into this and worked around it by creating a custom type instead of a table linked by a FK. It works, but custom types can be a pain to work with.

adriangb avatar Jan 17 '24 13:01 adriangb

I ran into this and worked around it by creating a custom type instead of a table linked by a FK. It works, but custom types can be a pain to work with.

curious what you mean by custom type. I just ended up creating "fake" foreign keys (just unreferenced) and I use a before delete trigger to clear all required targets (either NULL or DELETE) my understanding is that it's not worse performance wise because individual constraint triggers would still need to be executed, and in my case I'm doing three-four triggers in a single one.

jflambert avatar Jan 17 '24 14:01 jflambert

I mean I did CREATE TYPE ... and then have foo my_type[] in my hypertable.

adriangb avatar Jan 17 '24 15:01 adriangb

does this is fixed by #6989 and timescaledb 2.16.0?

Huluti avatar Aug 06 '24 11:08 Huluti