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.96) (envelope-from ) id 1vLBWY-007GuL-0x for pgsql-hackers@arkaria.postgresql.org; Tue, 18 Nov 2025 02:32:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vLBWW-003OhI-37 for pgsql-hackers@arkaria.postgresql.org; Tue, 18 Nov 2025 02:32:53 +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.96) (envelope-from ) id 1vLBWW-003OhA-1l for pgsql-hackers@lists.postgresql.org; Tue, 18 Nov 2025 02:32:52 +0000 Received: from mail-il1-x135.google.com ([2607:f8b0:4864:20::135]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vLBWT-0005cu-2w for pgsql-hackers@lists.postgresql.org; Tue, 18 Nov 2025 02:32:51 +0000 Received: by mail-il1-x135.google.com with SMTP id e9e14a558f8ab-4330d78f935so35008715ab.2 for ; Mon, 17 Nov 2025 18:32:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1763433170; x=1764037970; 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=MpZUS3Zvy3ihSQoLXeTictF0OUThfWylhgTWDJhKbEE=; b=nYPYkFdsnCWIj/spAgDi+OzF4v8NFidfhtJdbBhoBODVrTzEQ9LGtr2LGxX03+y3v2 FZQqpWBKZ87hq4NkgpoBDuQf8u9AIcm//5lV9gV/UJqjC+w7UEYd4M0SetVjlLZMjVgn qXKfT4aXAPslBBFX7WSrJdpbF+ATdaI32ROcmjELobuugKT11Hm5b8KTJCanY7sejVin 16X08aW6DPHcNG5DLL0U7eWQsPgeJS+bG82f3Jvbs12esdiGWreJjHLEHvETdeFMlTcx OYbQCsMtzGg0HmJ3tofEpCQZn28w9vG+pvrPPA6SVXvcX/b5/5sLRHrd1+dk2rF+Hldd 0tHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763433170; x=1764037970; 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=MpZUS3Zvy3ihSQoLXeTictF0OUThfWylhgTWDJhKbEE=; b=p0dAnByBKAMabjhcnpPHDHykz3eGmb8a+8Z14WSaatsOusYqN+Hn++wd2Fj9gY3q8X +lkVQVY+DOEpfAFxAC1zAHej9zVk3VSLWthp4b0kH+5YJcb6gjbctlmBwB1bOJBRkitQ 0WdbriDmGS/ieFEN3a4pWePv+G18tgFCf3HGvx/G22YyjS9dVmjx578mxCFkhoU7qxyo 4NwMIwRr60Qm27O3IF1vaRz8Ve+b4bKD4OhZp7GtAnpc6dQo68ChallsMCB0dsN/VMp2 3xce7EaHPKTMnecoqNcvvDYhqjlygwRpN/PnBzLnB4ISM6BZGO0RO0JxR5P6wpQUK2w2 6I4w== X-Forwarded-Encrypted: i=1; AJvYcCUyS1g08WLUhPaLaHseHkA99C5ViFrr+HZ4NXeiCnj/SOLS/niwd0A4U5TxC3yi8ifJJQXkhORjz6aPJZqV@lists.postgresql.org X-Gm-Message-State: AOJu0YwxwhIFbyPHju5chnS+jgF10zhLtWEGbXafZOUbPK5g7VeyWQ2Z 2fb6eGbAeqgffwMu9qLX7IHcg/1nVx817oxHCo4llDYg7vBzTDghEyWTcjda32PsPQKNIcfg3HO Lvn3g9GEio4C+vYwH3ce10S0Rr4ZQ+D0= X-Gm-Gg: ASbGncvL07XRvU9Scz/TmXNHga/D+FgThsPX1MMVXW7YivQTA26w4DfUTrTxwBEo50P kVv/lcluRQUZy6K7N1jH3azEoLgohGI34OU5GcYTobL67xbO9lvmaLh6ebQAaCNsBSMPLAT7a1g Eo6ElgN9nD2vnNgtPwsrweuIZ3dKDn6nCEKs+iU0w7J8sp/byqzPBHOy9lZvEWLia+SbZ59BcQ+ oEEvip4DQZAnOmW7PpqPXTeJMRuuR3WRYoqbSBF7zKpdTgPwKM3G+u+3o03q5KV30zqugE/4RCf g0L1R4u0IHj5tzhz49s45rzr0GA= X-Google-Smtp-Source: AGHT+IEliCFBcCUtJnUQG/fEpT2eSKJ6BQvFHxf9C2F4XSP2nYaShqSDAFzrPmpX6PXQ/D8OMvK7iGVGJkPypH81lTM= X-Received: by 2002:a92:ca46:0:b0:433:6c43:27fa with SMTP id e9e14a558f8ab-4348c8b757amr212832205ab.9.1763433169817; Mon, 17 Nov 2025 18:32:49 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Corey Huinker Date: Mon, 17 Nov 2025 21:32:37 -0500 X-Gm-Features: AWmQ_bkWbE7fYl5OznZm-6AwWsxvrVZrzqpRhjGrJ4t0olpWS7lRSNR6A11oh6U Message-ID: Subject: Re: Extended Statistics set/restore/clear functions. To: Michael Paquier Cc: jian he , Tomas Vondra , pgsql-hackers@lists.postgresql.org, tgl@sss.pgh.pa.us Content-Type: multipart/alternative; boundary="000000000000ac17d00643d547d4" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000ac17d00643d547d4 Content-Type: text/plain; charset="UTF-8" > > > This feels like a different pretty still compressed output for json. > I don't think we should change the output functions to do that, but if > you want to add a function that filters these contents a bit in the > tests for the input functions, sure, why not. > +1, I'll probably just use the replaces rather than define a function and give someone the false impression that the function exists elsewhere. > > Yes, this one is to reduce the translation work, and because the > messages are quite the same across the board and deal with the same > requirements: > - Single integer expected after a key (attnum or actual value). > - Array of attribute expected after a key. > - For the degree key, float value. > > > I had a feeling that was going to be requested. My question would be if > > that we want to stick to modeling the other combinations after the first > > longest combination, last longest, or if we want to defer those checks > > altogether until we have to validate against an actual stats object? > > I would tend to think that performing one round of validation once the > whole set of objects has been parsed is going to be cheaper than > periodic checks. > > One other thing would be to force a sort of the elements in the array > to match with the order these are generated when creating the stats. > We cannot do that in the input functions because we have no idea about > the order of the attributes in the statistics object yet. Applying a > sort sounds also important to me to make sure that we order the stats > based on what the group generation functions (aka > generate_combinations(), etc.) think on the matter, which would > enforce a stronger binary compatibility after we are sure that we have > a full set of attributes listed in an array with the input function of > course. I have briefly looked at the planner code where extended > stats are used, like selfuncs.c, and the ordering does not completely > matter, it seems, but it's cheap enough to enforce a stricter ordering > based on the K groups of N elements generated in the import function. > > >> Except for this argument, the input of pg_ndistinct feels OK in terms > >> of the guarantees that we'd want to enforce on an import. The same > >> argument applies in terms of attribute number guarantees for > >> pg_dependencies, based on DependencyGenerator_init() & friends in > >> dependencies.c. Could you look at that? > > > > Yes. I had already looked at it to verify that _all_ combinations were > > always generated (they are), because I had some vague memory of the > > generator dropping combinations that were statistically insignificant. In > > retrospect, I have no idea where I got that idea. > > Hmm. I would need to double-check the code to be sure, but I don't > think that we drop combinations, because the code prevents duplicates > to begin with, even for expressions: > create table aa (a int, b int); > create statistics stats (ndistinct) ON a, a, b, b from aa; > ERROR: 42701: duplicate column name in statistics definition > create statistics stats (ndistinct) ON (a + a), ((a + a)) from aa; > ERROR: 42701: duplicate expression in statistics definition > So I looked at the generator functions, hoping they'd have enough in common that they could be made generic. And they're just different enough that I think it's not worth it to try. But, if we don't care about the order of the combinations, I also don't think we need to expose the functions at all. We know exactly how many combinations there should be for any N attributes as each attribute must be unique. So if we have the right number of unique combinations, and they're all subsets of the first-longest, then we must have a complete set. Thoughts on that? Getting _too_ tight with the ordering and contents makes me concerned for the day when the format might change. We don't want to _fail_ an upgrade because some of the combinations were in the wrong order. > These don't make sense anyway because they have a predictible and > perfectly matching correlation relationship. > They do, for now, but are we willing to lock ourselves into that forever? > > > This is fairly simple to do. The dependency attnum is just appended to > the > > list of attnums, and the combinations are generated the same as > ndistinct, > > though obviously there are no single elements. > > Yeah. That should be not be bad, I hope. > > > There's probably some common code between the lists to be shared, > differing > > only in how they report missing combinations. > > I would like to agree on that, but it did not look that obvious to me > yesterday. If you think that something could be refactored, I'd > suggest a refactoring patch that applies on top of the rest of the > patch set, with new generic facilities in stat_util.c, or even a > new separate file, if that leads to a cleaner result (okay, a > definition of "clean" is up to one's taste). > Looking over those functions, they both could have use the same generator, but the dependencies-side decided that dependency order doesn't matter, which puts doubt in my head that the order is perfectly the same for both, so we'd better follow each individually IF we want to enforce order. --000000000000ac17d00643d547d4 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

This feels like a different pretty still compressed output for json.
I don't think we should change the output functions to do that, but if<= br> you want to add a function that filters these contents a bit in the
tests for the input functions, sure, why not.

+1, I'll probably just use the replaces rather than define a fun= ction and give someone the false impression that the function exists elsewh= ere.
=C2=A0

Yes, this one is to reduce the translation work, and because the
messages are quite the same across the board and deal with the same
requirements:
- Single integer expected after a key (attnum or actual value).
- Array of attribute expected after a key.
- For the degree key, float value.

> I had a feeling that was going to be requested. My question would be i= f
> that we want to stick to modeling the other combinations after the fir= st
> longest combination, last longest, or if we want to defer those checks=
> altogether until we have to validate against an actual stats object?
I would tend to think that performing one round of validation once the
whole set of objects has been parsed is going to be cheaper than
periodic checks.

One other thing would be to force a sort of the elements in the array
to match with the order these are generated when creating the stats.
We cannot do that in the input functions because we have no idea about
the order of the attributes in the statistics object yet.=C2=A0 Applying a<= br> sort sounds also important to me to make sure that we order the stats
based on what the group generation functions (aka
generate_combinations(), etc.) think on the matter, which would
enforce a stronger binary compatibility after we are sure that we have
a full set of attributes listed in an array with the input function of
course.=C2=A0 I have briefly looked at the planner code where extended
stats are used, like selfuncs.c, and the ordering does not completely
matter, it seems, but it's cheap enough to enforce a stricter ordering<= br> based on the K groups of N elements generated in the import function.

>> Except for this argument, the input of pg_ndistinct feels OK in te= rms
>> of the guarantees that we'd want to enforce on an import.=C2= =A0 The same
>> argument applies in terms of attribute number guarantees for
>> pg_dependencies, based on DependencyGenerator_init() & friends= in
>> dependencies.c.=C2=A0 Could you look at that?
>
> Yes. I had already looked at it to verify that _all_ combinations were=
> always generated (they are), because I had some vague memory of the > generator dropping combinations that were statistically insignificant.= In
> retrospect, I have no idea where I got that idea.

Hmm.=C2=A0 I would need to double-check the code to be sure, but I don'= t
think that we drop combinations, because the code prevents duplicates
to begin with, even for expressions:
create table aa (a int, b int);
create statistics stats (ndistinct) ON a, a, b, b from aa;
ERROR:=C2=A0 42701: duplicate column name in statistics definition
create statistics stats (ndistinct) ON (a + a), ((a + a)) from aa;
ERROR:=C2=A0 42701: duplicate expression in statistics definition

So I looked at the generator functions= , hoping they'd have enough in common that they could be made generic. And they're just different enough tha= t I think it's not worth it to try.

But, if we don&#= 39;t care about the order of the combinations, I also=20 don't think we need to expose the functions at all. We know exactly how= =20 many combinations there should be for any N attributes as each attribute must be unique. So if we have the right number of unique combinations,=20 and they're all subsets of the first-longest, then we must have a=20 complete set. Thoughts on that?

Getting _too_ tigh= t with the ordering and contents makes me concerned for the day when the fo= rmat might change. We don't want to _fail_ an upgrade because some of t= he combinations were in the wrong order.

=C2=A0
These don't make sense anyway because they have a predictible and
perfectly matching correlation relationship.

They do, for now, but are we willing to lock ourselves into that fore= ver?
=C2=A0

> This is fairly simple to do. The dependency attnum is just appended to= the
> list of attnums, and the combinations are generated the same as ndisti= nct,
> though obviously there are no single elements.

Yeah.=C2=A0 That should be not be bad, I hope.

> There's probably some common code between the lists to be shared, = differing
> only in how they report missing combinations.

I would like to agree on that, but it did not look that obvious to me
yesterday.=C2=A0 If you think that something could be refactored, I'd suggest a refactoring patch that applies on top of the rest of the
patch set, with new generic facilities in stat_util.c, or even a
new separate file, if that leads to a cleaner result (okay, a
definition of "clean" is up to one's taste).
=

Looking over those functions, they both could have= use the same generator, but the dependencies-side decided that dependency = order doesn't matter, which puts doubt in my head that the order is per= fectly the same for both, so we'd better follow each individually IF we= want to enforce order.

--000000000000ac17d00643d547d4--