RE: Request for Implementation of Custom Error Messages for CHECK Constraints

From: "Miguel Ferreira" <miguelmbferreira(at)gmail(dot)com>
To: "'David G(dot) Johnston'" <david(dot)g(dot)johnston(at)gmail(dot)com>
Cc: <pgsql-hackers(at)postgresql(dot)org>
Subject: RE: Request for Implementation of Custom Error Messages for CHECK Constraints
Date: 2025-05-11 11:03:30
Message-ID: 001001dbc264$54e5b520$feb11f60$@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

De: David G. Johnston <david(dot)g(dot)johnston(at)gmail(dot)com>
Enviada: 11 de maio de 2025 01:58

ALTER TABLE table_name

ADD CONSTRAINT constraint_name

CHECK (condition)

MESSAGE 'Custom error message when the condition is not met.';

>> I’m seeing some value here but, odds are, there is not enough obvious benefit or design to convince someone else to try and envision specifically how the behavior should work and effort to push it through.

if I have to use a trigger to set a different message, then I have to write the same rule twice, in CHECK and TRIGGER, which is redundant.

--------------------------------------------------------------------------------------------------------------------------

Improved User Experience: Applications could capture and display more contextual and helpful error messages to end-users, improving usability and reducing confusion.

>> Arguably a layering violation. To make this point more clearly, do you need to account for i18n?

No

--------------------------------------------------------------------------------------------------------------------------

Enhanced Debugging: Developers could immediately identify the specific business rule that has been violated, speeding up the debugging and resolution of data integrity issues.

>> Is there really a meaningful gap here?

I think there is a significant gap here, especially in complex constraints. The constraint name does not always reveal which specific part of the business rule was violated, which can delay debugging. Personalized messages could provide this information directly

--------------------------------------------------------------------------------------------------------------------------

Implicit Documentation: The custom message would serve as a way to document the intent of the constraint directly within the database schema, facilitating understanding and maintenance of the data model.

>> Constraints can be targeted by “comment on”.

I agree, but the proposal aims at specific error messages when the constraint is violated, for better feedback in development and in applications.

--------------------------------------------------------------------------------------------------------------------------

Consistency: It would allow for a more consistent approach to providing informative feedback on business rule violations, complementing the existing capability in triggers.

Two different tools that can do the same job. One with structure and one customizable. Because triggers exist the proposed feature is less useful.

Best regards,

Miguel Ferreira

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Matthias van de Meent 2025-05-11 12:06:49 Re: Suggestion to add --continue-client-on-abort option to pgbench
Previous Message Miguel Ferreira 2025-05-11 10:37:41 RE: Request for Implementation of Custom Error Messages for CHECK Constraints