Comparing two double values method

Started by Bowen Shialmost 3 years ago5 messageshackers
Beta feature

Hackorum builds and tests every patch posted to the lists, not only commitfest submissions. This is Hackorum's own CI rather than the PostgreSQL project's, and it is still under testing - please report anything that looks wrong.

appliessuccessCI history

You can run a PostgreSQL built from this patch straight from Docker, with no checkout and no build:

docker run --rm -p 5432:5432 ghcr.io/hackorum-dev/postgres-patch:t48573
psql -h localhost -U postgres

Built from patchset v1 (message #1), September 20, 2026 at 05:03 AM.

Every patchset is also pushed to a branch of our PostgreSQL fork, so you can check out the same tree CI built. Without a PostgreSQL checkout:

git clone --branch t48573_1 https://github.com/hackorum-dev/postgres.git

In a checkout you already have, add the fork once:

git remote add hackorum https://github.com/hackorum-dev/postgres.git

then, for this patchset and every later one:

git fetch hackorum t48573_1 && git checkout t48573_1

Patchset v1 (message #1) is on t48573_1

Jump to latest
#1Bowen Shi
zxwsbg12138@gmail.com

Dears,

I noticed that in the `check_GUC_init` function, there is a direct
comparison using the != operator for two double values, which seems
problematic.

I wrote this patch to fix this.

--
Regard,
Bowen Shi

Attachments:

t48573_1
v1-0001-Fix-double-value-compare-problem.patchapplication/octet-stream; name=v1-0001-Fix-double-value-compare-problem.patchDownload+2-2
#2Matthias van de Meent
boekewurm+postgres@gmail.com
In reply to: Bowen Shi (#1)
Re: Comparing two double values method

On Tue, 10 Oct 2023 at 12:33, Bowen Shi <zxwsbg12138@gmail.com> wrote:

Dears,

I noticed that in the `check_GUC_init` function, there is a direct
comparison using the != operator for two double values, which seems
problematic.

I don't think I understand the problem. The code checks that the
dynamic initialization values are equal to the current value of the
GUC, or 0. Why would a "margin for error" of 1e-6 be of any use?
Why was the margin of 1e-6 chosen instead of one based on the exponent
of the GUC's current value (if any)?

In my view, this would break the code, not fix it, as it would
decrease the cases where we detect broken GUC registrations.

Kind regards,

Matthias van de Meent
Neon (https://neon.tech)

#3Heikki Linnakangas
heikki.linnakangas@enterprisedb.com
In reply to: Bowen Shi (#1)
Re: Comparing two double values method

On 10/10/2023 13:31, Bowen Shi wrote:

Dears,

I noticed that in the `check_GUC_init` function, there is a direct
comparison using the != operator for two double values, which seems
problematic.

I wrote this patch to fix this.

No, the compile-time initial values should match exactly.

--
Heikki Linnakangas
Neon (https://neon.tech)

#4Bowen Shi
zxwsbg12138@gmail.com
In reply to: Heikki Linnakangas (#3)
Re: Comparing two double values method

You're right, I made a mistake.

Thanks for your explanation.

#5Tom Lane
tgl@sss.pgh.pa.us
In reply to: Heikki Linnakangas (#3)
Re: Comparing two double values method

Heikki Linnakangas <hlinnaka@iki.fi> writes:

On 10/10/2023 13:31, Bowen Shi wrote:

I noticed that in the `check_GUC_init` function, there is a direct
comparison using the != operator for two double values, which seems
problematic.

No, the compile-time initial values should match exactly.

Right. The point of this test is to catch cases where you wrote,
say,

double my_guc = 1.1;

but the boot_val for it in guc_tables.c is 1.2. There is no
reason to allow any divergence in the spellings of the two C
literals, so as long as they're compiled by the same compiler
there's no reason to expect that the compiled values wouldn't
be bit-equal.

The point of the exclusions for zero is to allow you to just
write

double my_guc;

without expressing an opinion about the initial value.
(Yes, this does mean that "double my_guc = 0.0;" could
be misleading. It's just a heuristic though.)

regards, tom lane