Array initialisation notation in syscache.c

Started by Thomas Munroover 3 years ago12 messageshackers
Jump to latest
#1Thomas Munro
thomas.munro@gmail.com

Hi,

While hacking on a new system catalogue for a nearby thread, it
occurred to me that syscache.c's table of entries could be made more
readable and less error prone. They look like this:

{AttributeRelationId, /* ATTNUM */
AttributeRelidNumIndexId,
2,
{
Anum_pg_attribute_attrelid,
Anum_pg_attribute_attnum,
0,
0
},
128
},

Do you think this is better?

[ATTNUM] = {
AttributeRelationId,
AttributeRelidNumIndexId,
{
Anum_pg_attribute_attrelid,
Anum_pg_attribute_attnum
},
128
},

We could also consider writing eg ".nbuckets = 128", but it's not a
complicated struct that the eye gets lost in, so I didn't bother with
that in the attached.

Attachments:

0001-Improve-notation-of-cacheinfo-table-in-syscache.c.patchtext/x-patch; charset=US-ASCII; name=0001-Improve-notation-of-cacheinfo-table-in-syscache.c.patchDownload+253-448
#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Thomas Munro (#1)
Re: Array initialisation notation in syscache.c

Thomas Munro <thomas.munro@gmail.com> writes:

Do you think this is better?

I'm not at all on board with adding runtime overhead to
save maintaining the nkeys fields.

Getting rid of the useless trailing zeroes in the key[] arrays
is clearly a win, though.

I'm kind of neutral on using "[N] = " as a substitute for
ordering the entries correctly. While that does remove
one failure mode, it seems like it adds another (ie
failure to provide an entry at all would be masked).

regards, tom lane

#3Thomas Munro
thomas.munro@gmail.com
In reply to: Tom Lane (#2)
Re: Array initialisation notation in syscache.c

On Wed, Dec 21, 2022 at 12:05 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:

Thomas Munro <thomas.munro@gmail.com> writes:

Do you think this is better?

I'm not at all on board with adding runtime overhead to
save maintaining the nkeys fields.

I don't see how to do it at compile time without getting the
preprocessor involved. What do you think about this version?

[ATTNUM] = {
AttributeRelationId,
AttributeRelidNumIndexId,
KEY(Anum_pg_attribute_attrelid,
Anum_pg_attribute_attnum),
128
},

I'm kind of neutral on using "[N] = " as a substitute for
ordering the entries correctly. While that does remove
one failure mode, it seems like it adds another (ie
failure to provide an entry at all would be masked).

It fails very early in testing if you do that. Admittedly, the
assertion is hard to understand, but if I add a new assertion close to
the cause with a new comment to say what you did wrong, I think that
should be good enough?

Attachments:

v2-0001-Improve-notation-of-cacheinfo-table-in-syscache.c.patchtext/x-patch; charset=US-ASCII; name=v2-0001-Improve-notation-of-cacheinfo-table-in-syscache.c.patchDownload+293-651
#4Thomas Munro
thomas.munro@gmail.com
In reply to: Thomas Munro (#3)
Re: Array initialisation notation in syscache.c

On Wed, Dec 21, 2022 at 1:33 PM Thomas Munro <thomas.munro@gmail.com> wrote:

KEY(Anum_pg_attribute_attrelid,
Anum_pg_attribute_attnum),

I independently rediscovered that our VA_ARGS_NARGS() macro in c.h
always returns 1 on MSVC via trial-by-CI. Derp. Here is the same
patch, no change from v2, but this time accompanied by Victor Spirin's
fix, which I found via one of the tab-completion-is-busted-on-Windows
discussions. I can't supply a useful commit message, because I
haven't understood why it works, but it does indeed seem to work and
this should make cfbot green.

Attachments:

v3-0001-Fix-VA_ARGS_NARGS-macro-on-MSVC.patchtext/x-patch; charset=US-ASCII; name=v3-0001-Fix-VA_ARGS_NARGS-macro-on-MSVC.patchDownload+15-1
v3-0002-Improve-notation-of-cacheinfo-table-in-syscache.c.patchtext/x-patch; charset=US-ASCII; name=v3-0002-Improve-notation-of-cacheinfo-table-in-syscache.c.patchDownload+293-651
#5Peter Eisentraut
peter_e@gmx.net
In reply to: Thomas Munro (#4)
Re: Array initialisation notation in syscache.c

On 21.12.22 04:16, Thomas Munro wrote:

On Wed, Dec 21, 2022 at 1:33 PM Thomas Munro <thomas.munro@gmail.com> wrote:

KEY(Anum_pg_attribute_attrelid,
Anum_pg_attribute_attnum),

I independently rediscovered that our VA_ARGS_NARGS() macro in c.h
always returns 1 on MSVC via trial-by-CI. Derp. Here is the same
patch, no change from v2, but this time accompanied by Victor Spirin's
fix, which I found via one of the tab-completion-is-busted-on-Windows
discussions. I can't supply a useful commit message, because I
haven't understood why it works, but it does indeed seem to work and
this should make cfbot green.

This looks like a good improvement to me.

(I have also thought about having this generated from the catalog
definition files somehow, but one step at a time ...)

#6Nikita Malakhov
hukutoc@gmail.com
In reply to: Peter Eisentraut (#5)
Re: Array initialisation notation in syscache.c

Hi!

Wanted to ask this since I encountered a need for a cache with 5 keys -
why is the syscache index still limited to 4 keys?

Thanks!

On Wed, Dec 21, 2022 at 7:36 PM Peter Eisentraut <
peter.eisentraut@enterprisedb.com> wrote:

On 21.12.22 04:16, Thomas Munro wrote:

On Wed, Dec 21, 2022 at 1:33 PM Thomas Munro <thomas.munro@gmail.com>

wrote:

KEY(Anum_pg_attribute_attrelid,
Anum_pg_attribute_attnum),

I independently rediscovered that our VA_ARGS_NARGS() macro in c.h
always returns 1 on MSVC via trial-by-CI. Derp. Here is the same
patch, no change from v2, but this time accompanied by Victor Spirin's
fix, which I found via one of the tab-completion-is-busted-on-Windows
discussions. I can't supply a useful commit message, because I
haven't understood why it works, but it does indeed seem to work and
this should make cfbot green.

This looks like a good improvement to me.

(I have also thought about having this generated from the catalog
definition files somehow, but one step at a time ...)

--
Regards,
Nikita Malakhov
Postgres Professional
https://postgrespro.ru/

#7Tom Lane
tgl@sss.pgh.pa.us
In reply to: Nikita Malakhov (#6)
Re: Array initialisation notation in syscache.c

Nikita Malakhov <hukutoc@gmail.com> writes:

Wanted to ask this since I encountered a need for a cache with 5 keys -
why is the syscache index still limited to 4 keys?

Because there are no cases requiring 5, so far.

(A unique index with as many as 5 keys seems a bit fishy btw.)

regards, tom lane

#8Thomas Munro
thomas.munro@gmail.com
In reply to: Peter Eisentraut (#5)
Re: Array initialisation notation in syscache.c

On Thu, Dec 22, 2022 at 5:36 AM Peter Eisentraut
<peter.eisentraut@enterprisedb.com> wrote:

This looks like a good improvement to me.

Thanks both. Pushed.

(I have also thought about having this generated from the catalog
definition files somehow, but one step at a time ...)

Good plan.

#9Thomas Munro
thomas.munro@gmail.com
In reply to: Thomas Munro (#8)
Re: Array initialisation notation in syscache.c

From the light relief department, here is some more variadic macrology:

-       tp = SearchSysCache1(TSPARSEROID, ObjectIdGetDatum(prsId));
+       tp = SearchSysCache(TSPARSEROID, ObjectIdGetDatum(prsId));

Attachments:

0001-Use-variadic-macros-for-syscache-lookup-functions.patchtext/x-patch; charset=US-ASCII; name=0001-Use-variadic-macros-for-syscache-lookup-functions.patchDownload+1488-1541
#10Peter Eisentraut
peter_e@gmx.net
In reply to: Thomas Munro (#9)
Re: Array initialisation notation in syscache.c

On 31.03.23 04:16, Thomas Munro wrote:

From the light relief department, here is some more variadic macrology:

-       tp = SearchSysCache1(TSPARSEROID, ObjectIdGetDatum(prsId));
+       tp = SearchSysCache(TSPARSEROID, ObjectIdGetDatum(prsId));

I'm worried that if we are removing the variants with the explicit
numbers, it will make it difficult for extensions to maintain
compatibility with previous PG major versions. They would probably have
to copy much of your syscache.h changes into their own code. Seems messy.

#11Thomas Munro
thomas.munro@gmail.com
In reply to: Peter Eisentraut (#10)
Re: Array initialisation notation in syscache.c

On Thu, Sep 21, 2023 at 8:19 PM Peter Eisentraut <peter@eisentraut.org> wrote:

On 31.03.23 04:16, Thomas Munro wrote:

From the light relief department, here is some more variadic macrology:

-       tp = SearchSysCache1(TSPARSEROID, ObjectIdGetDatum(prsId));
+       tp = SearchSysCache(TSPARSEROID, ObjectIdGetDatum(prsId));

I'm worried that if we are removing the variants with the explicit
numbers, it will make it difficult for extensions to maintain
compatibility with previous PG major versions. They would probably have
to copy much of your syscache.h changes into their own code. Seems messy.

I suppose we could also supply a set of macros with the numbers that
map straight onto the numberless ones, with a note that they will be
deleted after N releases. But maybe not worth the hassle for such a
tiny improvement in core code readability. I will withdraw this
entry. Thanks.

#12Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Thomas Munro (#11)
Re: Array initialisation notation in syscache.c

On 2023-Nov-14, Thomas Munro wrote:

I suppose we could also supply a set of macros with the numbers that
map straight onto the numberless ones, with a note that they will be
deleted after N releases.

Maybe just keep compatibility ones with 1 and 2 arguments (the ones most
used) forever, or 15 years, and drop the rest.

--
Álvaro Herrera Breisgau, Deutschland — https://www.EnterpriseDB.com/
“Cuando no hay humildad las personas se degradan” (A. Christie)