Re: Making type Datum be 8 bytes everywhere

Ranier Vilela <ranier.vf@gmail.com>

From: Ranier Vilela <ranier.vf@gmail.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Tomas Vondra <tomas@vondra.me>, Peter Eisentraut <peter@eisentraut.org>, Joe Conway <mail@joeconway.com>, Michael Paquier <michael@paquier.xyz>, Andres Freund <andres@anarazel.de>, pgsql-hackers@lists.postgresql.org, Robert Haas <robertmhaas@gmail.com>
Date: 2025-09-11T16:15:31Z
Lists: pgsql-hackers
Em qui., 11 de set. de 2025 às 12:36, Tom Lane <tgl@sss.pgh.pa.us> escreveu:

> Ranier Vilela <ranier.vf@gmail.com> writes:
> > Em qua., 10 de set. de 2025 às 17:35, Tom Lane <tgl@sss.pgh.pa.us>
> escreveu:
> >> This is silently assuming that sizeof(SortItem) is a multiple of
> >> alignof(Datum), which on a 32-bit-pointer platform is not true
> >> any longer.  We ought to MAXALIGN the two occurrences of
> >> data->numrows * sizeof(SortItem).
>
> > We possibly have two more instances?
>
> > 1. Function ndistinct_for_combination
> (src/backend/statistics/mvdistinct.c)
> > - items = (SortItem *) palloc(numrows * sizeof(SortItem));
> > + items = (SortItem *) palloc(MAXALIGN(numrows * sizeof(SortItem)));
>
> > 2. Function build_distinct_groups (src/backend/statistics/mcv.c)
> > - SortItem   *groups = (SortItem *) palloc(ngroups * sizeof(SortItem));
> > + SortItem   *groups = (SortItem *) palloc(MAXALIGN(ngroups *
> > sizeof(SortItem)));
>
> Neither of those have any hazard, because they are not trying to
> allocate multiple arrays using address arithmetic.  The part of
> build_sorted_items that was actually problematic was doing
>
>         ptr += data->numrows * sizeof(SortItem);
>
> and then assuming that the result was suitably aligned to be
> cast to Datum*.
>
Thanks Tom, for double checking.

best regards,
Ranier Vilela

Commits

Same data as JSON: GET /api/v1/messages/:b64id/commits the thread's linked commits as JSON, with link sources. API reference →
  1. Avoid faulty alignment of Datums in build_sorted_items().

  2. Grab the low-hanging fruit from forcing USE_FLOAT8_BYVAL to true.

  3. Grab the low-hanging fruit from forcing sizeof(Datum) to 8.

  4. Make type Datum be 8 bytes wide everywhere.

  5. Mop-up for Datum conversion cleanups.