From d6b2d7f7398392fe765ceefb7869dc6ec4d757f4 Mon Sep 17 00:00:00 2001 From: Thomas Munro Date: Wed, 4 Mar 2026 18:12:54 +1300 Subject: [PATCH v4 04/21] Provide PG_STACK_DIRECTION configuration macro. All modern architectures and ABIs have stacks that grow downward. Define this in pg_config_manual.h, instead of assuming it could go either way in every run-time check performed in stack_depth.c. A proposed patch would make greater use of this information at compile time. An automatic way of determining the value at compile-time could always be developed if another system with upward-growing-stack ever appears. Reviewed-by: Discussion: --- src/backend/utils/misc/stack_depth.c | 10 ++++++++-- src/include/pg_config_manual.h | 6 ++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/backend/utils/misc/stack_depth.c b/src/backend/utils/misc/stack_depth.c index 61a07cf824e..c6aeee5f475 100644 --- a/src/backend/utils/misc/stack_depth.c +++ b/src/backend/utils/misc/stack_depth.c @@ -118,9 +118,15 @@ stack_is_too_deep(void) /* * Take abs value, since stacks grow up on some machines, down on others + * (historical). */ - if (stack_depth < 0) - stack_depth = -stack_depth; + stack_depth *= -(PG_STACK_DIRECTION); + + /* + * If this assertion fails, either PG_STACK_DIRECTION is wrong or this + * system doesn't have a traditional stack as we expect. + */ + Assert(stack_depth >= 0); /* * Trouble? diff --git a/src/include/pg_config_manual.h b/src/include/pg_config_manual.h index 521b49b8888..abe371f5d69 100644 --- a/src/include/pg_config_manual.h +++ b/src/include/pg_config_manual.h @@ -222,6 +222,12 @@ */ #define PG_IO_ALIGN_SIZE 4096 +/* + * Assumed direction of the stack, -1 or +1. Historically this varied, but all + * modern systems have stacks that grow downward. + */ +#define PG_STACK_DIRECTION -1 + /* *------------------------------------------------------------------------ * The following symbols are for enabling debugging code, not for -- 2.50.1 (Apple Git-155)