Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-30 · container image postgres:17
In a query, a table without an alias is referred to by its name without the schema. If orders (your schema) and archive.orders are both in the FROM list, orders.id could mean either one, and PostgreSQL stops with 42P09 (ambiguous_alias).
This happens a lot when comparing or syncing with an archive, backup or staging schema. In all three cases below, giving each table its own alias fixed it.
01JOIN of two same-name tables, columns qualified by the table name
SELECT orders.id, orders.amount FROM orders JOIN archive.orders USING (id) failed with 42P09 pointing at the first orders.id.
With aliases o and a, the query returned current and archived amounts side by side (id 2: 200 vs 250).
Setup
DROP SCHEMA IF EXISTS archive CASCADE;
CREATE SCHEMA archive AUTHORIZATION lab_a;NOTICE: 00000: schema "archive" does not exist, skipping
DROP SCHEMA
CREATE SCHEMACREATE TABLE orders (id int PRIMARY KEY, amount int, status text);
INSERT INTO orders VALUES (1, 100, 'OPEN'), (2, 200, 'OPEN'), (3, 300, 'OPEN');
CREATE TABLE archive.orders (id int PRIMARY KEY, amount int, status text);
INSERT INTO archive.orders VALUES (1, 100, 'CLOSED'), (2, 250, 'CLOSED');CREATE TABLE
INSERT 0 3
CREATE TABLE
INSERT 0 2SELECT orders.id, orders.amount FROM orders JOIN archive.orders USING (id);ERROR: 42P09: table reference "orders" is ambiguous
LINE 1: SELECT orders.id, orders.amount FROM orders JOIN archive.ord...
^SELECT schemaname, tablename FROM pg_tables WHERE tablename = 'orders' ORDER BY schemaname; schemaname | tablename
------------+-----------
archive | orders
lab_a | orders
(2 rows)pg_tables shows two tables named orders: one in archive, one in lab_a. Both are in the FROM list.
SELECT count(*) FROM archive.orders; count
-------
2
(1 row)SELECT o.id, o.amount AS current_amount, a.amount AS archived_amount FROM orders o JOIN archive.orders a USING (id) ORDER BY o.id; id | current_amount | archived_amount
----+----------------+-----------------
1 | 100 | 100
2 | 200 | 250
(2 rows)02UPDATE ... FROM with a same-name table
UPDATE orders SET status = 'ARCHIVED' FROM archive.orders WHERE orders.id = archive.orders.id failed: the update target and the FROM table are both orders. Nothing was changed (all rows still OPEN).
With FROM archive.orders a WHERE orders.id = a.id the update ran (UPDATE 2), and ids 1 and 2 became ARCHIVED.
Setup
DROP SCHEMA IF EXISTS archive CASCADE;
CREATE SCHEMA archive AUTHORIZATION lab_a;NOTICE: 00000: schema "archive" does not exist, skipping
DROP SCHEMA
CREATE SCHEMACREATE TABLE orders (id int PRIMARY KEY, amount int, status text);
INSERT INTO orders VALUES (1, 100, 'OPEN'), (2, 200, 'OPEN'), (3, 300, 'OPEN');
CREATE TABLE archive.orders (id int PRIMARY KEY, amount int, status text);
INSERT INTO archive.orders VALUES (1, 100, 'CLOSED'), (2, 250, 'CLOSED');CREATE TABLE
INSERT 0 3
CREATE TABLE
INSERT 0 2UPDATE orders SET status = 'ARCHIVED' FROM archive.orders WHERE orders.id = archive.orders.id;ERROR: 42P09: table reference "orders" is ambiguous
LINE 1: ...SET status = 'ARCHIVED' FROM archive.orders WHERE orders.id ...
^SELECT schemaname, tablename FROM pg_tables WHERE tablename = 'orders' ORDER BY schemaname; schemaname | tablename
------------+-----------
archive | orders
lab_a | orders
(2 rows)The same two orders tables. The target orders and archive.orders share the reference name orders.
SELECT id, status FROM orders ORDER BY id; id | status
----+--------
1 | OPEN
2 | OPEN
3 | OPEN
(3 rows)UPDATE orders SET status = 'ARCHIVED' FROM archive.orders a WHERE orders.id = a.id;
SELECT id, status FROM orders ORDER BY id;UPDATE 2
id | status
----+----------
1 | ARCHIVED
2 | ARCHIVED
3 | OPEN
(3 rows)03DELETE ... USING with a same-name table
DELETE FROM orders USING archive.orders WHERE orders.id = archive.orders.id failed with 42P09 and deleted nothing.
DELETE FROM orders o USING archive.orders a WHERE o.id = a.id deleted 2 rows; only id 3, which is not archived, remained.
Setup
DROP SCHEMA IF EXISTS archive CASCADE;
CREATE SCHEMA archive AUTHORIZATION lab_a;NOTICE: 00000: schema "archive" does not exist, skipping
DROP SCHEMA
CREATE SCHEMACREATE TABLE orders (id int PRIMARY KEY, amount int, status text);
INSERT INTO orders VALUES (1, 100, 'OPEN'), (2, 200, 'OPEN'), (3, 300, 'OPEN');
CREATE TABLE archive.orders (id int PRIMARY KEY, amount int, status text);
INSERT INTO archive.orders VALUES (1, 100, 'CLOSED'), (2, 250, 'CLOSED');CREATE TABLE
INSERT 0 3
CREATE TABLE
INSERT 0 2DELETE FROM orders USING archive.orders WHERE orders.id = archive.orders.id;ERROR: 42P09: table reference "orders" is ambiguous
LINE 1: DELETE FROM orders USING archive.orders WHERE orders.id = ar...
^SELECT schemaname, tablename FROM pg_tables WHERE tablename = 'orders' ORDER BY schemaname; schemaname | tablename
------------+-----------
archive | orders
lab_a | orders
(2 rows)Again archive.orders and lab_a.orders. Writing archive.orders.id does not help, because orders.id on the other side is still ambiguous.
SELECT id FROM orders ORDER BY id; id
----
1
2
3
(3 rows)DELETE FROM orders o USING archive.orders a WHERE o.id = a.id;
SELECT id FROM orders ORDER BY id;DELETE 2
id
----
3
(1 row)FAQ
How do I fix "table reference is ambiguous"?
Give each table an alias (FROM orders o JOIN archive.orders a) and qualify columns with the alias, not the table name.
Why is it ambiguous when I wrote archive.orders?
Without an alias, archive.orders is referenced as orders, the same as your own orders. The error points at the unqualified orders.id.
Is 42P09 the same as 42712 table name specified more than once?
No. 42712 is when the same alias is given twice. 42P09 is when two different tables end up with the same reference name and you use it.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12