diff --git a/src/pg_query_normalize.c b/src/pg_query_normalize.c index 3b2472e4..ebaaf49c 100644 --- a/src/pg_query_normalize.c +++ b/src/pg_query_normalize.c @@ -347,11 +347,55 @@ static void RecordConstLocation(pgssConstLocations *jstate, int location) } } -static bool const_record_walker(Node *node, pgssConstLocations *jstate) +static bool const_record_walker(Node *node, pgssConstLocations *jstate); + +/* + * Walks a node of a type raw_expression_tree_walker() may not know about. + * + * This is its own function, rather than part of const_record_walker, because + * WebAssembly builds route every call made by a function that contains a + * PG_TRY block through the host engine, which uses up a lot of its native + * stack for each level of a deeply nested expression. + */ +static pg_noinline bool const_record_walker_ignore_errors(Node *node, pgssConstLocations *jstate) { - bool result; + volatile bool result = false; MemoryContext normalize_context = CurrentMemoryContext; + /* + * Note we must not return from inside PG_TRY, since that skips + * PG_END_TRY and leaves PG_exception_stack pointing at this + * frame after it is gone. + */ + PG_TRY(); + { + result = raw_expression_tree_walker(node, const_record_walker, (void*) jstate); + } + PG_CATCH(); + { + ErrorData *edata; + + MemoryContextSwitchTo(normalize_context); + edata = CopyErrorData(); + + /* + * The walker raises for node types it doesn't know, which we + * ignore. But if we ran out of stack, the rest of the tree + * was not walked, and swallowing that would return a partially + * normalized query as if it was complete - pass it on instead. + */ + if (edata->sqlerrcode == ERRCODE_STATEMENT_TOO_COMPLEX) + PG_RE_THROW(); + + /* edata is released together with the per-call memory context */ + FlushErrorState(); + } + PG_END_TRY(); + return result; +} + +static bool const_record_walker(Node *node, pgssConstLocations *jstate) +{ if (node == NULL) return false; /* @@ -588,40 +632,40 @@ static bool const_record_walker(Node *node, pgssConstLocations *jstate) if (jstate->normalize_utility_only) return false; return raw_expression_tree_walker(node, const_record_walker, (void*) jstate); } + case T_List: + case T_A_Expr: + case T_BoolExpr: + case T_SubLink: + case T_FuncCall: + case T_NamedArgExpr: + case T_ResTarget: + case T_TypeCast: + case T_CollateClause: + case T_ColumnRef: + case T_A_Indirection: + case T_A_Indices: + case T_A_ArrayExpr: + case T_RowExpr: + case T_CaseExpr: + case T_CoalesceExpr: + case T_MinMaxExpr: + case T_NullTest: + case T_BooleanTest: + case T_JoinExpr: + case T_RangeSubselect: + case T_RangeFunction: + case T_RangeVar: + case T_SortBy: + case T_WithClause: + case T_CommonTableExpr: + /* + * raw_expression_tree_walker() handles these node types, which + * make up most of a deeply nested expression, so they don't need + * const_record_walker_ignore_errors() and its PG_TRY block. + */ + return raw_expression_tree_walker(node, const_record_walker, (void*) jstate); default: - { - /* - * Note we must not return from inside PG_TRY, since that skips - * PG_END_TRY and leaves PG_exception_stack pointing at this - * frame after it is gone. - */ - result = false; - PG_TRY(); - { - result = raw_expression_tree_walker(node, const_record_walker, (void*) jstate); - } - PG_CATCH(); - { - ErrorData *edata; - - MemoryContextSwitchTo(normalize_context); - edata = CopyErrorData(); - - /* - * The walker raises for node types it doesn't know, which we - * ignore. But if we ran out of stack, the rest of the tree - * was not walked, and swallowing that would return a partially - * normalized query as if it was complete - pass it on instead. - */ - if (edata->sqlerrcode == ERRCODE_STATEMENT_TOO_COMPLEX) - PG_RE_THROW(); - - /* edata is released together with the per-call memory context */ - FlushErrorState(); - } - PG_END_TRY(); - return result; - } + return const_record_walker_ignore_errors(node, jstate); } return false; diff --git a/test/wasm_depth.c b/test/wasm_depth.c index 9e4f7102..0f696b07 100644 --- a/test/wasm_depth.c +++ b/test/wasm_depth.c @@ -87,6 +87,42 @@ static bool check_plpgsql_depth(const char *name, const char *open, const char * return ok; } +/* depth chained terms through pg_query_normalize, e.g. "SELECT 1+1+1" */ +static bool check_normalize_depth(const char *name, int depth) +{ + const char prefix[] = "SELECT 1"; + char *query = malloc(sizeof(prefix) + (depth - 1) * 2); + if (query == NULL) + { + fprintf(stderr, "FAIL %s: could not allocate query\n", name); + return false; + } + + char *p = query; + memcpy(p, prefix, sizeof(prefix) - 1); + p += sizeof(prefix) - 1; + for (int i = 1; i < depth; i++, p += 2) + memcpy(p, "+1", 2); + *p = '\0'; + + PgQueryNormalizeResult result = pg_query_normalize(query); + int params = 0; + if (result.normalized_query != NULL) + for (const char *c = result.normalized_query; *c; c++) + params += *c == '$'; + bool ok = result.error == NULL && params == depth; + if (ok) + printf("PASS %s: normalized successfully\n", name); + else if (result.error) + fprintf(stderr, "FAIL %s: expected a normalized query, got %s\n", name, result.error->message); + else + fprintf(stderr, "FAIL %s: expected %d parameters, got %d\n", name, depth, params); + + pg_query_free_normalize_result(result); + free(query); + return ok; +} + /* * Without arguments, run with the WebAssembly stack libpg-query-node links * (32MB). With "overflow", run with a 1MB stack, small enough that the stack @@ -101,6 +137,12 @@ int main(int argc, char **argv) status = EXIT_FAILURE; return status; } + /* + * First, while the host engine has not optimized anything yet: its native + * stack then holds the fewest levels. + */ + if (!check_normalize_depth("normalize 3000 chained terms", 3000)) + status = EXIT_FAILURE; if (!check_depth("2000 chained terms", "1+", 1999, false, false)) status = EXIT_FAILURE; if (!check_depth("1000 nested subqueries", "(SELECT ", 1000, true, false))