GCVCALLP-3196: opt-in db_postgres lazy_connect so idle SIP workers keep no PG sockets - #152
Open
sekharp-genesys wants to merge 1 commit into
Open
GCVCALLP-3196: opt-in db_postgres lazy_connect so idle SIP workers keep no PG sockets#152sekharp-genesys wants to merge 1 commit into
sekharp-genesys wants to merge 1 commit into
Conversation
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 13 |
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
sekharp-genesys
marked this pull request as ready for review
August 19, 2026 12:27
sekharp-genesys
requested review from
DHennessyGenesys,
SubhashC37,
bcnewlin,
benceszigeti and
kordian-pawelec
August 19, 2026 12:29
kordian-pawelec
approved these changes
Aug 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Every OpenSIPS child currently calls
PQconnectdbParams()indb_init(), even if that process never runs SQL. On sipproxy instances that means UDP/TCP/HEP/timer/MI children each hold idle RDS sessions for the life of the process. Withdb_virtualFAILOVER,db_initstill opened every configured URL; failover only chooses which one is used for queries.This adds
modparam("db_postgres", "lazy_connect", 1)(default 0).When enabled:
db_postgres_new_connection()allocates the handle only.PQconnectruns on first real use:PQescapeStringConn/PQescapeByteaConnwhile SQL is built (val2str),submit_query/submit_async_query, or async connection setup.ptr->conso the next query retries instead of using a finishedPGconn.Idle SIP workers that never query keep 0 backends (no idle sessions). A process that does query (rank-1 UDP load, MI,
dr_reload) opens the current FAILOVER URL only, typically the writer.Default 0 is not a no-op everywhere
Idle sockets / connect-at-init: historic.
db_init()stillPQconnects in every child.ensure_connected()inval2strandsubmit_queryis a no-op becauseptr->conis already set.Down-URL recovery is changed even at default 0. The
db_virtualprobe timer is not gated onlazy_connect(db_reconnect_with_timerdefaults on). Previously a down URL was markedCAN_USEifinit()returned non-NULL (postgres: TCP+auth only), then closed. It now requiresinit()plusSELECT 1on that temporary handle beforeCAN_USE. The ping is not done fromdb_virtual_init()(that would open sockets in every worker). Steady-state healthy URLs are unchanged; only recovery of a marked-down URL is stricter.That probe fix is required for lazy connect (
init()is then handle-alloc only). It is also a real behaviour change if nobody enableslazy_connect. Connect-ok / query-fail will no longer come back up.Failed-connect
ptr->con = 0and the async NULL guard are unconditional but not user-visible on the sipproxy path (eager fail still frees the handle; sipproxy does not use async SQL).Why val2str
db_check_table_version(dispatchermod_init, etc.) buildsWHERE table_name='...'withPQescapeStringConnbeforesubmit_query(). Connecting only insubmit_queryleft a NULLPGconnand startup failed withPQescapeStringConn failed. Connecting inval2strfor STRING/STR/BLOB is still “this process is running SQL.” Withlazy_connect=0it does not open an extra socket. Dispatcher still disconnects after the parent version check.Test plan ( locally validated using docker and sipproxyIT)
route_ib -t IB_MAPwith overlay binary,lazy_connect=1: OpenSIPS starts; dispatcher version check succeeds.ss -tp dst :5432on control:lazy_connect=1: 1 ESTAB (SIP UDP receiver). TCP/HEP/timer/FIFO/attendant have none. HTTPD opens one only after MI.lazy_connect=0: 6 ESTAB (MI FIFO, HTTPD, SIP UDP, SIP HEP, TCP receiver, Timer handler). Session count matches pre-change eager connect.lazy_connect=0(no sipproxy cfg): idle session count unchanged; down-URL recovery now needsSELECT 1.lazy_connect=1: querying process has one writer socket until failover; replica handle stays unconnected.SELECT 1must succeed beforeCAN_USE(both lazy 0 and 1).