Small optimization set tuple block/tableOid once

Started by Ranier Vilelaover 1 year ago3 messageshackers

Thread notes

1 note#performance#minor#enhancement
hackorum-bot#performance#minor#enhancement
hackorum-bot
1 day ago

@global #performance #minor #enhancement

#performance#minor#enhancement
Jump to latest
#1Ranier Vilela
ranier.vf@gmail.com

Hi.

Inspired by [1]Re: AIO writes vs hint bits vs checksums </messages/by-id/lxzj26ga6ippdeunz6kuncectr5gfuugmm2ry22qu6hcx6oid6@lzx3sjsqhmt6>
There is an opportunity for optimization according to the commit:
2904324 <http://2904324a88f672b2ecc22735279c16d6e1ee178c>
" Due to splitting the block id into two 16 bit integers, BlockIdSet() is
more expensive than one might think. Doing it once per returned tuple shows
up as a small but reliably reproducible cost. It's simple enough to set the
block number just once per block in pagemode, so do so."

By moving the invariants out of the loop, it really is an improvement.

Then the following sources can be optimized:
contrib/pg_visibility/pg_visibility.c
contrib/pgstattuple/pgstatapprox.c
src/backend/access/heap/heapam.c
src/backend/access/heap/heapam_handler.c
src/backend/access/heap/pruneheap.c
src/backend/access/heap/vacuumlazy.c
src/backend/commands/dbcommands.c

Attached all patchs.

best regards,
Ranier Vilela

[1]: Re: AIO writes vs hint bits vs checksums </messages/by-id/lxzj26ga6ippdeunz6kuncectr5gfuugmm2ry22qu6hcx6oid6@lzx3sjsqhmt6>
</messages/by-id/lxzj26ga6ippdeunz6kuncectr5gfuugmm2ry22qu6hcx6oid6@lzx3sjsqhmt6>

Attachments:

pg_visibility_set_tuple_block_once.patchapplication/octet-stream; name=pg_visibility_set_tuple_block_once.patchDownload+7-4
pgstattuple_set_tuple_block_once.patchapplication/octet-stream; name=pgstattuple_set_tuple_block_once.patchDownload+6-4
heapam_set_tuple_block_once.patchapplication/octet-stream; name=heapam_set_tuple_block_once.patchDownload+16-6
heapam_handler_set_tuple_block_once.patchapplication/octet-stream; name=heapam_handler_set_tuple_block_once.patchDownload+24-12
pruneheap_set_tuple_block_once.patchapplication/octet-stream; name=pruneheap_set_tuple_block_once.patchDownload+4-1
vacuumlazy_set_tuple_block_once.patchapplication/octet-stream; name=vacuumlazy_set_tuple_block_once.patchDownload+12-7
dbcommands_set_tuple_block_once.patchapplication/octet-stream; name=dbcommands_set_tuple_block_once.patchDownload+5-2
#2Ranier Vilela
ranier.vf@gmail.com
In reply to: Ranier Vilela (#1)
Re: Small optimization set tuple block/tableOid once

rebased heapam.c and heapam_handler.c

best regards,
Ranier Vilela

Attachments:

t51351_2
v1-heapam_set_tuple_block_once.patchapplication/octet-stream; name=v1-heapam_set_tuple_block_once.patchDownload+16-6
v1-heapam_handler_set_tuple_block_once.patchapplication/octet-stream; name=v1-heapam_handler_set_tuple_block_once.patchDownload+24-12
#3David Rowley
dgrowleyml@gmail.com
In reply to: Ranier Vilela (#2)
Re: Small optimization set tuple block/tableOid once

On Thu, 3 Apr 2025 at 23:09, Ranier Vilela <ranier.vf@gmail.com> wrote:

rebased heapam.c and heapam_handler.c

This doesn't work. The tids are not being set in all cases.

patched:
select tableoid,ctid,oid from pg_class TABLESAMPLE BERNOULLI (100);
tableoid | ctid | oid
----------+---------+-------
1259 | (0,0) | 16409
1259 | (0,0) | 16403
1259 | (0,0) | 16412
1259 | (0,0) | 16407
1259 | (0,0) | 16415
1259 | (0,0) | 16418
1259 | (0,0) | 16434
...

master:
tableoid | ctid | oid
----------+---------+-------
1259 | (0,10) | 16409
1259 | (0,11) | 16403
1259 | (0,12) | 16412
1259 | (0,13) | 16407
1259 | (0,15) | 16415
1259 | (0,16) | 16418
1259 | (0,17) | 16434
...

David