tablecmds: clarify recurse vs recusing

Started by Chao Liabout 2 months ago8 messages
Jump to latest
#1Chao Li
li.evan.chao@gmail.com

Hi Hacker,

Many ALTER TABLE-related functions take two boolean parameters, "recurse"
and "recursing", whose names are easy to confuse. For example:
```
/*
* ALTER TABLE ALTER COLUMN DROP IDENTITY
*
* Return the address of the affected column.
*/
static ObjectAddress
ATExecDropIdentity(Relation rel, const char *colName, bool missing_ok,
LOCKMODE lockmode,
bool recurse, bool recursing)
```

The "recurse" parameter actually indicates whether ONLY was *not* specified
in the ALTER TABLE command. It’s supposed to affect both inherited tables
and partitioned tables.

In contrast, "recursing" is an internal indicator that only affects
inherited tables.

To reduce this confusion, I’m proposing to rename "recurse" to "no_only",
which more directly reflects its meaning.

In this patch set:

* 0001 - only renames “recurse” to “no_only” together with minimum comment
updates.
* 0002 - performs a small mechanical refactoring, replacing “if (no_only)
cmd->no_only = true” to “cmd->no_only = no_only”.

Related patches are [1]/messages/by-id/CAEoWx2nJ71hy8R614HQr7vQhkBReO9AANPODPg0aSQs74eOdLQ@mail.gmail.com, [2]/messages/by-id/CAEoWx2=mYhCfsnHaN96Qqwq5b0GVS2YgO3zpVqPPRd_iO52wRw@mail.gmail.com and [3]/messages/by-id/CAEoWx2=SLga-xH09Cq_PAvsHhQHrBK+V0vF821JKgzS=Bm0haA@mail.gmail.com:

[1]: /messages/by-id/CAEoWx2nJ71hy8R614HQr7vQhkBReO9AANPODPg0aSQs74eOdLQ@mail.gmail.com
/messages/by-id/CAEoWx2nJ71hy8R614HQr7vQhkBReO9AANPODPg0aSQs74eOdLQ@mail.gmail.com
[2]: /messages/by-id/CAEoWx2=mYhCfsnHaN96Qqwq5b0GVS2YgO3zpVqPPRd_iO52wRw@mail.gmail.com
/messages/by-id/CAEoWx2=mYhCfsnHaN96Qqwq5b0GVS2YgO3zpVqPPRd_iO52wRw@mail.gmail.com
[3]: /messages/by-id/CAEoWx2=SLga-xH09Cq_PAvsHhQHrBK+V0vF821JKgzS=Bm0haA@mail.gmail.com
/messages/by-id/CAEoWx2=SLga-xH09Cq_PAvsHhQHrBK+V0vF821JKgzS=Bm0haA@mail.gmail.com

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

Attachments:

v1-0002-tablecmds-simplify-propagation-of-no_only-flag.patchapplication/octet-stream; name=v1-0002-tablecmds-simplify-propagation-of-no_only-flag.patchDownload+11-23
v1-0001-tablecmds-rename-recurse-to-no_only.patchapplication/octet-stream; name=v1-0001-tablecmds-rename-recurse-to-no_only.patchDownload+187-187
#2Peter Eisentraut
peter_e@gmx.net
In reply to: Chao Li (#1)
Re: tablecmds: clarify recurse vs recusing

On 19.01.26 08:14, Chao Li wrote:

Many ALTER TABLE-related functions take two boolean parameters,
"recurse" and "recursing", whose names are easy to confuse.

I'm not bothered by this.

To reduce this confusion, I’m proposing to rename "recurse" to
"no_only", which more directly reflects its meaning.

This seems worse. Especially since no_only is almost a double negative.

#3Ashutosh Bapat
ashutosh.bapat@enterprisedb.com
In reply to: Peter Eisentraut (#2)
Re: tablecmds: clarify recurse vs recusing

On Mon, Jan 19, 2026 at 9:20 PM Peter Eisentraut <peter@eisentraut.org> wrote:

On 19.01.26 08:14, Chao Li wrote:

Many ALTER TABLE-related functions take two boolean parameters,
"recurse" and "recursing", whose names are easy to confuse.

I'm not bothered by this.

+1. I had stumbled upon this when working on identity support. I had
the same initial reaction that it's confusing. But soon I got used to
it and any other option wasn't improving the situation. AFAIR, all the
4 possible combinations of those two booleans are possible, but I
can't remember what could recurse = false, recursing = true mean.
recurse = true, recursing = false happens at the start of the
inheritance tree. recurse = false, recursing = false; maybe for a
table which isn't part of inheritance or partition tree. recurse =
true, recursing = true happens when working on a non-leaf non-root
table. If we could find out the impossible combinations and then unify
these two booleans into a single enum variable, that might make code
clearer - at least we will know which combinations are not possible
and which combinations are.

--
Best Wishes,
Ashutosh Bapat

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Peter Eisentraut (#2)
Re: tablecmds: clarify recurse vs recusing

Peter Eisentraut <peter@eisentraut.org> writes:

On 19.01.26 08:14, Chao Li wrote:

Many ALTER TABLE-related functions take two boolean parameters,
"recurse" and "recursing", whose names are easy to confuse.

I'm not bothered by this.

To reduce this confusion, I’m proposing to rename "recurse" to
"no_only", which more directly reflects its meaning.

This seems worse. Especially since no_only is almost a double negative.

Yeah, I don't find this change an improvement either. I think the
actual problem here is lack of documentation: unlike many other
places, there is next to zero commentary in tablecmds.c about what
all the function parameters are. It would probably help to define
these, along the lines of

* recurse: true if we should recurse to children of this table
* recursing: true if we are already recursing from some parent table

If we fleshed out the header comment for ATPrepCmd and maybe a few
other key functions along these lines, that would make the logic
a good deal more intelligible, I think.

regards, tom lane

#5Tom Lane
tgl@sss.pgh.pa.us
In reply to: Ashutosh Bapat (#3)
Re: tablecmds: clarify recurse vs recusing

Ashutosh Bapat <ashutosh.bapat.oss@gmail.com> writes:

... but I
can't remember what could recurse = false, recursing = true mean.

It'd mean that we are operating on a child table but should not
recurse to its children (if any). Not sure if the case actually
arises, although it could make sense if we flattened the inheritance
tree into a list of target tables earlier on, which does happen in
many cases. But most of the code paths that do it that way don't
bother with recurse/recursing parameters.

regards, tom lane

#6Chao Li
li.evan.chao@gmail.com
In reply to: Tom Lane (#4)
Re: tablecmds: clarify recurse vs recusing

On Jan 20, 2026, at 00:14, Tom Lane <tgl@sss.pgh.pa.us> wrote:

Peter Eisentraut <peter@eisentraut.org> writes:

On 19.01.26 08:14, Chao Li wrote:

Many ALTER TABLE-related functions take two boolean parameters,
"recurse" and "recursing", whose names are easy to confuse.

I'm not bothered by this.

To reduce this confusion, I’m proposing to rename "recurse" to
"no_only", which more directly reflects its meaning.

This seems worse. Especially since no_only is almost a double negative.

Yeah, I don't find this change an improvement either. I think the
actual problem here is lack of documentation: unlike many other
places, there is next to zero commentary in tablecmds.c about what
all the function parameters are. It would probably help to define
these, along the lines of

* recurse: true if we should recurse to children of this table
* recursing: true if we are already recursing from some parent table

If we fleshed out the header comment for ATPrepCmd and maybe a few
other key functions along these lines, that would make the logic
a good deal more intelligible, I think.

regards, tom lane

Enhancing the header comments also helps here.

PSA v2:

0001 - Adds detailed descriptions in the header comments of ATController()
and ATPrepCmd(). For the other 16 functions that take both “recurse” and
“recursing”, I only added brief references to those header comments.

0002 - While working on 0001, I noticed that the header comment of
addFkConstraint() explains all parameters except “is_internal”. I added a
line for “is_internal”, so that the comment fully reflects the function
signature. This change is really trivial and can be squashed into 0001 if
preferred.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

Attachments:

v2-0001-tablecmds-clarify-recurse-recursing-semantics-in-.patchapplication/octet-stream; name=v2-0001-tablecmds-clarify-recurse-recursing-semantics-in-.patchDownload+55-6
v2-0002-tablecmds-complete-parameter-documentation-for-ad.patchapplication/octet-stream; name=v2-0002-tablecmds-complete-parameter-documentation-for-ad.patchDownload+1-1
#7Tom Lane
tgl@sss.pgh.pa.us
In reply to: Chao Li (#6)
Re: tablecmds: clarify recurse vs recusing

Chao Li <li.evan.chao@gmail.com> writes:

Enhancing the header comments also helps here.
PSA v2:

I had something more like the attached in mind. I'm not generally
a fan of documenting only some of the arguments of a function, so
I don't care for the way you handled the issue for other functions
in tablecmds.c either. We can either assume that people read
ATPrepCmd's comment and can extrapolate to the other functions,
or we can do something similar to this for all of them.

I do agree with your 0002, but I see no point in pushing that
separately.

regards, tom lane

Attachments:

v3-document-ATPrepCmd-arguments.patchtext/x-diff; charset=us-ascii; name=v3-document-ATPrepCmd-arguments.patchDownload+15-1
#8Chao Li
li.evan.chao@gmail.com
In reply to: Tom Lane (#7)
Re: tablecmds: clarify recurse vs recusing

On Jan 21, 2026, at 04:07, Tom Lane <tgl@sss.pgh.pa.us> wrote:

Chao Li <li.evan.chao@gmail.com> writes:

Enhancing the header comments also helps here.
PSA v2:

I had something more like the attached in mind. I'm not generally
a fan of documenting only some of the arguments of a function, so
I don't care for the way you handled the issue for other functions
in tablecmds.c either. We can either assume that people read
ATPrepCmd's comment and can extrapolate to the other functions,
or we can do something similar to this for all of them.

Got it, thanks. In v4, I’ve limited the changes to fully documenting all
ATPrepCmd() arguments in its header comment and removed the scattered
recurse/recursing references from other functions.

I do agree with your 0002, but I see no point in pushing that
separately.

Absolutely. 0002 was too trivial to be a separate commit. I have squashed
it into 0001 in v4.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

Attachments:

v4-0001-tablecmds-clarify-recurse-recursing-semantics-in-.patchapplication/octet-stream; name=v4-0001-tablecmds-clarify-recurse-recursing-semantics-in-.patchDownload+28-8