Show expression of virtual columns in error messages - Mailing list pgsql-hackers

From Matheus Alcantara
Subject Show expression of virtual columns in error messages
Date
Msg-id DG5DV8SED62G.2WFJB46D7WIT8@gmail.com
Whole thread Raw
Responses Re: Show expression of virtual columns in error messages
List pgsql-hackers
Hi,

When a constraint violation occurs on a table with virtual generated
columns, the "Failing row contains" error message currently displays the
literal string "virtual" as a placeholder:

  CREATE TABLE t (a int, b int, c int GENERATED ALWAYS AS (a * 2) VIRTUAL CHECK (c < 10));
  INSERT INTO t VALUES (5, 10);

  ERROR:  new row for relation "t" violates check constraint "t_c_check"
  DETAIL:  Failing row contains (5, 10, virtual).

This isn't very helpful for debugging, especially when the user needs to
understand why the check constraint failed or if multiple virtual
generated columns are involved on the original statement.

The attached patch changes this behavior to show the virtual column
expression instead:

  ERROR:  new row for relation "t" violates check constraint "t_c_check"
  DETAIL:  Failing row contains (5, 10, a * 2).

An alternative approach would be to compute and display the actual value
of the virtual column (e.g., "10" instead of "a * 2"). However, I chose
to show the expression because:

1. It's simpler to implement (no need to evaluate the expression)
2. It shows the user *how* the value is computed, which may be more
   useful for understanding why a constraint failed
3. It avoids potential issues with expression evaluation in error paths

Thoughts?

--
Matheus Alcantara
EDB: https://www.enterprisedb.com

Attachment

pgsql-hackers by date:

Previous
From: Shinya Kato
Date:
Subject: Add LIMIT option to COPY FROM
Next
From: "David G. Johnston"
Date:
Subject: Re: Add LIMIT option to COPY FROM