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
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);CREATE TABLE
CREATE TABLE
INSERT 0 1
INSERT 0 1SELECT id, name, amount
FROM orders o JOIN customers c ON c.id = o.customer_id;ERROR: 42702: column reference "id" is ambiguous
LINE 1: SELECT id, name, amount
^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; 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.
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; 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
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 $$;CREATE TABLE
INSERT 0 2
CREATE FUNCTIONSELECT * FROM find_user('kim');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 QUERYCREATE 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 $$;CREATE FUNCTIONGive 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.
SELECT * FROM find_user('kim'); 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
CREATE TABLE page_views (page text PRIMARY KEY, views int NOT NULL);
INSERT INTO page_views VALUES ('/home', 1);CREATE TABLE
INSERT 0 1INSERT INTO page_views (page, views) VALUES ('/home', 1)
ON CONFLICT (page) DO UPDATE SET views = views + 1;ERROR: 42702: column reference "views" is ambiguous
LINE 2: ON CONFLICT (page) DO UPDATE SET views = views + 1;
^SELECT * FROM page_views; 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.
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;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
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;
$$;CREATE TABLE
INSERT 0 3
CREATE FUNCTIONSELECT customer_total(10);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 statementCREATE 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;
$$;CREATE FUNCTIONQualifying 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.
SELECT customer_total(10); 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