From: Peter Eisentraut Date: Fri, 24 Apr 2026 06:18:21 +0000 (+0200) Subject: Check for stack overflow when rewriting graph queries X-Git-Url: http://git.ipfire.org/gitweb/?a=commitdiff_plain;h=2ff289d03939498a656a0c6cf2da08623f8357b4;p=thirdparty%2Fpostgresql.git Check for stack overflow when rewriting graph queries 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 Reviewed-by: Ashutosh Bapat Discussion: https://www.postgresql.org/message-id/CAHg+QDfgK0xddH8f3eAb+UVn7sBDOnv8RvM6OkP4HtHAt6aD7w@mail.gmail.com --- diff --git a/src/backend/rewrite/rewriteGraphTable.c b/src/backend/rewrite/rewriteGraphTable.c index 3519ea3beae..3aac29f5840 100644 --- a/src/backend/rewrite/rewriteGraphTable.c +++ b/src/backend/rewrite/rewriteGraphTable.c @@ -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) {