Get reference count of reverse foreign key relations Image I have a simple user table like this:

ID|Name
-------
12|Foo


I want to know: How many reverse foreign key relations point to the user with ID 12? I mean the number of rows which are a foreign key to user and contain the value 12.

I use PostgresSQL, but a portable solution would be better ...
I don't know postgres well, but I am not aware of any way to do this in a single query. I also doubt there'll be a truly portable way, but the approach is similar.

Generally, I think you'd need to:


Query system (meta) tables to find all foreign key table and column names.
Use this to generate a (possibly massive) select query that counts all the 12s