From 80ead181462d1ed4f897a96c6d9fe8ac74249cb3 Mon Sep 17 00:00:00 2001
From: jian he <jian.universality@gmail.com>
Date: Mon, 16 Dec 2024 16:27:48 +0800
Subject: [PATCH v12 2/3] add error position for these 3 functions:
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

transformColumnDefinition
transformAlterTableStmt
transformTableConstraint

Author: Kirill Reshke <reshkekirill@gmail.com>
Author: Jian He <jian.universality@gmail.com>
Reviewed-By: Michaël Paquier <michael@paquier.xyz>
Reviewed-By: Álvaro Herrera <alvherre@alvh.no-ip.org>

discussion: https://postgr.es/m/CALdSSPhqfvKbDwqJaY=yEePi_aq61GmMpW88i6ZH7CMG_2Z4Cg@mail.gmail.com
---
 src/backend/parser/parse_utilcmd.c        | 18 ++++++++++++------
 src/test/regress/expected/constraints.out | 14 ++++++++++++++
 src/test/regress/expected/identity.out    |  2 ++
 3 files changed, 28 insertions(+), 6 deletions(-)

diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c
index 0f324ee4e3..b43923c21d 100644
--- a/src/backend/parser/parse_utilcmd.c
+++ b/src/backend/parser/parse_utilcmd.c
@@ -756,7 +756,8 @@ transformColumnDefinition(CreateStmtContext *cxt, ColumnDef *column)
 				if (cxt->ispartitioned && constraint->is_no_inherit)
 					ereport(ERROR,
 							errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-							errmsg("not-null constraints on partitioned tables cannot be NO INHERIT"));
+							errmsg("not-null constraints on partitioned tables cannot be NO INHERIT"),
+							parser_errposition(cxt->pstate, constraint->location));
 
 				/* Disallow conflicting [NOT] NULL markings */
 				if (saw_nullable && !column->is_not_null)
@@ -771,7 +772,8 @@ transformColumnDefinition(CreateStmtContext *cxt, ColumnDef *column)
 					ereport(ERROR,
 							errcode(ERRCODE_SYNTAX_ERROR),
 							errmsg("conflicting NO INHERIT declarations for not-null constraints on column \"%s\"",
-								   column->colname));
+								   column->colname),
+							parser_errposition(cxt->pstate, constraint->location));
 
 				/*
 				 * If this is the first time we see this column being marked
@@ -806,7 +808,8 @@ transformColumnDefinition(CreateStmtContext *cxt, ColumnDef *column)
 						ereport(ERROR,
 								errcode(ERRCODE_SYNTAX_ERROR),
 								errmsg("conflicting NO INHERIT declarations for not-null constraints on column \"%s\"",
-									   column->colname));
+									   column->colname),
+								parser_errposition(cxt->pstate, constraint->location));
 
 					if (!notnull_constraint->conname && constraint->conname)
 						notnull_constraint->conname = constraint->conname;
@@ -1072,7 +1075,8 @@ transformTableConstraint(CreateStmtContext *cxt, Constraint *constraint)
 			if (cxt->ispartitioned && constraint->is_no_inherit)
 				ereport(ERROR,
 						errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-						errmsg("not-null constraints on partitioned tables cannot be NO INHERIT"));
+						errmsg("not-null constraints on partitioned tables cannot be NO INHERIT"),
+						parser_errposition(cxt->pstate, constraint->location));
 
 			cxt->nnconstraints = lappend(cxt->nnconstraints, constraint);
 			break;
@@ -3627,7 +3631,8 @@ transformAlterTableStmt(Oid relid, AlterTableStmt *stmt,
 							ereport(ERROR,
 									(errcode(ERRCODE_UNDEFINED_COLUMN),
 									 errmsg("column \"%s\" of relation \"%s\" does not exist",
-											cmd->name, RelationGetRelationName(rel))));
+											cmd->name, RelationGetRelationName(rel)),
+									 parser_errposition(pstate, def->location)));
 
 						if (attnum > 0 &&
 							TupleDescAttr(tupdesc, attnum - 1)->attidentity)
@@ -3667,7 +3672,8 @@ transformAlterTableStmt(Oid relid, AlterTableStmt *stmt,
 						ereport(ERROR,
 								(errcode(ERRCODE_UNDEFINED_COLUMN),
 								 errmsg("column \"%s\" of relation \"%s\" does not exist",
-										cmd->name, RelationGetRelationName(rel))));
+										cmd->name, RelationGetRelationName(rel)),
+								 parser_errposition(pstate, def->location)));
 
 					generateSerialExtraStmts(&cxt, newdef,
 											 get_atttype(relid, attnum),
diff --git a/src/test/regress/expected/constraints.out b/src/test/regress/expected/constraints.out
index 71200c90ed..ff3b710468 100644
--- a/src/test/regress/expected/constraints.out
+++ b/src/test/regress/expected/constraints.out
@@ -925,6 +925,8 @@ create table notnull_tbl_fail (a serial constraint foo not null constraint bar n
 ERROR:  conflicting not-null constraint names "foo" and "bar"
 create table notnull_tbl_fail (a serial constraint foo not null no inherit constraint foo not null);
 ERROR:  conflicting NO INHERIT declarations for not-null constraints on column "a"
+LINE 1: create table notnull_tbl_fail (a serial constraint foo not n...
+                                                ^
 create table notnull_tbl_fail (a int constraint foo not null, constraint foo not null a no inherit);
 ERROR:  conflicting NO INHERIT declaration for not-null constraint on column "a"
 create table notnull_tbl_fail (a serial constraint foo not null, constraint bar not null a);
@@ -935,12 +937,18 @@ create table notnull_tbl_fail (a serial, constraint foo not null a no inherit);
 ERROR:  conflicting NO INHERIT declaration for not-null constraint on column "a"
 create table notnull_tbl_fail (a serial not null no inherit);
 ERROR:  conflicting NO INHERIT declarations for not-null constraints on column "a"
+LINE 1: create table notnull_tbl_fail (a serial not null no inherit)...
+                                                ^
 create table notnull_tbl_fail (like notnull_tbl1, constraint foo2 not null a);
 ERROR:  conflicting not-null constraint names "foo" and "foo2"
 create table notnull_tbl_fail (a int primary key constraint foo not null no inherit);
 ERROR:  conflicting NO INHERIT declarations for not-null constraints on column "a"
+LINE 1: create table notnull_tbl_fail (a int primary key constraint ...
+                                                         ^
 create table notnull_tbl_fail (a int not null no inherit primary key);
 ERROR:  conflicting NO INHERIT declarations for not-null constraints on column "a"
+LINE 1: create table notnull_tbl_fail (a int not null no inherit pri...
+                                             ^
 create table notnull_tbl_fail (a int primary key, not null a no inherit);
 ERROR:  conflicting NO INHERIT declaration for not-null constraint on column "a"
 create table notnull_tbl_fail (a int, primary key(a), not null a no inherit);
@@ -949,6 +957,8 @@ create table notnull_tbl_fail (a int generated by default as identity, constrain
 ERROR:  conflicting NO INHERIT declaration for not-null constraint on column "a"
 create table notnull_tbl_fail (a int generated by default as identity not null no inherit);
 ERROR:  conflicting NO INHERIT declarations for not-null constraints on column "a"
+LINE 1: ..._tbl_fail (a int generated by default as identity not null n...
+                                                             ^
 drop table notnull_tbl1;
 -- NOT NULL NO INHERIT
 CREATE TABLE ATACC1 (a int, NOT NULL a NO INHERIT);
@@ -998,8 +1008,12 @@ DROP TABLE ATACC1, ATACC2, ATACC3;
 -- NOT NULL NO INHERIT is not possible on partitioned tables
 CREATE TABLE ATACC1 (a int NOT NULL NO INHERIT) PARTITION BY LIST (a);
 ERROR:  not-null constraints on partitioned tables cannot be NO INHERIT
+LINE 1: CREATE TABLE ATACC1 (a int NOT NULL NO INHERIT) PARTITION BY...
+                                   ^
 CREATE TABLE ATACC1 (a int, NOT NULL a NO INHERIT) PARTITION BY LIST (a);
 ERROR:  not-null constraints on partitioned tables cannot be NO INHERIT
+LINE 1: CREATE TABLE ATACC1 (a int, NOT NULL a NO INHERIT) PARTITION...
+                                    ^
 -- it's not possible to override a no-inherit constraint with an inheritable one
 CREATE TABLE ATACC2 (a int, CONSTRAINT a_is_not_null NOT NULL a NO INHERIT);
 CREATE TABLE ATACC1 (a int);
diff --git a/src/test/regress/expected/identity.out b/src/test/regress/expected/identity.out
index 0398a19484..0b370235ea 100644
--- a/src/test/regress/expected/identity.out
+++ b/src/test/regress/expected/identity.out
@@ -45,6 +45,8 @@ ERROR:  column "a" of relation "itest4" must be declared NOT NULL before identit
 ALTER TABLE itest4 ALTER COLUMN a SET NOT NULL;
 ALTER TABLE itest4 ALTER COLUMN c ADD GENERATED ALWAYS AS IDENTITY;  -- error, column c does not exist
 ERROR:  column "c" of relation "itest4" does not exist
+LINE 1: ALTER TABLE itest4 ALTER COLUMN c ADD GENERATED ALWAYS AS ID...
+                                              ^
 ALTER TABLE itest4 ALTER COLUMN a ADD GENERATED ALWAYS AS IDENTITY;  -- ok
 ALTER TABLE itest4 ALTER COLUMN a DROP NOT NULL;  -- error, disallowed
 ERROR:  column "a" of relation "itest4" is an identity column
-- 
2.34.1

