Proposal: Include CHECK constraint expression in violation error DETAIL

From: Koshino Taiki <koshino(at)sraoss(dot)co(dot)jp>
To: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Cc: Yugo Nagata <nagata(at)sraoss(dot)co(dot)jp>
Subject: Proposal: Include CHECK constraint expression in violation error DETAIL
Date: 2026-10-23 00:50:48
Message-ID: OS9P286MB648659674DE8406AE4742D8094642@OS9P286MB6486.JPNP286.PROD.OUTLOOK.COM
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

When a CHECK constraint is violated, PostgreSQL reports the constraint name
and the failing row, but not the constraint expression:

ERROR: new row for relation "staff_sales" violates check constraint "staff_age_check"
DETAIL: Failing row contains (1, 15).

Users need to inspect the table with \d to find the violated condition.
Is there a reason why the expression is not included in the error detail?
(For example, having such information appear in logs could pose a
security risk.)

Including it in DETAIL would avoid this extra step:

DETAIL: Failing row contains (1, 15). Check constraint expression: (age >= 18).

I have attached a concept patch to illustrate the idea. Please note that
this is a work in progress.

For the implementation, I wrote the expression generation by referring to
pg_get_constraintdef_worker().

Regarding security, I have not yet added any permission checks. I plan to
investigate whether they are needed at all, and if so what form they should
take, using the existing handling of the failing-row detail and the
previous proposal that was already committed as references:
<https://www.postgresql.org/message-id/flat/9f9dc7ae-14f0-4a25-5485-964d9bfc19bd%40lab.ntt.co.jp>

If this proposal is considered worthwhile, I would like to continue
developing the patch myself.

Regards,

Taiki Koshino<koshino(at)sraoss(dot)co(dot)jp>
SRA OSS K.K.
TEL: 03-5979-2701 FAX: 03-5979-2702
URL: https://www.sraoss.co.jp/

Attachment Content-Type Size
v1-0001-Report-CHECK-expression-in-constraint-violation-d.patch application/octet-stream 3.4 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Bharath Rupireddy 2026-10-23 00:51:34 Re: Parallel autovacuum: leader crashes when no DSM segment can be created
Previous Message Baji Shaik 2026-10-22 23:32:57 [PATCH] Improve CREATE PUBLICATION error detail for a standalone table/schema name