(SQL/PGQ) cache lookup failed for label - Mailing list pgsql-hackers

From zengman
Subject (SQL/PGQ) cache lookup failed for label
Date
Msg-id tencent_43D9888041FA4FDE498C7BF1@qq.com
Whole thread
List pgsql-hackers
Hi all, 

I noticed that the following SQL statement triggers the error message `cache lookup failed for label`.

```sql
CREATE TABLE vt (id text PRIMARY KEY, name text, age int);
CREATE TABLE et (id text PRIMARY KEY, src text REFERENCES vt(id), dst text REFERENCES vt(id));
INSERT INTO vt VALUES ('a', 'Alice', 30), ('b', 'Bob', 25);
INSERT INTO et VALUES ('e1', 'a', 'b');

CREATE PROPERTY GRAPH g                
  VERTEX TABLES (vt LABEL l1 PROPERTIES (name) LABEL l2 PROPERTIES (name, age))
  EDGE TABLES (et SOURCE KEY (src) REFERENCES vt(id) DESTINATION KEY (dst) REFERENCES vt(id));


CREATE VIEW v1 AS SELECT * FROM GRAPH_TABLE(g MATCH (a IS l2)-[e IS et]->(b IS l2) COLUMNS (a.name, a.age, b.name AS
bname));                                                                                    
 

                                                                 
 
ALTER PROPERTY GRAPH g ALTER VERTEX TABLE vt DROP LABEL l2;
                                                                                                         
 
SELECT * FROM v1;
```

Here are the actual test results; it appears to be caused by missing dependency information.

```sql
test=# CREATE TABLE vt (id text PRIMARY KEY, name text, age int);
CREATE TABLE
test=# CREATE TABLE et (id text PRIMARY KEY, src text REFERENCES vt(id), dst text REFERENCES vt(id));
CREATE TABLE
test=# INSERT INTO vt VALUES ('a', 'Alice', 30), ('b', 'Bob', 25);
INSERT 0 2
test=# INSERT INTO et VALUES ('e1', 'a', 'b');
INSERT 0 1
test=# CREATE PROPERTY GRAPH g                
  VERTEX TABLES (vt LABEL l1 PROPERTIES (name) LABEL l2 PROPERTIES (name, age))
  EDGE TABLES (et SOURCE KEY (src) REFERENCES vt(id) DESTINATION KEY (dst) REFERENCES vt(id));
CREATE PROPERTY GRAPH
test=# CREATE VIEW v1 AS SELECT * FROM GRAPH_TABLE(g MATCH (a IS l2)-[e IS et]->(b IS l2) COLUMNS (a.name, a.age,
b.nameAS bname));
 
CREATE VIEW
test=# ALTER PROPERTY GRAPH g ALTER VERTEX TABLE vt DROP LABEL l2; 
ALTER PROPERTY GRAPH
test=# SELECT * FROM v1;
2026-05-08 15:38:37.121 CST [175953] ERROR:  cache lookup failed for label 16472
2026-05-08 15:38:37.121 CST [175953] STATEMENT:  SELECT * FROM v1;
ERROR:  cache lookup failed for label 16472
test=# 
```

I've made some minor modifications; this is my diffs file. I'm not sure if anything is missing, so feel free to add to
orsupplement it.
 

```c
diff --git a/src/backend/catalog/dependency.c b/src/backend/catalog/dependency.c
index fdb8e67e1f5..6a73b74fc9b 100644
--- a/src/backend/catalog/dependency.c
+++ b/src/backend/catalog/dependency.c
@@ -2247,6 +2247,22 @@ find_expr_references_walker(Node *node,
                                                           context->addrs);
                /* fall through to examine substructure */
        }
+       if (IsA(node, GraphLabelRef))
+       {
+               GraphLabelRef *lref = (GraphLabelRef *) node;
+
+               add_object_address(PropgraphLabelRelationId, lref->labelid, 0,
+                                                  context->addrs);
+               return false;
+       }
+       if (IsA(node, GraphPropertyRef))
+       {
+               GraphPropertyRef *gpr = (GraphPropertyRef *) node;
+
+               add_object_address(PropgraphPropertyRelationId, gpr->propid, 0,
+                                                  context->addrs);
+               return false;
+       }
        else if (IsA(node, Query))
        {
                /* Recurse into RTE subquery or not-yet-planned sublink subquery */
@@ -2277,9 +2293,31 @@ find_expr_references_walker(Node *node,
                        switch (rte->rtekind)
                        {
                                case RTE_RELATION:
+                                       add_object_address(RelationRelationId, rte->relid, 0,
+                                                                          context->addrs);
+                                       break;
                                case RTE_GRAPH_TABLE:
                                        add_object_address(RelationRelationId, rte->relid, 0,
                                                                           context->addrs);
+
+                                       if (rte->graph_pattern)
+                                       {
+                                               GraphPattern *gp = rte->graph_pattern;
+                                               ListCell   *lc1;
+
+                                               foreach(lc1, gp->path_pattern_list)
+                                               {
+                                                       List       *path_term = lfirst_node(List, lc1);
+                                                       ListCell   *lc2;
+
+                                                       foreach(lc2, path_term)
+                                                       {
+                                                               GraphElementPattern *gep =
lfirst_node(GraphElementPattern,lc2);
 
+
+                                                               find_expr_references_walker(gep->labelexpr, context);
+                                                       }
+                                               }
+                                       }
                                        break;
                                case RTE_JOIN:

```


Final running results

```sql
test=# CREATE TABLE vt (id text PRIMARY KEY, name text, age int);
CREATE TABLE et (id text PRIMARY KEY, src text REFERENCES vt(id), dst text REFERENCES vt(id));
INSERT INTO vt VALUES ('a', 'Alice', 30), ('b', 'Bob', 25);
INSERT INTO et VALUES ('e1', 'a', 'b');

CREATE PROPERTY GRAPH g                
  VERTEX TABLES (vt LABEL l1 PROPERTIES (name) LABEL l2 PROPERTIES (name, age))
  EDGE TABLES (et SOURCE KEY (src) REFERENCES vt(id) DESTINATION KEY (dst) REFERENCES vt(id));


CREATE VIEW v1 AS SELECT * FROM GRAPH_TABLE(g MATCH (a IS l2)-[e IS et]->(b IS l2) COLUMNS (a.name, a.age, b.name AS
bname));                                                                                    
 

                                                                 
 
ALTER PROPERTY GRAPH g ALTER VERTEX TABLE vt DROP LABEL l2;
                                                                                                         
 
SELECT * FROM v1;
CREATE TABLE
CREATE TABLE
INSERT 0 2
INSERT 0 1
CREATE PROPERTY GRAPH
CREATE VIEW
2026-05-08 16:24:59.938 CST [182833] ERROR:  cannot drop label l2 of property graph g because other objects depend on
it
2026-05-08 16:24:59.938 CST [182833] DETAIL:  view v1 depends on label l2 of property graph g
2026-05-08 16:24:59.938 CST [182833] HINT:  Use DROP ... CASCADE to drop the dependent objects too.
2026-05-08 16:24:59.938 CST [182833] STATEMENT:  ALTER PROPERTY GRAPH g ALTER VERTEX TABLE vt DROP LABEL l2;
ERROR:  cannot drop label l2 of property graph g because other objects depend on it
DETAIL:  view v1 depends on label l2 of property graph g
HINT:  Use DROP ... CASCADE to drop the dependent objects too.
 name  | age | bname 
-------+-----+-------
 Alice |  30 | Bob
(1 row)
```

--
regards,
Man Zeng

pgsql-hackers by date:

Previous
From: Tobias Bussmann
Date:
Subject: Re: Broken build on macOS (Universal / Intel): cpuid instruction not available
Next
From: Daniel Gustafsson
Date:
Subject: Re: remove obsolete comment in AtEOXact_Inval