From d968667cf4808f0ff5ff2c3bd37b9bf85c9d4a11 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Thu, 8 Aug 2024 08:27:26 +0200 Subject: [PATCH 1/3] Don't overwrite scan key in systable_beginscan() When systable_beginscan() and systable_beginscan_ordered() choose an index scan, they remap the attribute numbers in the passed-in scan keys to the attribute numbers of the index, and then write those remapped attribute numbers back into the scan key passed by the caller. This second part is surprising and gratuitous. It means that a scan key cannot safely be used more than once (but it might sometimes work, depending on circumstances). Also, there is no value in providing these remapped attribute numbers back to the caller, since they can't do anything with that. Fix that by making a copy of the scan keys passed by the caller and make the modifications there. --- src/backend/access/index/genam.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/backend/access/index/genam.c b/src/backend/access/index/genam.c index de751e8e4a3..b5eba549d3e 100644 --- a/src/backend/access/index/genam.c +++ b/src/backend/access/index/genam.c @@ -371,7 +371,7 @@ index_compute_xid_horizon_for_tuples(Relation irel, * nkeys, key: scan keys * * The attribute numbers in the scan key should be set for the heap case. - * If we choose to index, we reset them to 1..n to reference the index + * If we choose to index, we convert them to 1..n to reference the index * columns. Note this means there must be one scankey qualification per * index column! This is checked by the Asserts in the normal, index-using * case, but won't be checked if the heapscan path is taken. @@ -419,17 +419,22 @@ systable_beginscan(Relation heapRelation, if (irel) { int i; + ScanKey idxkey; - /* Change attribute numbers to be index column numbers. */ + idxkey = palloc_array(ScanKeyData, nkeys); + + /* Convert attribute numbers to be index column numbers. */ for (i = 0; i < nkeys; i++) { int j; + memcpy(&idxkey[i], &key[i], sizeof(ScanKeyData)); + for (j = 0; j < IndexRelationGetNumberOfAttributes(irel); j++) { if (key[i].sk_attno == irel->rd_index->indkey.values[j]) { - key[i].sk_attno = j + 1; + idxkey[i].sk_attno = j + 1; break; } } @@ -439,7 +444,7 @@ systable_beginscan(Relation heapRelation, sysscan->iscan = index_beginscan(heapRelation, irel, snapshot, nkeys, 0); - index_rescan(sysscan->iscan, key, nkeys, NULL, 0); + index_rescan(sysscan->iscan, idxkey, nkeys, NULL, 0); sysscan->scan = NULL; } else @@ -647,6 +652,7 @@ systable_beginscan_ordered(Relation heapRelation, { SysScanDesc sysscan; int i; + ScanKey idxkey; /* REINDEX can probably be a hard error here ... */ if (ReindexIsProcessingIndex(RelationGetRelid(indexRelation))) @@ -678,16 +684,20 @@ systable_beginscan_ordered(Relation heapRelation, sysscan->snapshot = NULL; } - /* Change attribute numbers to be index column numbers. */ + idxkey = palloc_array(ScanKeyData, nkeys); + + /* Convert attribute numbers to be index column numbers. */ for (i = 0; i < nkeys; i++) { int j; + memcpy(&idxkey[i], &key[i], sizeof(ScanKeyData)); + for (j = 0; j < IndexRelationGetNumberOfAttributes(indexRelation); j++) { if (key[i].sk_attno == indexRelation->rd_index->indkey.values[j]) { - key[i].sk_attno = j + 1; + idxkey[i].sk_attno = j + 1; break; } } @@ -697,7 +707,7 @@ systable_beginscan_ordered(Relation heapRelation, sysscan->iscan = index_beginscan(heapRelation, indexRelation, snapshot, nkeys, 0); - index_rescan(sysscan->iscan, key, nkeys, NULL, 0); + index_rescan(sysscan->iscan, idxkey, nkeys, NULL, 0); sysscan->scan = NULL; return sysscan; base-commit: e56ccc8e4204d9faf86f3bd2e435a0788b3d0429 -- 2.46.0