Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1vHUy7-009Cwv-T6 for pgsql-hackers@arkaria.postgresql.org; Fri, 07 Nov 2025 22:30:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1vHUx7-00Gn7h-Nl for pgsql-hackers@arkaria.postgresql.org; Fri, 07 Nov 2025 22:29:05 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1vHUx7-00Gn7Y-3O for pgsql-hackers@lists.postgresql.org; Fri, 07 Nov 2025 22:29:05 +0000 Received: from mail-io1-xd2f.google.com ([2607:f8b0:4864:20::d2f]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vHUx5-005yM4-0D for pgsql-hackers@lists.postgresql.org; Fri, 07 Nov 2025 22:29:04 +0000 Received: by mail-io1-xd2f.google.com with SMTP id ca18e2360f4ac-9486b567baaso98460039f.0 for ; Fri, 07 Nov 2025 14:29:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762554542; x=1763159342; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=NYvPBwkFttaT1HaAiTS+Ogb8XRypfI3beRzNAz8EM7M=; b=BHx0TZs/tqBP+SZD8YQmEwtLuxvcVbkiWW8AFatq86B9tO+qkWY5SZYCZ55d1yvhfm 0u9Rnu4b+TrYERBm99HJMqTNvIXksLypKSykzgkWGVR8nCgTX2t+kHL2XzFGKZoeKsdE P5SclODxfTYtCUdI/+4FT7L/knSlqbomqiD90C753fbpIhIyvAEby9UhouCWoVGeh74/ Ot2qYcoaWTFzpEPGgUUarc04VudG4oWzArEjKcapAp2kZd2BonC1KAJfj6oVvi7Lv7RX FjLdfrSaEsX0hRAjN4U8ebldifgwJ4+20VO0pkFIyy0q0VDl5mIbnrtBXAxhmMvRJYda +45Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762554542; x=1763159342; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=NYvPBwkFttaT1HaAiTS+Ogb8XRypfI3beRzNAz8EM7M=; b=LTFEctEnUFToLVW0/wRaP2JAwcsNn6/HBXMYKwlpEl88kXE1rpLArQXhTeaQ4jqx0x vjA4/ddnzUQRtfKZc0DwR3CA4ZeDKP8+xRKCqWTmuI6/iU1f15TPN33SyldDNy2CcAYF r2gJ6TXAoUSyl/kgUcTMUGoTfrgoY84rpekifWXwL8xG6/dBm5x4X2m7fVx9y8mWvkXq GipQESi4FYXbMkHiR8spPbbL7hL5UfnSzybo+7ILegOSg7GG9nISd62jztKM/8BQ+lnJ d/AYpuQ/ZsCIMKsboz6GZdbO8NjPCmB3xCHBnT0asBDQtoXVtati764SFTJZTZFqYXZI z2Ew== X-Forwarded-Encrypted: i=1; AJvYcCUgyLSXaTreXNTUt1+05Cn++fAkp1Do9Pcls6aWonqU8+A8JYtZ6Bn56uOuhPISBHDwKB/R/rjhQJFrWGW3@lists.postgresql.org X-Gm-Message-State: AOJu0YxzXEhUrtbDIOrDLzigev3XWrzir8k5Z6DHmOChel+8G8P+aukp KtO0gFnWYFMLhAe7xxL+HPuprcI4BRHvPs4zcPCS1DISxeEc6LIxvLs/hf6kCi3yfs+qn3ZXpqe J57ezLNCmPRQqhBdm3BMjP3O0W7Ol+EA= X-Gm-Gg: ASbGncvfyW+FjWQV4s+IltvDNjiHRU2S9WQsw7iaHRXlnci8xJA607DdhNx0mP/TvkP MGWcCIssJVZNohfphL/JtJsBov0muTGsuw+Lm1wDUKEkz5jSULraFJgxSDiFIUWUf/XglUH5hqp Jkuyf1QrnUf08RJA3QHwW/+gl4XPSK0PrjU8YRgR8H2mTXIcAj2mNPzIhvCapwJVZplTWgSvWre hg6sveGKSkAa2LlqGJ0c3r3gWLyqV2slRTVnpm42hLTdq/cVK2TKhP9dR2FZtjk2aZL1lU= X-Google-Smtp-Source: AGHT+IH6nFCsHFtfTkc2KwMjeUpY0jAgRPLCrKe62kdUdd0q/zV1be9K3k0XDzN7izmb8hiieUbj6I/JJWm+EMe51oM= X-Received: by 2002:a05:6e02:1a24:b0:433:481d:fd60 with SMTP id e9e14a558f8ab-43367e7a593mr17292625ab.17.1762554542151; Fri, 07 Nov 2025 14:29:02 -0800 (PST) MIME-Version: 1.0 References: <439a9947-e6a1-4ea9-84d8-1f843deaf6b1@vondra.me> In-Reply-To: From: Corey Huinker Date: Fri, 7 Nov 2025 17:28:50 -0500 X-Gm-Features: AWmQ_bksjPpwlYodySc9MeNVrpNCxXwfi694ZZ4QU8GsI7PJ-pg5X6zXf7i0ZuY Message-ID: Subject: Re: Extended Statistics set/restore/clear functions. To: Michael Paquier Cc: Tomas Vondra , jian he , pgsql-hackers@lists.postgresql.org, tgl@sss.pgh.pa.us Content-Type: multipart/alternative; boundary="00000000000061e085064308b539" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --00000000000061e085064308b539 Content-Type: text/plain; charset="UTF-8" > > > Patch 0001 for ndistinct was missing a documentation update, we have > one query in perform.sgml that looks at stxdndistinct. Patch 0003 is > looking OK here as well. > Well spotted. > For dependencies, the format switches from a single json object > with key/vals like that: > "3 => 4": 1.000000 > To a JSON array made of elements like that: > {"degree": 1.000000, "attributes": [3],"dependency": 4}, > > For ndistincts, we move from a JSON blob with key/vals like that: > "3, 4": 11 > To a JSON array made of the following elements: > {"ndistinct": 11, "attributes": [3,4]} > > Using a keyword within each element would force a stronger validation > when these get imported back, which is a good thing. I like that. > > Before going in-depth into the input functions to cross-check the > amount of validation we should do, have folks any comments about the > proposed format? That's the key point this patch set depends on, and > I'd rather not spend more time the whole thing if somebody would like > a different format. This is the format that Tomas has mentioned at > the top of the thread. Note: as noted upthread, pg_dump would be in > charge of transferring the data of the old format to the new format at > the end. > I'm open to other formats, but aside from renaming the json keys (maybe "attnums" or "keys" instead of "attributes"?), I'm not sure what really could be done and still be JSON. I suppose we could go with a tuple format like this: '{({3,4},11),...}' for pg_ndistinct and '{({3},4,1.00000),...}' for pg_dependencies. Those would certainly be more compact, but makes for a hard read by humans, and while the JSON code is big, it's also proven in other parts of the codebase, hence less risky. > > While looking at 0002 and 0004 (which have a couple of issues > actually), I have been wondering about moving into a new file the four > data-type functions (in, out, send and receive) and the new input > functions that rely on a new JSON lexer and parser logic into for both > ndistinct and dependencies. The new set of headers added at the top > of mvdistinct.c and dependencies.c for the new code points that a > separation may be better in the long-term, because the new code relies > on parts of the backend that the existing code does not care about, > and these files become larger than the relation and attribute stats > files. I would be tempted to name these new files pg_dependencies.c > and pg_ndistinct.c, mapping with their catalog types. With this > separation, it looks like the "core" parts in charge of the > calculations with ndistinct and dependencies can be kept on its own. > What do you think? > A part of me thinks that everything that remains after removing in/out/send/recv is just taking a table sample data structure and crunching numbers to come up with the deserialized data structure...that's in/out with a different starting/ending points. There's no denying that JSON parsing is a very different code style than statistical number crunching, and mixing the two is incongruous, so it's worth a shot, and I'll try that for v9. > > A second comment is for 0005. The routines of attributes.c are > applied to the new clear and restore functions. Shouldn't these be in > stats_utils.c at the end? That's where the "common" functions used by > the stats manipulation logic are. > I assume you're referring to attribute_stats.c. I think that would cause stats_utils.c to have to pull in a lot of things from attribute_stats.c, and that would create the exact sort of include-pollution that you're trying to avoid in the mvdistinct.c/dependencies.c situation mentioned above. The one lone exception to this is text_to_stavalues(), which is a fancy wrapper around array_in() and could perhaps be turned to even more generic usage outside of stats in general. The functions in question are needed because the exprs value is itself an array of partly-filled-out pg_attribute tuples, so it's common to those two needs, but specific to stats about attributes. Maybe we need an attr_stats_utils.h? --00000000000061e085064308b539 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Patch 0001 for ndistinct was missing a documentation update, we have
one query in perform.sgml that looks at stxdndistinct.=C2=A0 Patch 0003 is<= br> looking OK here as well.

Well spotted.<= /div>
=C2=A0
For dependencies, the format switches from a single json object
with key/vals like that:
"3 =3D> 4": 1.000000
To a JSON array made of elements like that:
{"degree": 1.000000, "attributes": [3],"dependency= ": 4},

For ndistincts, we move from a JSON blob with key/vals like that:
"3, 4": 11
To a JSON array made of the following elements:
{"ndistinct": 11, "attributes": [3,4]}

Using a keyword within each element would force a stronger validation
when these get imported back, which is a good thing.=C2=A0 I like that.

Before going in-depth into the input functions to cross-check the
amount of validation we should do, have folks any comments about the
proposed format?=C2=A0 That's the key point this patch set depends on, = and
I'd rather not spend more time the whole thing if somebody would like a different format.=C2=A0 This is the format that Tomas has mentioned at the top of the thread.=C2=A0 Note: as noted upthread, pg_dump would be in charge of transferring the data of the old format to the new format at
the end.

I'm open to other formats,= but aside from renaming the json keys (maybe "attnums" or "= keys" instead of "attributes"?), I'm not sure what reall= y could be done and still be JSON. I suppose we could go with a tuple forma= t like this:

'{({3,4},11),...}' for pg_ndistinct = and
'{({3},4,1.00000),...}'=C2=A0 for pg_dependencies= .

Those would certainly be more compact, but makes= for a hard read by humans, and while the JSON code is big, it's also p= roven in other parts of the codebase, hence less risky.

=C2=A0

While looking at 0002 and 0004 (which have a couple of issues
actually), I have been wondering about moving into a new file the four
data-type functions (in, out, send and receive) and the new input
functions that rely on a new JSON lexer and parser logic into for both
ndistinct and dependencies.=C2=A0 The new set of headers added at the top of mvdistinct.c and dependencies.c for the new code points that a
separation may be better in the long-term, because the new code relies
on parts of the backend that the existing code does not care about,
and these files become larger than the relation and attribute stats
files.=C2=A0 I would be tempted to name these new files pg_dependencies.c and pg_ndistinct.c, mapping with their catalog types.=C2=A0 With this
separation, it looks like the "core" parts in charge of the
calculations with ndistinct and dependencies can be kept on its own.
What do you think?

A part of me thinks = that everything that remains after removing in/out/send/recv is just taking= a table sample data structure and crunching numbers to come up with the de= serialized data structure...that's in/out with a different starting/end= ing points.

There's no denying that JSON parsi= ng is a very different code style than statistical number crunching, and mi= xing the two is incongruous, so it's worth a shot, and I'll try tha= t for v9.
=C2=A0

A second comment is for 0005.=C2=A0 The routines of attributes.c are
applied to the new clear and restore functions.=C2=A0 Shouldn't these b= e in
stats_utils.c at the end?=C2=A0 That's where the "common" fun= ctions used by
the stats manipulation logic are.

I assume you're referring to attribute_stats.c. I think that woul= d cause stats_utils.c to have to pull in a lot of things from attribute_sta= ts.c, and that would create the exact sort of include-pollution that you= 9;re trying to avoid in the mvdistinct.c/dependencies.c situation mentioned= above.

The one lone exception to this is text_to_= stavalues(), which is a fancy wrapper around array_in() and could perhaps b= e turned to even more generic usage outside of stats in general.
=
The functions in question are needed because the exprs=C2=A0= value is itself an array of partly-filled-out pg_attribute tuples, so it= 9;s common to those two needs, but specific to stats about attributes. Mayb= e we need an attr_stats_utils.h?
--00000000000061e085064308b539--