#8861·citus

Aliased join expressions fail with `Unrecognized range table id`

Author: KardelenCerenCreated Sep 22, 2026Updated Sep 24, 2026

Description

Citus fails when a complete join expression is given an alias and a result column is referenced through that alias. The issue occurs with joins using either USING or ON, while equivalent joins without a join-level alias work as expected.

This may be related to #2012 and #8278. Found while investigating #8823.

The issue was reproduced with:

Citus 14.3.0 on PostgreSQL 18.6 Citus 13.5.0 on PostgreSQL 17.11 Citus 12.1.14 on PostgreSQL 16.15

Reproduction

sql
CREATE TABLE a (id int);
CREATE TABLE b (id int);

SELECT create_distributed_table('a', 'id');
SELECT create_distributed_table('b', 'id');

INSERT INTO a VALUES (1);
INSERT INTO b VALUES (1);

-- Works and returns 1
SELECT id
FROM a JOIN b USING (id);

-- Fails with: ERROR: Unrecognized range table id 3
SELECT j.id
FROM (a JOIN b USING (id)) AS j;

-- Works and returns 1
SELECT a.id
FROM a JOIN b ON a.id = b.id;

-- Fails with: ERROR: Unrecognized range table id 3
SELECT j.a_id
FROM (a JOIN b ON a.id = b.id) AS j(a_id, b_id);

Note

An aliased subquery containing a join works and returns 1:

sql
SELECT DISTINCT l.id
FROM (
    SELECT id, random()
    FROM a JOIN b USING (id)
) AS l;

This suggests that the issue is specific to aliases assigned directly to complete join expressions rather than aliases assigned to subqueries containing joins.

Expected behavior

All four reproduction queries should return 1. PostgreSQL supports aliases on complete join expressions, so Citus should resolve columns referenced through the join alias.

Possible root cause

PostgreSQL represents a column referenced through a named join using the join's range table entry (RTE_JOIN). In these examples, that entry has range table ID 3.

During physical query planning, Citus attempts to remap this ID using NewTableId(). The reconstructed range table tracks the underlying relation entries but not the named join entry, so the lookup fails with:

ERROR: Unrecognized range table id 3