[PATCH] Add target-column context for assignment coercion errors - Mailing list pgsql-hackers

From Midhush Karthic
Subject [PATCH] Add target-column context for assignment coercion errors
Date
Msg-id CAHTNRASEE1RBxYDsbdFb=nnoRK+bJsvFA5bJynP1_-3yBv6_GA@mail.gmail.com
Whole thread
Responses Re: [PATCH] Add target-column context for assignment coercion errors
List pgsql-hackers
Hi hackers,

I'd like to propose a patch to identify the target column when an assignment fails because a value exceeds a varchar(n) length limit.

For example:
```
CREATE TABLE t (
    a varchar(4),
    b varchar(2)
);

INSERT INTO t VALUES ('abcd', 'xyz');
```

Currently, this reports:
```
ERROR:  value too long for type character varying(2)
```

With the patch, it reports:
```
ERROR:  value too long for type character varying(2)
CONTEXT:  column "b" of relation "t"
```

This has been discussed previously, including:
One difficulty discussed in those threads is that the error can occur well below the point where the target column is known, including during constant folding. My patch attempts to address that difficulty in three parts:
  • Preserve destination identity: The patch records the destination relation OID and attribute number on the FuncExpr nodes representing assignment length coercions. The executor installs an ErrorContextCallback around a labeled coercion call and resolves the current relation and column names only when producing error context. The datatype functions retain their existing messages, SQLSTATEs, and diagnostic fields.
  • Scope error context to the coercion: The callback is installed only after evaluating the coercion's arguments, so failures in the source expression do not acquire misleading destination-column context. A dedicated expression opcode uses a shared C helper for both interpreted and LLVM execution. Constant folding reaches the same helper through evaluate_expr().
  • Preserve metadata across transformations: Using relation OID and attribute number allows the context to reflect renames and preserves the destination association in stored expressions. The patch preserves this metadata during expression simplification, retargets copied or inherited defaults, and prevents SQL-function inlining from discarding an annotated coercion's context.
Although varchar(n) motivated the change, the callback applies to labeled assignment length coercions generally, so errors from types such as char(n) and numeric also gain context.

Coverage is deliberately limited: domain constraint errors, failures during input conversion before annotation, and some composite and domain-default cases retain their existing behavior.

The patch is against master and includes regression coverage for planning-time and runtime errors, prepared statements and renames, defaults and generated columns, source-error attribution, and callback cleanup. All 239 core regression tests pass locally. The LLVM dispatch is implemented and tested as well.

I'd particularly appreciate feedback on whether FuncExpr is the appropriate place to preserve the destination identity, whether the scoped callback is a suitable approach, and whether there are expression transformations or stored-expression cases that need additional handling.

Patch attached.

Best,
Midhush
Attachment

pgsql-hackers by date:

Previous
From: Scott Ray
Date:
Subject: Re: Recovery conflict resolution misses backends that import snapshots
Next
From: Michael Paquier
Date:
Subject: Re: BUG: pg_class.relchecks overflow, making table undroppable