Re: Violation of principle that plan trees are read-only

Tom Lane <tgl@sss.pgh.pa.us>

From: Tom Lane <tgl@sss.pgh.pa.us>
To: Isaac Morland <isaac.morland@gmail.com>
Cc: Robert Haas <robertmhaas@gmail.com>, pgsql-hackers@lists.postgresql.org, Andres Freund <andres@anarazel.de>
Date: 2025-05-19T14:45:47Z
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 →
  1. In ExecInitModifyTable, don't scribble on the source plan.

  2. Rely on executor utils to build targetlist for DML RETURNING.

Isaac Morland <isaac.morland@gmail.com> writes:
> I assume this question has an obvious negative answer, but why can't we
> attach const declarations to the various structures that make up the plan
> tree (at all levels, all the way down)? I know const doesn't actually
> prevent a value from changing, but at least the compiler would complain if
> code accidentally tried.

The big problem is that a "const" attached to a top-level pointer
doesn't inherently propagate down to sub-nodes.  So if I had, say,
"const Query *stmt", the compiler would complain about

	stmt->jointree = foo;

but not about

	stmt->jointree->quals = foo;

I guess we could imagine developing an entirely parallel set of
struct declarations with "const" on all pointer fields, like

typedef struct ConstQuery
{
	...
	const ConstFromExpr   *jointree;
	...
} ConstQuery;

but even with automated maintenance of the ConstFoo doppelganger
typedefs, it seems like that'd be a notational nightmare.  For
one thing, I'm not sure how to teach the compiler that casting
"Query *" to "ConstQuery *" is okay but vice versa isn't.

Does C++ have a better story in this area?  I haven't touched it
in so long that I don't remember.

			regards, tom lane