3

In our project we benefit a lot from the inheritance feature of the PostgreSQL database!

We implement the dynamical creation of schemes for different agencies. This structure permits us to solve a lot of security issues which occurs immediately in the case of bulk tables (without partitioning).

The only problem we encountered is to guarantee the database integrity, which usually (in the sense of the structure without inheritance) is realised by the foreign key constraints.

Since the PostgreSQL has certain limitations (see inheritance caveats) we're forced to maintain the tables structure without the constraints.

Is there any possibility to 'simulate', even supposing the relative performance decay, the foreign keys constraints by means of triggers and/or checks?

Any suggestions are very appreciated! Thank you.

1
  • The key word as I understood would be "Enforcing Foreign Keys Programmatically in PostgreSQL" Commented Sep 9, 2010 at 7:12

2 Answers 2

4

The only problematic situation is that you reference from a table, which is shared, to a parent table. You can work around this with the shared table parent_ids with one column id int primary key. You'll have to maintain this table using triggers on your child tables, but it is very simple — insert to parent_ids on insert, delete from it on delete, update it on update, which changes id.

Then instead of referencing your parent table you'll reference this parent_ids table. This would ensure reference integrity and that you'll not have conflicting id in 2 child tables.

It would leak used ids to any user, but will not allow access to any other data.

Sign up to request clarification or add additional context in comments.

1 Comment

Good hint! Thank you for sharing.
0

In most cases, it should be possible to write regular triggers on the tables that verifies the relationships when data is modified.

1 Comment

agree, but to guarantee that your triggers behave in an appropriate manner would be quite a hard job ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.