Re: Beautify read stream "per buffer data" APIs

Started by Nazir Bilal Yavuz8 months ago4 messageshackers

Thread notes

1 note#refactor
hackorum-bot#refactor
hackorum-bot
2 days ago

@global #refactor

#refactor
Jump to latest
#1Nazir Bilal Yavuz
byavuz81@gmail.com

Hi,

The discussion of this $subject is actually started in the 'Confine
vacuum skip logic to lazy_scan_skip' [1]/messages/by-id/CA+hUKG+SWMtu9D1eevnbdzf=vurfuDjdFVM5WnX28Fxp-H3mYg@mail.gmail.com thread and it already has a
commitfest entry [2]https://commitfest.postgresql.org/patch/5617/. I wanted to create seperate thread to make
discussion easier.

Latest patches need rebase, so I took patches in the [3]/messages/by-id/CA+hUKGLa7ba7USyT+JR7uRiawWeCVJ96wyRsoEXk7r2gngPv=A@mail.gmail.com; rebased them
on top of the current master. Also there were new read_stream_* calls
so I updated them too.

0001 LGTM and one comment about 0002:

The read_stream_put_value() macro doesn't accept literal constant
values, we need to pass a variable to it. Otherwise, the compilation
fails with:

```
../../postgres/src/include/storage/read_stream.h:169:36: error: lvalue
required as unary ‘&’ operand
169 | memcpy((per_buffer_data), &(value), sizeof(value)))
| ^
../../postgres/src/backend/access/heap/vacuumlazy.c:1703:17: note: in
expansion of macro ‘read_stream_put_value’
1703 | read_stream_put_value(stream, per_buffer_data, false);
```

If that is not intentional, I think it would be better if we can
convert read_stream_put_value() to a way that it accepts rvalues.

[1]: /messages/by-id/CA+hUKG+SWMtu9D1eevnbdzf=vurfuDjdFVM5WnX28Fxp-H3mYg@mail.gmail.com
[2]: https://commitfest.postgresql.org/patch/5617/
[3]: /messages/by-id/CA+hUKGLa7ba7USyT+JR7uRiawWeCVJ96wyRsoEXk7r2gngPv=A@mail.gmail.com

--
Regards,
Nazir Bilal Yavuz
Microsoft

Attachments:

v3-0001-Improve-API-for-retrieving-data-from-read-streams.patchtext/x-patch; charset=US-ASCII; name=v3-0001-Improve-API-for-retrieving-data-from-read-streams.patchDownload+99-35
v3-0002-Improve-API-for-storing-data-in-read-streams.patchtext/x-patch; charset=US-ASCII; name=v3-0002-Improve-API-for-storing-data-in-read-streams.patchDownload+17-11
#2Melanie Plageman
melanieplageman@gmail.com
In reply to: Nazir Bilal Yavuz (#1)

Thanks for rebasing, putting this together, and extracting from that
other length thread!

On Fri, Mar 27, 2026 at 8:02 AM Nazir Bilal Yavuz <byavuz81@gmail.com> wrote:

The read_stream_put_value() macro doesn't accept literal constant
values, we need to pass a variable to it. Otherwise, the compilation
fails with:

```
../../postgres/src/include/storage/read_stream.h:169:36: error: lvalue
required as unary ‘&’ operand
169 | memcpy((per_buffer_data), &(value), sizeof(value)))
| ^
../../postgres/src/backend/access/heap/vacuumlazy.c:1703:17: note: in
expansion of macro ‘read_stream_put_value’
1703 | read_stream_put_value(stream, per_buffer_data, false);
```

If that is not intentional, I think it would be better if we can
convert read_stream_put_value() to a way that it accepts rvalues.

I agree this would be much better from an API usability standpoint.
I'm not sure we can do this in a way people would find acceptable in
Postgres code, though. Apparently this works:

read_stream_put_value(stream, per_buffer_data, (bool){false});

but it is a little weird.

As we discussed off-list, I think the BitmapHeapScanNextBlock()
read_stream_get_buffer_and_pointer() transition should be moved from
0002 to 0001 for clarity.

I think this is unequivocally an improvement in the API. But it
probably needs more time to bake now that you've started a new thread.
It only helps in development, so we can commit it early in 20 and
still get much of the same benefit. External users would have to wait.
But it is probably better to wait than to have an API in 19 that
changes again in 20.

I don't have strong objections if Thomas wants to commit them, though.

- Melanie

#3Nazir Bilal Yavuz
byavuz81@gmail.com
In reply to: Melanie Plageman (#2)

Hi,

Thank you for looking into this!

On Tue, 31 Mar 2026 at 18:47, Melanie Plageman
<melanieplageman@gmail.com> wrote:

Thanks for rebasing, putting this together, and extracting from that
other length thread!

On Fri, Mar 27, 2026 at 8:02 AM Nazir Bilal Yavuz <byavuz81@gmail.com> wrote:

The read_stream_put_value() macro doesn't accept literal constant
values, we need to pass a variable to it. Otherwise, the compilation
fails with:

```
../../postgres/src/include/storage/read_stream.h:169:36: error: lvalue
required as unary ‘&’ operand
169 | memcpy((per_buffer_data), &(value), sizeof(value)))
| ^
../../postgres/src/backend/access/heap/vacuumlazy.c:1703:17: note: in
expansion of macro ‘read_stream_put_value’
1703 | read_stream_put_value(stream, per_buffer_data, false);
```

If that is not intentional, I think it would be better if we can
convert read_stream_put_value() to a way that it accepts rvalues.

I agree this would be much better from an API usability standpoint.
I'm not sure we can do this in a way people would find acceptable in
Postgres code, though. Apparently this works:

read_stream_put_value(stream, per_buffer_data, (bool){false});

but it is a little weird.

Yes, I don't recall seeing this usage elsewhere in the Postgres code.

As we discussed off-list, I think the BitmapHeapScanNextBlock()
read_stream_get_buffer_and_pointer() transition should be moved from
0002 to 0001 for clarity.

Fixed.

I think this is unequivocally an improvement in the API. But it
probably needs more time to bake now that you've started a new thread.
It only helps in development, so we can commit it early in 20 and
still get much of the same benefit. External users would have to wait.
But it is probably better to wait than to have an API in 19 that
changes again in 20.

I agree. These changes improve the type safety of read_stream_* calls,
but existing read stream users are not currently experiencing issues.

--
Regards,
Nazir Bilal Yavuz
Microsoft

Attachments:

t139146_3
v4-0001-Improve-API-for-retrieving-data-from-read-streams.patchtext/x-patch; charset=US-ASCII; name=v4-0001-Improve-API-for-retrieving-data-from-read-streams.patchDownload+101-42
v4-0002-Improve-API-for-storing-data-in-read-streams.patchtext/x-patch; charset=US-ASCII; name=v4-0002-Improve-API-for-storing-data-in-read-streams.patchDownload+15-4
#4solai v
solai.cdac@gmail.com
In reply to: Nazir Bilal Yavuz (#3)

Hi,

I tested both patches on the current master.
The patches applied cleanly, and I reviewed the API changes in
read_stream.h along with the updated call sites, especially in
vacuumlazy. c.
The helper APIs make the code easier to read and reduce the direct
void handling and manual casts in callers.
I also tried reproducing the issue mentioned by Nazir about
read_stream_put_value() not accepting literal constants/rvalues.
Replacing:
read_stream_put_value(stream, per_buffer_data, temp);
with:
read_stream_put_value(stream, per_buffer_data, false);
still results in the following compilation error:
error: lvalue required as unary '&' operand
So the limitation discussed in the thread is still reproducible from my testing.
Other than that, the cleanup and API refactoring look good to me overall.

Regards,
Solai