Skip to content

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
3.6-genesysfrom
GCVCALLP-3196-lazy-connect
Open

GCVCALLP-3196: opt-in db_postgres lazy_connect so idle SIP workers keep no PG sockets#152
sekharp-genesys wants to merge 1 commit into
3.6-genesysfrom
GCVCALLP-3196-lazy-connect

Conversation

@sekharp-genesys

@sekharp-genesys sekharp-genesys commented Aug 19, 2026

Copy link
Copy Markdown
Collaborator

Summary

Every OpenSIPS child currently calls PQconnectdbParams() in db_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. With db_virtual FAILOVER, db_init still 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.
  • PQconnect runs on first real use: PQescapeStringConn / PQescapeByteaConn while SQL is built (val2str), submit_query / submit_async_query, or async connection setup.
  • A failed connect zeros ptr->con so the next query retries instead of using a finished PGconn.

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() still PQconnects in every child. ensure_connected() in val2str and submit_query is a no-op because ptr->con is already set.

Down-URL recovery is changed even at default 0. The db_virtual probe timer is not gated on lazy_connect (db_reconnect_with_timer defaults on). Previously a down URL was marked CAN_USE if init() returned non-NULL (postgres: TCP+auth only), then closed. It now requires init() plus SELECT 1 on that temporary handle before CAN_USE. The ping is not done from db_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 enables lazy_connect. Connect-ok / query-fail will no longer come back up.

Failed-connect ptr->con = 0 and 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 (dispatcher mod_init, etc.) builds WHERE table_name='...' with PQescapeStringConn before submit_query(). Connecting only in submit_query left a NULL PGconn and startup failed with PQescapeStringConn failed. Connecting in val2str for STRING/STR/BLOB is still “this process is running SQL.” With lazy_connect=0 it does not open an extra socket. Dispatcher still disconnects after the parent version check.

Test plan ( locally validated using docker and sipproxyIT)

  • sipproxyIT route_ib -t IB_MAP with overlay binary, lazy_connect=1: OpenSIPS starts; dispatcher version check succeeds.
  • Same IT layout, ss -tp dst :5432 on 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.
  • Default lazy_connect=0 (no sipproxy cfg): idle session count unchanged; down-URL recovery now needs SELECT 1.
  • FAILOVER with two URLs and lazy_connect=1: querying process has one writer socket until failover; replica handle stays unconnected.
  • After marking a URL down, probe timer SELECT 1 must succeed before CAN_USE (both lazy 0 and 1).

@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 13 complexity

Metric Results
Complexity 13

View in Codacy

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants