[PATCH] heap_insert() and heap_update() optimization

Started by Andrey Klychkovalmost 8 years ago4 messageshackers
Beta feature

Hackorum builds and tests every patch posted to the lists, not only commitfest submissions. This is Hackorum's own CI rather than the PostgreSQL project's, and it is still under testing - please report anything that looks wrong.

won't retrysuccessCI history

You can run a PostgreSQL built from this patch straight from Docker, with no checkout and no build:

docker run --rm -p 5432:5432 ghcr.io/hackorum-dev/postgres-patch:t39512
psql -h localhost -U postgres

Built from patchset v1 (message #1), July 28, 2026 at 05:09 AM.

Every patchset is also pushed to a branch of our PostgreSQL fork, so you can check out the same tree CI built. Without a PostgreSQL checkout:

git clone --branch t39512_1 https://github.com/hackorum-dev/postgres.git

In a checkout you already have, add the fork once:

git remote add hackorum https://github.com/hackorum-dev/postgres.git

then, for this patchset and every later one:

git fetch hackorum t39512_1 && git checkout t39512_1

Patchset v1 (message #1) is on t39512_1

Jump to latest
#1Andrey Klychkov
aaklychkov@mail.ru

Hello, hackers
I suggest the small attached patch that gives a bit of heap_insert() and heap_update() optimization
by reducing calls of BufferGetPage(buffer) into them.
I measured call time of these:
heap_insert(): avg origin 13394 ns, avg patched 12685 ns; perf increases +5.59%
heap_update(): avg origin 15728 ns, avg patched 13936 ns; perf increases +11.39%
This can be notable when there are handling many rows.
--
Regards,
Andrew K.

Attachments:

t39512_1
v1-00-heap_insert-and-heap_update_optimized.patchapplication/x-patch; name="=?UTF-8?B?djEtMDAtaGVhcF9pbnNlcnQtYW5kLWhlYXBfdXBkYXRlX29wdGltaXplZC5w?= =?UTF-8?B?YXRjaA==?="Download+14-9
#2Andres Freund
andres@anarazel.de
In reply to: Andrey Klychkov (#1)
Re: [PATCH] heap_insert() and heap_update() optimization

Hi,

On 2018-10-16 11:28:17 +0300, Andrey Klychkov wrote:

I suggest the small attached patch that gives a bit of heap_insert() and heap_update() optimization
by reducing calls of BufferGetPage(buffer) into them.
I measured call time of these:
heap_insert(): avg origin 13394 ns, avg patched 12685 ns; perf increases +5.59%
heap_update(): avg origin 15728 ns, avg patched 13936 ns; perf increases +11.39%
This can be notable when there are handling many rows.

Interesting. That's with an optimized build, or an assertion build?

Wonder what precisely prevents the optimizer to recognize
BufferGetPage() with a constant argument will always be the same
result. I assume it's that it doesn't recognize that BufferBlocks can't
change across other function calls? Might also be the pointer math, or
the if block...

Wonder if we could force the compiler's hand by making BufferGetPage an
inline function and decorating it with __attribute__((pure)) or such.

I see little reason to not apply what you have here, but there's a lot
of other places that access buffers...

Greetings,

Andres Freund

#3Andrey Klychkov
aaklychkov@mail.ru
In reply to: Andres Freund (#2)
Re[2]: [PATCH] heap_insert() and heap_update() optimization

 Interesting. That's with an optimized build, or an assertion build?

Hello,
That was an optimized build.

However I've just done some extra time tests and didn't notice so significant difference as early.
Even more - avg origin 1272, avg patched 1303.

Maybe there was the autovacuum / analyze / checkpoint or something else that could influence on the yesterday tests.

Thanks a lot for explanation!

Вторник, 16 октября 2018, 22:57 +03:00 от Andres Freund <andres@anarazel.de>:

Hi,

On 2018-10-16 11:28:17 +0300, Andrey Klychkov wrote:

I suggest the small attached patch that gives a bit of heap_insert() and heap_update() optimization
by reducing calls of BufferGetPage(buffer) into them.
I measured call time of these:
heap_insert(): avg origin 13394 ns, avg patched 12685 ns; perf increases +5.59%
heap_update(): avg origin 15728 ns, avg patched 13936 ns; perf increases +11.39%
This can be notable when there are handling many rows.

Interesting. That's with an optimized build, or an assertion build?

Wonder what precisely prevents the optimizer to recognize
BufferGetPage() with a constant argument will always be the same
result. I assume it's that it doesn't recognize that BufferBlocks can't
change across other function calls? Might also be the pointer math, or
the if block...

Wonder if we could force the compiler's hand by making BufferGetPage an
inline function and decorating it with __attribute__((pure)) or such.

I see little reason to not apply what you have here, but there's a lot
of other places that access buffers...

Greetings,

Andres Freund

--
Regards,
Andrey Klychkov

#4Andres Freund
andres@anarazel.de
In reply to: Andrey Klychkov (#3)
Re: [PATCH] heap_insert() and heap_update() optimization

Hi,

On 2018-10-17 09:48:19 +0300, Andrey Klychkov wrote:

�Interesting. That's with an optimized build, or an assertion build?

Hello,
That was an optimized build.

However I've just done some extra time tests and didn't notice so significant difference as early.
Even more - avg origin�1272, avg patched�1303.

Maybe there was the autovacuum / analyze / checkpoint or something else that could influence on the yesterday tests.

Probably worth looking at the generated code. I can see some difference,
but what you measured seemed pretty large.

Greetings,

Andres Freund