ProvSQL C/C++ API
Adding support for provenance and uncertainty management to PostgreSQL databases
Loading...
Searching...
No Matches
kcmcp_supervisor.c File Reference

Background worker that launches and supervises the managed KCMCP knowledge-compiler server (the "managed mode" of the KCMCP client). More...

#include "postgres.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "lib/stringinfo.h"
#include "postmaster/bgworker.h"
#include "postmaster/postmaster.h"
#include "storage/latch.h"
#include "storage/ipc.h"
#include "utils/guc.h"
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>
#include <errno.h>
#include "provsql_utils.h"
#include "provsql_shmem.h"
#include "provsql_error.h"
Include dependency graph for kcmcp_supervisor.c:

Go to the source code of this file.

Macros

#define WL_EXIT_ON_PM_DEATH   WL_POSTMASTER_DEATH

Functions

static void kcmcp_sigterm (SIGNAL_ARGS)
static void kcmcp_sighup (SIGNAL_ARGS)
const char * provsql_kcmcp_managed_endpoint (void)
 Read the live endpoint of the managed KCMCP server from shared memory (e.g.
static void publish_endpoint (const char *endpoint)
static char * build_server_command (const char *tmpl, const char *endpoint)
static pid_t launch_server (const char *cmd)
static void kill_server (pid_t child)
static bool kcmcp_wait (long timeout_ms)
void provsql_kcmcp_worker (Datum ignored)
void RegisterProvSQLKCMCPWorker (void)
 Register the supervisor background worker that launches and supervises the managed KCMCP server; called from _PG_init alongside the mmap worker.

Variables

static volatile sig_atomic_t got_sigterm = false
static volatile sig_atomic_t got_sighup = false

Detailed Description

Background worker that launches and supervises the managed KCMCP knowledge-compiler server (the "managed mode" of the KCMCP client).

When the provsql.kcmcp_server GUC is non-empty it is a shell command to start a KCMCP server (see doc/source/dev/kc-server-protocol.rst), with the literal {endpoint} replaced by a Unix-socket path this worker picks. The worker forks/execs that command in its own process group, publishes the endpoint in shared memory (read by the in-extension client, kcmcp_client.cpp, for a registry record whose endpoint is 'managed'), and supervises it: on the server's exit it relaunches, on a config reload it restarts a changed command, and on shutdown it kills the whole group. When the GUC is empty the worker simply idles on its latch.

Modeled on RegisterProvSQLMMapWorker (src/provsql_mmap.c); like it, the worker needs only BGWORKER_SHMEM_ACCESS (no database connection): the GUC is a process-global and the endpoint lives in the shared segment.

Definition in file kcmcp_supervisor.c.

Macro Definition Documentation

◆ WL_EXIT_ON_PM_DEATH

#define WL_EXIT_ON_PM_DEATH   WL_POSTMASTER_DEATH

Definition at line 44 of file kcmcp_supervisor.c.

Function Documentation

◆ build_server_command()

char * build_server_command ( const char * tmpl,
const char * endpoint )
static

Definition at line 91 of file kcmcp_supervisor.c.

Here is the caller graph for this function:

◆ kcmcp_sighup()

void kcmcp_sighup ( SIGNAL_ARGS )
static

Definition at line 58 of file kcmcp_supervisor.c.

Here is the caller graph for this function:

◆ kcmcp_sigterm()

void kcmcp_sigterm ( SIGNAL_ARGS )
static

Definition at line 50 of file kcmcp_supervisor.c.

Here is the caller graph for this function:

◆ kcmcp_wait()

bool kcmcp_wait ( long timeout_ms)
static

Definition at line 137 of file kcmcp_supervisor.c.

Here is the caller graph for this function:

◆ kill_server()

void kill_server ( pid_t child)
static

Definition at line 122 of file kcmcp_supervisor.c.

Here is the caller graph for this function:

◆ launch_server()

pid_t launch_server ( const char * cmd)
static

Definition at line 106 of file kcmcp_supervisor.c.

Here is the caller graph for this function:

◆ provsql_kcmcp_managed_endpoint()

const char * provsql_kcmcp_managed_endpoint ( void )

Read the live endpoint of the managed KCMCP server from shared memory (e.g.

"unix:/tmp/..."), or an empty string when none is running.

Definition at line 66 of file kcmcp_supervisor.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ provsql_kcmcp_worker()

void provsql_kcmcp_worker ( Datum ignored)

Definition at line 150 of file kcmcp_supervisor.c.

Here is the call graph for this function:

◆ publish_endpoint()

void publish_endpoint ( const char * endpoint)
static

Definition at line 78 of file kcmcp_supervisor.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ RegisterProvSQLKCMCPWorker()

void RegisterProvSQLKCMCPWorker ( void )

Register the supervisor background worker that launches and supervises the managed KCMCP server; called from _PG_init alongside the mmap worker.

Definition at line 236 of file kcmcp_supervisor.c.

Here is the caller graph for this function:

Variable Documentation

◆ got_sighup

volatile sig_atomic_t got_sighup = false
static

Definition at line 48 of file kcmcp_supervisor.c.

◆ got_sigterm

volatile sig_atomic_t got_sigterm = false
static

Definition at line 47 of file kcmcp_supervisor.c.