use indnkeyatts not indnatts in loops that read rd_indcollation

Started by jian heabout 2 months ago3 messageshackers

Thread notes

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

@global #bug

#bug
Jump to latest
#1jian he
jian.universality@gmail.com

Hi

While reivew /messages/by-id/CAGRkXqR7VXacmRP41UBaM5u4aseeCH5ECVmQgn_YS=L7=xdhTw@mail.gmail.com
I accidentally made Claude discover this issue. (At that time, I was
asking Claude about a collation-related issue.)

In RelationInitIndexAccessInfo, we have
```
relation->rd_indcollation = (Oid *)
MemoryContextAllocZero(indexcxt, indnkeyatts * sizeof(Oid));
```
rd_indcollation is corresponds to indnkeyatts, *not* indnatts.
Therefore any rd_indcollation related places loop using indnatts if
is there is wrong.
For example, below infer_collation_opclass_match is wrong.
```
for (natt = 1; natt <= idxRel->rd_att->natts; natt++)
```

Some index type does not support INCLUDE index, but to be
future-proof, if somewhere rd_indcollation related, loop should using
indnkeyatts.

--
jian
https://www.enterprisedb.com/

Attachments:

t254123_1
v1-0001-using-indnkeyatts-not-using-indnatts-in-some-loops.patchtext/x-patch; charset=US-ASCII; name=v1-0001-using-indnkeyatts-not-using-indnatts-in-some-loops.patchDownload+10-9
#2Matthias van de Meent
boekewurm+postgres@gmail.com
In reply to: jian he (#1)
Re: use indnkeyatts not indnatts in loops that read rd_indcollation

On Fri, 9 Oct 2026 at 10:41, jian he <jian.universality@gmail.com> wrote:

Hi

While reivew /messages/by-id/CAGRkXqR7VXacmRP41UBaM5u4aseeCH5ECVmQgn_YS=L7=xdhTw@mail.gmail.com
I accidentally made Claude discover this issue. (At that time, I was
asking Claude about a collation-related issue.)

In RelationInitIndexAccessInfo, we have
```
relation->rd_indcollation = (Oid *)
MemoryContextAllocZero(indexcxt, indnkeyatts * sizeof(Oid));
```
rd_indcollation is corresponds to indnkeyatts, *not* indnatts.
Therefore any rd_indcollation related places loop using indnatts if
is there is wrong.
For example, below infer_collation_opclass_match is wrong.
```
for (natt = 1; natt <= idxRel->rd_att->natts; natt++)
```

Some index type does not support INCLUDE index, but to be
future-proof, if somewhere rd_indcollation related, loop should using
indnkeyatts.

I think the changes to BRIN are meaningless churn; it can never
support INCLUDE columns.

GIN could eventually support amgettuple again (which was removed way
back when the pending list was introduced, with ff301d6e690b), and
with even more effort and community goodwill might eventually get
INCLUDE support, so changing its code is not the worst choice, but
feels a lot like it's just churn to get it in line with other AMs.

Apart from that commentary, LGTM.

Kind regards,

Matthias van de Meent
Databricks (https://www.databricks.com)

PS. Note that relcache.c's write_relcache_init_file() *also* uses
natts, not nkeyatts, for the length of these nkeyatts-sized arrays.
My patch at [0]https://commitfest.postgresql.org/patch/7237/ includes a fix for that.
[0]: https://commitfest.postgresql.org/patch/7237/

#3Alvaro Herrera
alvherre@kurilemu.de
In reply to: Matthias van de Meent (#2)
Re: use indnkeyatts not indnatts in loops that read rd_indcollation

On 2026-Oct-09, Matthias van de Meent wrote:

On Fri, 9 Oct 2026 at 10:41, jian he <jian.universality@gmail.com> wrote:

In RelationInitIndexAccessInfo, we have
```
relation->rd_indcollation = (Oid *)
MemoryContextAllocZero(indexcxt, indnkeyatts * sizeof(Oid));
```
rd_indcollation is corresponds to indnkeyatts, *not* indnatts.
Therefore any rd_indcollation related places loop using indnatts if
is there is wrong.
For example, below infer_collation_opclass_match is wrong.
```
for (natt = 1; natt <= idxRel->rd_att->natts; natt++)
```

AFAICT this is correct. I have pushed this change to all branches.

I think the changes to BRIN are meaningless churn; it can never
support INCLUDE columns.

GIN could eventually support amgettuple again (which was removed way
back when the pending list was introduced, with ff301d6e690b), and
with even more effort and community goodwill might eventually get
INCLUDE support, so changing its code is not the worst choice, but
feels a lot like it's just churn to get it in line with other AMs.

Right. Bloom does not support include columns either. I left out all
the AMs in the patch. Adding INCLUDE column support for any of those
AMs wouldn't be patch-free, so adding one more line to change is not
really a huge issue; and TBH I find it hard to believe that we would
fail to notice.

Thanks

--
Álvaro Herrera 48°01'N 7°57'E — https://www.EnterpriseDB.com/
"Through AI, we have democratized bureaucracy. It's Kafka-as-a-service."
("The world has moved on", Cory Doctorow)
https://pluralistic.net/2026/06/11/lapsarianism/#nostalgia-is-a-toxic-impulse