From: Alvaro Herrera <alvherre@alvh.no-ip.org>
To: David Rowley <dgrowleyml@gmail.com>
Cc: Justin Pryzby <pryzby@telsasoft.com>
Cc: pgsql-hackers@postgresql.org, Tomas Vondra <tomas.vondra@postgresql.org>
Cc: Peter Smith <smithpb2250@gmail.com>
Subject: Re: shadow variables - pg15 edition
Date: Thu, 6 Oct 2022 09:32:36 +0200
Message-ID: <20221006073236.s3nr57erpqoybvx2@alvherre.pgsql> (raw)
In-Reply-To: <CAApHDvqESZZxLrqx==J0qQoQmL=ki40+jC6JZZ0o-StzBq_cMg@mail.gmail.com>
On 2022-Oct-06, David Rowley wrote:
> On Thu, 6 Oct 2022 at 02:34, Alvaro Herrera <alvherre@alvh.no-ip.org> wrote:
> > A simpler idea might be to just remove the inner declaration, and have
> > that block set the outer var. There's no damage, since the block is
> > going to end and not access the previous value anymore.
> >
> > diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c
> > index aa1a3541fe..91a067859b 100644
> > --- a/src/bin/pgbench/pgbench.c
> > +++ b/src/bin/pgbench/pgbench.c
> > @@ -7506,7 +7506,7 @@ threadRun(void *arg)
> > /* progress report is made by thread 0 for all threads */
> > if (progress && thread->tid == 0)
> > {
> > - pg_time_usec_t now = pg_time_now();
> > + now = pg_time_now(); /* not lazy; clobbers outer value */
>
> I didn't want to do it that way because all this code is in a while
> loop and the outer "now" will be reused after it's set by the code
> above. It's not really immediately obvious to me what repercussions
> that would have, but it didn't seem worth taking any risks.
No, it's re-initialized to zero every time through the loop, so setting
it to something else at the bottom doesn't have any further effect.
If it were *not* reinitialized every time through the loop, then what
would happen is that every iteration in the loop (and each operation
within) would see exactly the same value of "now", because it's only set
"lazily" (meaning, if already set, don't change it.)
--
Álvaro Herrera Breisgau, Deutschland — https://www.EnterpriseDB.com/
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-hackers@postgresql.org
Cc: alvherre@alvh.no-ip.org, dgrowleyml@gmail.com, pryzby@telsasoft.com, tomas.vondra@postgresql.org, smithpb2250@gmail.com
Subject: Re: shadow variables - pg15 edition
In-Reply-To: <20221006073236.s3nr57erpqoybvx2@alvherre.pgsql>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox