Subject: Re: Error position support for ComputeIndexAttrs

From: jian he <[email-masked]>
Date: Sun, 4 Jan 2026 12:19:25 +0800

On Wed, Dec 31, 2025 at 10:37 PM Kirill Reshke <[email-masked]> wrote:
>
> 4)  DefineIndex inside ATAddIndex. It is triggered for patterns like
> "alter table t add constraint c unique (ii);" - current patch set
> missing support for them. I tried to pass pstate here, but no success,
> because exprLocation returns -1 in ComputeIndexAttrs. Please see my
> attempt attached. I guess this can be completed to get location
> support, but I do not have any time today.

hi.

This will not work for ``ALTER TABLE t ADD CONSTRAINT c UNIQUE (ii);``.
In this case, gram.y produces a single Constraint node, and Constraint node
contain only one location field. However, a unique location is required for each
IndexElem node.
Simply assigning the same location value to all IndexElem nodes does not seem
worth the effort required to add support for this.

see transformIndexConstraint:
``
foreach(lc, constraint->keys)
{
            /* OK, add it to the index definition */
            iparam = makeNode(IndexElem);
             ........
            iparam->location = -1;
            index->indexParams = lappend(index->indexParams, iparam);
}
``

also
``ALTER TABLE t ADD CONSTRAINT c UNIQUE (ii);``
the Constraint.location is the location of the word "CONSTRAINT",
which is far from the IndexElem location we want to report.

--
jian
https://www.enterprisedb.com

---

From: Tom Lane <[email-masked]>
Date: Sun, 04 Jan 2026 12:28:25 -0500

> contain only one location field. However, a unique location is required for each

Yeah, the actual problem is that the column name(s) in Constraint are
just a list of String nodes without per-name locations.
transformIndexConstraint throws some errors using constraint->location
that really ought to point at an individual column name, so there's
room for improvement there, as seen in this example from the
regression tests:

-- UNIQUE with a range column/PERIOD that isn't there:
CREATE TABLE temporal_rng3 (
  id INTEGER,
  CONSTRAINT temporal_rng3_uq UNIQUE (id, valid_at WITHOUT OVERLAPS)
);
ERROR:  column "valid_at" named in key does not exist
LINE 3:   CONSTRAINT temporal_rng3_uq UNIQUE (id, valid_at WITHOUT O...
          ^

But this seems like material for a separate patch.

			regards, tom lane