Can we use Statistics Import and Export feature to perforamance testing?
Can we use Statistics Import and Export feature to perforamance testing?
Hi,
I have a question about Statistics Import and Export.
* Background
I'm working for PGEcons[1], which is the PostgreSQL consortium in Japan.
Several companies participating in PGEcons have the following request for PostgreSQL.
They have two environments, production environment and staging environment.
Production environment has real customer's data and staging environment has dummy testing data.
When adding some application, they want to run the performance test on staging environment and
then apply to the production environment.
In the performance test, they want to use the same statistics as the production environment
to reduce the trouble in production environment.
* Question
By using Statistics Import and Export feature, is it possible to achieve the above request by following procedure?
(1) Export the statistics from production environment by using pg_dump --statistics-only.
(2) On the staging environment, set the autovacuum related parameters to prevent autovacuum from running.
(3) Import the statistics to staging environment by using the result of (1).
[1] PGECons is a non profit organization comprised of companies
in Japan to promote PostgreSQL (https://www.pgecons.org).
Regards,
Ryohei Takahashi
Re: Can we use Statistics Import and Export feature to perforamance testing?
On Tue, 8 Apr 2025 at 12:21, Ryohei Takahashi (Fujitsu) wrote: > By using Statistics Import and Export feature, is it possible to achieve the above request by following procedure? > (1) Export the statistics from production environment by using pg_dump --statistics-only. > (2) On the staging environment, set the autovacuum related parameters to prevent autovacuum from running. > (3) Import the statistics to staging environment by using the result of (1). You could certainly test the performance, but this method isn't guaranteed to give meaningful results just because the table stats match. One important thing to remember is that the planner also looks at the *actual size* of the relation and takes that into account when scaling the statistics (see table_block_relation_estimate_size() in tableam.c). If the table sizes don't match between the two servers then there's no guarantees the planner will produce the same plan. Also, there might be other subtleties regarding OIDs of indexes which are not guaranteed to be the same after dump/restore. Given some fuzzily close enough cost estimates (See add_path() and compare_path_costs_fuzzily()), it is possible a plan would switch to using another index if sorting the indexes by their OIDs didn't match on each server. The chances of that might be fairly small, but not zero. You'd also need to ensure the configs are the same in terms of GUCs that are used for costs. You could probably use get_relation_info_hook to overwrite the sizes and make sure the indexes are in the same order, etc. David
RE: Can we use Statistics Import and Export feature to perforamance testing?
Hi, Thank you for your reply. I understand that the access plans are not guaranteed to be the same. Can we add these notes to the pg_dump page in the PostgreSQL Documentation in order to prevent users from asking the same question? Regards, Ryohei Takahashi
Re: Can we use Statistics Import and Export feature to perforamance testing?
at the *actual size* of the relation and takes that into account when
scaling the statistics (see table_block_relation_estimate_size() in
tableam.c). If the table sizes don't match between the two servers
then there's no guarantees the planner will produce the same plan.
Re: Can we use Statistics Import and Export feature to perforamance testing?
* Question
By using Statistics Import and Export feature, is it possible to achieve the above request by following procedure?
(1) Export the statistics from production environment by using pg_dump --statistics-only.
(2) On the staging environment, set the autovacuum related parameters to prevent autovacuum from running.
(3) Import the statistics to staging environment by using the result of (1).
Re: Can we use Statistics Import and Export feature to perforamance testing?
On Sat, 12 Apr 2025 at 20:29, Corey Huinker wrote: >> >> at the *actual size* of the relation and takes that into account when >> scaling the statistics (see table_block_relation_estimate_size() in >> tableam.c). If the table sizes don't match between the two servers >> then there's no guarantees the planner will produce the same plan. > > Sorry that I didn't see this thread until now. I would like to note that table_block_relation_estimate_size() determines the actual size of the relation by asking pg_class, and the relevant values there are set by pg_restore_relation_stats(). Sorry, this isn't correct. I suspect you're probably misreading the code. On a fleeting glance, you might have seen the "relpages = (BlockNumber) rel->rd_rel->relpages;" line and come to this conclusion. A more careful study will reveal the truth. Check for "curpages = RelationGetNumberOfBlocks(rel);" and an unconditional "*pages = curpages;". You might be getting confused because the code does look at the pg_class fields, but that's only to estimate the tuple density. When pg_class has those estimates, they're used to calculate the estimated density by doing reltuples / relpages, but that average rows per page is then applied to the *actual* number of pages in the relation. This method allows the stats to be scaled as the table grows and that take effect without waiting for vacuum or analyze to update the pg_class fields. The planner checks the current size of the table every time it plans a query. That's very well understood and documented. See [1]. David [1] https://www.postgresql.org/docs/current/row-estimation-examples.html#ROW-ESTIMATION-EXAMPLES
Re: Can we use Statistics Import and Export feature to perforamance testing?
You might be getting confused because the code does look at the
pg_class fields, but that's only to estimate the tuple density. When
pg_class has those estimates, they're used to calculate the estimated
density by doing reltuples / relpages, but that average rows per page
Re: Can we use Statistics Import and Export feature to perforamance testing?
Hi,
On Tue, 8 Apr 2025 12:14:08 +0000
"Ryohei Takahashi (Fujitsu)" wrote:
> Thank you for your reply.
> I understand that the access plans are not guaranteed to be the same.
>
> Can we add these notes to the pg_dump page in the PostgreSQL Documentation
> in order to prevent users from asking the same question?
I agree that it would be helpful to add this description, since the wording
“statistics manipulation functions” might give the impression that they can
influence generated plans.
I’ve attached a patch that adds a new paragraph to the warning section of the
documentation on statistics manipulation functions:
Manually restored statistics do not guarantee that the same query plans
will be generated as in the source environment, since factors such as
relation sizes, index OIDs, and configuration parameters may affect
planner behavior.
What do you think?
Regards,
Yugo Nagata
--
Yugo Nagata
RE: Can we use Statistics Import and Export feature to perforamance testing?
Hi Nagata san, Thank you. Your patch is good for me. I think this kind of documentation can prevent users confusing. Regards, Ryohei Takahashi
Re: Can we use Statistics Import and Export feature to perforamance testing?
On Fri, 24 Oct 2025 00:00:42 +0000 "Ryohei Takahashi (Fujitsu)" wrote: > Hi Nagata san, > > > Thank you. > Your patch is good for me. > > I think this kind of documentation can prevent users confusing. Thank you for your review. I have registered this patch in the commitfest app. https://commitfest.postgresql.org/patch/6155/ Regards, Yugo Nagata -- Yugo Nagata
Re: Can we use Statistics Import and Export feature to perforamance testing?
On Wed, 27 May 2026 05:54:06 +0000 vellaipandiyan sm wrote: > I prepared a small documentation follow-up patch adding a cross-reference to the planner statistics documentation section from the statistics manipulation warning. > > The patch builds cleanly with: > > `make -C doc/src/sgml html` > > I will send the patch to the mailing list thread as well. I may have missed it, but I couldn't find the patch in the thread. Could you please point me to it? Regards, Yugo Nagata -- Yugo Nagata
Re: Can we use Statistics Import and Export feature to perforamance testing?
Hi, apologies to jump-in. Bear with me to share some observations. Please point out if there is any misunderstanding. Summary: Regarding the purpose, I think there are 3 concerns about this. 1)"relation sizes" is ambiguous 2)"Index OIDs" might be misleading 3)Histogram bounds feature can reinforce not guaranteeing plan Possible alternative: since factors such as actual relation file sizes, the creation order of indexes, actual data range in indexes, or configuration parameters may affect planner behavior. Reasons: 1)"relation sizes" is ambiguous I think "relation sizes" is ambiguous and could be confused with pg_class.relpages. I would suggest using "actual relation file sizes" instead. Would that be clearer? 2)"Index OIDs" might be misleading I initially misread this as meaning that statistics are restored by OID, but I think the intent is different. If two indexes have fuzzily equal costs, the planner may choose between them based on the order in which they are listed internally, which follows the order of their OIDs. Since index OIDs are not guaranteed to be the same after dump/restore, the chosen index could differ between environments even with identical statistics. If that is the correct interpretation, I would suggest rephrasing to something like "the creation order of indexes" to avoid the misunderstanding that statistics are identified or restored by OID. 3)Histogram bounds feature(get_actual_variable_range()) The planner uses actual data range read from indexes when a WHERE condition value falls outside the histogram bounds. This is done via get_actual_variable_range() in selfuncs.c. Since this reads live index data rather than restored statistics, the same restored statistics can produce different selectivity estimates if the actual data range in the target environment differs. I think this feature can strongly explain that we can not guarantee a plan to fix statistics. Any thoughts on this? Kenichiro Tanaka 2026年6月5日(金) 17:21 Yugo Nagata : > > On Wed, 27 May 2026 05:54:06 +0000 > vellaipandiyan sm wrote: > > > I prepared a small documentation follow-up patch adding a cross-reference to the planner statistics documentation section from the statistics manipulation warning. > > > > The patch builds cleanly with: > > > > `make -C doc/src/sgml html` > > > > I will send the patch to the mailing list thread as well. > > I may have missed it, but I couldn't find the patch in the thread. > > Could you please point me to it? > > Regards, > Yugo Nagata > > -- > Yugo Nagata > >