Mark function arguments of type "T *" as "const T *" where possible

From: Bertrand Drouvot <bertranddrouvot(dot)pg(at)gmail(dot)com>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Mark function arguments of type "T *" as "const T *" where possible
Date: 2025-12-09 16:20:57
Message-ID: aThMaTPjSeFq3qTf@ip-10-97-1-34.eu-west-3.compute.internal
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi hackers,

Several functions in the codebase accept "T *" parameters but do not modify
the pointed-to data. These have been updated to take "const T *" instead,
improving type safety, making the interfaces clearer about their intent and may
enable additional optimizations.

This change helps the compiler catch accidental modifications and better documents
immutability of arguments.

This patch is the same idea as 8a27d418f8f but for any type of pointers.

Regarding the concern [1] raised during review of 8a27d418f8f about back-patching
pain: I believe the benefits of improved type safety and clearer interfaces
outweigh the inconvenience of merge conflicts, which are typically straightforward
to resolve for signature changes. That said, that's a large patch and I understand
the concern.

To avoid any risks:

- cases that produce -Wdiscarded-qualifiers warnings have been discarded as
they would need more investigation.

- double pointers are excluded to keep the changes straightforward.

- cases that produce new -Wcast-qual warnings have been discarded.

As in 8a27d418f8f, no functional behavior is changed.

FWIW, the patch has been generated with the help of a coccinelle script [2].

Thoughts?

[1]: https://www.postgresql.org/message-id/aNajnjxr7RbpSaMP%40nathan
[2]: https://github.com/bdrouvot/coccinelle_on_pg/blob/main/misc/mark_const_where_possible.cocci

Regards,

--
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com

Attachment Content-Type Size
v1-0001-Mark-function-arguments-of-type-T-as-const-T-wher.patch text/x-diff 406.0 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Ranier Vilela 2025-12-09 16:24:14 Re: Avoid use scoped block variable
Previous Message Tomas Vondra 2025-12-09 16:20:48 Re: Avoid use scoped block variable