]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Check for stack overflow when rewriting graph queries
authorPeter Eisentraut <peter@eisentraut.org>
Fri, 24 Apr 2026 06:18:21 +0000 (08:18 +0200)
committerPeter Eisentraut <peter@eisentraut.org>
Fri, 24 Apr 2026 06:18:21 +0000 (08:18 +0200)
generate_queries_for_path_pattern_recurse() and
generate_setop_from_pathqueries() are recursive functions.  For a
property graph with hundreds of tables, a graph pattern with a handful
element patterns can cause stack overflow.  Fix it by calling
check_stack_depth() at the beginning of these functions.

Author: Satyanarayana Narlapuram <satyanarlapuram@gmail.com>
Reviewed-by: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
Discussion: https://www.postgresql.org/message-id/CAHg+QDfgK0xddH8f3eAb+UVn7sBDOnv8RvM6OkP4HtHAt6aD7w@mail.gmail.com

src/backend/rewrite/rewriteGraphTable.c

index 3519ea3beae3815a678395ede6da2f1bdc97d14b..3aac29f5840b2effb57771f2256b01036b687799 100644 (file)
@@ -23,6 +23,7 @@
 #include "catalog/pg_propgraph_label.h"
 #include "catalog/pg_propgraph_label_property.h"
 #include "catalog/pg_propgraph_property.h"
+#include "miscadmin.h"
 #include "nodes/makefuncs.h"
 #include "nodes/nodeFuncs.h"
 #include "optimizer/optimizer.h"
@@ -361,6 +362,9 @@ generate_queries_for_path_pattern_recurse(RangeTblEntry *rte, List *pathqueries,
 {
        List       *path_elems = list_nth_node(List, path_elem_lists, elempos);
 
+       /* Guard against stack overflow due to complex path patterns. */
+       check_stack_depth();
+
        foreach_ptr(struct path_element, pe, path_elems)
        {
                /* Update current path being built with current element. */
@@ -698,6 +702,9 @@ generate_setop_from_pathqueries(List *pathqueries, List **rtable, List **targetl
        List       *rtargetlist;
        ParseNamespaceItem *pni;
 
+       /* Guard against stack overflow due to many path queries. */
+       check_stack_depth();
+
        /* Recursion termination condition. */
        if (list_length(pathqueries) == 0)
        {