Add error context during loading of libraries

Started by Andres Freundabout 3 years ago1 messageshackers
Beta feature

Hackorum builds and tests every patch posted to the lists, not only commitfest submissions. This is Hackorum's own CI rather than the PostgreSQL project's, and it is still under testing - please report anything that looks wrong.

won't retrysuccessCI history

You can run a PostgreSQL built from this patch straight from Docker, with no checkout and no build:

docker run --rm -p 5432:5432 ghcr.io/hackorum-dev/postgres-patch:t48074
psql -h localhost -U postgres

Built from patchset v1 (message #1), July 28, 2026 at 12:30 AM.

Every patchset is also pushed to a branch of our PostgreSQL fork, so you can check out the same tree CI built. Without a PostgreSQL checkout:

git clone --branch t48074_1 https://github.com/hackorum-dev/postgres.git

In a checkout you already have, add the fork once:

git remote add hackorum https://github.com/hackorum-dev/postgres.git

then, for this patchset and every later one:

git fetch hackorum t48074_1 && git checkout t48074_1

Patchset v1 (message #1) is on t48074_1

Jump to latest
#1Andres Freund
andres@anarazel.de

Hi,

This discussion started at /messages/by-id/20230627212423.p56zxuqeh5kyatqg@awork3.anarazel.de
but isn't really related to the bug.

On 2023-06-27 17:44:57 -0400, Tom Lane wrote:

Andres Freund <andres@anarazel.de> writes:

That's not going to help you / the reporter, but to make issues like this
easier to debug in the future, I think we should
a) install an error context in load_libraries() printing the GUC name and
b) install an error context in internal_load_library() printing the name of
the shared library name

+1. I'm not sure this specific issue justifies it, but it seems like that
might make it easier to diagnose other shared-library-load-time issues
as well.

Yea, this bug alone wouldn't have made me suggest it, but I've looked at
enough issues where I regretted not knowing what library caused and error
and/or what caused a library to be loaded that I think it's worth it.

I first added an error context to both the places mentioned above, but that
leads to annoyingly redundant messages. And I realized that it'd also be
useful to print a message when loading a library due to
load_external_function() - I've definitely wondered about errors triggered
below that in the past.

I ended up adding a reason enum and a detail const char* to
internal_load_library(). I don't like that approach a whole lot, but couldn't
really come up with something better.

Example errors that now have a context:

Error in _PG_init() of library called via shared_preload_libraries (error added by me):

FATAL: not today
CONTEXT: in "_PG_init()" callback of library "/tmp/meson-install/lib/x86_64-linux-gnu/postgresql/auto_explain.so"
library load for "shared_preload_libraries" parameter

or

FATAL: could not access file "dont_exist": No such file or directory
CONTEXT: library load for "shared_preload_libraries" parameter

Creating a C function referencing a library that needs to be loaded with
shared_preload_libraries:

=# CREATE FUNCTION frak()
RETURNS text IMMUTABLE STRICT
AS '/srv/dev/build/m/src/test/modules/test_slru/test_slru.so' LANGUAGE C;
ERROR: XX000: cannot load "test_slru" after startup
DETAIL: "test_slru" must be loaded with shared_preload_libraries.
CONTEXT: in "_PG_init()" callback of library "/srv/dev/build/m/src/test/modules/test_slru/test_slru.so"
library load for C function "frak"

LOAD of a non-postgres library:

=# LOAD '/usr/lib/libarmadillo.so.11';
ERROR: XX000: incompatible library "/usr/lib/libarmadillo.so.11": missing magic block
HINT: Extension libraries are required to use the PG_MODULE_MAGIC macro.
CONTEXT: library load for LOAD statement

Note that here the errcontext callback prints the reason for the library being
loaded, but not the library name. I made it so that the library name is only
printed during _PG_init(), otherwise it's always duplicating the primary error
message. Which looks messy - but perhaps it's more important to be
"predictable"?

I don't love "library load for ..." and played around with a few other
variants, but I didn't come up with anything particularly satisfying.

I was tempted to invent a separate "library load reason" for
fmgr_info_C_lang() and other uses of load_external_function(), but concluded
that that reaches diminishing-returns territory.

Is it worth adding tests for:
1) an error during shared_preload_libraries, local_preload_libraries, session_preload_libraries
2) loading a non-postgres library and hitting "missing magic block"
3) local_preload_libraries not being allowed to load libraries outside of plugins/?
4) session_preload_libraries being allowed to load libraries outside of plugins/?
?

Greetings,

Andres Freund

Attachments:

t48074_1
v1-0001-Add-error-context-callback-to-internal_load_libra.patchtext/x-diff; charset=us-asciiDownload+102-9