mirror of
https://github.com/postgres/postgres.git
synced 2026-10-23 13:29:09 +03:00
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/flat/20260630173053.51.noahmisch%40microsoft.com
This commit is contained in:
@@ -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};
|
||||
@@ -901,12 +908,30 @@ insert_property_records(Oid graphid, Oid ellabeloid, Oid pgerelid, const PropGra
|
||||
resolveTargetListUnknowns(pstate, tp);
|
||||
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);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -1003,6 +1028,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};
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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),
|
||||
|
||||
Reference in New Issue
Block a user