From: Melanie Plageman Date: Mon, 30 Mar 2026 17:27:34 +0000 (-0400) Subject: Pass down information on table modification to scan nodes X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=50eb5faea2959125a575bae0a062ccb33b49c43d;p=thirdparty%2Fpostgresql.git Pass down information on table modification to scan nodes Pass down information to sequential scan, index [only] scan, bitmap table scan, sample scan, and TID range scan nodes on whether or not the query modifies the relation being scanned. A later commit will use this information to update the VM during on-access pruning only if the relation is not modified by the query. Author: Melanie Plageman Reviewed-by: Andres Freund Reviewed-by: Andrey Borodin Reviewed-by: Tomas Vondra Reviewed-by: Chao Li Discussion: https://postgr.es/m/4379FDA3-9446-4E2C-9C15-32EFE8D4F31B%40yandex-team.ru --- diff --git a/src/backend/executor/execUtils.c b/src/backend/executor/execUtils.c index 36c5285d252..1eb6b9f1f40 100644 --- a/src/backend/executor/execUtils.c +++ b/src/backend/executor/execUtils.c @@ -736,6 +736,27 @@ ExecRelationIsTargetRelation(EState *estate, Index scanrelid) return bms_is_member(scanrelid, estate->es_plannedstmt->resultRelationRelids); } +/* + * Return true if the scan node's relation is not modified by the query. + * + * This is not perfectly accurate. INSERT ... SELECT from the same table does + * not add the scan relation to resultRelationRelids, so it will be reported + * as read-only even though the query modifies it. + * + * Conversely, when any relation in the query has a modifying row mark, all + * other relations get a ROW_MARK_REFERENCE, causing them to be reported as + * not read-only even though they may be. + */ +bool +ScanRelIsReadOnly(ScanState *ss) +{ + Index scanrelid = ((Scan *) ss->ps.plan)->scanrelid; + PlannedStmt *pstmt = ss->ps.state->es_plannedstmt; + + return !bms_is_member(scanrelid, pstmt->resultRelationRelids) && + !bms_is_member(scanrelid, pstmt->rowMarkRelids); +} + /* ---------------------------------------------------------------- * ExecOpenScanRelation * diff --git a/src/backend/executor/nodeBitmapHeapscan.c b/src/backend/executor/nodeBitmapHeapscan.c index 69683d81527..73831aed451 100644 --- a/src/backend/executor/nodeBitmapHeapscan.c +++ b/src/backend/executor/nodeBitmapHeapscan.c @@ -149,7 +149,8 @@ BitmapTableScanSetup(BitmapHeapScanState *node) node->ss.ps.state->es_snapshot, 0, NULL, - SO_NONE); + ScanRelIsReadOnly(&node->ss) ? + SO_HINT_REL_READ_ONLY : SO_NONE); } node->ss.ss_currentScanDesc->st.rs_tbmiterator = tbmiterator; diff --git a/src/backend/executor/nodeIndexonlyscan.c b/src/backend/executor/nodeIndexonlyscan.c index 02df40f32c5..de6154fd541 100644 --- a/src/backend/executor/nodeIndexonlyscan.c +++ b/src/backend/executor/nodeIndexonlyscan.c @@ -96,7 +96,8 @@ IndexOnlyNext(IndexOnlyScanState *node) node->ioss_Instrument, node->ioss_NumScanKeys, node->ioss_NumOrderByKeys, - SO_NONE); + ScanRelIsReadOnly(&node->ss) ? + SO_HINT_REL_READ_ONLY : SO_NONE); node->ioss_ScanDesc = scandesc; @@ -796,7 +797,8 @@ ExecIndexOnlyScanInitializeDSM(IndexOnlyScanState *node, node->ioss_NumScanKeys, node->ioss_NumOrderByKeys, piscan, - SO_NONE); + ScanRelIsReadOnly(&node->ss) ? + SO_HINT_REL_READ_ONLY : SO_NONE); node->ioss_ScanDesc->xs_want_itup = true; node->ioss_VMBuffer = InvalidBuffer; @@ -863,7 +865,8 @@ ExecIndexOnlyScanInitializeWorker(IndexOnlyScanState *node, node->ioss_NumScanKeys, node->ioss_NumOrderByKeys, piscan, - SO_NONE); + ScanRelIsReadOnly(&node->ss) ? + SO_HINT_REL_READ_ONLY : SO_NONE); node->ioss_ScanDesc->xs_want_itup = true; /* diff --git a/src/backend/executor/nodeIndexscan.c b/src/backend/executor/nodeIndexscan.c index 3c0b8daf664..1620d146071 100644 --- a/src/backend/executor/nodeIndexscan.c +++ b/src/backend/executor/nodeIndexscan.c @@ -114,7 +114,8 @@ IndexNext(IndexScanState *node) node->iss_Instrument, node->iss_NumScanKeys, node->iss_NumOrderByKeys, - SO_NONE); + ScanRelIsReadOnly(&node->ss) ? + SO_HINT_REL_READ_ONLY : SO_NONE); node->iss_ScanDesc = scandesc; @@ -211,7 +212,8 @@ IndexNextWithReorder(IndexScanState *node) node->iss_Instrument, node->iss_NumScanKeys, node->iss_NumOrderByKeys, - SO_NONE); + ScanRelIsReadOnly(&node->ss) ? + SO_HINT_REL_READ_ONLY : SO_NONE); node->iss_ScanDesc = scandesc; @@ -1733,7 +1735,8 @@ ExecIndexScanInitializeDSM(IndexScanState *node, node->iss_NumScanKeys, node->iss_NumOrderByKeys, piscan, - SO_NONE); + ScanRelIsReadOnly(&node->ss) ? + SO_HINT_REL_READ_ONLY : SO_NONE); /* * If no run-time keys to calculate or they are ready, go ahead and pass @@ -1798,7 +1801,8 @@ ExecIndexScanInitializeWorker(IndexScanState *node, node->iss_NumScanKeys, node->iss_NumOrderByKeys, piscan, - SO_NONE); + ScanRelIsReadOnly(&node->ss) ? + SO_HINT_REL_READ_ONLY : SO_NONE); /* * If no run-time keys to calculate or they are ready, go ahead and pass diff --git a/src/backend/executor/nodeSamplescan.c b/src/backend/executor/nodeSamplescan.c index cf32df33d82..f3d273e1c5e 100644 --- a/src/backend/executor/nodeSamplescan.c +++ b/src/backend/executor/nodeSamplescan.c @@ -299,7 +299,8 @@ tablesample_init(SampleScanState *scanstate) scanstate->use_bulkread, allow_sync, scanstate->use_pagemode, - SO_NONE); + ScanRelIsReadOnly(&scanstate->ss) ? + SO_HINT_REL_READ_ONLY : SO_NONE); } else { diff --git a/src/backend/executor/nodeSeqscan.c b/src/backend/executor/nodeSeqscan.c index 09ccc65de1c..04803b0e37d 100644 --- a/src/backend/executor/nodeSeqscan.c +++ b/src/backend/executor/nodeSeqscan.c @@ -72,7 +72,8 @@ SeqNext(SeqScanState *node) scandesc = table_beginscan(node->ss.ss_currentRelation, estate->es_snapshot, 0, NULL, - SO_NONE); + ScanRelIsReadOnly(&node->ss) ? + SO_HINT_REL_READ_ONLY : SO_NONE); node->ss.ss_currentScanDesc = scandesc; } @@ -375,9 +376,11 @@ ExecSeqScanInitializeDSM(SeqScanState *node, pscan, estate->es_snapshot); shm_toc_insert(pcxt->toc, node->ss.ps.plan->plan_node_id, pscan); + node->ss.ss_currentScanDesc = table_beginscan_parallel(node->ss.ss_currentRelation, pscan, - SO_NONE); + ScanRelIsReadOnly(&node->ss) ? + SO_HINT_REL_READ_ONLY : SO_NONE); } /* ---------------------------------------------------------------- @@ -411,5 +414,6 @@ ExecSeqScanInitializeWorker(SeqScanState *node, pscan = shm_toc_lookup(pwcxt->toc, node->ss.ps.plan->plan_node_id, false); node->ss.ss_currentScanDesc = table_beginscan_parallel(node->ss.ss_currentRelation, pscan, - SO_NONE); + ScanRelIsReadOnly(&node->ss) ? + SO_HINT_REL_READ_ONLY : SO_NONE); } diff --git a/src/backend/executor/nodeTidrangescan.c b/src/backend/executor/nodeTidrangescan.c index 084e4c6ec90..4a8fe91b2b3 100644 --- a/src/backend/executor/nodeTidrangescan.c +++ b/src/backend/executor/nodeTidrangescan.c @@ -246,7 +246,8 @@ TidRangeNext(TidRangeScanState *node) estate->es_snapshot, &node->trss_mintid, &node->trss_maxtid, - SO_NONE); + ScanRelIsReadOnly(&node->ss) ? + SO_HINT_REL_READ_ONLY : SO_NONE); node->ss.ss_currentScanDesc = scandesc; } else @@ -461,7 +462,9 @@ ExecTidRangeScanInitializeDSM(TidRangeScanState *node, ParallelContext *pcxt) shm_toc_insert(pcxt->toc, node->ss.ps.plan->plan_node_id, pscan); node->ss.ss_currentScanDesc = table_beginscan_parallel_tidrange(node->ss.ss_currentRelation, - pscan, SO_NONE); + pscan, + ScanRelIsReadOnly(&node->ss) ? + SO_HINT_REL_READ_ONLY : SO_NONE); } /* ---------------------------------------------------------------- @@ -495,5 +498,7 @@ ExecTidRangeScanInitializeWorker(TidRangeScanState *node, pscan = shm_toc_lookup(pwcxt->toc, node->ss.ps.plan->plan_node_id, false); node->ss.ss_currentScanDesc = table_beginscan_parallel_tidrange(node->ss.ss_currentRelation, - pscan, SO_NONE); + pscan, + ScanRelIsReadOnly(&node->ss) ? + SO_HINT_REL_READ_ONLY : SO_NONE); } diff --git a/src/include/access/tableam.h b/src/include/access/tableam.h index e0d27afa876..ab2e7fc1dfe 100644 --- a/src/include/access/tableam.h +++ b/src/include/access/tableam.h @@ -65,6 +65,9 @@ typedef enum ScanOptions /* unregister snapshot at scan end? */ SO_TEMP_SNAPSHOT = 1 << 9, + + /* set if the query doesn't modify the relation */ + SO_HINT_REL_READ_ONLY = 1 << 10, } ScanOptions; /* diff --git a/src/include/executor/executor.h b/src/include/executor/executor.h index 07f4b1f7490..7979a17e4ec 100644 --- a/src/include/executor/executor.h +++ b/src/include/executor/executor.h @@ -690,6 +690,8 @@ extern void ExecCreateScanSlotFromOuterPlan(EState *estate, extern bool ExecRelationIsTargetRelation(EState *estate, Index scanrelid); +extern bool ScanRelIsReadOnly(ScanState *ss); + extern Relation ExecOpenScanRelation(EState *estate, Index scanrelid, int eflags); extern void ExecInitRangeTable(EState *estate, List *rangeTable, List *permInfos,