PG 9.1 - FK + Check constraint

From: Patrick B <patrickbakerbr(at)gmail(dot)com>
To: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: PG 9.1 - FK + Check constraint
Date: 2017-07-19 01:56:41
Message-ID: CAJNY3ivqAzdUZGwHMq163tHXj13NKbyz_iSaRk2cVUqW=pUgnQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi guys!

The column ent_id bigint on table table1 refers to other tables.

if ent_id = 1 then = table2.idif ent_id = 2 then = table3.idelse = no checks

I need a FK Constraint on table table1 checking if records on table2.id and
table3.idexists.

I know that currently, CHECK expressions cannot contain subqueries nor
refer to variables other than columns of the current row. Another solution
would be to create an IMMUTABLE functiondoing the check and use that in a
CHECK constraint [1]. However, I'm concern about doing this as I use
replication slaves, where all my selects are ran in there. I'm worried
about replication lag in this case.

Is there any other way to perform this? I'm using PG 9.1 for this.

[1]
https://stackoverflow.com/questions/10135754/how-to-make-a-foreign-key-with-a-constraint-on-the-referenced-table-in-postgresq

Thanks!

Patrick

Responses

Browse pgsql-general by date

  From Date Subject
Next Message David G. Johnston 2017-07-19 02:30:15 Re: PG 9.1 - FK + Check constraint
Previous Message Craig Ringer 2017-07-19 01:47:07 Re: [GENERAL] huge RAM use in multi-command ALTER of table heirarchy