Skip to content

Commit

Permalink
Fix DELETE on compressed chunk with non-btree operators
Browse files Browse the repository at this point in the history
  • Loading branch information
svenklemm committed Feb 1, 2025
1 parent 39049fe commit fb7f931
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 14 deletions.
1 change: 1 addition & 0 deletions .unreleased/pr_7645
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixes: #7645 Fix DELETE on compressed chunk with non-btree operators
14 changes: 12 additions & 2 deletions tsl/src/compression/compression_dml.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ decompress_batches_for_update_delete(HypertableModifyState *ht_state, Chunk *chu
scankeys = build_update_delete_scankeys(comp_chunk_rel,
heap_filters,
&num_scankeys,
&null_columns);
&null_columns,
&delete_only);
}

if (matching_index_rel)
Expand Down Expand Up @@ -957,7 +958,16 @@ process_predicates(Chunk *ch, CompressionSettings *settings, List *predicates,
break;
}
default:
/* Do nothing for unknown operator strategies. */
*heap_filters = lappend(*heap_filters,
make_batchfilter(column_name,
op_strategy,
collation,
opcode,
arg_value,
false, /* is_null_check */
false, /* is_null */
false /* is_array_op */
));
break;
}
continue;
Expand Down
2 changes: 1 addition & 1 deletion tsl/src/compression/compression_dml.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ ScanKeyData *build_heap_scankeys(Oid hypertable_relid, Relation in_rel, Relation
CompressionSettings *settings, Bitmapset *key_columns,
Bitmapset **null_columns, TupleTableSlot *slot, int *num_scankeys);
ScanKeyData *build_update_delete_scankeys(Relation in_rel, List *heap_filters, int *num_scankeys,
Bitmapset **null_columns);
Bitmapset **null_columns, bool *delete_only);
32 changes: 21 additions & 11 deletions tsl/src/compression/compression_scankey.c
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ build_index_scankeys_using_slot(Oid hypertable_relid, Relation in_rel, Relation
*/
ScanKeyData *
build_update_delete_scankeys(Relation in_rel, List *heap_filters, int *num_scankeys,
Bitmapset **null_columns)
Bitmapset **null_columns, bool *delete_only)
{
ListCell *lc;
BatchFilter *filter;
Expand All @@ -455,16 +455,26 @@ build_update_delete_scankeys(Relation in_rel, List *heap_filters, int *num_scank
NameStr(filter->column_name),
RelationGetRelationName(in_rel))));

key_index = create_segment_filter_scankey(in_rel,
NameStr(filter->column_name),
filter->strategy,
deduce_filter_subtype(filter, typoid),
scankeys,
key_index,
null_columns,
filter->value ? filter->value->constvalue : 0,
filter->is_null_check,
filter->is_array_op);
int new_index = create_segment_filter_scankey(in_rel,
NameStr(filter->column_name),
filter->strategy,
deduce_filter_subtype(filter, typoid),
scankeys,
key_index,
null_columns,
filter->value ? filter->value->constvalue : 0,
filter->is_null_check,
filter->is_array_op);
/*
* When we plan to DELETE directly on compressed chunks we
* need to ensure all query constraints could be applied
* to the compressed scan and disable direct DELETE when
* we are skipping filters.
*/
if (*delete_only && new_index == key_index)
*delete_only = false;

key_index = new_index;
}
*num_scankeys = key_index;
return scankeys;
Expand Down
22 changes: 22 additions & 0 deletions tsl/test/shared/expected/compression_dml.out
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,28 @@ SELECT count(*) FROM direct_delete WHERE reading='r2';
0
(1 row)

ROLLBACK;
-- issue #7644
-- make sure non-btree operators don't delete unrelated batches
BEGIN;
:ANALYZE DELETE FROM direct_delete WHERE reading<>'r2';
QUERY PLAN
Custom Scan (HypertableModify) (actual rows=0 loops=1)
Batches decompressed: 6
Tuples decompressed: 6
-> Delete on direct_delete (actual rows=0 loops=1)
Delete on _hyper_X_X_chunk direct_delete_1
-> Seq Scan on _hyper_X_X_chunk direct_delete_1 (actual rows=4 loops=1)
Filter: (reading <> 'r2'::text)
Rows Removed by Filter: 2
(8 rows)

-- 2 tuples should still be there
SELECT count(*) FROM direct_delete;
count
2
(1 row)

ROLLBACK;
-- combining constraints on segmentby columns should work
BEGIN;
Expand Down
8 changes: 8 additions & 0 deletions tsl/test/shared/sql/compression_dml.sql
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,14 @@ BEGIN;
SELECT count(*) FROM direct_delete WHERE reading='r2';
ROLLBACK;

-- issue #7644
-- make sure non-btree operators don't delete unrelated batches
BEGIN;
:ANALYZE DELETE FROM direct_delete WHERE reading<>'r2';
-- 2 tuples should still be there
SELECT count(*) FROM direct_delete;
ROLLBACK;

-- combining constraints on segmentby columns should work
BEGIN;
-- should be 1 batches directly deleted
Expand Down

0 comments on commit fb7f931

Please sign in to comment.