I added a foreign key on my table which references pg_class.

ALTER TABLE summary_table 
ADD FOREIGN KEY (table_oid_column) REFERENCES pg_class (oid) ON DELETE CASCADE;

I have a summary table which I keep some information about tables with their relational ids. When I drop a table I want to delete automatically related row from my summary table, so I added constraint above. But in my case, when I drop a table nothing happens.

I tried it with a test table, when I delete related row from test table, it works as expected. Such as with this foreign key constraint:

ALTER TABLE summary_table 

What can be the problem? Can it be related with being a catalog table(pg_class) or a hidden system column(oid)?

  • 2
    You cannot create foreign key references to a system table.
    – user1822
    Dec 4, 2012 at 14:06
  • With this option: "allow_system_table_mods = on" I created.
    – metdos
    Dec 4, 2012 at 14:06
  • 5
    Wow. What hack. You shouldn't do that.
    – user1822
    Dec 4, 2012 at 14:08
  • Assume that, I'm also willing to change source code. I want to understand underlying logic, and why this "hack" is not working.
    – metdos
    Dec 4, 2012 at 14:10
  • And as I stated I want to delete related row(s) from my table upon dropping tables. I will be glad to hear new ideas about how to accomplish this in better ways.
    – metdos
    Dec 4, 2012 at 14:14

2 Answers 2


Constraints are only checked when you execute SQL-level DML commands such as DELETE or UPDATE. They are not checked when you effect a system catalog data change using a DDL command such as DROP TABLE. Those take a different code path internally, even though they end up modifying the same data.

As a consequence, you currently cannot do what you are trying to do. In PostgreSQL 9.3, there will be event triggers, which will address the space your problem is in.


AFAIU it will not work (at least in <9.2) because foreign key constraints are based on triggers and triggers are not implemented for system catalog tables.

Some work is held in the direction of so called event triggers in the upcoming 9.3 so it will be possible to catch DDL events like CREATE, ALTER, DROP. However things are still too limited.

  • I'm using 9.1.3, although it does not work as expected, I'm definitely able to create foreign key constraint as stated in comments of the question.
    – metdos
    Dec 4, 2012 at 22:15
  • It possible will not produce any error but it will not be functional.
    – grayhemp
    Dec 4, 2012 at 22:26
  • 1
    You might be right there, it would be good to have error messages.
    – metdos
    Dec 4, 2012 at 22:28
  • I fully agree. I think it it worth mentioning in [email protected] or even [email protected].
    – grayhemp
    Dec 4, 2012 at 22:31

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.