How to Resolve “Ambiguous Column Reference” Error in PostgreSQL PL/pgSQL Function with INSERT … ON DUPLICATE KEY UPDATE?

I’m working on a PostgreSQL PL/pgSQL function that inserts a record into a table or updates it using ON CONFLICT. However, when I run the function, I encounter the following error:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[42702] ERROR: column reference "email" is ambiguous
Detail: It could refer to either a PL/pgSQL variable or a table column.
Where: PL/pgSQL function create_email_verification(text, text, timestamp with time zone) line 3 at RETURN QUERY
</code>
<code>[42702] ERROR: column reference "email" is ambiguous Detail: It could refer to either a PL/pgSQL variable or a table column. Where: PL/pgSQL function create_email_verification(text, text, timestamp with time zone) line 3 at RETURN QUERY </code>
[42702] ERROR: column reference "email" is ambiguous
Detail: It could refer to either a PL/pgSQL variable or a table column.
Where: PL/pgSQL function create_email_verification(text, text, timestamp with time zone) line 3 at RETURN QUERY

Table structure:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>CREATE TABLE email_verifications
(
id INTEGER DEFAULT nextval('email_verifications_id_seq'::regclass) NOT NULL PRIMARY KEY,
email TEXT NOT NULL CONSTRAINT unique_email UNIQUE,
code TEXT NOT NULL,
expires_at TIMESTAMP WITH TIME ZONE NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
verified_at TIMESTAMP WITH TIME ZONE
);
</code>
<code>CREATE TABLE email_verifications ( id INTEGER DEFAULT nextval('email_verifications_id_seq'::regclass) NOT NULL PRIMARY KEY, email TEXT NOT NULL CONSTRAINT unique_email UNIQUE, code TEXT NOT NULL, expires_at TIMESTAMP WITH TIME ZONE NOT NULL, created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, verified_at TIMESTAMP WITH TIME ZONE ); </code>
CREATE TABLE email_verifications
(
    id          INTEGER DEFAULT nextval('email_verifications_id_seq'::regclass) NOT NULL PRIMARY KEY,
    email       TEXT NOT NULL CONSTRAINT unique_email UNIQUE,
    code        TEXT NOT NULL,
    expires_at  TIMESTAMP WITH TIME ZONE NOT NULL,
    created_at  TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    verified_at TIMESTAMP WITH TIME ZONE
);

Function:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>create function create_email_verification(
p_email text,
p_code text,
p_expires_at timestamp with time zone)
returns TABLE(
id integer,
email text,
code text,
expires_at timestamp with time zone,
created_at timestamp with time zone
) language plpgsql
as
$$
BEGIN
RETURN QUERY
INSERT INTO email_verifications (email, code, expires_at)
VALUES (p_email, p_code, p_expires_at)
ON CONFLICT (email)
DO UPDATE
SET
code = EXCLUDED.code,
expires_at = EXCLUDED.expires_at
RETURNING
email_verifications.id,
email_verifications.email,
email_verifications.code,
email_verifications.expires_at,
email_verifications.created_at;
END;
$$;
</code>
<code>create function create_email_verification( p_email text, p_code text, p_expires_at timestamp with time zone) returns TABLE( id integer, email text, code text, expires_at timestamp with time zone, created_at timestamp with time zone ) language plpgsql as $$ BEGIN RETURN QUERY INSERT INTO email_verifications (email, code, expires_at) VALUES (p_email, p_code, p_expires_at) ON CONFLICT (email) DO UPDATE SET code = EXCLUDED.code, expires_at = EXCLUDED.expires_at RETURNING email_verifications.id, email_verifications.email, email_verifications.code, email_verifications.expires_at, email_verifications.created_at; END; $$; </code>
create function create_email_verification(
    p_email text, 
    p_code text, 
    p_expires_at timestamp with time zone)

returns TABLE(
    id integer, 
    email text, 
    code text, 
    expires_at timestamp with time zone, 
    created_at timestamp with time zone
) language plpgsql
as
$$
BEGIN
    RETURN QUERY
        INSERT INTO email_verifications (email, code, expires_at)
        VALUES (p_email, p_code, p_expires_at)
        ON CONFLICT (email)
        DO UPDATE 
        SET
            code = EXCLUDED.code, 
            expires_at = EXCLUDED.expires_at
        RETURNING 
            email_verifications.id,
            email_verifications.email, 
            email_verifications.code,
            email_verifications.expires_at,
            email_verifications.created_at;
END;
$$;

Bonus information :

Running the same query outside the function works perfectly fine. For example, the following query executes without issues:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>INSERT INTO email_verifications (email, code, expires_at)
VALUES ('[email protected]', '123456', NOW())
ON CONFLICT (email)
DO UPDATE
SET
code = EXCLUDED.code,
expires_at = EXCLUDED.expires_at
RETURNING id, email, code, expires_at, created_at;
</code>
<code>INSERT INTO email_verifications (email, code, expires_at) VALUES ('[email protected]', '123456', NOW()) ON CONFLICT (email) DO UPDATE SET code = EXCLUDED.code, expires_at = EXCLUDED.expires_at RETURNING id, email, code, expires_at, created_at; </code>
INSERT INTO email_verifications (email, code, expires_at)
VALUES ('[email protected]', '123456', NOW())
ON CONFLICT (email)
DO UPDATE 
SET
    code = EXCLUDED.code, 
    expires_at = EXCLUDED.expires_at
RETURNING id, email, code, expires_at, created_at;

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật