[PATCH] set_byte() with a count argument
[PATCH] set_byte() with a count argument
Hi hackers,
0002 adds a four-argument set_byte():
set_byte(bytes bytea, n integer, newvalue integer, count integer)
setting count consecutive bytes from byte n to newvalue:
SELECT set_byte('\x0000000000'::bytea, 1, 255, 3);
=> \x00ffffff00
Today this needs nested set_byte() calls, each copying the whole
value. The new form does one copy and one memset().
The result keeps the input's length. count < 0 is an error. A range
past the end is an error. count = 0 returns the input unchanged and
allows n = length. Because of that, the index error reports 0..length
when count is 0 and 0..length-1 if non-zero.
0001 is a separate fix. set_byte() never checked newvalue. It just
stored the low byte, so 256 sets \x00 and -1 sets \xff. Both forms now
reject values outside 0..255, as set_bit() does for its bit. I don't
think it should be backpatched.
Docs and tests included and make check passes with each patch applied.
Regards,
-- Sehrope Sarkuni
Founder & CEO | JackDB, Inc. | https://www.jackdb.com/
Re: [PATCH] set_byte() with a count argument
От:
Matthias van de Meent <boekewurm+postgres@gmail.com>
Дата:
On Mon, 21 Sept 2026 at 14:23, Sehrope Sarkuni wrote:
>
> Hi hackers,
>
> 0002 adds a four-argument set_byte():
>
> set_byte(bytes bytea, n integer, newvalue integer, count integer)
>
> setting count consecutive bytes from byte n to newvalue:
>
> SELECT set_byte('\x0000000000'::bytea, 1, 255, 3);
> => \x00ffffff00
>
> Today this needs nested set_byte() calls, each copying the whole
> value. The new form does one copy and one memset().
Doesn't the `overlay(bytea, ...)` function already provide the feature
you're looking for (possibly combined with repeat() for ease of use)?
See https://www.postgresql.org/docs/current/functions-binarystring.html#id-1.5.8.11.5.2.2.6.1.1.1
and/or the ISO SQL spec for its specification.
Kind regards,
Matthias van de Meent
Databricks (https://www.databricks.com)
Re: [PATCH] set_byte() with a count argument
От:
Sehrope Sarkuni <sehrope@jackdb.com>
Дата:
On Mon, Sep 21, 2026 at 8:45 AM Matthias van de Meent
wrote:
> On Mon, 21 Sept 2026 at 14:23, Sehrope Sarkuni wrote:
> > Today this needs nested set_byte() calls, each copying the whole
> > value. The new form does one copy and one memset().
>
> Doesn't the `overlay(bytea, ...)` function already provide the feature
> you're looking for (possibly combined with repeat() for ease of use)?
> See https://www.postgresql.org/docs/current/functions-binarystring.html#id-1.5.8.11.5.2.2.6.1.1.1
> and/or the ISO SQL spec for its specification.
Yes, it's possible, but seems clunky and has the extra intermediate
alloc for the repeat and decode:
SELECT overlay('\xaabbccddeeff'::bytea
placing decode(repeat('00', 3), 'hex')
from 3 for 3);
v.s.
SELECT set_byte('\xaabbccddeeff'::bytea, 2, 0, 3);
Regards,
-- Sehrope Sarkuni
Founder & CEO | JackDB, Inc. | https://www.jackdb.com/
Re: [PATCH] set_byte() with a count argument
От:
Matthias van de Meent <boekewurm+postgres@gmail.com>
Дата:
On Mon, 21 Sept 2026 at 15:17, Sehrope Sarkuni wrote:
>
> On Mon, Sep 21, 2026 at 8:45 AM Matthias van de Meent
> wrote:
> > On Mon, 21 Sept 2026 at 14:23, Sehrope Sarkuni wrote:
> > > Today this needs nested set_byte() calls, each copying the whole
> > > value. The new form does one copy and one memset().
> >
> > Doesn't the `overlay(bytea, ...)` function already provide the feature
> > you're looking for (possibly combined with repeat() for ease of use)?
> > See https://www.postgresql.org/docs/current/functions-binarystring.html#id-1.5.8.11.5.2.2.6.1.1.1
> > and/or the ISO SQL spec for its specification.
>
> Yes, it's possible, but seems clunky and has the extra intermediate
> alloc for the repeat and decode:
Right, but implementing a repeat() for bytea should remove the alloc
and memory scan of decode(), and (IMO) would be a more universally
useful addition.
Do you have a workload in mind that would hit this operation hard
enough that the additional allocs (and function calls) becomes a
significant overhead, or other reasons why overlay's behaviour is
impractical for your workload?
Note, I'm not a huge fan of a 'count' argument in set_byte, as the
function name implies a singular byte being set.
Kind regards,
Matthias van de Meent
Databricks (https://www.databricks.com)
PS, for the reader: Even if it avoids the cost of decode() by using a
direct text-to-bytea binary cast, repeat('\xXX', N)::bytea is not a
good enough solution because that rejects NUL bytes, and won't produce
byteas that are not valid as text (i.e. ones filled with only hex
values 0x80-0xFF).
Re: [PATCH] set_byte() with a count argument
От:
Sehrope Sarkuni <sehrope@jackdb.com>
Дата:
On Tue, Sep 22, 2026 at 5:55 AM Matthias van de Meent wrote: > > On Mon, 21 Sept 2026 at 15:17, Sehrope Sarkuni wrote: > > > > Yes, it's possible, but seems clunky and has the extra intermediate > > alloc for the repeat and decode: > > Right, but implementing a repeat() for bytea should remove the alloc > and memory scan of decode(), and (IMO) would be a more universally > useful addition. I'll take a look at that separately as repeat(...) for bytea is definitely useful. > Do you have a workload in mind that would hit this operation hard > enough that the additional allocs (and function calls) becomes a > significant overhead, or other reasons why overlay's behaviour is > impractical for your workload? More the clunkiness than the allocs. I think the intent is clearer with the count arg. I was reading through some corruption validation patches and there was a lot of set_byte(set_byte(set_byte(...))) in the tests. Some of it was distinct values and overlay(...) was better fit. But a bunch were all the same value, hence the thought of the additional count arg for set_byte(...). > Note, I'm not a huge fan of a 'count' argument in set_byte, as the > function name implies a singular byte being set. I always thought of it like memset(...) so the count arg seems natural. I considered a separate set_bytes(...) but it'd duplicate set_byte(...) with a count of 1, so the optional arg seemed tidier. Regards, -- Sehrope Sarkuni Founder & CEO | JackDB, Inc. | https://www.jackdb.com/