Skip to content

2.31.0.0-b162

@kai-franz kai-franz tagged this 12 Jun 23:24
Summary:
A YugabyteDB tserver opens internal libpq connections to its own postgres for several purposes — rebuilding the relcache init file, running auto-analyze, and (soon) draining the xCluster DDL queue. Today they are all lumped together under a single `yb_is_internal_connection` flag, which makes it impossible to give one kind different behavior from another.

This diff adds the framework **only**; it does not yet route any connection through it, so there is no behavior change. The stacked child diffs move the relcache-init connection (D53799) and then auto-analyze (D53800) onto it.

== Framework: `yb_internal_conn.{h,c}` ==

- `enum YbInternalConnKind` names each kind (NONE sentinel + RELCACHE_INIT).
- `YbInternalConnKindDescriptor` records, per kind: the wire name sent in the `yb_internal_conn_kind` libpq startup parameter, the assigned `BackendType`, the `pg_stat_activity.backend_type` string, and per-kind preload behavior bits (`use_minimal_preload`, `preload_lists_in_minimal_mode`).
- Helpers: `YbLookupInternalConnKindByName` / `...ByBackendType` / `YbIsInternalConnBackendType`.

== Effects of registering a kind ==

Once a connection sends `yb_internal_conn_kind=<wire_name>` in its libpq startup packet, the kind shows up at six places, all driven by the same registry lookup:

# **Postmaster startup-packet parsing** (`postmaster.c`) — looks the wire name up in `YbInternalConnKindDescriptors` and assigns the descriptor's `backend_type` to `MyBackendType`. An unrecognized wire name causes `FATAL`.
# **Authentication gate** (`auth.c`) — `YbIsInternalConnBackendType(MyBackendType)` is true, so the connection must have used `yb-tserver-key` auth (matching the hardcoded `local all postgres yb-tserver-key` HBA rule). Any other auth method causes `FATAL`.
# **`pg_stat_activity.backend_type`** (`miscinit.c` `GetBackendTypeDesc`) — a registry lookup at the top of the function returns the descriptor's `backend_desc` string for any registered kind. So the connection shows up as e.g. `"yb relcache init backend"` instead of `"client backend"`.
# **`pg_stat` equivalence-class checks** — the kind counts as "client-backend-equivalent" in three places via `YbIsInternalConnBackendType`:
  - `backend_status.c` — increments the `yb_new_conn` counter and sets `st_userid` from the session user.
  - `pgstatfuncs.c` — `pg_stat_get_activity` uses `BackendPidGetProc` (not `AuxiliaryPidGetProc`) to fetch the `PGPROC`.
  - `yb_pg_metrics.c` — same `BackendPidGetProc` classification for the YB metrics view.
# **libpq client side** (`libpq_utils.{h,cc}`) — `PGConnSettings.yb_internal_conn_kind` and a `CreateInternalPGConnBuilder` parameter carry the wire name from tserver-side code through the conninfo string. `YbInternalConnKindWireName::k…` constants name each kind so call sites avoid magic strings.
# **Per-kind preload behavior** — `use_minimal_preload` and `preload_lists_in_minimal_mode` are **declared** on the descriptor here, but **consumed only in D53799** (relcache layer), where `YbUseMinimalCatalogCachesPreload` and `YbShouldPreloadCatcacheLists` switch from the blanket `yb_is_internal_connection` check to consulting the descriptor.

== Adding a new kind later ==

# Append a value to `YbInternalConnKind` (before `_MAX`).
# Add a matching `BackendType` in `miscadmin.h`.
# Add a descriptor row in `yb_internal_conn.c`.
# Add a `constexpr` wire-name constant in `libpq_utils.h`.
# Pass it to `CreateInternalPGConnBuilder` at the call site.

No other files need touching.

Test Plan: Jenkins

Reviewers: myang, #db-approvers

Reviewed By: myang

Subscribers: yql, ybase

Differential Revision: https://phorge.dev.yugabyte.com/D53506
Assets 2
Loading