RE: alter table xxx alter column yyy set (n_distinct=nnnn );

From: "Tefft, Michael J" <Michael(dot)J(dot)Tefft(at)snapon(dot)com>
To: Greg Sabino Mullane <htamfids(at)gmail(dot)com>, Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
Cc: "pgsql-general(at)lists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: RE: alter table xxx alter column yyy set (n_distinct=nnnn );
Date: 2024-03-11 17:49:43
Message-ID: BN8PR04MB62894EF8DC3C2336F9801D1ED0242@BN8PR04MB6289.namprd04.prod.outlook.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

OK, thanks to both of you, it is now working for me. Many thanks.

This is what I determined was the missing ingredient from the secret sauce…

One of the things I had tried (before posting here) was
Alter table xxx alter column p_id set statistics [varying values];

Including zero.

When I first tried the suggestion of also setting n_distinct_inherited, these columns were still set to “statistics 0”.

When I altered them back to the default value i.e.
Alter table xxx alter column p_id set statistics -1;
(or, in fact, any nonzero value), the desired n_distinct values appeared after ANALYZE.

So, to set n_distinct for column col1 in a partitioned table sss.xxx:

1. Make sure column col1 has “statistics” != 0
2. alter table sss.xxx alter column p_id set (n_distinct_inherited=[desired_value] );
3. analyze sss.xxx;
4. verify with: select attname, n_distinct from pg_stats where schemaname='sss' and tablename='xxx' and attname = ‘col1’;

So setting n_distinct on the column at the parent/partitioned-table level is not relevant for this.

Thanks again.

Mike Tefft

From: Greg Sabino Mullane <htamfids(at)gmail(dot)com>
Sent: Monday, March 11, 2024 12:23 PM
To: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
Cc: Tefft, Michael J <Michael(dot)J(dot)Tefft(at)snapon(dot)com>; pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: alter table xxx alter column yyy set (n_distinct=nnnn );

Works for me on Postgres 14 as well. Maybe you are looking at the wrong table? Might help to show the exact steps you typed in. ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍

Works for me on Postgres 14 as well. Maybe you are looking at the wrong table? Might help to show the exact steps you typed in.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Lorusso Domenico 2024-03-11 20:25:14 merge with view
Previous Message Michał Kłeczek 2024-03-11 17:40:47 Re: Pgxs - How to reference another extension