Thread

  1. Re: (SQL/PGQ) cache lookup failed for label

    Ashutosh Bapat <ashutosh.bapat.oss@gmail.com> — 2026-05-12T11:58:48Z

    On Fri, May 8, 2026 at 2:10 PM zengman <zengman@halodbtech.com> wrote:
    >
    > 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.name AS 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 or supplement it.
    
    Thanks for the report and the fix. Can you please create a patch/diff
    file and attach it to the email please? It's easy to apply an
    attachment than copying diff to a file and then applying it.
    
    Please find some comments.
    
    >
    > ```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);
    > +                                                       }
    > +                                               }
    > +                                       }
    
    You could use foreach_node() instead of foreach(). But I am wondering
    whether we can directly call find_expr_references_walker() on
    rte->graph_pattern. We need to walk rte->graph_table_columns as well.
    
    >                                         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));
    
    The patch needs a test. graph_table.sql already has some view
    definitions, some of them using elements with multiple labels. Can you
    please add a test using those views? For example after CREATE VIEW
    customer_us, you could add a statement dropping label list_items from
    all of the elements associated with that label. I guess
    pg_get_viewdef() itself should throw an error with the fix, but you
    could select from that view as well, if necessary. We also need a test
    for drop property. Remember that the property is completely dropped
    from a property graph only when it is dropped from all the labels
    containing that property. Please apply patches from [1] before adding
    tests to your patch. With those patches added your test queries above
    will throw a different error.
    
    [1] https://www.postgresql.org/message-id/CAExHW5tCCQhgDEfBTKWqe7bDqCUXhPpsqoGipL7Vpf0epcKkXA@mail.gmail.com
    -- 
    Best Wishes,
    Ashutosh Bapat