Skip to content

Commit

Permalink
Do DELETE instead of TRUNCATE when locks aren't acquired
Browse files Browse the repository at this point in the history
After compression, the uncompressed part of the chunk is truncated.
This requires upgrading the `ExclusiveLock` to an
`AccessExclusiveLock` and hence is sometimes blocked by other other
operations, including reads, on the chunk. This leads to longer
compress times or potential deadlocks.

Instead of this, we fall back to deleting the tuples in the chunk
row-by-row when the upgraded lock isn't immediately acquired.
  • Loading branch information
kpan2034 committed Mar 3, 2025
1 parent b665a4b commit 43e54cd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
10 changes: 9 additions & 1 deletion tsl/src/compression/compression.c
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,15 @@ compress_chunk(Oid in_table, Oid out_table, int insert_options)
if (!ts_guc_enable_delete_after_compression)
{
DEBUG_WAITPOINT("compression_done_before_truncate_uncompressed");
truncate_relation(in_table);
if (ConditionalLockRelation(in_rel, AccessExclusiveLock))
{
truncate_relation(in_table);
}
else
{
/* Instead of waiting, delete rows one-by-one */
delete_relation_rows(in_table);
}
DEBUG_WAITPOINT("compression_done_after_truncate_uncompressed");
}
else
Expand Down
13 changes: 6 additions & 7 deletions tsl/test/isolation/expected/compression_conflicts_iso.out
Original file line number Diff line number Diff line change
Expand Up @@ -3023,7 +3023,12 @@ step CA1:
CASE WHEN compress_chunk(ch) IS NOT NULL THEN true ELSE false END AS compress
FROM show_chunks('ts_device_table') AS ch
ORDER BY ch::text;
<waiting ...>

compress
--------
t
(1 row)

step SA: SELECT * FROM ts_device_table;
time|device|location|value
----+------+--------+-----
Expand All @@ -3041,10 +3046,4 @@ time|device|location|value

step SF:
step SR: ROLLBACK;
step CA1: <... completed>
compress
--------
t
(1 row)

step CAc: COMMIT;

0 comments on commit 43e54cd

Please sign in to comment.