Skip to content

Commit 7ac1e2e

Browse files
author
Nikita Glukhov
committed
Optimize recompression in ATRewriteTable()
1 parent a3d9903 commit 7ac1e2e

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

src/backend/access/heap/heapam.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2571,7 +2571,7 @@ heap_insert(Relation relation, HeapTuple tup, TupleDesc tupdesc,
25712571
return HeapTupleGetOid(tup);
25722572
}
25732573

2574-
static inline bool
2574+
bool
25752575
tuple_attr_compression_equals(TupleDesc td1, TupleDesc td2, AttrNumber attidx)
25762576
{
25772577
if (td1->attrs[attidx]->attcompression !=

src/backend/commands/tablecmds.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4554,6 +4554,7 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap, LOCKMODE lockmode)
45544554
List *dropped_attrs = NIL;
45554555
ListCell *lc;
45564556
Snapshot snapshot;
4557+
bool *recompress;
45574558

45584559
if (newrel)
45594560
ereport(DEBUG1,
@@ -4602,6 +4603,22 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap, LOCKMODE lockmode)
46024603
dropped_attrs = lappend_int(dropped_attrs, i);
46034604
}
46044605

4606+
if (tab->newvals != NIL)
4607+
recompress = NULL; /* recompress all */
4608+
else
4609+
{
4610+
int natts = Min(oldTupDesc->natts, newTupDesc->natts);
4611+
4612+
recompress = palloc0(sizeof(bool) * oldTupDesc->natts);
4613+
4614+
for (i = 0; i < natts; i++)
4615+
if (!tuple_attr_compression_equals(oldTupDesc, newTupDesc, i))
4616+
recompress[i] = true;
4617+
4618+
foreach(lc, dropped_attrs)
4619+
recompress[lfirst_int(lc)] = false;
4620+
}
4621+
46054622
/*
46064623
* Scan through the rows, generating a new row if needed and then
46074624
* checking all the constraints.
@@ -4622,7 +4639,8 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap, LOCKMODE lockmode)
46224639
Oid tupOid = InvalidOid;
46234640

46244641
/* Extract data from old tuple */
4625-
heap_deform_tuple(tuple, oldTupDesc, values, isnull);
4642+
heap_deform_tuple_decompress(tuple, oldTupDesc, values, isnull,
4643+
recompress);
46264644
if (oldTupDesc->tdhasoid)
46274645
tupOid = HeapTupleGetOid(tuple);
46284646

@@ -4650,7 +4668,8 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap, LOCKMODE lockmode)
46504668
* Form the new tuple. Note that we don't explicitly pfree it,
46514669
* since the per-tuple memory context will be reset shortly.
46524670
*/
4653-
tuple = heap_form_tuple(newTupDesc, values, isnull);
4671+
tuple = heap_form_tuple_compress(newTupDesc, values, isnull,
4672+
recompress);
46544673

46554674
/* Preserve OID, if any */
46564675
if (newTupDesc->tdhasoid)

src/include/access/heapam.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ extern void simple_heap_update(Relation relation, ItemPointer otid,
181181
extern void heap_sync(Relation relation);
182182
extern void heap_update_snapshot(HeapScanDesc scan, Snapshot snapshot);
183183

184+
extern bool tuple_attr_compression_equals(TupleDesc td1, TupleDesc td2,
185+
AttrNumber attidx);
186+
184187
/* in heap/pruneheap.c */
185188
extern void heap_page_prune_opt(Relation relation, Buffer buffer);
186189
extern int heap_page_prune(Relation relation, Buffer buffer,

0 commit comments

Comments
 (0)