Skip to content

Apply tunable session settings during the snapshot index/constraint build phase #983

Description

@kvch

Problem

During an initial snapshot, pgstream loads table data via COPY and then builds indexes and constraints afterward (the pg_dump post-data section, restored in restoreIndicesAndConstraints at snapshot_pg_dump_restore_generator.go:281, plus the pre-data conflict-target PK/unique constraints at line 259). This deferred index build is often a significant chunk of snapshot time, and it currently runs with the target server's default session settings.

Operators today can only speed it up by configuring the target server (ALTER ROLE … SET, PGOPTIONS, or ?options= on the connection URL). All of those require touching the database/role rather than pgstream config.

We should let pgstream apply index-build tuning itself, scoped to just that phase.

Proposed solution

Prepend a configurable set of SET statements to the index/constraint dump before restoring it. Because the plain-format restore pipes the whole dump to a single psql session (pg_restore.go:80), the settings apply to every CREATE INDEX / ADD CONSTRAINT in that phase and auto-reset when the session ends.

Default settings (all tunable/disable-able):
SET maintenance_work_mem = '4GB'
SET max_parallel_maintenance_workers = 4
SET synchronous_commit = off
SET statement_timeout = 0
SET lock_timeout = 0

Requirements

  • Configurable list of session settings applied only to the index/constraint restore.
  • Empty/unset config means current behaviour unchanged.
  • Data COPY phase and steady-state CDC are unaffected.
  • Unit test asserting the settings are prepended to the dump; test asserting the WAL-restore path ignores them.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions