Fully documenting the design of nbtree row comparison scan keys
Peter Geoghegan <pg@bowt.ie>
From: Peter Geoghegan <pg@bowt.ie>
To: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Date: 2025-10-30T21:34:29Z
Lists: pgsql-hackers
Commits
Same data as JSON:
GET /api/v1/messages/:b64id/commits
the thread's linked commits as JSON, with link sources.
API reference →
-
Document nbtree row comparison design.
- b8f1c62807a5 19 (unreleased) landed
- 6c3b1df878a6 18.1 landed
-
Make row compares robust during nbtree array scans.
- bd3f59fdb717 19 (unreleased) cited
-
Fix incorrect handling of NULL index entries in indexed ROW() comparisons.
- a298a1e06fb0 9.6.0 cited
Attachments
- v1-0001-Document-nbtree-row-comparison-design.patch (application/octet-stream) patch v1-0001
nbtree row comparison scan keys consist of one header key (which appears in so->keyData[]), and 2 or more subkeys (which _bt_check_rowcompare accesses by following a pointer stored in the header key). This design makes sense to me, but it's not at all obvious why the scan keys are structured in this way. Attached patch adds comments about all this above _bt_check_rowcompare. It also adds a couple of new documenting assertions. This clears up why don't we just include the subkeys in the main so->keyData[] array instead [1], and why it's useful to sometimes treat a row compare inequality as if it was a similar scalar inequality (on the most significant row member's index column). I didn't understand every nuance of row compare inequalities myself until quite recently. The rules with NULLs are particularly tricky. It seems worthwhile to clear things up now in large part due to the recent addition of code in places like _bt_advance_array_keys -- code that wants to to treat row compare keys as if they were just a simple scalar inequality on the row compare's most significant column. That general behavior isn't new (e.g., _bt_first has long ignored row compare scan key markings when deducing a NOT NULL constraint), but it's not easy to see why it's correct. I'd like to commit this on both the master branch and the 18 branch in the next couple of days. Seems worth keeping them in sync for this. [1] https://www.postgresql.org/message-id/24134.1137366192%40sss.pgh.pa.us -- Peter Geoghegan