Re: Avoid copying MinimalTuple twice in ExecHashSkewTableInsert()

From: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
To: cca5507 <cca5507(at)qq(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Avoid copying MinimalTuple twice in ExecHashSkewTableInsert()
Date: 2026-11-20 08:21:05
Message-ID: EC4AB28B-4079-4549-8EFF-88BE2719AA11@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> On Nov 18, 2026, at 22:56, cca5507 <cca5507(at)qq(dot)com> wrote:
>
> Hi,
>
> We currently copy MinimalTuple twice in ExecHashSkewTableInsert() if the input
> slot is not a MinimalTupleTableSlot. We can avoid this by using
> ExecCopySlotMinimalTupleExtra().
>
> Patch attached.
>
> --
> Regards,
> ChangAo Chen
> <v1-0001-Avoid-copying-MinimalTuple-twice-in-ExecHashSkewT.patch>

The current code does:

* ExecFetchSlotMinimalTuple(slot, &shouldFree)
* if slot->tts_ops->get_minimal_tuple is not NULL, slot->tts_ops->get_minimal_tuple(slot);
* else slot->tts_ops->copy_minimal_tuple(slot, 0);
* hashTuple = (HashJoinTuple) MemoryContextAlloc()
* memcpy(HJTUPLE_MINTUPLE(hashTuple), tuple, tuple->t_len);
* if (shouldFree) heap_free_minimal_tuple(tuple);

The patch does:

* tuple = ExecCopySlotMinimalTupleExtra(slot, HJTUPLE_OVERHEAD);
* slot->tts_ops->copy_minimal_tuple(slot, extra);
* hashTuple = (HashJoinTuple) ((char *) tuple - HJTUPLE_OVERHEAD);

When slot->tts_ops->get_minimal_tuple is not NULL, the old and new code do almost the same amount of work. But when slot->tts_ops->get_minimal_tuple is NULL, this patch saves an allocation, a memcpy(), and a pfree().

So, this patch can improve performance a little bit. Plus, it simplifies the code, so I think it's an improvement.

In summary, v1 looks good to me. I also tried to build and run tests, everything passed.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Kyotaro Horiguchi 2026-11-20 08:22:10 Re: [PATCH v1] Preserve implicit composite casts in ROW deparsing
Previous Message Xuneng Zhou 2026-11-20 08:19:27 Re: [BUG] Take a long time to reach consistent after pg_rewind