From cb435e0f7d8f5936a2ec05bf8b9f9eaa2bd0b008 Mon Sep 17 00:00:00 2001
From: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
Date: Thu, 2 Jul 2026 15:27:15 +0530
Subject: [PATCH v20260708 1/7] Report duplicate property and label names with
 a proper error

Adding a property with the same name multiple times to a label on an
element, either within a single PROPERTIES clause or across statements
via ALTER PROPERTY GRAPH ... ADD PROPERTIES, previously failed with a
unique-index violation on pg_propgraph_label_property. The same class
of bug existed for labels: listing the same label multiple times on one
element in CREATE PROPERTY GRAPH, or adding a label to an element that
already has it via ALTER PROPERTY GRAPH ... ADD LABEL, failed with a
unique-index violation on pg_propgraph_element_label. Detect the
duplicates up front and raise a friendlier error in both cases.

For properties, the cross-statement duplicate is caught by a syscache
probe before insertion. The in-clause duplicate could have been caught
by the same probe if we issued a CommandCounterIncrement() between
property inserts, but that forces a catalog invalidation per property
purely to detect a condition we can check for free on the in-memory
target list. The list-based check is only needed when the properties
are listed explicitly; when they are derived from the table's
attributes, names are already unique.

For labels, a single syscache probe on pg_propgraph_element_label
suffices for both the in-clause and cross-statement cases because
insert_element_record() already issues CommandCounterIncrement()
between successive label inserts.

Author: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
Reported by: Noah Misch <noah@leadboat.com>
Discussion: https://www.postgresql.org/message-id/20260630173053.51.noahmisch@microsoft.com
---
 src/backend/commands/propgraphcmds.c          | 37 +++++++++++++++++--
 .../expected/create_property_graph.out        | 14 +++++++
 .../regress/sql/create_property_graph.sql     | 12 ++++++
 3 files changed, 60 insertions(+), 3 deletions(-)

diff --git a/src/backend/commands/propgraphcmds.c b/src/backend/commands/propgraphcmds.c
index 6939a448895..107c0449302 100644
--- a/src/backend/commands/propgraphcmds.c
+++ b/src/backend/commands/propgraphcmds.c
@@ -784,6 +784,13 @@ insert_label_record(Oid graphid, Oid peoid, const char *label)
 	/*
 	 * Insert into pg_propgraph_element_label
 	 */
+	if (SearchSysCacheExists2(PROPGRAPHELEMENTLABELELEMENTLABEL,
+							  ObjectIdGetDatum(peoid),
+							  ObjectIdGetDatum(labeloid)))
+		ereport(ERROR,
+				errcode(ERRCODE_DUPLICATE_OBJECT),
+				errmsg("label \"%s\" already exists", label));
+	else
 	{
 		Relation	rel;
 		Datum		values[Natts_pg_propgraph_element_label] = {0};
@@ -899,12 +906,30 @@ insert_property_records(Oid graphid, Oid ellabeloid, Oid pgerelid, const PropGra
 	tp = transformTargetList(pstate, proplist, EXPR_KIND_PROPGRAPH_PROPERTY);
 	assign_expr_collations(pstate, (Node *) tp);
 
-	foreach(lc, tp)
+	/*
+	 * When properties are derived from the table's attributes, names are
+	 * already unique. Reject duplicate property names within an explicit
+	 * PROPERTIES clause. Do this after transformTargetList() so that any
+	 * names derived by transformTargetList() are considered.
+	 */
+	if (!properties->all)
 	{
-		TargetEntry *te = lfirst_node(TargetEntry, lc);
+		List	   *seen = NIL;
 
-		insert_property_record(graphid, ellabeloid, pgerelid, te->resname, te->expr);
+		foreach_node(TargetEntry, te, tp)
+		{
+			String	   *name = makeString(te->resname);
+
+			if (list_member(seen, name))
+				ereport(ERROR,
+						errcode(ERRCODE_DUPLICATE_OBJECT),
+						errmsg("property \"%s\" specified more than once", te->resname));
+			seen = lappend(seen, name);
+		}
 	}
+
+	foreach_node(TargetEntry, te, tp)
+		insert_property_record(graphid, ellabeloid, pgerelid, te->resname, te->expr);
 }
 
 /*
@@ -1001,6 +1026,12 @@ insert_property_record(Oid graphid, Oid ellabeloid, Oid pgerelid, const char *pr
 	/*
 	 * Insert into pg_propgraph_label_property
 	 */
+	if (SearchSysCacheExists2(PROPGRAPHLABELPROP, ObjectIdGetDatum(ellabeloid),
+							  ObjectIdGetDatum(propoid)))
+		ereport(ERROR,
+				errcode(ERRCODE_DUPLICATE_OBJECT),
+				errmsg("property \"%s\" already exists", propname));
+	else
 	{
 		Relation	rel;
 		Datum		values[Natts_pg_propgraph_label_property] = {0};
diff --git a/src/test/regress/expected/create_property_graph.out b/src/test/regress/expected/create_property_graph.out
index 3638f7f9f68..f8730e96340 100644
--- a/src/test/regress/expected/create_property_graph.out
+++ b/src/test/regress/expected/create_property_graph.out
@@ -207,6 +207,20 @@ CREATE PROPERTY GRAPH gx
     );
 DROP PROPERTY GRAPH gx;
 DROP TABLE t1x, t2x;
+CREATE PROPERTY GRAPH gx
+    VERTEX TABLES (
+        t1 KEY (a) LABEL l1 PROPERTIES (a AS p, a AS p)  -- duplicate property on label
+    );
+ERROR:  property "p" specified more than once
+ALTER PROPERTY GRAPH g4 ALTER VERTEX TABLE t2 ALTER LABEL t2 ADD PROPERTIES (k * 2 AS i_j);  -- duplicate property on label
+ERROR:  property "i_j" already exists
+CREATE PROPERTY GRAPH gx
+    VERTEX TABLES (
+        t1 KEY (a) LABEL l1 LABEL l1  -- duplicate label on element
+    );
+ERROR:  label "l1" already exists
+ALTER PROPERTY GRAPH g4 ALTER VERTEX TABLE t3 ADD LABEL t3l1 NO PROPERTIES;  -- duplicate label on element
+ERROR:  label "t3l1" already exists
 CREATE PROPERTY GRAPH gx
     VERTEX TABLES (
         t1 KEY (a) LABEL l1 PROPERTIES (a, a AS aa),
diff --git a/src/test/regress/sql/create_property_graph.sql b/src/test/regress/sql/create_property_graph.sql
index 5262971342c..72885be949a 100644
--- a/src/test/regress/sql/create_property_graph.sql
+++ b/src/test/regress/sql/create_property_graph.sql
@@ -161,6 +161,18 @@ CREATE PROPERTY GRAPH gx
 DROP PROPERTY GRAPH gx;
 DROP TABLE t1x, t2x;
 
+CREATE PROPERTY GRAPH gx
+    VERTEX TABLES (
+        t1 KEY (a) LABEL l1 PROPERTIES (a AS p, a AS p)  -- duplicate property on label
+    );
+ALTER PROPERTY GRAPH g4 ALTER VERTEX TABLE t2 ALTER LABEL t2 ADD PROPERTIES (k * 2 AS i_j);  -- duplicate property on label
+
+CREATE PROPERTY GRAPH gx
+    VERTEX TABLES (
+        t1 KEY (a) LABEL l1 LABEL l1  -- duplicate label on element
+    );
+ALTER PROPERTY GRAPH g4 ALTER VERTEX TABLE t3 ADD LABEL t3l1 NO PROPERTIES;  -- duplicate label on element
+
 CREATE PROPERTY GRAPH gx
     VERTEX TABLES (
         t1 KEY (a) LABEL l1 PROPERTIES (a, a AS aa),

base-commit: 8daeaa9b642c3c23cbc516da80d50aade6f4dc07
-- 
2.34.1

