Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Foreign Key to hypertable #498

Open
jvujcic opened this issue Apr 13, 2018 · 19 comments
Open

Foreign Key to hypertable #498

jvujcic opened this issue Apr 13, 2018 · 19 comments
Labels
core tech-debt Needs refactoring and improvement tasks related to the source code and its architecture.

Comments

@jvujcic
Copy link

jvujcic commented Apr 13, 2018

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

@erimatnor
Copy link
Contributor

@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.

@mfreed
Copy link
Member

mfreed commented Apr 14, 2018

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.

@njern
Copy link

njern commented Jul 11, 2018

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.

@jvujcic
Copy link
Author

jvujcic commented Feb 7, 2019

@erimatnor Any news on this limitation?

@cercata
Copy link

cercata commented Feb 15, 2019

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 ?

@mashaalmemon
Copy link

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?

@Norbo11
Copy link

Norbo11 commented May 12, 2019

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?

@kancsuki
Copy link

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?

@jvujcic
Copy link
Author

jvujcic commented Oct 31, 2019

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

@utx0
Copy link

utx0 commented Nov 14, 2019

If so how?

@jvujcic
Copy link
Author

jvujcic commented Nov 14, 2019

@bboule bboule added the core label Feb 19, 2020
@NunoFilipeSantos NunoFilipeSantos added tech-debt Needs refactoring and improvement tasks related to the source code and its architecture. and removed feature-enhancement limitation labels Sep 27, 2021
@nestormh
Copy link

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.

@jvanns
Copy link

jvanns commented Dec 6, 2022

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 ....'

@jomatt
Copy link

jomatt commented Dec 6, 2023

+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

@erimatnor
Copy link
Contributor

@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.

@jvanns
Copy link

jvanns commented Dec 6, 2023

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!

@adriangb
Copy link

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.

@jflambert
Copy link

jflambert commented Jan 17, 2024

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.

@adriangb
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core tech-debt Needs refactoring and improvement tasks related to the source code and its architecture.
Projects
None yet
Development

No branches or pull requests