Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions scripts/publication_benchmark/capacity_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,28 @@ class CapacityBatch:
container_name: str | None = None


def _pg_checkpoints_fragment(engine) -> str:
"""Return the checkpoint-count subselect for this PostgreSQL version.

PostgreSQL 17 moved checkpoint stats out of ``pg_stat_bgwriter`` into
``pg_stat_checkpointer`` (columns renamed to ``num_timed``/
``num_requested``), and pre-17 PostgreSQL keeps them in
``pg_stat_bgwriter`` (``checkpoints_timed``/``checkpoints_req``).
Detect the server version so the capacity probe works on both.
"""
with engine.connect() as connection:
version_number = int(
connection.execute(text("SHOW server_version_num")).scalar_one()
)
if version_number >= 170000:
return "(SELECT num_timed + num_requested FROM pg_stat_checkpointer)"
return "(SELECT checkpoints_timed + checkpoints_req FROM pg_stat_bgwriter)"


def _database_resources(database_url: str, spec: RetrievalProbeSpec) -> dict[str, int]:
engine = create_engine(database_url_for_owner(database_url, owner="sync"))
try:
checkpoints_fragment = _pg_checkpoints_fragment(engine)
with engine.connect() as connection:
row = (
connection.execute(
Expand All @@ -58,8 +77,8 @@ def _database_resources(database_url: str, spec: RetrievalProbeSpec) -> dict[str
"(SELECT temp_bytes FROM pg_stat_database WHERE datname = current_database()) "
"AS temp_bytes, (SELECT deadlocks FROM pg_stat_database "
"WHERE datname = current_database()) AS deadlocks, "
"(SELECT checkpoints_timed + checkpoints_req FROM pg_stat_bgwriter) "
"AS checkpoints, pg_database_size(current_database()) AS database_bytes, "
+ checkpoints_fragment
+ " AS checkpoints, pg_database_size(current_database()) AS database_bytes, "
"(SELECT coalesce(sum(pg_relation_size(c.oid)), 0) FROM pg_class c "
"WHERE c.relkind IN ('r', 'm') AND c.relnamespace IN "
"(SELECT oid FROM pg_namespace WHERE nspname = 'public')) AS table_bytes, "
Expand Down