public inbox for [email protected]
help / color / mirror / Atom feedFrom: SATYANARAYANA NARLAPURAM <[email protected]>
To: Ashutosh Bapat <[email protected]>
Cc: PostgreSQL Hackers <[email protected]>
Subject: Re: [Bug][patch]: After dropping the last label from a property graph element, invoking pg_get_propgraphdef() triggers an assertion failure
Date: Tue, 21 Apr 2026 00:58:58 -0700
Message-ID: <CAHg+QDdAoW2cU2Wa-yKQs2awHxc19mkHGaAvDUZN=PZUDVsD=A@mail.gmail.com> (raw)
In-Reply-To: <CAExHW5t2YtTMnK7qbvL++pBet-5B8bzgU+8wMcC5aqZwBH0C9w@mail.gmail.com>
References: <CAHg+QDeP=mTHTV48R23zKMy1SBmCKZ_L7-z5zKnYyw+K0x-gCg@mail.gmail.com>
<CAExHW5tsOK7ja2dMykD_qQSLwsuZxCTHx_jFyx-7-O+9WRBDXQ@mail.gmail.com>
<CAHg+QDcXxmeiuySsyR5K=8Wk9Pq2sZ_4HNb6EMonN=yjLz_fyw@mail.gmail.com>
<CAExHW5t2YtTMnK7qbvL++pBet-5B8bzgU+8wMcC5aqZwBH0C9w@mail.gmail.com>
Hi,
On Mon, Apr 20, 2026 at 11:58 PM Ashutosh Bapat <
[email protected]> wrote:
> On Tue, Apr 21, 2026 at 11:28 AM SATYANARAYANA NARLAPURAM
> <[email protected]> wrote:
> >
> > HI Ashutosh,
> >
> > On Mon, Apr 20, 2026 at 10:34 PM Ashutosh Bapat <
> [email protected]> wrote:
> >>
> >> On Mon, Apr 20, 2026 at 11:42 PM SATYANARAYANA NARLAPURAM
> >> <[email protected]> wrote:
> >> >
> >> > Hi hackers,
> >> >
> >> > ALTER PROPERTY GRAPH ... ALTER ... DROP LABEL currently allows
> removing
> >> > the last label from an element, leaving it with zero labels.
> >> >
> >> > On assert-enabled builds, pg_get_propgraphdef() hits
> >> > TRAP: failed Assert("count > 0"), File: "ruleutils.c", Line: 1837,
> PID: 1821840
> >> >
> >> > Repro:
> >> >
> >> > CREATE TABLE t (x int PRIMARY KEY, y int, z int);
> >> > CREATE PROPERTY GRAPH g VERTEX TABLES (t KEY (x) LABEL l1 LABEL l2);
> >> > ALTER PROPERTY GRAPH g ALTER VERTEX TABLE t DROP LABEL l2;
> >> > ALTER PROPERTY GRAPH g ALTER VERTEX TABLE t DROP LABEL l1;
> >> > SELECT pg_get_propgraphdef('g'::regclass);
> >> >
> >> > We can fix it two ways, (1) Prevent dropping the last label; (2)
> handle zero labels.
> >> > I feel it is easier to prevent dropping the last label than handling
> zero labels. Thoughts?
> >> >
> >>
> >> SQL/PGQ standard section 11.25 syntax rule 6 says
> >> "Element table descriptor shall include two or more labels, one of
> >> which has an <identifier> that is equivalent to the <label name>
> >> simply contained in the <drop element table label clause>."
> >>
> >> IIUC this simply means that the last label can not be dropped. That
> >> agrees with your chosen option.
> >>
> >> In the patch,
> >> + while (HeapTupleIsValid(systable_getnext(elscan)))
> >> + nlabels++;
> >>
> >> It's better to break from the while loop after incrementing nlabels
> >> and avoid scanning the entire table in the worst case. All we want to
> >> check is whether another label exists and not all the labels.
> >
> >
> > Please find the attached v2 patch.
> >
> >>
> >>
> >> > The attached patch adds a check in AlterPropGraph() before
> >> > performDeletion(). It scans pg_propgraph_element_label to count labels
> >> > for the element, and raises an error if only one remains. A
> regression test is included
> >> > that drops labels down to the last one, verifies the error, then
> re-adds them back.
> >>
> >> I would add a test to make sure ADD LABEL ... DROP LABEL .. is allowed.
> >
> >
> > +ALTER PROPERTY GRAPH g3 ALTER VERTEX TABLE t3 DROP LABEL t3l1; --
> error: last label
> > +ALTER PROPERTY GRAPH g3 ALTER VERTEX TABLE t3 ADD LABEL t3l2 PROPERTIES
> ALL COLUMNS;
> >
> > Are you looking for any additional coverage?
>
> I thought specifying ADD LABEL and DROP LABEL is supported in the same
> DDL. But that's not the case. Sorry.
>
> Will review the patch soon.
>
> Additionally, the patch should update the DDL document to mention that
> the DDL does not allow dropping the last label on the given graph
> element table.
>
Addressed this in v3 patch.
Thanks,
Satya
Attachments:
[application/octet-stream] v3-0001-Prevent-dropping-the-last-label-from-a-property-grap.patch (5.2K, 3-v3-0001-Prevent-dropping-the-last-label-from-a-property-grap.patch)
download | inline diff:
From 2046f489f06685131b4908292f806e1c44f6e1d7 Mon Sep 17 00:00:00 2001
From: satyanarayana narlapuram <[email protected]>
Date: Tue, 21 Apr 2026 07:52:56 +0000
Subject: [PATCH] Prevent dropping the last label from a property graph element
ALTER PROPERTY GRAPH ... DROP LABEL allowed removing the last label
from an element, leaving it with zero labels. This causes
pg_get_propgraphdef() to crash.
Add a check in AlterPropGraph() that counts the element's labels
before deletion and errors out if only one remains. The label count
loop breaks early once it finds more than one label, avoiding a full
scan of the element_label catalog.
Update the ALTER PROPERTY GRAPH documentation to mention the
restriction.
Reported-by: Satyanarayana Narlapuram <[email protected]>
---
doc/src/sgml/ref/alter_property_graph.sgml | 2 ++
src/backend/commands/propgraphcmds.c | 34 +++++++++++++++++++
.../expected/create_property_graph.out | 6 ++++
.../regress/sql/create_property_graph.sql | 4 +++
4 files changed, 46 insertions(+)
diff --git a/doc/src/sgml/ref/alter_property_graph.sgml b/doc/src/sgml/ref/alter_property_graph.sgml
index 19352c06..8bcca0ec 100644
--- a/doc/src/sgml/ref/alter_property_graph.sgml
+++ b/doc/src/sgml/ref/alter_property_graph.sgml
@@ -100,6 +100,8 @@ ALTER PROPERTY GRAPH [ IF EXISTS ] <replaceable class="parameter">name</replacea
<listitem>
<para>
This form removes a label from an existing vertex or edge table.
+ The last label on an element table cannot be dropped; every element
+ table must have at least one label.
</para>
</listitem>
</varlistentry>
diff --git a/src/backend/commands/propgraphcmds.c b/src/backend/commands/propgraphcmds.c
index 45d2ff1b..b1c5e8ae 100644
--- a/src/backend/commands/propgraphcmds.c
+++ b/src/backend/commands/propgraphcmds.c
@@ -1522,6 +1522,40 @@ AlterPropGraph(ParseState *pstate, const AlterPropGraphStmt *stmt)
get_rel_name(pgrelid), stmt->element_alias, stmt->drop_label),
parser_errposition(pstate, -1));
+ /*
+ * Prevent dropping the last label from an element. Every element
+ * must have at least one label.
+ */
+ {
+ Relation elrel;
+ SysScanDesc elscan;
+ ScanKeyData elkey[1];
+ int nlabels = 0;
+
+ elrel = table_open(PropgraphElementLabelRelationId, AccessShareLock);
+ ScanKeyInit(&elkey[0],
+ Anum_pg_propgraph_element_label_pgelelid,
+ BTEqualStrategyNumber, F_OIDEQ,
+ ObjectIdGetDatum(peoid));
+ elscan = systable_beginscan(elrel, PropgraphElementLabelElementLabelIndexId,
+ true, NULL, 1, elkey);
+ while (HeapTupleIsValid(systable_getnext(elscan)))
+ {
+ nlabels++;
+ if (nlabels > 1)
+ break;
+ }
+ systable_endscan(elscan);
+ table_close(elrel, AccessShareLock);
+
+ if (nlabels <= 1)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
+ errmsg("cannot drop the last label from element \"%s\"",
+ stmt->element_alias),
+ errhint("Every element must have at least one label.")));
+ }
+
ObjectAddressSet(obj, PropgraphElementLabelRelationId, ellabeloid);
performDeletion(&obj, stmt->drop_behavior, 0);
diff --git a/src/test/regress/expected/create_property_graph.out b/src/test/regress/expected/create_property_graph.out
index bc9a596e..740f886c 100644
--- a/src/test/regress/expected/create_property_graph.out
+++ b/src/test/regress/expected/create_property_graph.out
@@ -57,6 +57,12 @@ ALTER PROPERTY GRAPH g3
ALTER PROPERTY GRAPH g3 ALTER VERTEX TABLE t3 DROP LABEL t3l3x; -- error
ERROR: property graph "g3" element "t3" has no label "t3l3x"
ALTER PROPERTY GRAPH g3 ALTER VERTEX TABLE t3 DROP LABEL t3l3;
+-- Test that the last label on an element cannot be dropped
+ALTER PROPERTY GRAPH g3 ALTER VERTEX TABLE t3 DROP LABEL t3l2;
+ALTER PROPERTY GRAPH g3 ALTER VERTEX TABLE t3 DROP LABEL t3l1; -- error: last label
+ERROR: cannot drop the last label from element "t3"
+HINT: Every element must have at least one label.
+ALTER PROPERTY GRAPH g3 ALTER VERTEX TABLE t3 ADD LABEL t3l2 PROPERTIES ALL COLUMNS;
ALTER PROPERTY GRAPH g3 DROP VERTEX TABLES (t2); -- fail
ERROR: cannot drop vertex t2 of property graph g3 because other objects depend on it
DETAIL: edge e1 of property graph g3 depends on vertex t2 of property graph g3
diff --git a/src/test/regress/sql/create_property_graph.sql b/src/test/regress/sql/create_property_graph.sql
index 241f93df..4cf77159 100644
--- a/src/test/regress/sql/create_property_graph.sql
+++ b/src/test/regress/sql/create_property_graph.sql
@@ -52,6 +52,10 @@ ALTER PROPERTY GRAPH g3
ADD LABEL t3l3 PROPERTIES ALL COLUMNS;
ALTER PROPERTY GRAPH g3 ALTER VERTEX TABLE t3 DROP LABEL t3l3x; -- error
ALTER PROPERTY GRAPH g3 ALTER VERTEX TABLE t3 DROP LABEL t3l3;
+-- Test that the last label on an element cannot be dropped
+ALTER PROPERTY GRAPH g3 ALTER VERTEX TABLE t3 DROP LABEL t3l2;
+ALTER PROPERTY GRAPH g3 ALTER VERTEX TABLE t3 DROP LABEL t3l1; -- error: last label
+ALTER PROPERTY GRAPH g3 ALTER VERTEX TABLE t3 ADD LABEL t3l2 PROPERTIES ALL COLUMNS;
ALTER PROPERTY GRAPH g3 DROP VERTEX TABLES (t2); -- fail
ALTER PROPERTY GRAPH g3 DROP VERTEX TABLES (t2) CASCADE;
ALTER PROPERTY GRAPH g3 DROP EDGE TABLES (e2);
--
2.43.0
view thread (13+ messages) latest in thread
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: [email protected]
Cc: [email protected], [email protected], [email protected]
Subject: Re: [Bug][patch]: After dropping the last label from a property graph element, invoking pg_get_propgraphdef() triggers an assertion failure
In-Reply-To: <CAHg+QDdAoW2cU2Wa-yKQs2awHxc19mkHGaAvDUZN=PZUDVsD=A@mail.gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox