View pg_foreign_keys

Creates a view to get a list of foreign keys in the database. That includes the check for an existing single index, see boolean result of column “is_indexed”.

Columns

Column Name datatype Description
constraint_name name The name of the constraint.
is_deferrable character varying Whether the setting can be changed within a transaction.
initially_deferred character varying Whether each row is checked on insert or at the end of the transaction.
enforced character varying For PostgreSQL versions <18 it is alway TRUE as the feature is not availabe, starting with PostgreSQL 18 it is TRUE, when the constraint is enforced.
table_schema name The name of the schema
table_name name The name of the table.
column_name name Name of the column used in the foreign key constraint.
foreign_table_schema name The name of the foreign schema.
foreign_table_name name The name of the foreign table.
foreign_column_name name Name of the column used in the foreign key constraint of a foreign table.
is_indexed boolean Whether an index exists for the foreign key constraint, or not.

Example

SELECT *
FROM pg_foreign_keys;
constraint_name is_deferrable initially_deferred enforced table_schema table_name column_name foreign_table_schema foreign_table_name foreign_column_name is_indexed
FK_AlbumArtistId NO NO YES public Album ArtistId public Artist ArtistId true
FK_CustomerSupportRepId NO NO YES public Customer SupportRepId public Employee EmployeeId true
FK_EmployeeReportsTo NO NO YES public Employee ReportsTo public Employee EmployeeId true
FK_InvoiceCustomerId NO NO YES public Invoice CustomerId public Customer CustomerId true
FK_InvoiceLineInvoiceId NO NO YES public InvoiceLine InvoiceId public Invoice InvoiceId true
FK_InvoiceLineTrackId NO NO YES public InvoiceLine TrackId public Track TrackId true
FK_PlaylistTrackPlaylistId NO NO YES public PlaylistTrack PlaylistId public Playlist PlaylistId true
FK_PlaylistTrackTrackId NO NO YES public PlaylistTrack TrackId public Track TrackId true
FK_TrackAlbumId NO NO YES public Track AlbumId public Album AlbumId true
FK_TrackGenreId NO NO YES public Track GenreId public Genre GenreId true
FK_TrackMediaTypeId NO NO YES public Track MediaTypeId public MediaType MediaTypeId true