How can I see which entries depend on a given entry using PostgreSQL ? (dependent meaning “having a foreign key referencing the entry”).
Basically, I want to check which entries might be cascaded when I DELETE a given entry of a table.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
To see all actual rows depending via fk constraint, identify the columns with the tools described below.
Where foreign key constraints are defined with
ON DELETE CASCADE, depending rows will be deleted (possibly cascading theDELETEto more depending tables).Where foreign key constraints are defined with
ON DELETE SET NULL/ON DELETE SET DEFAULT, only the value in the columns will be reset toNULL/ default value.Else a
DELETEon rows with dependent rows would fail with an exception.Then run queries like the following on the identified tables / columns:
pgAdmin supplies this feature:
Pick the the object in the object browser to the left and chose the dependents pane top right.
pgAdmin uses a couple of queries to the system catalog to assemble the list. You could log the commands issued if you want to build a query yourself.
Also, when deleting an object where you are not completely sure about dependents, try a plain
DROPfirst (withoutCASCADE). You will get an error message if any dependent exists …And finally, but proceed with caution!, you can start a transaction and just issue the command:
Then, if you like what you see:
If you don’t:
And it will be like it never happened. 😉
You will see something like this: