From: Jim Nasby <jim.nasby@gmail.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
To: Ron Johnson <ronljohnsonjr@gmail.com>
Cc: Yongtao Huang <yongtaoh2022@gmail.com>
Cc: pgsql-general@postgresql.org
Subject: Re: Why scan all columns when we select distinct c1?
Date: Tue, 16 Jan 2024 13:48:33 -0600
Message-ID: <aef36ed6-87ca-4cba-b6b8-7ce960e9a15a@gmail.com> (raw)
In-Reply-To: <1239078.1705247163@sss.pgh.pa.us>
References: <CAOe1Go3uHkTrgBMm1O2pVWQAV09ZxVW_vaKM1GeAkAu_RByt2A@mail.gmail.com>
<CANzqJaDG4guxWQX=OpxR_BOTO2bi2pDCKU1ppNj2znczJNSv8g@mail.gmail.com>
<1239078.1705247163@sss.pgh.pa.us>
On 1/14/24 9:46 AM, Tom Lane wrote:
> Ron Johnson<ronljohnsonjr@gmail.com> writes:
>> You can't scan just one column of a row-oriented table.
Technically you kinda can, depending on your definition of "scan".
>> The real question is why it mentions c2.
> The planner did that so that the SeqScan step doesn't have to
> perform a projection: it can just return (a pointer to)
> the physical tuple it found in the table, without doing extra
> work to form a tuple containing only c1.
The piece of info that's not mentioned here is how tuples (rows) are
actually processed to extract individual datums (columns). The full
details are in heap_deform_tuple() in backend/access/common/heaptuple.c,
but the general gist is that (ignoring nulls) to read a tuple the code
has to go datum by datum, computing the size of each datum to determine
the physical location of the *next* datum. So if you want the 3rd datum
in a tuple, you need to calculate the size of the 1st datum to see where
the 2nd datum lives, and then compute the size of the 2nd datum to see
where the 3rd one lives.
In this example, if c1 is literally the first column in the table, then
heap_deform_tuple is free to ignore everything else in the tuple, so
long as the code calling heap_deform_tuple() knows to ask for only 1 datum.
If all that sounds kinda expensive and tedious: you're right, it is, and
it's why deforming tuples is generally done as late as possible. Based
on what Tom's saying, in 9.6+ the HashAggregate code would be calling
heap_deform_tuple(), and I'd expect it to only be retrieving c1. Without
going through all the code, I think what's happening in 9.4 is the
projection ends up calling heap_deform_tuple instead of the HashAgg
code. It's still only grabbing c1; it's just doing it sooner rather than
later. In this particular case I don't think it'd make much difference,
but in more complicated queries it could certainly have a noticable effect.
Either way, the explain output is kinda confusing. IMO it'd be more
accurate if it said something like "Output: t1". And Ron's main point
that you're going to be reading an entire row of t1 from the OS is also
true.
BTW, there's another place where the code waits as long as possible to
access actual data in the hopes of avoiding needless work and that's
values that have been TOASTed. heap_deform_tuple() doesn't actually need
to de-toast data, so it will simply return a Datum that is a "pointer"
(not a C pointer) to the toasted data. That will only be detoasted if
something actually needs the actual data. In some cases that be a big
performance win.
--
Jim Nasby, Data Architect, Austin TX
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: jim.nasby@gmail.com, tgl@sss.pgh.pa.us, ronljohnsonjr@gmail.com, yongtaoh2022@gmail.com
Subject: Re: Why scan all columns when we select distinct c1?
In-Reply-To: <aef36ed6-87ca-4cba-b6b8-7ce960e9a15a@gmail.com>
References: <CAOe1Go3uHkTrgBMm1O2pVWQAV09ZxVW_vaKM1GeAkAu_RByt2A@mail.gmail.com> <CANzqJaDG4guxWQX=OpxR_BOTO2bi2pDCKU1ppNj2znczJNSv8g@mail.gmail.com> <1239078.1705247163@sss.pgh.pa.us> <aef36ed6-87ca-4cba-b6b8-7ce960e9a15a@gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox