Skip to content

Conversation

@NikolayS
Copy link
Owner

Add a rows_filtered column to pg_stat_statements to track the total number of rows removed by filter conditions during query execution. This metric helps identify queries that may benefit from better indexing, as high rows_filtered values indicate that many rows are being scanned but then discarded.

The implementation:

  • Enables per-node instrumentation with INSTRUMENT_ROWS before ExecutorStart (using INSTRUMENT_ROWS instead of INSTRUMENT_ALL to avoid the overhead of timing instrumentation)
  • Walks the plan tree in ExecutorEnd to sum nfiltered1 (scanqual/joinqual) and nfiltered2 (other quals) from all nodes
  • Includes the new column in the SQL function and view for version 1.14

Example output:

  query                      | rows | rows_filtered
  ---------------------------+------+---------------
  SELECT * FROM t WHERE x=$1 |   10 |           990

This helps identify inefficient queries where many rows are scanned but few are returned, suggesting an index might help.

@NikolayS NikolayS force-pushed the claude/pg-stat-statements-rows-filtered-ddwRA branch from 1938136 to 3e66693 Compare December 27, 2025 21:47
Add a new rows_filtered column to pg_stat_statements that tracks the
total number of rows removed by filter conditions across all plan nodes.
This helps identify queries that would benefit from better indexing.

The feature is controlled by a new GUC pg_stat_statements.track_rows_filtered
(default: off). When enabled, the module uses INSTRUMENT_ROWS instrumentation
to collect nfiltered1 (scan/join qual) and nfiltered2 (recheck conditions)
from all plan nodes via planstate_tree_walker.

Key implementation details:
- Uses INSTRUMENT_ROWS (not INSTRUMENT_ALL) to avoid timing overhead
- Only increments tuple counters, which has negligible performance impact
- GUC is superuser-only (PGC_SUSET), can be changed at runtime
- Stats file header bumped to invalidate old stats files

This addresses a common DBA need to identify inefficient sequential scans
where adding an index could significantly improve performance.
@NikolayS NikolayS force-pushed the claude/pg-stat-statements-rows-filtered-ddwRA branch from 3e66693 to b577803 Compare December 27, 2025 22:08
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.

3 participants