Re: Use virtual tuple slot for Unique node - Mailing list pgsql-hackers

From Ashutosh Bapat
Subject Re: Use virtual tuple slot for Unique node
Date
Msg-id CAExHW5txkhatn+6sCFyni-f-LVSdqKtByT=zj6+7zVHyEnxmaw@mail.gmail.com
Whole thread Raw
In response to Re: Use virtual tuple slot for Unique node  (David Rowley <dgrowleyml@gmail.com>)
Responses Re: Use virtual tuple slot for Unique node
List pgsql-hackers
On Thu, Oct 19, 2023 at 4:26 PM David Rowley <dgrowleyml@gmail.com> wrote:
>
> On Thu, 19 Oct 2023 at 22:29, David Rowley <dgrowleyml@gmail.com> wrote:
> > It's hard to imagine why there would be a slowdown as this query uses
> > a TTSOpsMinimalTuple slot type in the patch and the unpatched version.
>
> I shrunk down your table sizes to 10k rows instead of 1 million rows
> to reduce the CPU cache pressure on the queries.
>
> I ran pgbench for 1 minute on each query and did pg_prewarm on each
> table. Here are the times I got in milliseconds:
>
> Query   master   Master + 0001   compare
> Q1        2.576     1.979                 130.17%
> Q2        9.546     9.941                   96.03%
> Q3        9.069     9.536                   95.10%
> Q4        7.285     7.208                 101.07%
> Q5        7.585     6.904                 109.86%
> Q6     162.253 161.434                100.51%
> Q7       62.507   58.922                106.08%
>
> I also noted down the slot type that nodeUnique.c is using in each of
> the queries:
>
> Q1 TTSOpsVirtual
> Q2 TTSOpsVirtual
> Q3 TTSOpsVirtual
> Q4 TTSOpsMinimalTuple
> Q5 TTSOpsVirtual
> Q6 TTSOpsMinimalTuple
> Q7 TTSOpsMinimalTuple
>
> So, I'm not really expecting Q4, Q6 or Q7 to change much. However, Q7
> does seem to be above noise level faster and I'm not sure why.

I ran my experiments again. It seems on my machine the execution times
do vary a bit. I ran EXPLAIN ANALYZE on the query 5 times and took
average of execution times. I did this three times. For each run the
standard deviation was within 2%. Here are the numbers
master: 13548.33, 13878.88, 14572.52
master + 0001: 13734.58, 14193.83, 14574.73

So for me, I would say, this particular query performs the same with
or without patch.

>
> We can see that Q2 and Q3 become a bit slower.  This makes sense as
> tts_virtual_materialize() is quite a bit more complex than
> heap_copy_minimal_tuple() which is a simple palloc/memcpy.
>

If the source slot is a materialized virtual slot,
tts_virtual_copyslot() could perform a memcpy of the materialized data
itself rather than materialising from datums. That might be more
efficient.

> We'd likely see Q2 and Q3 do better with the patched version if there
> were more duplicates as there'd be less tuple deforming going on
> because of the virtual slots.
>
> Overall, the patched version is 5.55% faster than master.  However,
> it's pretty hard to say if we should do this or not. Q3 has a mix of
> varlena and byval types and that came out slower with the patched
> version.

Theoretically using the same slot type is supposed to be faster. We
use same slot types for input and output in other places where as
well. May be we should fix the above said inefficiency in
tt_virtual_copyslot()?

--
Best Wishes,
Ashutosh Bapat



pgsql-hackers by date:

Previous
From: Aleksander Alekseev
Date:
Subject: Re: [patch] pg_basebackup: mention that spread checkpoints are the default in --help
Next
From: Ashutosh Bapat
Date:
Subject: Re: BRIN minmax multi - incorrect distance for infinite timestamp/date