perf(postgres): reduce per-record allocations in the CDC pull path by lukejudd-lux · Pull Request #4920 · PeerDB-io/peerdb · GitHub
Skip to content

perf(postgres): reduce per-record allocations in the CDC pull path - #4920

Open
lukejudd-lux wants to merge 1 commit into
PeerDB-io:mainfrom
lukejudd-lux:perf/cdc-reduce-per-record-allocs
Open

lukejudd-lux wants to merge 1 commit into
PeerDB-io:mainfrom
lukejudd-lux:perf/cdc-reduce-per-record-allocs

Conversation

@lukejudd-lux

Copy link
Copy Markdown
Contributor

Was profiling a Postgres source mirror and noticed CPU is mostly GC/malloc, with a couple of avoidable allocations on the per-record path in PullCdcRecords.

Two small fixes:

  • processMessage was rebuilding the logger from context on every message. It's already available in PullCdcRecords, so pass it down instead.

  • The unchanged-toast metric was recording with a freshly built attribute set on every update record. Accumulate the counts and flush them once a minute with the other CDC counters, the same way fetched bytes and friends are already handled.

No behaviour change, just fewer allocations on the hot path.

@lukejudd-lux
lukejudd-lux requested a review from a team as a code owner October 14, 2026 05:33
@lukejudd-lux
lukejudd-lux temporarily deployed to external-contributor October 14, 2026 05:33 — with GitHub Actions Inactive
Comment thread flow/connectors/postgres/cdc.go Outdated
Two spots in the per-WAL-record path (PullCdcRecords) allocate more than they
need to. In a CPU/heap profile of a Postgres source mirror both are a
meaningful share of allocations, and the CPU profile is dominated by GC/malloc,
so trimming them frees CPU.

- processMessage called internal.LoggerFromCtx(ctx) on every message, which
  goes through temporal log.With and allocates per call. The logger is already
  built once in PullCdcRecords; pass it in as a parameter.

- The unchanged-toast metric was recorded with UnchangedToastValuesCounter.Add
  plus a fresh attribute.NewSet on every update record. Accumulate the counts in
  atomics and flush them once a minute with the other CDC counters, reusing two
  pre-built attribute sets (same pattern already used for FetchedBytesCounter).

No behaviour change: same logs, same metric values, fewer allocations.

This branch was previously deployed

1 inactive deployment
external-contributor — 66587da6 Deployed Oct 14, 2026 by lukejudd-lux via gate #24251
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