Share
Embed
Using PostgreSQL's Background Worker Processes For Fun and Profit
Alexey Palazhchenko
February 22, 2025
Transcript
88 slides
-
Using PostgreSQL's Background Worker Processes For Fun and Pro fi
t Alexey Palazhchenko, FerretDB Inc. Armenia PostgreSQL User Group meetup February 22, 2025 -
About me • Developer since early childhood • Go developer
since r60 (2011) -
About me • Developer since early childhood • Go developer
since r60 (2011) • PostgreSQL contributor since 2024 (by accident) -
About me • Developer since early childhood • Go developer
since r60 (2011) • PostgreSQL contributor since 2024 (by accident) • Qik->Skype->Microsoft, […], Percona, Sidero Labs -
About me • Developer since early childhood • Go developer
since r60 (2011) • PostgreSQL contributor since 2024 (by accident) • Qik->Skype->Microsoft, […], Percona, Sidero Labs • FerretDB Inc. co-founder (with other ex-Perconians) and CTO -
FerretDB overview PostgreSQL + DocumentDB extension FerretDB Any application Any
MongoDB driver MongoDB protocol BSON PostgreSQL protocol SQL -
Why PostgreSQL? • Very mature and reliable • Many companies
run it (sometimes side-by-side with MongoDB) -
Why PostgreSQL? • Very mature and reliable • Many companies
run it (sometimes side-by-side with MongoDB) • Open-source, huge community -
Why PostgreSQL? • Very mature and reliable • Many companies
run it (sometimes side-by-side with MongoDB) • Open-source, huge community • Extensible -
Why not extension? • Di ff erent processes scale di
ff erently • Stateless -
Why not extension? • Di ff erent processes scale di
ff erently • Stateless • Better at handling MongoDB client connections (there are many of them!) -
Why not extension? • Di ff erent processes scale di
ff erently • Stateless • Better at handling MongoDB client connections (there are many of them!) • Some potential features would require GPUs -
— Database administrator in a big company «It would be
easier to sneak a PostgreSQL extension to production» -
FerretDB extension PostgreSQL + DocumentDB extension FerretDB Any application Any
MongoDB driver MongoDB protocol BSON PostgreSQL protocol SQL -
The idea • Go (garbage-collected language with runtime, di ff
erent ABI, etc) -
The idea • Go (garbage-collected language with runtime, di ff
erent ABI, etc) • cgo -
The idea • Go (garbage-collected language with runtime, di ff
erent ABI, etc) • cgo • FerretDB Go module -
Background workers • PostgreSQL can be extended to run user-supplied
code in separate processes. Such processes are started, stopped and monitored by postgres, which permits them to have a lifetime closely linked to the server's status. -
Background workers • PostgreSQL can be extended to run user-supplied
code in separate processes. Such processes are started, stopped and monitored by postgres, which permits them to have a lifetime closely linked to the server's status. • These processes are attached to PostgreSQL's shared memory area and have the option to connect to databases internally; they can also run multiple transactions serially, just like a regular client-connected server process. -
Background workers • PostgreSQL can be extended to run user-supplied
code in separate processes. Such processes are started, stopped and monitored by postgres, which permits them to have a lifetime closely linked to the server's status. • These processes are attached to PostgreSQL's shared memory area and have the option to connect to databases internally; they can also run multiple transactions serially, just like a regular client-connected server process. • Also, by linking to libpq they can connect to the server and behave like a regular client application. -
Not extension • No control fi les, no CREATE EXTENSION,
no upgrades, etc -
Not extension • No control fi les, no CREATE EXTENSION,
no upgrades, etc • Just add .so fi le to shared_preload_libraries -
Not extension • No control fi les, no CREATE EXTENSION,
no upgrades, etc • Just add .so fi le to shared_preload_libraries • postmaster preloads it -
Not extension • No control fi les, no CREATE EXTENSION,
no upgrades, etc • Just add .so fi le to shared_preload_libraries • postmaster preloads it • registers background worker -
Not extension • No control fi les, no CREATE EXTENSION,
no upgrades, etc • Just add .so fi le to shared_preload_libraries • postmaster preloads it • registers background worker • then forks a background worker process -
Duct tape • _PG_init: calls RegisterBackgroundWorker for a C function
• C: sets up signal handlers, calls Go/cgo function -
Duct tape • _PG_init: calls RegisterBackgroundWorker for a C function
• C: sets up signal handlers, calls Go/cgo function • Go: starts FerretDB Go module -
_PG_init void _PG_init(void) { BackgroundWorker worker; MemSet(&worker, 0, sizeof(BackgroundWorker)); snprintf(worker.bgw_name,
BGW_MAXLEN, "FerretDB"); worker.bgw_flags = BGWORKER_SHMEM_ACCESS; worker.bgw_start_time = BgWorkerStart_RecoveryFinished; worker.bgw_restart_time = BGW_NEVER_RESTART; snprintf(worker.bgw_library_name, BGW_MAXLEN, "ferretdb"); snprintf(worker.bgw_function_name, BGW_MAXLEN, "background_main"); RegisterBackgroundWorker(&worker); } -
background_main #include "postgres.h" #include "postmaster/bgworker.h" #include "storage/ipc.h" #include "storage/proc.h" #include
"_cgo_export.h" PG_MODULE_MAGIC; void background_main(Datum main_arg) { BackgroundWorkerUnblockSignals(); BackgroundWorkerMain(); proc_exit(0); } -
Go package main / * extern void BackgroundWorkerMain(); * /
import "C" func main() { panic("not reached") } / / export BackgroundWorkerMain func BackgroundWorkerMain() { // starts FerretDB Go module } -
Make fi le build: env CGO_CFLAGS="-I $(shell pg_config -- includedir-server)"
\ CGO_LDFLAGS="-shared" \ go build -v -buildmode=c-shared -o ferretdb.so cp ferretdb.so $(shell pg_config - - pkglibdir) -
Not fun, no pro fi t :( • Di ff
erent Go version? -
Not fun, no pro fi t :( • Di ff
erent Go version? • Di ff erent PostgreSQL version? -
Not fun, no pro fi t :( • Di ff
erent Go version? • Di ff erent PostgreSQL version? • Compiler/linker fl ags? -
Not fun, no pro fi t :( • Di ff
erent Go version? • Di ff erent PostgreSQL version? • Compiler/linker fl ags? • Signals handling? -
Not fun, no pro fi t :( • Di ff
erent Go version? • Di ff erent PostgreSQL version? • Compiler/linker fl ags? • Signals handling? • PID 1 problem? -
Not fun, no pro fi t :( • Di ff
erent Go version? • Di ff erent PostgreSQL version? • Compiler/linker fl ags? • Signals handling? • PID 1 problem? • PostgreSQL preloads .so -
Not fun, no pro fi t :( • Di ff
erent Go version? • Di ff erent PostgreSQL version? • Compiler/linker fl ags? • Signals handling? • PID 1 problem? • PostgreSQL preloads .so • .so starts the Go runtime thread -
Not fun, no pro fi t :( • Di ff
erent Go version? • Di ff erent PostgreSQL version? • Compiler/linker fl ags? • Signals handling? • PID 1 problem? • PostgreSQL preloads .so • .so starts the Go runtime thread • PostgreSQL forks the process with threads -
Not fun, no pro fi t :( • Di ff
erent Go version? • Di ff erent PostgreSQL version? • Compiler/linker fl ags? • Signals handling? • PID 1 problem? • PostgreSQL preloads .so • .so starts the Go runtime thread • PostgreSQL forks the process with threads • 💣 -
— Ian Lance Taylor https://github.com/golang/go/issues/15538#issuecomment-216916265 «A multi-threaded program can not
safely call fork; this is a general rule, not speci fi c to Go. A single-threaded program that dlopen's a Go shared library becomes a multi-threaded program. At that point, it can not call fork.» -
What can we do? • Wait for multi-threaded PostgreSQL: https://wiki.postgresql.org/wiki/Multithreading
• Make the PostgreSQL fork process fi rst, then load a shared library -
What can we do? • Wait for multi-threaded PostgreSQL: https://wiki.postgresql.org/wiki/Multithreading
• Make the PostgreSQL fork process fi rst, then load a shared library • No other way -
Preloading and loading • PostgreSQL: • preloads shared library (dlopen)
• registers background worker (dlsym _PG_init, function call) -
Preloading and loading • PostgreSQL: • preloads shared library (dlopen)
• registers background worker (dlsym _PG_init, function call) • starts background worker process (fork, function call) -
Preloading and loading • PostgreSQL: • preloads shared library (dlopen)
• registers background worker (dlsym _PG_init, function call) • starts background worker process (fork, function call) • FerretDBLoader: -
Preloading and loading • PostgreSQL: • preloads shared library (dlopen)
• registers background worker (dlsym _PG_init, function call) • starts background worker process (fork, function call) • FerretDBLoader: • loads FerretDB shared library (dlopen) -
Preloading and loading • PostgreSQL: • preloads shared library (dlopen)
• registers background worker (dlsym _PG_init, function call) • starts background worker process (fork, function call) • FerretDBLoader: • loads FerretDB shared library (dlopen) • fi nds entry point (dlsym BackgroundWorkerMain) -
Preloading and loading • PostgreSQL: • preloads shared library (dlopen)
• registers background worker (dlsym _PG_init, function call) • starts background worker process (fork, function call) • FerretDBLoader: • loads FerretDB shared library (dlopen) • fi nds entry point (dlsym BackgroundWorkerMain) • calls entry point (function call) -
New background_main void background_main(Datum main_arg) { h = dlopen(path, RTLD_NOW
| RTLD_GLOBAL); f = (bgworker_main_type)dlsym(h, "BackgroundWorkerMain"); f(main_arg); dlclose(h); proc_exit(0); } -
🎉 DEBUG: starting background worker process "FerretDBLoader" LOG: database system
is ready to accept connections DEBUG: ferretdb_loader: loading go shared lib "/usr/lib/postgresql/16/lib/ferretdb.so" 2025/02/22 09:31:22 FerretDB is starting 2025/02/22 09:31:22 FerretDB is running at mongodb://127.0.0.1:27027/ -
Pro fi t • PostgreSQL background worker processes infrastructure is
useful • It can be used to run any code -
Pro fi t • PostgreSQL background worker processes infrastructure is
useful • It can be used to run any code • Some duct tape might be required