Skip to content

information_schema foreign-key metadata is empty or wrong; IN subquery over information_schema fails (plan is not resolved) #3752

Open

Description

@baibhavsukla

Environment

  • Doltgres 1.4.0, Windows amd64 portable build, fresh data directory, default postgres user.
  • Client: Npgsql 10.0.3 (.NET 10), autocommit.
  • Expected results: the same SQL run against PostgreSQL 18.6 and 16.2.
  • After the XX000 error, the Npgsql connection was closed; the server stayed up.

Steps

Run once with <s> = public and once with <s> = s (after create schema s). The database is named r_d5.

create table <s>.p (a varchar(10), b varchar(10), primary key (a, b));
create table <s>.c (id varchar(10) primary key, a varchar(10), b varchar(10),
  constraint fk_c foreign key (a, b) references <s>.p (a, b));

-- Q1
select constraint_name, table_name, column_name from information_schema.constraint_column_usage
where table_schema = '<s>' order by 1, 2, 3;

-- Q2
select constraint_name, unique_constraint_schema, unique_constraint_name, unique_constraint_name is null
from information_schema.referential_constraints where constraint_schema = '<s>';

-- Q3 (common FK listing)
select tc.table_name, kcu.column_name, ccu.table_name as ref_table, ccu.column_name as ref_column
from information_schema.table_constraints tc
join information_schema.key_column_usage kcu on tc.constraint_name = kcu.constraint_name and tc.table_schema = kcu.table_schema
join information_schema.constraint_column_usage ccu on ccu.constraint_name = tc.constraint_name and ccu.table_schema = tc.table_schema
where tc.constraint_type = 'FOREIGN KEY' and tc.table_schema = '<s>' order by 2, 4;

-- Q4 (referenced columns by position)
select kcu.table_name, kcu.column_name, pk.table_name as ref_table, pk.column_name as ref_column
from information_schema.referential_constraints rc
join information_schema.key_column_usage kcu on kcu.constraint_schema = rc.constraint_schema and kcu.constraint_name = rc.constraint_name
join information_schema.key_column_usage pk on pk.constraint_schema = rc.unique_constraint_schema and pk.constraint_name = rc.unique_constraint_name
  and pk.ordinal_position = kcu.position_in_unique_constraint
where rc.constraint_schema = '<s>' order by kcu.ordinal_position;

-- Q5
select kcu.table_name, kcu.column_name from information_schema.key_column_usage kcu
where kcu.table_schema = '<s>'
  and kcu.constraint_name in (select constraint_name from information_schema.table_constraints where constraint_type = 'FOREIGN KEY')
order by kcu.ordinal_position;

Actual vs expected

Doltgres gave the same result for both schemas unless a schema is shown.

Query Doltgres 1.4.0 PostgreSQL 18.6 / 16.2
Q1 No rows (c_pkey,c,id) (fk_c,p,a) (fk_c,p,b) (p_pkey,p,a) (p_pkey,p,b); 18.6 also lists the NOT NULL constraints
Q2, public (fk_c, r_d5, PRIMARY, false) (fk_c, public, p_pkey, false)
Q2, s (fk_c, r_d5, NULL, true) (fk_c, s, p_pkey, false)
Q3 No rows (c,a,p,a) (c,a,p,b) (c,b,p,a) (c,b,p,b)
Q4 No rows (c,a,p,a) (c,b,p,b)
Q5 XX000 plan is not resolved because of node '*plan.Project' (c,a) (c,b)

Workaround

Read foreign keys from pg_constraint, then resolve the column numbers through pg_attribute. The query returns (fk_c, c, p, {2,3}, {1,2}) for public and (fk_c, s.c, s.p, {2,3}, {1,2}) for s.

select conname, conrelid::regclass::text, confrelid::regclass::text, conkey, confkey
from pg_constraint where contype = 'f';

Activity

  1. Hydrocharged commented on Nov 4, 2026

    @Hydrocharged
    Collaborator

    Thank you for the issue! We will look into this and try to get a fix out this week!

  2. roxblnfk commented on Nov 25, 2026

    @roxblnfk
    Contributor

    On 1.5.3 the FK listing join no longer returns empty results: with a filter on the table it panics. The table_constraints ⋈ constraint_column_usage part alone is enough to trigger it, on a fresh database (Windows amd64 build, PHP PDO pgsql):

    CREATE TABLE a (id int PRIMARY KEY);
    
    SELECT tc.constraint_name
    FROM information_schema.table_constraints tc
    JOIN information_schema.constraint_column_usage ccu ON ccu.constraint_name = tc.constraint_name
    WHERE tc.table_name = 'a';
    ERROR:  receiveMessage recovered panic: interface conversion: types.StringType is not sql.ExtendedType: missing method ConvertToType
    

    PostgreSQL 18 returns a_id_not_null and a_pkey. The same panic happens with a row-value filter (WHERE (tc.table_schema, tc.table_name) IN (('public', 'a'))); without any WHERE the join returns no rows.

    It is raised while planning the hash join:

    github.com/dolthub/go-mysql-server/sql/plan.NewHashLookup
    	github.com/dolthub/go-mysql-server@v0.20.1-0.20261123180908-67b5e0fa40a7/sql/plan/hash_lookup.go:40
    github.com/dolthub/go-mysql-server/sql/memo.(*ExecBuilder).buildHashJoin
    	.../sql/memo/exec_builder.go:208
    github.com/dolthub/go-mysql-server/sql/analyzer.replanJoin
    	.../sql/analyzer/indexed_joins.go:254
    

    table_constraints.constraint_name comes from the MySQL view (see #3825), while constraint_column_usage already has PostgreSQL columns, so the guess is that the hash key mixes a MySQL string type with a Doltgres one; not checked in the code. Cycle ORM (cycle/database, PHP) runs this join for every table it introspects, and 24 of its PostgreSQL tests fail on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions