Skip to content

Commit c00c54a

Browse files
Fix nbtree pgstats accounting with parallel scans.
Commit 5bf748b, which enhanced nbtree ScalarArrayOp execution, made parallel index scans work with the new design for arrays via explicit scheduling of primitive index scans. Under this scheme a parallel index scan with array keys will perform the same number of index descents as an equivalent serial index scan (barring corner cases where an individual parallel worker discovers that it can advance the scan's array keys without anybody needing to perform another descent of the index to get to the relevant page on the leaf level). Despite all this, the pgstats accounting wasn't updated; it continued to increment the total number of index scans for the rel once per _bt_first call, no matter the details. As a result, the number of (primitive) index scans could be over-counted during parallel scans. To fix, delay incrementing the count of index scans until after we've established that another descent of the index (using either _bt_search or _bt_endpoint) is required. That way pg_stat_user_tables.idx_scan always advances in the same way, regardless of whether or not the scan makes use of parallelism. Oversight in commit 5bf748b, which enhanced nbtree ScalarArrayOp execution. Author: Peter Geoghegan <pg@bowt.ie> Reviewed-By: Tomas Vondra <tomas@vondra.me> Discussion: https://postgr.es/m/CAH2-Wz=E7XrkvscBN0U6V81NK3Q-dQOmivvbEsjG-zwEfDdFpg@mail.gmail.com Discussion: https://postgr.es/m/CAH2-WzkRqvaqR2CTNqTZP0z6FuL4-3ED6eQB0yx38XBNj1v-4Q@mail.gmail.com Backpatch: 17-, where nbtree SAOP execution was enhanced.
1 parent d35e293 commit c00c54a

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/backend/access/nbtree/nbtsearch.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -896,8 +896,6 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
896896

897897
Assert(!BTScanPosIsValid(so->currPos));
898898

899-
pgstat_count_index_scan(rel);
900-
901899
/*
902900
* Examine the scan keys and eliminate any redundant keys; also mark the
903901
* keys that must be matched to continue the scan.
@@ -960,6 +958,12 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
960958
_bt_start_array_keys(scan, dir);
961959
}
962960

961+
/*
962+
* Count an indexscan for stats, now that we know that we'll call
963+
* _bt_search/_bt_endpoint below
964+
*/
965+
pgstat_count_index_scan(rel);
966+
963967
/*----------
964968
* Examine the scan keys to discover where we need to start the scan.
965969
*

0 commit comments

Comments
 (0)