From: | Peter Geoghegan <pg(at)bowt(dot)ie> |
---|---|
To: | PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org> |
Subject: | Saving stack space in nbtree's _bt_first function |
Date: | 2025-07-07 00:22:21 |
Message-ID: | CAH2-Wzm=1kJMSZhhTLoM5BPbwQNWxUj-ynOEh=89ptDZAVgauw@mail.gmail.com |
Views: | Whole Thread | Raw Message | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-hackers |
nbtree's _bt_first function uses arrays on the stack to store the scan
keys that will be used for initial positioning purposes. We could use
dynamic allocation here instead, but experience has shown that that
would cause performance issues -- particularly during nestloop joins
with an inner index scan. The amount of stack space used by _bt_first
does still seem kind of excessive, though.
Perhaps most notably, "BTScanInsertData inskey" takes up 2328 bytes.
There isn't much we can do about that, unless we're willing to make
_bt_first more complicated. The "inskey" variable is so large because
it ends with an array of INDEX_MAX_KEYS-many (generally 32)
ScanKeyData structs -- these structs are themselves fairly large
struct (they're 72 bytes each).
However, there's no reason why "ScanKeyData
notnullkeys[INDEX_MAX_KEYS]" needs to be an array at all. In practice,
_bt_first will only need a single temp notnullkeys ScanKeyData, since
there can never be more than a single deduced NOT NULL constraint used
within our final insertion scan key.
Attached patch shows how this could work. It saves us a little over 2
KiB of stack space, which seems worthwhile given that the patch is so
simple.
I'll submit this patch to the next open commitfest for Postgres 19.
--
Peter Geoghegan
Attachment | Content-Type | Size |
---|---|---|
v1-0001-nbtree-Use-only-one-notnullkey-ScanKeyData.patch | application/octet-stream | 1.3 KB |
From | Date | Subject | |
---|---|---|---|
Next Message | Michael Paquier | 2025-07-07 02:06:06 | Re: [PATCH] Add support for displaying database service in psql prompt |
Previous Message | Michael Paquier | 2025-07-07 00:20:05 | Re: Fix deprecation warning with libxml2 2.14 in contrib/xml2/ |