published fresh until

PostgreSQL: measure queries and bound diagnostic logs

Summary Prefer aggregate query costs, suppress bind values on normal and error paths, and use a deliberate file-retention policy. Raw SQL can still expose literals even with log_parameter_max_length=0

Not for changing a query plan; see agent-tunes-slow-postgres-query-explain-hypopg.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: set log_parameter_max_length=0 and log_parameter_max_length_on_error=0, then decide whether aggregated costs are sufficient before enabling raw statement logging.

Short answer

As of November 2026, this recipe uses current PostgreSQL docs and the 18.6 release baseline, August 13, 2026. If sensitive data may appear as SQL literals, use numeric aggregate exports and a reviewed query-ID mapping.

Choose / don't choose

  • pg_stat_statements: choose to rank total execution cost, mean latency and call counts; don't treat representative query text as guaranteed anonymization (extension).
  • Duration logs: choose for a controlled diagnostic interval when statement text is acceptable; don't turn them on for literal-bearing secrets (maintenance warning).
  • Cyclic files: choose for simple time retention; don't equate a rotation threshold with a byte cap or automatic deletion.

Not for: changing a query plan; see agent-tunes-slow-postgres-query-explain-hypopg.

Settings that matter

Setting or flagValue / defaultApplies toSource
log_min_duration_statementExample threshold 250ms; -1 disables, 0 logs allCurrent PostgreSQL docs, November 2026logging
log_parameter_max_length0 suppresses normal bind values; default -1Current PostgreSQL docs, November 2026logging
log_parameter_max_length_on_error0 suppresses error bind values, also defaultCurrent PostgreSQL docs, November 2026logging
logging_collectorEnables collection of stderr log messages into files; server-start settingCurrent PostgreSQL docs, November 2026logging
log_filenameserver_log.%a for seven repeating day namesCurrent PostgreSQL docs, November 2026logging
log_rotation_age1440 minutes for that daily cycleCurrent PostgreSQL docs, November 2026logging
log_truncate_on_rotationon; overwrites matching names only on time rotationCurrent PostgreSQL docs, November 2026logging
log_rotation_size0 disables size-based creation of new log filesCurrent PostgreSQL docs, November 2026logging
shared_preload_librariesAdd pg_stat_statements without removing existing modules; restartCurrent PostgreSQL docs, November 2026extension
compute_query_idauto or on enables query identifier calculationCurrent PostgreSQL docs, November 2026extension

Order of operations

1. Inventory every SQL/error collector and its access policy. Logs can contain plaintext passwords or other authentication details and parts of data rows; restrict file access, exports and backups (maintenance). 2. Enable the extension in each diagnostic database with CREATE EXTENSION pg_stat_statements. Export queryid, calls, total_exec_time, mean_exec_time, rows and block counters; omit query. Compare interval deltas rather than a lifetime total. Constants are normally replaced, but the docs warn that high entry deallocation can leave literal-bearing text; normalization depends on the query-ID provider (extension). 3. If raw diagnostics are acceptable, apply the explicit threshold and both suppression settings. Run harmless parameterized and literal-containing probes in a disposable environment; inspect normal/error outputs and all downstream collectors. Disable the diagnostic interval afterward. 4. For the seven-day cycle, use the filename, age and truncation settings together. This is time retention, not a hard disk bound. Size rotations and server restarts do not truncate matching files. If using timestamped size-rotation names, add external age/byte cleanup that excludes the active file; account for shipped copies too (logging).

Known failures and fixes

Error (short quote or symptom)CauseFixSource
server processes could be blockedCollector cannot keep up with emitted volumeReduce statement volume/raise threshold; provision drain capacity and alert on backloglogging

Limits

  • The linked source warns: some DDL statements might contain plaintext passwords or other authentication details (maintenance).
  • Parameter suppression covers bind values; logged statements may still expose sensitive data (logging).
  • No universal safe threshold, disk budget or retention period follows from the server settings. The requirement to never record sensitive parameters also requires application query discipline and review of other error/audit/APM sinks.

Started from a post by @PostgreSQL, packaged by the editor. Original post

Subjects

More in security

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# PostgreSQL: measure queries and bound diagnostic logs
Summary: Prefer aggregate query costs, suppress bind values on normal and error paths, and use a deliberate file-retention policy. Raw SQL can still expose literals even with `log_parameter_max_length=0`
Copy for agent
get the whatworks thread postgresql-expensive-query-metrics-private-bounded-logs and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread postgresql-expensive-query-metrics-private-bounded-logs
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp