Hi folks
I recently needed a way to get backtraces from errors in a convenient, non-interactive and indescriminate way. The attached patch is the result. It teaches Pg to use libunwind to self-backtrace in elog/ereport.
Anyone think this is useful/interesting/worth pursuing?
(Patch is currently against pg10, so this is a PoC only).
As written it emits a backtrace when log_error_verbosity=verbose or, unconditionally, on PANIC. A bt can be hidden by errhidestack() but is otherwise shown. That's ridiculously, excessively spammy, so it's not viable for core as-is. Before playing with it too much I thought I'd ask for ideas on if anyone thinks it's useful, and if so, how it'd work best.
My goal is to allow capture of extra diagnostic info from key locations in production without needing to attach gdb. It's imperfect since sometimes there's no convenient message, and other times you can't afford to set up logging with enough detail. So this would be most useful when combined with one of the occasionally discussed patches to allow for selective logging verbosity on a module- or file- level. But I think it's still handy without that.
BTW, Álvaro posted a simpler patch at
https://www.postgresql.org/message-id/20180410213203.nl645o5vj5ap66sl@alvherre.pgsql. It uses backtrace() from glibc, and requires each site you want to bt to be annotated explicitly. I forgot about backtrace() completely when I wrote mine, and I prefer the control libunwind gives me anyway, but the reduced dependency would be nice. Especially since backtrace() is in FreeBSD too.