PostgreSQL 17.12Lab-verified · 2026-12-01

PostgreSQL 42702: column reference "id" is ambiguous (ambiguous_column)

A column name could mean more than one thing (two tables, or a column and a PL/pgSQL variable), so PostgreSQL refuses to guess.

PostgreSQL 17.12
42702: column reference "id" is ambiguous

Jump to a cause

  1. 01Unqualified column in a JOIN where both tables have it
  2. 02PL/pgSQL function with RETURNS TABLE (id, name) selecting id, name from a table
  3. 03ON CONFLICT DO UPDATE SET views = views + 1
  4. 04A PL/pgSQL function parameter has the same name as a column

Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-12-01 · container image postgres:17

42702 means the same name is visible from more than one place and PostgreSQL will not pick one. In a JOIN that is two tables with the same column; in PL/pgSQL it is a table column and an output parameter of RETURNS TABLE; in ON CONFLICT DO UPDATE it is the target table and EXCLUDED.

The fix is always the same idea: prefix the column with a table name or alias. 3 cases reproduced on PostgreSQL 17.

01Unqualified column in a JOIN where both tables have it

orders and customers both have id and name. SELECT id, name, amount does not say which table, so the error points at id.

Setup
Setuprun as lab_a
CREATE TABLE customers (id int PRIMARY KEY, name text);
CREATE TABLE orders (id int PRIMARY KEY, customer_id int, name text, amount int);
INSERT INTO customers VALUES (1, 'kim');
INSERT INTO orders VALUES (10, 1, 'pen', 3000);
OUTPUT
CREATE TABLE
CREATE TABLE
INSERT 0 1
INSERT 0 1
Reproducerun as lab_a
SELECT id, name, amount
FROM orders o JOIN customers c ON c.id = o.customer_id;
✕ ERROR
ERROR:  42702: column reference "id" is ambiguous
LINE 1: SELECT id, name, amount
               ^
Checkrun as lab_a
SELECT column_name, string_agg(table_name, ', ' ORDER BY table_name) AS tables
FROM information_schema.columns
WHERE table_schema = 'lab_a' AND table_name IN ('orders', 'customers')
GROUP BY column_name HAVING count(*) > 1;
OUTPUT
 column_name |      tables
-------------+-------------------
 id          | customers, orders
 name        | customers, orders
(2 rows)

information_schema.columns shows the shared names: id and name exist in both tables. Writing o.id, c.name AS customer, o.name AS item returns the row.

After the fixrun as lab_a
SELECT o.id, c.name AS customer, o.name AS item, o.amount
FROM orders o JOIN customers c ON c.id = o.customer_id;
✓ SUCCESS
 id | customer | item | amount
----+----------+------+--------
 10 | kim      | pen  |   3000
(1 row)

02PL/pgSQL function with RETURNS TABLE (id, name) selecting id, name from a table

The output columns of RETURNS TABLE are also variables inside the function. SELECT id, name FROM users could mean the variable or the column. The error only appears when the function is called, with DETAIL: It could refer to either a PL/pgSQL variable or a table column.

Setup
Setuprun as lab_a
CREATE TABLE users (id int PRIMARY KEY, name text);
INSERT INTO users VALUES (1, 'kim'), (2, 'lee');
CREATE FUNCTION find_user(p_name text)
RETURNS TABLE (id int, name text) LANGUAGE plpgsql AS $$
BEGIN
  RETURN QUERY SELECT id, name FROM users WHERE name = p_name;
END $$;
OUTPUT
CREATE TABLE
INSERT 0 2
CREATE FUNCTION
Reproducerun as lab_a
SELECT * FROM find_user('kim');
✕ ERROR
ERROR:  42702: column reference "id" is ambiguous
LINE 1: SELECT id, name FROM users WHERE name = p_name
               ^
DETAIL:  It could refer to either a PL/pgSQL variable or a table column.
QUERY:  SELECT id, name FROM users WHERE name = p_name
CONTEXT:  PL/pgSQL function find_user(text) line 3 at RETURN QUERY
Fixrun as lab_a
CREATE OR REPLACE FUNCTION find_user(p_name text)
RETURNS TABLE (id int, name text) LANGUAGE plpgsql AS $$
BEGIN
  RETURN QUERY SELECT u.id, u.name FROM users u WHERE u.name = p_name;
END $$;
OUTPUT
CREATE FUNCTION

Give the table an alias and qualify every column: SELECT u.id, u.name FROM users u WHERE u.name = p_name. After CREATE OR REPLACE the call returns 1 | kim.

After the fixrun as lab_a
SELECT * FROM find_user('kim');
✓ SUCCESS
 id | name
----+------
  1 | kim
(1 row)

03ON CONFLICT DO UPDATE SET views = views + 1

Inside DO UPDATE both the existing row (page_views) and the proposed row (EXCLUDED) are visible, so views on the right side is ambiguous.

Setup
Setuprun as lab_a
CREATE TABLE page_views (page text PRIMARY KEY, views int NOT NULL);
INSERT INTO page_views VALUES ('/home', 1);
OUTPUT
CREATE TABLE
INSERT 0 1
Reproducerun as lab_a
INSERT INTO page_views (page, views) VALUES ('/home', 1)
ON CONFLICT (page) DO UPDATE SET views = views + 1;
✕ ERROR
ERROR:  42702: column reference "views" is ambiguous
LINE 2: ON CONFLICT (page) DO UPDATE SET views = views + 1;
                                                 ^
Checkrun as lab_a
SELECT * FROM page_views;
OUTPUT
 page  | views
-------+-------
 /home |     1
(1 row)

Write SET views = page_views.views + EXCLUDED.views. The existing value 1 became 2. The column on the left of SET is written as plain views.

After the fixrun as lab_a
INSERT INTO page_views (page, views) VALUES ('/home', 1)
ON CONFLICT (page) DO UPDATE SET views = page_views.views + EXCLUDED.views;
SELECT * FROM page_views;
✓ SUCCESS
INSERT 0 1
 page  | views
-------+-------
 /home |     2
(1 row)

04A PL/pgSQL function parameter has the same name as a column

The function parameter is called customer_id, and orders also has a customer_id column. In WHERE customer_id = customer_id, PostgreSQL cannot tell whether each side is the parameter or the column, so it raised 42702. DETAIL says It could refer to either a PL/pgSQL variable or a table column, and CONTEXT gives the function name and line (line 5).

Setup
Setuprun as lab_a
CREATE TABLE orders (id int PRIMARY KEY, customer_id int, amount numeric);
INSERT INTO orders VALUES (1, 10, 100), (2, 10, 250), (3, 20, 50);
CREATE FUNCTION customer_total(customer_id int) RETURNS numeric
LANGUAGE plpgsql AS $$
DECLARE
  v_total numeric;
BEGIN
  SELECT sum(amount) INTO v_total FROM orders WHERE customer_id = customer_id;
  RETURN v_total;
END;
$$;
OUTPUT
CREATE TABLE
INSERT 0 3
CREATE FUNCTION
Reproducerun as lab_a
SELECT customer_total(10);
✕ ERROR
ERROR:  42702: column reference "customer_id" is ambiguous
LINE 1: SELECT sum(amount)              FROM orders WHERE customer_i...
                                                          ^
DETAIL:  It could refer to either a PL/pgSQL variable or a table column.
QUERY:  SELECT sum(amount)              FROM orders WHERE customer_id = customer_id
CONTEXT:  PL/pgSQL function customer_total(integer) line 5 at SQL statement
Fixrun as lab_a
CREATE OR REPLACE FUNCTION customer_total(customer_id int) RETURNS numeric
LANGUAGE plpgsql AS $$
DECLARE
  v_total numeric;
BEGIN
  SELECT sum(o.amount) INTO v_total
  FROM orders o
  WHERE o.customer_id = customer_total.customer_id;
  RETURN v_total;
END;
$$;
OUTPUT
CREATE FUNCTION

Qualifying the column with an alias, o.customer_id, and the parameter with the function name, customer_total.customer_id, returned 350 for customer 10. This fix keeps the function signature, including the parameter name, unchanged.

After the fixrun as lab_a
SELECT customer_total(10);
✓ SUCCESS
 customer_total
----------------
            350
(1 row)

FAQ

How do I fix column reference is ambiguous in PostgreSQL?

Prefix the column with the table name or alias (o.id instead of id). To see which names are shared, query information_schema.columns for both tables and group by column_name.

Why do I get 42702 in a PL/pgSQL function with RETURNS TABLE?

The RETURNS TABLE columns are variables inside the function. If the query uses the same names unqualified, PostgreSQL cannot tell variable from column. Alias the table and write u.id, u.name.

How do I reference the old value in ON CONFLICT DO UPDATE?

Use the target table name for the existing row and EXCLUDED for the new one, e.g. SET views = page_views.views + EXCLUDED.views.

Does the error happen when I create the function?

No. CREATE FUNCTION succeeded here; the 42702 came when the function was called.

PL/pgSQL says It could refer to either a PL/pgSQL variable or a table column.

A parameter or variable has the same name as a table column. Qualify the column with a table alias (o.customer_id) and the parameter with the function name (function_name.customer_id).

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