mirror of
https://github.com/postgres/postgres.git
synced 2025-09-05 02:22:28 +03:00
Ignore BRIN indexes when checking for HOT updates
When determining whether an index update may be skipped by using HOT, we can ignore attributes indexed by block summarizing indexes without references to individual tuples that need to be cleaned up. A new type TU_UpdateIndexes provides a signal to the executor to determine which indexes to update - no indexes, all indexes, or only the summarizing indexes. This also removes rd_indexattr list, and replaces it with rd_attrsvalid flag. The list was not used anywhere, and a simple flag is sufficient. This was originally committed as5753d4ee32
, but then got reverted bye3fcca0d0d
because of correctness issues. Original patch by Josef Simanek, various fixes and improvements by Tomas Vondra and me. Authors: Matthias van de Meent, Josef Simanek, Tomas Vondra Reviewed-by: Tomas Vondra, Alvaro Herrera Discussion: https://postgr.es/m/05ebcb44-f383-86e3-4f31-0a97a55634cf@enterprisedb.com Discussion: https://postgr.es/m/CAFp7QwpMRGcDAQumN7onN9HjrJ3u4X3ZRXdGFT0K5G2JWvnbWg%40mail.gmail.com
This commit is contained in:
@@ -109,6 +109,7 @@ brinhandler(PG_FUNCTION_ARGS)
|
||||
amroutine->amcanparallel = false;
|
||||
amroutine->amcaninclude = false;
|
||||
amroutine->amusemaintenanceworkmem = false;
|
||||
amroutine->amsummarizing = true;
|
||||
amroutine->amparallelvacuumoptions =
|
||||
VACUUM_OPTION_PARALLEL_CLEANUP;
|
||||
amroutine->amkeytype = InvalidOid;
|
||||
|
@@ -56,6 +56,7 @@ ginhandler(PG_FUNCTION_ARGS)
|
||||
amroutine->amcanparallel = false;
|
||||
amroutine->amcaninclude = false;
|
||||
amroutine->amusemaintenanceworkmem = true;
|
||||
amroutine->amsummarizing = false;
|
||||
amroutine->amparallelvacuumoptions =
|
||||
VACUUM_OPTION_PARALLEL_BULKDEL | VACUUM_OPTION_PARALLEL_CLEANUP;
|
||||
amroutine->amkeytype = InvalidOid;
|
||||
|
@@ -78,6 +78,7 @@ gisthandler(PG_FUNCTION_ARGS)
|
||||
amroutine->amcanparallel = false;
|
||||
amroutine->amcaninclude = true;
|
||||
amroutine->amusemaintenanceworkmem = false;
|
||||
amroutine->amsummarizing = false;
|
||||
amroutine->amparallelvacuumoptions =
|
||||
VACUUM_OPTION_PARALLEL_BULKDEL | VACUUM_OPTION_PARALLEL_COND_CLEANUP;
|
||||
amroutine->amkeytype = InvalidOid;
|
||||
|
@@ -75,6 +75,7 @@ hashhandler(PG_FUNCTION_ARGS)
|
||||
amroutine->amcanparallel = false;
|
||||
amroutine->amcaninclude = false;
|
||||
amroutine->amusemaintenanceworkmem = false;
|
||||
amroutine->amsummarizing = false;
|
||||
amroutine->amparallelvacuumoptions =
|
||||
VACUUM_OPTION_PARALLEL_BULKDEL;
|
||||
amroutine->amkeytype = INT4OID;
|
||||
|
@@ -2924,11 +2924,13 @@ simple_heap_delete(Relation relation, ItemPointer tid)
|
||||
TM_Result
|
||||
heap_update(Relation relation, ItemPointer otid, HeapTuple newtup,
|
||||
CommandId cid, Snapshot crosscheck, bool wait,
|
||||
TM_FailureData *tmfd, LockTupleMode *lockmode)
|
||||
TM_FailureData *tmfd, LockTupleMode *lockmode,
|
||||
TU_UpdateIndexes *update_indexes)
|
||||
{
|
||||
TM_Result result;
|
||||
TransactionId xid = GetCurrentTransactionId();
|
||||
Bitmapset *hot_attrs;
|
||||
Bitmapset *sum_attrs;
|
||||
Bitmapset *key_attrs;
|
||||
Bitmapset *id_attrs;
|
||||
Bitmapset *interesting_attrs;
|
||||
@@ -2951,6 +2953,7 @@ heap_update(Relation relation, ItemPointer otid, HeapTuple newtup,
|
||||
bool have_tuple_lock = false;
|
||||
bool iscombo;
|
||||
bool use_hot_update = false;
|
||||
bool summarized_update = false;
|
||||
bool key_intact;
|
||||
bool all_visible_cleared = false;
|
||||
bool all_visible_cleared_new = false;
|
||||
@@ -2996,12 +2999,16 @@ heap_update(Relation relation, ItemPointer otid, HeapTuple newtup,
|
||||
* Note that we get copies of each bitmap, so we need not worry about
|
||||
* relcache flush happening midway through.
|
||||
*/
|
||||
hot_attrs = RelationGetIndexAttrBitmap(relation, INDEX_ATTR_BITMAP_ALL);
|
||||
hot_attrs = RelationGetIndexAttrBitmap(relation,
|
||||
INDEX_ATTR_BITMAP_HOT_BLOCKING);
|
||||
sum_attrs = RelationGetIndexAttrBitmap(relation,
|
||||
INDEX_ATTR_BITMAP_SUMMARIZED);
|
||||
key_attrs = RelationGetIndexAttrBitmap(relation, INDEX_ATTR_BITMAP_KEY);
|
||||
id_attrs = RelationGetIndexAttrBitmap(relation,
|
||||
INDEX_ATTR_BITMAP_IDENTITY_KEY);
|
||||
interesting_attrs = NULL;
|
||||
interesting_attrs = bms_add_members(interesting_attrs, hot_attrs);
|
||||
interesting_attrs = bms_add_members(interesting_attrs, sum_attrs);
|
||||
interesting_attrs = bms_add_members(interesting_attrs, key_attrs);
|
||||
interesting_attrs = bms_add_members(interesting_attrs, id_attrs);
|
||||
|
||||
@@ -3311,7 +3318,10 @@ l2:
|
||||
UnlockTupleTuplock(relation, &(oldtup.t_self), *lockmode);
|
||||
if (vmbuffer != InvalidBuffer)
|
||||
ReleaseBuffer(vmbuffer);
|
||||
*update_indexes = TU_None;
|
||||
|
||||
bms_free(hot_attrs);
|
||||
bms_free(sum_attrs);
|
||||
bms_free(key_attrs);
|
||||
bms_free(id_attrs);
|
||||
bms_free(modified_attrs);
|
||||
@@ -3633,7 +3643,19 @@ l2:
|
||||
* changed.
|
||||
*/
|
||||
if (!bms_overlap(modified_attrs, hot_attrs))
|
||||
{
|
||||
use_hot_update = true;
|
||||
|
||||
/*
|
||||
* If none of the columns that are used in hot-blocking indexes
|
||||
* were updated, we can apply HOT, but we do still need to check
|
||||
* if we need to update the summarizing indexes, and update those
|
||||
* indexes if the columns were updated, or we may fail to detect
|
||||
* e.g. value bound changes in BRIN minmax indexes.
|
||||
*/
|
||||
if (bms_overlap(modified_attrs, sum_attrs))
|
||||
summarized_update = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -3793,10 +3815,27 @@ l2:
|
||||
heap_freetuple(heaptup);
|
||||
}
|
||||
|
||||
/*
|
||||
* If it is a HOT update, the update may still need to update summarized
|
||||
* indexes, lest we fail to update those summaries and get incorrect
|
||||
* results (for example, minmax bounds of the block may change with this
|
||||
* update).
|
||||
*/
|
||||
if (use_hot_update)
|
||||
{
|
||||
if (summarized_update)
|
||||
*update_indexes = TU_Summarizing;
|
||||
else
|
||||
*update_indexes = TU_None;
|
||||
}
|
||||
else
|
||||
*update_indexes = TU_All;
|
||||
|
||||
if (old_key_tuple != NULL && old_key_copied)
|
||||
heap_freetuple(old_key_tuple);
|
||||
|
||||
bms_free(hot_attrs);
|
||||
bms_free(sum_attrs);
|
||||
bms_free(key_attrs);
|
||||
bms_free(id_attrs);
|
||||
bms_free(modified_attrs);
|
||||
@@ -3951,7 +3990,8 @@ HeapDetermineColumnsInfo(Relation relation,
|
||||
* via ereport().
|
||||
*/
|
||||
void
|
||||
simple_heap_update(Relation relation, ItemPointer otid, HeapTuple tup)
|
||||
simple_heap_update(Relation relation, ItemPointer otid, HeapTuple tup,
|
||||
TU_UpdateIndexes *update_indexes)
|
||||
{
|
||||
TM_Result result;
|
||||
TM_FailureData tmfd;
|
||||
@@ -3960,7 +4000,7 @@ simple_heap_update(Relation relation, ItemPointer otid, HeapTuple tup)
|
||||
result = heap_update(relation, otid, tup,
|
||||
GetCurrentCommandId(true), InvalidSnapshot,
|
||||
true /* wait for commit */ ,
|
||||
&tmfd, &lockmode);
|
||||
&tmfd, &lockmode, update_indexes);
|
||||
switch (result)
|
||||
{
|
||||
case TM_SelfModified:
|
||||
|
@@ -314,7 +314,7 @@ static TM_Result
|
||||
heapam_tuple_update(Relation relation, ItemPointer otid, TupleTableSlot *slot,
|
||||
CommandId cid, Snapshot snapshot, Snapshot crosscheck,
|
||||
bool wait, TM_FailureData *tmfd,
|
||||
LockTupleMode *lockmode, bool *update_indexes)
|
||||
LockTupleMode *lockmode, TU_UpdateIndexes *update_indexes)
|
||||
{
|
||||
bool shouldFree = true;
|
||||
HeapTuple tuple = ExecFetchSlotHeapTuple(slot, true, &shouldFree);
|
||||
@@ -325,7 +325,7 @@ heapam_tuple_update(Relation relation, ItemPointer otid, TupleTableSlot *slot,
|
||||
tuple->t_tableOid = slot->tts_tableOid;
|
||||
|
||||
result = heap_update(relation, otid, tuple, cid, crosscheck, wait,
|
||||
tmfd, lockmode);
|
||||
tmfd, lockmode, update_indexes);
|
||||
ItemPointerCopy(&tuple->t_self, &slot->tts_tid);
|
||||
|
||||
/*
|
||||
@@ -334,9 +334,20 @@ heapam_tuple_update(Relation relation, ItemPointer otid, TupleTableSlot *slot,
|
||||
* Note: heap_update returns the tid (location) of the new tuple in the
|
||||
* t_self field.
|
||||
*
|
||||
* If it's a HOT update, we mustn't insert new index entries.
|
||||
* If the update is not HOT, we must update all indexes. If the update
|
||||
* is HOT, it could be that we updated summarized columns, so we either
|
||||
* update only summarized indexes, or none at all.
|
||||
*/
|
||||
*update_indexes = result == TM_Ok && !HeapTupleIsHeapOnly(tuple);
|
||||
if (result != TM_Ok)
|
||||
{
|
||||
Assert(*update_indexes == TU_None);
|
||||
*update_indexes = TU_None;
|
||||
}
|
||||
else if (!HeapTupleIsHeapOnly(tuple))
|
||||
Assert(*update_indexes == TU_All);
|
||||
else
|
||||
Assert((*update_indexes == TU_Summarizing) ||
|
||||
(*update_indexes == TU_None));
|
||||
|
||||
if (shouldFree)
|
||||
pfree(tuple);
|
||||
|
@@ -114,6 +114,7 @@ bthandler(PG_FUNCTION_ARGS)
|
||||
amroutine->amcanparallel = true;
|
||||
amroutine->amcaninclude = true;
|
||||
amroutine->amusemaintenanceworkmem = false;
|
||||
amroutine->amsummarizing = false;
|
||||
amroutine->amparallelvacuumoptions =
|
||||
VACUUM_OPTION_PARALLEL_BULKDEL | VACUUM_OPTION_PARALLEL_COND_CLEANUP;
|
||||
amroutine->amkeytype = InvalidOid;
|
||||
|
@@ -62,6 +62,7 @@ spghandler(PG_FUNCTION_ARGS)
|
||||
amroutine->amcanparallel = false;
|
||||
amroutine->amcaninclude = true;
|
||||
amroutine->amusemaintenanceworkmem = false;
|
||||
amroutine->amsummarizing = false;
|
||||
amroutine->amparallelvacuumoptions =
|
||||
VACUUM_OPTION_PARALLEL_BULKDEL | VACUUM_OPTION_PARALLEL_COND_CLEANUP;
|
||||
amroutine->amkeytype = InvalidOid;
|
||||
|
@@ -345,7 +345,7 @@ void
|
||||
simple_table_tuple_update(Relation rel, ItemPointer otid,
|
||||
TupleTableSlot *slot,
|
||||
Snapshot snapshot,
|
||||
bool *update_indexes)
|
||||
TU_UpdateIndexes *update_indexes)
|
||||
{
|
||||
TM_Result result;
|
||||
TM_FailureData tmfd;
|
||||
|
Reference in New Issue
Block a user