Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jKjR4-0005Xv-9Q for pgsql-general@arkaria.postgresql.org; Sat, 04 Apr 2020 14:06:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jKjR2-0005tU-4L for pgsql-general@arkaria.postgresql.org; Sat, 04 Apr 2020 14:06:08 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jKjR1-0005tM-SL for pgsql-general@lists.postgresql.org; Sat, 04 Apr 2020 14:06:07 +0000 Received: from mail.hjp.at ([212.17.106.138] helo=rorschach.hjp.at) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jKjQu-0007rq-Cu for pgsql-general@lists.postgresql.org; Sat, 04 Apr 2020 14:06:04 +0000 Received: by rorschach.hjp.at (Postfix, from userid 1000) id EE64D2B6F; Sat, 4 Apr 2020 16:05:57 +0200 (CEST) Date: Sat, 4 Apr 2020 16:05:57 +0200 From: "Peter J. Holzer" To: pgsql-general@lists.postgresql.org Subject: Re: Why there is 30000 rows is sample Message-ID: <20200404140557.GA10460@hjp.at> References: <3D72D8D12557453AB3E0EB46FB911C17@dell2> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="IJpNTDwzlM2Ie8A6" Content-Disposition: inline In-Reply-To: <3D72D8D12557453AB3E0EB46FB911C17@dell2> User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --IJpNTDwzlM2Ie8A6 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2020-04-04 10:07:51 +0300, Andrus wrote: > vacuumdb: vacuuming database "mydb" > INFO: analyzing "public.mytable" > INFO: "mytable": scanned 2709 of 2709 pages, containing 10834 live rows = and > 0 dead rows; 10834 rows in sample, 10834 estimated total rows >=20 > For tables with more than 30000 rows, it shows that there are 30000 rows = in sample. >=20 > postgresql.conf does not set default_statistics_target value. > It contains >=20 > #default_statistics_target =3D 100 # range 1-10000 >=20 > So I expect that there should be 100 rows is sample. The statistics target determines the size of gathered statistics: A value of 100 means that Postgres should store (at most) the 100 most frequent values and that the histogram should have 100 buckets. Obviously you can't determine the 100 most frequent values if you on=F6y sample 100 rows, and a histogram with 100 buckets based on only 100 rows will be quite empty. So you need to sample more rows to compute those statistics. How much more? As it turns out (see the link Julien posted), it depends a bit on the size of the table, but not that much, so a factor of 300 is good enough for a wide range of sizes. > Why Postgres uses 30000 or number of rows in table for smaller tables ? >=20 > Is 30000 some magical value, how to control it. The factor of 300 is a bit magical but well founded. You can't control it (except by changing the source code and recompiling, of course - but why would you want to?). The value 100 can be controlled either by changing default_statistics_target or by changing the statistics target of a specific column of a specific table (alter table ... alter column =2E.. set statistics ...) hp --=20 _ | Peter J. Holzer | Story must make more sense than reality. |_|_) | | | | | hjp@hjp.at | -- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!" --IJpNTDwzlM2Ie8A6 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAl6IlEEACgkQ8g5IURL+ KF2z0A//anoJuFMDzN9BR2SGOVy1rbwioTnOCOFM3p6K/J4vbgB90uA3OEL1k7zk zsxhay+9LE3uvnGpZOdI4z7u7QFgKWRRvsIqkewjIwt8TyB6Ke/KNwSHPRFk+KAI d1LEAP7rwZCPTGsFt5XIQDtP17m5UwU7UruY/M16ozpUpGA+o+bQAdkoWnPB02Kk vaFb4wONsapuCnGfskbWNlULStcDDdYpYiuIRpIRTCmsPSXfd/YsDxej4cm/tWrp 9rcBvAUX/G80YeoqhtPct9HCf3NTi7LwKZSnON0Q9ea6j69G3HBjaLTOfXlqLe7/ 6TsrBuouvL5hpuLn5XrEZydFgG6kjjbFEoetqiqd674Xd8zEImAuzH6N3Uy2LoxD xjDOxK823xZj0ovLeuT5/HIlfuS85peUS2V+903Xfpnicl7IdBeYQQpGms4/CVuQ mEEI5vs6rR9/tf1pliAzY5PtdwRXM0fRx9xcHlNq3/JmTbyWkXUpWvSBJL7yQ9g3 KNYxzJNCBN1+n9d8hfQrIMbiESypELQ0v0EOzT2+i8sRqN+jYftDCVrUdWAXwdax PGzKizj7FJdlz9WkjjpPJo3UJIz0sDTeXj7UY9FMCyV1xZ6oj+wXV/UsymeikrjT ZFXIdDGjX2xxJ5yIv2qIoaXJxdPAPh2S6/Wh5gC8ATxLH4NRR9s= =Z7yS -----END PGP SIGNATURE----- --IJpNTDwzlM2Ie8A6--