From 88a9318fbbecab3ce0533a9796bc07d7f56a0ed9 Mon Sep 17 00:00:00 2001 From: wuhao Date: Tue, 21 Nov 2023 09:11:23 +0800 Subject: [PATCH 1/3] Check index unique & skip prefetch for non-heap relation * Check index unique in the AM callback if set. The callback is designed to support unique index for AO tables, but it could also work for other table AM. * Skip prefetch page for non-heap relation. Prefetch loads a typical pg buffer to scan, but it can't work for non-page based storage. --- src/backend/access/table/tableam.c | 2 +- src/backend/executor/nodeBitmapHeapscan.c | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/backend/access/table/tableam.c b/src/backend/access/table/tableam.c index 1eee611e022..992d3fbbd42 100644 --- a/src/backend/access/table/tableam.c +++ b/src/backend/access/table/tableam.c @@ -288,7 +288,7 @@ table_index_fetch_tuple_check(Relation rel, * overhead is significant for AO/CO relations. For details, please refer to * table_index_unique_check(). */ - if (RelationIsAppendOptimized(rel)) + if (rel->rd_tableam->index_unique_check) return table_index_unique_check(rel, tid, snapshot, all_dead); slot = table_slot_create(rel, NULL); diff --git a/src/backend/executor/nodeBitmapHeapscan.c b/src/backend/executor/nodeBitmapHeapscan.c index 3144d37b186..c66f1903c21 100644 --- a/src/backend/executor/nodeBitmapHeapscan.c +++ b/src/backend/executor/nodeBitmapHeapscan.c @@ -73,6 +73,20 @@ static inline void BitmapPrefetch(BitmapHeapScanState *node, static bool BitmapShouldInitializeSharedState(ParallelBitmapHeapState *pstate); static void ExecEagerFreeBitmapHeapScan(BitmapHeapScanState *node); +/* + * Other non-heap table access method may use bitmap scan, + * the prefetch will not work for them if they have + * non-standard page-based storage. + */ +static inline bool +RelationSupportPrefetch(Relation rel) +{ + bool non_standard; + non_standard = (rel->rd_rel->relkind == RELKIND_RELATION || + rel->rd_rel->relkind == RELKIND_MATVIEW) && + !RelationIsHeap(rel); + return !non_standard; +} /* * Free the state relevant to bitmaps @@ -325,7 +339,8 @@ BitmapHeapNext(BitmapHeapScanState *node) * XXX: It's a layering violation that we do these checks above * tableam, they should probably moved below it at some point. */ - BitmapPrefetch(node, scan); + if (RelationSupportPrefetch(scan->rs_rd)) + BitmapPrefetch(node, scan); if (node->return_empty_tuples > 0) { From 66b9c71727a389fafb86782f3d0db403eba873eb Mon Sep 17 00:00:00 2001 From: Hao Wu Date: Fri, 15 Dec 2023 21:12:47 +0800 Subject: [PATCH 2/3] update comment --- src/backend/access/table/tableam.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/backend/access/table/tableam.c b/src/backend/access/table/tableam.c index 992d3fbbd42..6ff94ac7d76 100644 --- a/src/backend/access/table/tableam.c +++ b/src/backend/access/table/tableam.c @@ -284,8 +284,8 @@ table_index_fetch_tuple_check(Relation rel, bool found; /* - * Optimized path for AO/CO relations as the aforementioned per-tuple - * overhead is significant for AO/CO relations. For details, please refer to + * Optimized path for non-heap relations as the aforementioned per-tuple + * overhead is significant for non-heap relations. For details, please refer to * table_index_unique_check(). */ if (rel->rd_tableam->index_unique_check) From 404ee4a6f573f03cf6c849c0e198506e1fba57d2 Mon Sep 17 00:00:00 2001 From: Hao Wu Date: Mon, 18 Dec 2023 14:31:02 +0800 Subject: [PATCH 3/3] update RelationSupportPrefetch --- src/backend/executor/nodeBitmapHeapscan.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/backend/executor/nodeBitmapHeapscan.c b/src/backend/executor/nodeBitmapHeapscan.c index c66f1903c21..15fa3775bdf 100644 --- a/src/backend/executor/nodeBitmapHeapscan.c +++ b/src/backend/executor/nodeBitmapHeapscan.c @@ -81,11 +81,7 @@ static void ExecEagerFreeBitmapHeapScan(BitmapHeapScanState *node); static inline bool RelationSupportPrefetch(Relation rel) { - bool non_standard; - non_standard = (rel->rd_rel->relkind == RELKIND_RELATION || - rel->rd_rel->relkind == RELKIND_MATVIEW) && - !RelationIsHeap(rel); - return !non_standard; + return RelationIsHeap(rel); } /*