Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

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 
ADD FOREIGN KEY (table_oid_column) REFERENCES test (id) ON DELETE CASCADE;

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

share|improve this question

migrated from stackoverflow.com Dec 4 '12 at 19:38

This question came from our site for professional and enthusiast programmers.

2  
You cannot create foreign key references to a system table. –  a_horse_with_no_name Dec 4 '12 at 14:06
    
With this option: "allow_system_table_mods = on" I created. –  metdos Dec 4 '12 at 14:06
5  
Wow. What hack. You shouldn't do that. –  a_horse_with_no_name Dec 4 '12 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 '12 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 '12 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.

share|improve this answer

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.

share|improve this answer
    
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 '12 at 22:15
    
It possible will not produce any error but it will not be functional. –  grayhemp Dec 4 '12 at 22:26
1  
You might be right there, it would be good to have error messages. –  metdos Dec 4 '12 at 22:28
    
I fully agree. I think it it worth mentioning in pgsql-general@postgresql.org or even pgsql-bugs@postgresql.org. –  grayhemp Dec 4 '12 at 22:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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