From 5de11fde244608100d76ba92c642f986877d0455 Mon Sep 17 00:00:00 2001
From: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
Date: Mon, 16 Mar 2026 21:10:28 +0530
Subject: [PATCH v20260316 2/2] pg_indent and review changes

---
 src/backend/utils/adt/ruleutils.c | 31 +++++++++++++++++--------------
 1 file changed, 17 insertions(+), 14 deletions(-)

diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index dc736c6bd37..07617eb4a00 100644
--- a/src/backend/utils/adt/ruleutils.c
+++ b/src/backend/utils/adt/ruleutils.c
@@ -1774,8 +1774,8 @@ make_propgraphdef_elements(StringInfo buf, Oid pgrelid, char pgekind)
 
 struct oid_str_pair
 {
-	Oid oid;
-	char *str;
+	Oid			oid;
+	char	   *str;
 };
 
 static int
@@ -1808,18 +1808,10 @@ make_propgraphdef_labels(StringInfo buf, Oid elid, const char *elalias, Oid elre
 				BTEqualStrategyNumber, F_OIDEQ,
 				ObjectIdGetDatum(elid));
 
-	count = 0;
-	scan = systable_beginscan(pglrel, PropgraphElementLabelElementLabelIndexId, true, NULL, 1, scankey);
-	while ((tup = systable_getnext(scan)))
-	{
-		count++;
-	}
-	systable_endscan(scan);
-
 	/*
-	 * Collect the labels into a list, then sort before printing, to get stable dump output.
+	 * Collect the labels into a list, then sort before printing, to get
+	 * stable dump output.
 	 */
-
 	scan = systable_beginscan(pglrel, PropgraphElementLabelElementLabelIndexId, true, NULL, 1, scankey);
 
 	while ((tup = systable_getnext(scan)))
@@ -1835,7 +1827,20 @@ make_propgraphdef_labels(StringInfo buf, Oid elid, const char *elalias, Oid elre
 	}
 
 	systable_endscan(scan);
+	table_close(pglrel, AccessShareLock);
 
+	count = list_length(label_list);
+
+	/*
+	 * Each element has at least one label. It has default label if no
+	 * declared label exists.
+	 */
+	Assert(count > 0);
+
+	/*
+	 * It is enough for the comparison function to compare just labels, since
+	 * all the labels of a element table should have distinct names.
+	 */
 	list_sort(label_list, list_oid_str_pair_cmp_by_str);
 
 	foreach_ptr(struct oid_str_pair, osp, label_list)
@@ -1851,8 +1856,6 @@ make_propgraphdef_labels(StringInfo buf, Oid elid, const char *elalias, Oid elre
 
 		make_propgraphdef_properties(buf, osp->oid, elrelid);
 	}
-
-	table_close(pglrel, AccessShareLock);
 }
 
 /*
-- 
2.34.1

