anonymous unions (C11)

Started by Peter Eisentraut7 months ago15 messageshackers
Jump to latest
#1Peter Eisentraut
peter_e@gmx.net

Here is another patch set to sprinkle some C11 features around the
code. My aim is to make a little bit of use of several C11 features
as examples and encouragement for future code, and to test compilers
(although we're not yet at the point where this should really stress
any compiler).

Here, I'm proposing to make some use of anonymous unions, which are a
nice notational simplification if you nest unions inside structs,
which is pretty common in PostgreSQL code.

Often times, you have something like

struct important_thing
{
int a;
int b;
union
{
int foo;
int bar;
} u;
}

(in PostgreSQL source code, often "u" is used, sometimes "d"), and
then you have to address foo as varname.u.foo, which often looks
cryptic (especially if there is more nesting). With anonymous unions,
you can declare this as

struct important_thing
{
int a;
int b;
union
{
int foo;
int bar;
};
}

and write varname.foo, which often seems clearer.

(C++ also allows this, so this will preserve C++ compatibility of the
headers.)

That said, I did go overboard here and converted all the struct/union
combinations I could find, but I'm not necessarily proposing to apply
all of them. I'm proposing patches 0001 through 0004, which are
relatively simple or in areas that have already changed a few times
recently (so backpatching would not be trivial anyway), and/or they
are somewhat close to my heart because they originally motivated this
work a long time ago. But if someone finds among the other patches
one that they particularly like, we could add that one as well.

Attachments:

0001-C11-anonymous-unions-pg_locale_t.patchtext/plain; charset=UTF-8; name=0001-C11-anonymous-unions-pg_locale_t.patchDownload+55-56
0002-C11-anonymous-unions-reorderbuffer-xact_time.patchtext/plain; charset=UTF-8; name=0002-C11-anonymous-unions-reorderbuffer-xact_time.patchDownload+22-23
0003-C11-anonymous-unions-pgcrypto.patchtext/plain; charset=UTF-8; name=0003-C11-anonymous-unions-pgcrypto.patchDownload+8-9
0004-C11-anonymous-unions-plpython.patchtext/plain; charset=UTF-8; name=0004-C11-anonymous-unions-plpython.patchDownload+70-71
0005-C11-anonymous-unions-libpq.patchtext/plain; charset=UTF-8; name=0005-C11-anonymous-unions-libpq.patchDownload+26-27
0006-C11-anonymous-unions-reorderbuffer-data.patchtext/plain; charset=UTF-8; name=0006-C11-anonymous-unions-reorderbuffer-data.patchDownload+174-175
0007-C11-anonymous-unions-ecpg.patchtext/plain; charset=UTF-8; name=0007-C11-anonymous-unions-ecpg.patchDownload+78-79
0008-C11-anonymous-unions-pgbench.patchtext/plain; charset=UTF-8; name=0008-C11-anonymous-unions-pgbench.patchDownload+39-40
0009-C11-anonymous-unions-reloptions.patchtext/plain; charset=UTF-8; name=0009-C11-anonymous-unions-reloptions.patchDownload+18-19
0010-C11-anonymous-unions-gist.patchtext/plain; charset=UTF-8; name=0010-C11-anonymous-unions-gist.patchDownload+15-16
0011-C11-anonymous-unions-deparse.patchtext/plain; charset=UTF-8; name=0011-C11-anonymous-unions-deparse.patchDownload+35-36
0012-C11-anonymous-unions-tsearch-ParsedWord.patchtext/plain; charset=UTF-8; name=0012-C11-anonymous-unions-tsearch-ParsedWord.patchDownload+32-33
0013-C11-anonymous-unions-tsearch-spell.patchtext/plain; charset=UTF-8; name=0013-C11-anonymous-unions-tsearch-spell.patchDownload+40-41
0014-C11-anonymous-unions-predicate.patchtext/plain; charset=UTF-8; name=0014-C11-anonymous-unions-predicate.patchDownload+5-6
0015-C11-anonymous-unions-cryptohash.patchtext/plain; charset=UTF-8; name=0015-C11-anonymous-unions-cryptohash.patchDownload+1-2
0016-C11-anonymous-unions-freepage.patchtext/plain; charset=UTF-8; name=0016-C11-anonymous-unions-freepage.patchDownload+72-73
0017-C11-anonymous-unions-typcache.patchtext/plain; charset=UTF-8; name=0017-C11-anonymous-unions-typcache.patchDownload+13-14
0018-C11-anonymous-unions-tsrank.patchtext/plain; charset=UTF-8; name=0018-C11-anonymous-unions-tsrank.patchDownload+17-18
0019-C11-anonymous-unions-fd.patchtext/plain; charset=UTF-8; name=0019-C11-anonymous-unions-fd.patchDownload+17-18
0020-C11-anonymous-unions-HeapTupleFields.patchtext/plain; charset=UTF-8; name=0020-C11-anonymous-unions-HeapTupleFields.patchDownload+7-8
0021-C11-anonymous-unions-testint128.patchtext/plain; charset=UTF-8; name=0021-C11-anonymous-unions-testint128.patchDownload+54-55
0022-C11-anonymous-unions-executor.patchtext/plain; charset=UTF-8; name=0022-C11-anonymous-unions-executor.patchDownload+728-729
0023-C11-anonymous-unions-json.patchtext/plain; charset=UTF-8; name=0023-C11-anonymous-unions-json.patchDownload+678-680
#2Joel Jacobson
joel@compiler.org
In reply to: Peter Eisentraut (#1)
Re: anonymous unions (C11)

On Tue, Sep 23, 2025, at 11:38, Peter Eisentraut wrote:

Here is another patch set to sprinkle some C11 features around the
code. My aim is to make a little bit of use of several C11 features
as examples and encouragement for future code, and to test compilers
(although we're not yet at the point where this should really stress
any compiler).

Nice, didn't know about this C11 feature.

I've eyeballed the entire output of `git diff --word-diff-regex=.` and can't see any oddities.

I've also run the full test suite and it passes.

/Joel

#3Nathan Bossart
nathandbossart@gmail.com
In reply to: Peter Eisentraut (#1)
Re: anonymous unions (C11)

On Tue, Sep 23, 2025 at 11:38:22AM +0200, Peter Eisentraut wrote:

That said, I did go overboard here and converted all the struct/union
combinations I could find, but I'm not necessarily proposing to apply
all of them. I'm proposing patches 0001 through 0004, which are
relatively simple or in areas that have already changed a few times
recently (so backpatching would not be trivial anyway), and/or they
are somewhat close to my heart because they originally motivated this
work a long time ago. But if someone finds among the other patches
one that they particularly like, we could add that one as well.

I would have used this in the DSM registry if it was available. Patch
attached.

--
nathan

Attachments:

0001-C11-anonymous-unions-DSM-registry.patchtext/plain; charset=us-asciiDownload+9-10
#4Chao Li
li.evan.chao@gmail.com
In reply to: Peter Eisentraut (#1)
Re: anonymous unions (C11)

On Tue, Sep 23, 2025 at 5:38 PM Peter Eisentraut <peter@eisentraut.org>
wrote:

That said, I did go overboard here and converted all the struct/union
combinations I could find, but I'm not necessarily proposing to apply
all of them. I'm proposing patches 0001 through 0004, which are
relatively simple or in areas that have already changed a few times
recently (so backpatching would not be trivial anyway), and/or they
are somewhat close to my heart because they originally motivated this
work a long time ago. But if someone finds among the other patches
one that they particularly like, we could add that one as well.

I went through all commits and code changes are neat and clear.

But when I tried to build on my Macbook M4, it got 18 errors
against cryptohash.c:
```
cryptohash.c:108:22: error: no member named 'data' in 'struct
pg_cryptohash_ctx'
108 | pg_md5_init(&ctx->data.md5);
| ~~~ ^
cryptohash.c:111:23: error: no member named 'data' in 'struct
pg_cryptohash_ctx'
111 | pg_sha1_init(&ctx->data.sha1);
| ~~~ ^
cryptohash.c:114:25: error: no member named 'data' in 'struct
pg_cryptohash_ctx'
114 | pg_sha224_init(&ctx->data.sha224);
| ~~~ ^
cryptohash.c:117:25: error: no member named 'data' in 'struct
pg_cryptohash_ctx'
117 | pg_sha256_init(&ctx->data.sha256);
| ~~~ ^
cryptohash.c:120:25: error: no member named 'data' in 'struct
pg_cryptohash_ctx'
120 | pg_sha384_init(&ctx->data.sha384);
| ~~~ ^
cryptohash.c:123:25: error: no member named 'data' in 'struct
pg_cryptohash_ctx'
123 | pg_sha512_init(&ctx->data.sha512);
| ~~~ ^
cryptohash.c:144:24: error: no member named 'data' in 'struct
pg_cryptohash_ctx'
144 | pg_md5_update(&ctx->data.md5, data, len);
| ~~~ ^
cryptohash.c:147:25: error: no member named 'data' in 'struct
pg_cryptohash_ctx'
147 | pg_sha1_update(&ctx->data.sha1, data, len);
| ~~~ ^
cryptohash.c:150:27: error: no member named 'data' in 'struct
pg_cryptohash_ctx'
150 | pg_sha224_update(&ctx->data.sha224, data,
len);
Fixed build failure on Macbook M4
| ~~~ ^
cryptohash.c:153:27: error: no member named 'data' in 'struct
pg_cryptohash_ctx'
153 | pg_sha256_update(&ctx->data.sha256, data,
len);
| ~~~ ^
cryptohash.c:156:27: error: no member named 'data' in 'struct
pg_cryptohash_ctx'
156 | pg_sha384_update(&ctx->data.sha384, data,
len);
| ~~~ ^
cryptohash.c:159:27: error: no member named 'data' in 'struct
pg_cryptohash_ctx'
159 | pg_sha512_update(&ctx->data.sha512, data,
len);
| ~~~ ^
cryptohash.c:185:23: error: no member named 'data' in 'struct
pg_cryptohash_ctx'
185 | pg_md5_final(&ctx->data.md5, dest);
| ~~~ ^
cryptohash.c:193:24: error: no member named 'data' in 'struct
pg_cryptohash_ctx'
193 | pg_sha1_final(&ctx->data.sha1, dest);
| ~~~ ^
cryptohash.c:201:26: error: no member named 'data' in 'struct
pg_cryptohash_ctx'
201 | pg_sha224_final(&ctx->data.sha224, dest);
| ~~~ ^
cryptohash.c:209:26: error: no member named 'data' in 'struct
pg_cryptohash_ctx'
209 | pg_sha256_final(&ctx->data.sha256, dest);
| ~~~ ^
cryptohash.c:217:26: error: no member named 'data' in 'struct
pg_cryptohash_ctx'
217 | pg_sha384_final(&ctx->data.sha384, dest);
| ~~~ ^
cryptohash.c:225:26: error: no member named 'data' in 'struct
pg_cryptohash_ctx'
225 | pg_sha512_final(&ctx->data.sha512, dest);
| ~~~ ^
18 errors generated.
```

With the attached fix, the build passed then, "make check" also passed.

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

Attachments:

0024-Fixed-build-failure-on-Macbook-M4.patchapplication/octet-stream; name=0024-Fixed-build-failure-on-Macbook-M4.patchDownload+18-19
#5Ashutosh Bapat
ashutosh.bapat@enterprisedb.com
In reply to: Chao Li (#4)
Re: anonymous unions (C11)

On Wed, Sep 24, 2025 at 4:49 AM Chao Li <li.evan.chao@gmail.com> wrote:

On Tue, Sep 23, 2025 at 5:38 PM Peter Eisentraut <peter@eisentraut.org> wrote:

That said, I did go overboard here and converted all the struct/union
combinations I could find, but I'm not necessarily proposing to apply
all of them. I'm proposing patches 0001 through 0004, which are
relatively simple or in areas that have already changed a few times
recently (so backpatching would not be trivial anyway), and/or they
are somewhat close to my heart because they originally motivated this
work a long time ago. But if someone finds among the other patches
one that they particularly like, we could add that one as well.

I went through all commits and code changes are neat and clear.

But when I tried to build on my Macbook M4, it got 18 errors against cryptohash.c:
```
cryptohash.c:108:22: error: no member named 'data' in 'struct pg_cryptohash_ctx'
108 | pg_md5_init(&ctx->data.md5);
| ~~~ ^

... snip ...

cryptohash.c:225:26: error: no member named 'data' in 'struct pg_cryptohash_ctx'
225 | pg_sha512_final(&ctx->data.sha512, dest);
| ~~~ ^
18 errors generated.

I am using meson and gcc with following versions

$ gcc --version
gcc (Ubuntu 11.4.0-1ubuntu1~22.04.2) 11.4.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ meson --version
0.61.2

I am getting opposite of those errors

../../coderoot/pg/src/common/cryptohash.c: In function ‘pg_cryptohash_init’:
../../coderoot/pg/src/common/cryptohash.c:108:41: error:
‘pg_cryptohash_ctx’ has no member named ‘md5’
108 | pg_md5_init(&ctx->md5);
| ^~
... snip ...
../../coderoot/pg/src/common/cryptohash.c:225:45: error:
‘pg_cryptohash_ctx’ has no member named ‘sha512’
225 | pg_sha512_final(&ctx->sha512, dest);

Attached patch fixes those errors for me.

--
Best Wishes,
Ashutosh Bapat

Attachments:

make_union_anonymous.patchtext/x-patch; charset=US-ASCII; name=make_union_anonymous.patchDownload+1-1
#6Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Ashutosh Bapat (#5)
Re: anonymous unions (C11)

On 2025-Sep-30, Ashutosh Bapat wrote:

Attached patch fixes those errors for me.

diff --git a/src/common/cryptohash.c b/src/common/cryptohash.c
index fc0555d2f99..e8a35c2ec35 100644
--- a/src/common/cryptohash.c
+++ b/src/common/cryptohash.c
@@ -61,7 +61,7 @@ struct pg_cryptohash_ctx
pg_sha256_ctx sha256;
pg_sha384_ctx sha384;
pg_sha512_ctx sha512;
-	}			data;
+	};
};

Isn't this patch 0015 in Peter's series?

--
Álvaro Herrera PostgreSQL Developer — https://www.EnterpriseDB.com/
"Saca el libro que tu religión considere como el indicado para encontrar la
oración que traiga paz a tu alma. Luego rebootea el computador
y ve si funciona" (Carlos Duclós)

#7Ashutosh Bapat
ashutosh.bapat@enterprisedb.com
In reply to: Alvaro Herrera (#6)
Re: anonymous unions (C11)

On Tue, Sep 30, 2025 at 5:14 PM Álvaro Herrera <alvherre@kurilemu.de> wrote:

On 2025-Sep-30, Ashutosh Bapat wrote:

Attached patch fixes those errors for me.

diff --git a/src/common/cryptohash.c b/src/common/cryptohash.c
index fc0555d2f99..e8a35c2ec35 100644
--- a/src/common/cryptohash.c
+++ b/src/common/cryptohash.c
@@ -61,7 +61,7 @@ struct pg_cryptohash_ctx
pg_sha256_ctx sha256;
pg_sha384_ctx sha384;
pg_sha512_ctx sha512;
-     }                       data;
+     };
};

Isn't this patch 0015 in Peter's series?

I found the compilation errors after pulling the latest sources at
that time. I thought the patch series is committed and hence this
report and patch. Didn't look at the patch series.

But the problematic commit has been reverted since then.

--
Best Wishes,
Ashutosh Bapat

#8Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Ashutosh Bapat (#7)
Re: anonymous unions (C11)

On 2025-Sep-30, Ashutosh Bapat wrote:

I found the compilation errors after pulling the latest sources at
that time. I thought the patch series is committed and hence this
report and patch. Didn't look at the patch series.

Ah, okay.

--
Álvaro Herrera 48°01'N 7°57'E — https://www.EnterpriseDB.com/

#9Peter Eisentraut
peter_e@gmx.net
In reply to: Nathan Bossart (#3)
Re: anonymous unions (C11)

On 23.09.25 16:17, Nathan Bossart wrote:

On Tue, Sep 23, 2025 at 11:38:22AM +0200, Peter Eisentraut wrote:

That said, I did go overboard here and converted all the struct/union
combinations I could find, but I'm not necessarily proposing to apply
all of them. I'm proposing patches 0001 through 0004, which are
relatively simple or in areas that have already changed a few times
recently (so backpatching would not be trivial anyway), and/or they
are somewhat close to my heart because they originally motivated this
work a long time ago. But if someone finds among the other patches
one that they particularly like, we could add that one as well.

I would have used this in the DSM registry if it was available. Patch
attached.

This looks good to me, and also mostly harmless in terms of backpatching.

#10Nathan Bossart
nathandbossart@gmail.com
In reply to: Peter Eisentraut (#9)
Re: anonymous unions (C11)

On Fri, Oct 03, 2025 at 09:01:26AM +0200, Peter Eisentraut wrote:

On 23.09.25 16:17, Nathan Bossart wrote:

I would have used this in the DSM registry if it was available. Patch
attached.

This looks good to me, and also mostly harmless in terms of backpatching.

Committed, thanks for reviewing.

--
nathan

#11Jacob Champion
jacob.champion@enterprisedb.com
In reply to: Nathan Bossart (#3)
Re: anonymous unions (C11)

On Tue, Sep 23, 2025 at 7:18 AM Nathan Bossart <nathandbossart@gmail.com> wrote:

I would have used this in the DSM registry if it was available. Patch
attached.

Likewise for libpq-oauth, as attached.

Thanks,
--Jacob

Attachments:

0001-Make-some-use-of-anonymous-unions-libpq-oauth.patchapplication/octet-stream; name=0001-Make-some-use-of-anonymous-unions-libpq-oauth.patchDownload+10-11
#12Nathan Bossart
nathandbossart@gmail.com
In reply to: Jacob Champion (#11)
Re: anonymous unions (C11)

On Thu, Oct 16, 2025 at 10:40:10AM -0700, Jacob Champion wrote:

Likewise for libpq-oauth, as attached.

LGTM

--
nathan

#13Jacob Champion
jacob.champion@enterprisedb.com
In reply to: Nathan Bossart (#12)
Re: anonymous unions (C11)

On Fri, Oct 17, 2025 at 7:23 AM Nathan Bossart <nathandbossart@gmail.com> wrote:

LGTM

(Pushed, thanks!)

--Jacob

#14Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Peter Eisentraut (#1)
Re: anonymous unions (C11)

On 2025-Sep-23, Peter Eisentraut wrote:

Here is another patch set to sprinkle some C11 features around the
code. My aim is to make a little bit of use of several C11 features
as examples and encouragement for future code, and to test compilers
(although we're not yet at the point where this should really stress
any compiler).

I was going over patch [1]/messages/by-id/4047390.3Lj2Plt8kZ@thinkpad-pgpro today and came across the union in
relopt_value. Vaguely remembering this thread, I patched it out
(attached) ... and now that I come here to post it, I realize that it's
probably identical to your patch 0009 here.

Would you push it or can I?

[1]: /messages/by-id/4047390.3Lj2Plt8kZ@thinkpad-pgpro

--
Álvaro Herrera PostgreSQL Developer — https://www.EnterpriseDB.com/

Attachments:

0001-Make-some-use-of-anonymous-unions.patchtext/x-diff; charset=utf-8Download+18-19
#15Peter Eisentraut
peter_e@gmx.net
In reply to: Alvaro Herrera (#14)
Re: anonymous unions (C11)

On 19.01.26 22:10, Álvaro Herrera wrote:

On 2025-Sep-23, Peter Eisentraut wrote:

Here is another patch set to sprinkle some C11 features around the
code. My aim is to make a little bit of use of several C11 features
as examples and encouragement for future code, and to test compilers
(although we're not yet at the point where this should really stress
any compiler).

I was going over patch [1] today and came across the union in
relopt_value. Vaguely remembering this thread, I patched it out
(attached) ... and now that I come here to post it, I realize that it's
probably identical to your patch 0009 here.

Would you push it or can I?

Go ahead please.