Re: shadow variables - pg15 edition

From: Justin Pryzby <pryzby(at)telsasoft(dot)com>
To: Michael Paquier <michael(at)paquier(dot)xyz>
Cc: Peter Smith <smithpb2250(at)gmail(dot)com>, pgsql-hackers(at)postgresql(dot)org, Tomas Vondra <tomas(dot)vondra(at)postgresql(dot)org>, David Rowley <dgrowleyml(at)gmail(dot)com>
Subject: Re: shadow variables - pg15 edition
Date: 2022-08-18 02:36:26
Message-ID: 20220818023626.GJ26426@telsasoft.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Thu, Aug 18, 2022 at 09:39:02AM +0900, Michael Paquier wrote:
> On Thu, Aug 18, 2022 at 08:49:14AM +1000, Peter Smith wrote:
> > I'd started looking at these [1] last year and spent a day trying to
> > categorise them all in a spreadsheet (shadows a global, shadows a
> > parameter, shadows a local var etc) but I became swamped by the
> > volume, and then other work/life got in the way.
> >
> > +1 from me.
>
> A lot of the changes proposed here update the code so as the same
> variable gets used across more code paths by removing declarations,
> but we have two variables defined because both are aimed to be used in
> a different context (see AttachPartitionEnsureIndexes() in tablecmds.c
> for example).

> Wouldn't it be a saner approach in a lot of cases to rename the
> shadowed variables (aka the ones getting removed in your patches) and
> keep them local to the code paths where we use them?

The cases where I removed a declaration are ones where the variable either
hasn't yet been assigned in the outer scope (so it's safe to use first in the
inner scope, since its value is later overwriten in the outer scope). Or it's
no longer used in the outer scope, so it's safe to re-use it in the inner scope
(as in AttachPartitionEnsureIndexes). Since you think it's saner, I changed to
rename them.

In the case of "first", the var is used in two independent loops, the same way,
and re-initialized. In the case of found_whole_row, the var is ignored, as the
comments say, so it would be silly to declare more vars to be additionally
ignored.

--
Justin

PS. I hadn't sent the other patches which rename the variables, having assumed
that the discussion would be bikeshedded to death and derail without having
fixed the lowest hanging fruits. I'm attaching them those now to see what
happens.

Attachment Content-Type Size
0001-avoid-shadow-vars-pg_dump.c-i_oid.patch text/x-diff 875 bytes
0002-avoid-shadow-vars-pg_dump.c-tbinfo.patch text/x-diff 1.7 KB
0003-avoid-shadow-vars-pg_dump.c-owning_tab.patch text/x-diff 1.8 KB
0004-avoid-shadow-vars-tablesync.c-first.patch text/x-diff 1.1 KB
0005-avoid-shadow-vars-tablesync.c-slot.patch text/x-diff 2.0 KB
0006-avoid-shadow-vars-basebackup_target.c-ttype.patch text/x-diff 1.5 KB
0007-avoid-shadow-vars-parse_jsontable.c-jtc.patch text/x-diff 1.4 KB
0008-avoid-shadow-vars-res.patch text/x-diff 1.1 KB
0009-avoid-shadow-vars-clauses.c-querytree_list.patch text/x-diff 1.4 KB
0010-avoid-shadow-vars-tablecmds.c-constraintOid.patch text/x-diff 1.2 KB
0011-avoid-shadow-vars-tablecmds.c-copyTuple.patch text/x-diff 1.8 KB
0012-avoid-shadow-vars-heap.c-rel-tuple.patch text/x-diff 1.7 KB
0013-avoid-shadow-vars-copyfrom.c-attnum.patch text/x-diff 1.3 KB
0014-avoid-shadow-vars-nodeAgg-transno.patch text/x-diff 1.2 KB
0015-avoid-shadow-vars-trigger.c-partitionId.patch text/x-diff 1.1 KB
0016-avoid-shadow-vars-execPartition.c-found_whole_row.patch text/x-diff 1.2 KB
0017-avoid-shadow-vars-brin-keyno.patch text/x-diff 1.3 KB
0018-avoid-shadow-vars-bufmgr.c-j.patch text/x-diff 1.5 KB
0019-avoid-shadow-vars-psql-command.c-host.patch text/x-diff 2.4 KB
0020-avoid-shadow-vars-ruleutils-dpns.patch text/x-diff 1.0 KB
0021-avoid-shadow-vars-costsize.c-subpath.patch text/x-diff 1.2 KB
0022-avoid-shadow-vars-partitionfuncs.c-relid.patch text/x-diff 1.1 KB
0023-avoid-shadow-vars-rangetypes_gist.c-range.patch text/x-diff 1.2 KB
0024-avoid-shadow-vars-ecpglib-execute.c-len.patch text/x-diff 1.3 KB
0025-avoid-shadow-vars-autovacuum.c-db.patch text/x-diff 1.3 KB
0026-avoid-shadow-vars-basebackup.c-ti.patch text/x-diff 1.9 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Amit Kapila 2022-08-18 03:13:08 Re: Handle infinite recursion in logical replication setup
Previous Message John Naylor 2022-08-18 02:11:55 Re: fix typos