Skip to content

PostgreSQL: Constraints not showing when connected as a read-only user #1653

@pauljura

Description

@pauljura

Preconditions

  • HeidiSQL version: 12.1.0.6540
  • Database type and version: postgresql 14.4.0
  • OS: Windows

Describe the bug

HeidiSQL is looking for constraints (e.g. foreign key) by examining the information_schema.constraint_column_usage table. Unfortunately, columns will only be returned if the user is the owner of the table. See: https://www.postgresql.org/docs/current/infoschema-constraint-column-usage.html So if I connect to the database as any other user, e.g. a read-only user, then this information is not being shown.

As an alternative, foreign keys can be discovered with a slightly different query:

SELECT 
  conrelid::regclass AS table_from,
  conname,
  pg_get_constraintdef(c.oid) AS constraintdef
FROM pg_constraint c 
JOIN pg_namespace n 
  ON n.oid = c.connamespace 
WHERE contype IN ('f') 
AND n.nspname = 'schema_name_here';

To Reproduce

  1. Create a database, schema, and a table with a foreign key to another table
  2. Create a user with read only privileges: grant usage on schema "your_schema_name" to "ro_user", grant select on all tables in schema "your_schema_name" to "ro_user";
  3. Connect to the database as "ro_user" and try to view foreign keys on your table, you will see 0.
  4. Connect to the database as a super user, you will see the foreign keys.

Screenshots or Crash reports

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions