diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c index b185c1b..b5d7f80 100644 --- a/src/backend/tcop/postgres.c +++ b/src/backend/tcop/postgres.c @@ -3120,6 +3120,24 @@ check_stack_depth(void) } } +#ifdef DEBUG_STACK_DEPTH +long last_stack_depth = 0; +long high_stack_depth = 0; +long high_stack_incr = 0; +bool stack_depth_on_proc_exit = 0; + +/* + * on_proc_exit handler to log end of session + */ +static void +log_stack_depth(int code, Datum arg) +{ + ereport(LOG, + (errmsg("disconnection: highest stack depth: %lu largest stack increment: %lu", + high_stack_depth, high_stack_incr))); +} +#endif + bool stack_is_too_deep(void) { @@ -3137,6 +3155,21 @@ stack_is_too_deep(void) if (stack_depth < 0) stack_depth = -stack_depth; +#ifdef DEBUG_STACK_DEPTH + /* book-keeping for measuring STACK_DEPTH_SLOP */ + if (stack_depth > high_stack_depth) + high_stack_depth = stack_depth; + if (stack_depth - last_stack_depth > high_stack_incr) + high_stack_incr = stack_depth - last_stack_depth; + last_stack_depth = stack_depth; + + if (!stack_depth_on_proc_exit) + { + stack_depth_on_proc_exit = 1; + on_proc_exit(log_stack_depth, 0); + } +#endif + /* * Trouble? *