pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedRow estimates for empty tables
11+ messages / 4 participants
[nested] [flat]
Commits from this thread:
2020-08-30 3d351d916b Redefine pg_class.reltuples to be -1 before the first VACUUM or ANALYZE.
* Row estimates for empty tables
@ 2020-07-24 04:01 Christophe Pettus <xof@thebuild.com>
2020-07-24 04:56 ` Re: Row estimates for empty tables David Rowley <dgrowleyml@gmail.com>
0 siblings, 1 reply; 11+ messages in thread
From: Christophe Pettus @ 2020-07-24 04:01 UTC (permalink / raw)
To: postgre <pgsql-general@lists.postgresql.org>
I realize I've never quite known this; where does the planner get the row estimates for an empty table? Example:
psql (11.8)
Type "help" for help.
xof=# CREATE TABLE t (i integer, t text, j integer);
CREATE TABLE
xof=# VACUUM ANALYZE t;
VACUUM
xof=# EXPLAIN ANALYZE SELECT * FROM t;
QUERY PLAN
------------------------------------------------------------------------------------------------
Seq Scan on t (cost=0.00..22.00 rows=1200 width=40) (actual time=0.015..0.015 rows=0 loops=1)
Planning Time: 5.014 ms
Execution Time: 0.094 ms
(3 rows)
xof=# INSERT INTO t values(1, 'this', 2);
INSERT 0 1
xof=# EXPLAIN ANALYZE SELECT * FROM t;
QUERY PLAN
------------------------------------------------------------------------------------------------
Seq Scan on t (cost=0.00..22.00 rows=1200 width=40) (actual time=0.010..0.011 rows=1 loops=1)
Planning Time: 0.039 ms
Execution Time: 0.021 ms
(3 rows)
xof=# VACUUM ANALYZE t;
VACUUM
xof=# EXPLAIN ANALYZE SELECT * FROM t;
QUERY PLAN
--------------------------------------------------------------------------------------------
Seq Scan on t (cost=0.00..1.01 rows=1 width=13) (actual time=0.008..0.008 rows=1 loops=1)
Planning Time: 0.069 ms
Execution Time: 0.019 ms
(3 rows)
xof=# DELETE FROM t;
DELETE 0
xof=# VACUUM ANALYZE t;
VACUUM
xof=# EXPLAIN ANALYZE SELECT * FROM t;
QUERY PLAN
------------------------------------------------------------------------------------------------
Seq Scan on t (cost=0.00..29.90 rows=1990 width=13) (actual time=0.004..0.004 rows=0 loops=1)
Planning Time: 0.034 ms
Execution Time: 0.015 ms
(3 rows)
--
-- Christophe Pettus
xof@thebuild.com
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Row estimates for empty tables
2020-07-24 04:01 Row estimates for empty tables Christophe Pettus <xof@thebuild.com>
@ 2020-07-24 04:56 ` David Rowley <dgrowleyml@gmail.com>
2020-07-24 13:48 ` Re: Row estimates for empty tables Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 11+ messages in thread
From: David Rowley @ 2020-07-24 04:56 UTC (permalink / raw)
To: Christophe Pettus <xof@thebuild.com>; +Cc: postgre <pgsql-general@lists.postgresql.org>
On Fri, 24 Jul 2020 at 16:01, Christophe Pettus <xof@thebuild.com> wrote:
> I realize I've never quite known this; where does the planner get the row estimates for an empty table? Example:
We just assume there are 10 pages if the relation has not yet been
vacuumed or analyzed. The row estimates you see are the number of
times 1 tuple is likely to fit onto a single page multiplied by the
assumed 10 pages. If you had made your table wider then the planner
would have assumed fewer rows
There's a comment that justifies the 10 pages, which, as of master is
in table_block_relation_estimate_size(). It'll be somewhere else in
pg12.
* HACK: if the relation has never yet been vacuumed, use a minimum size
* estimate of 10 pages. The idea here is to avoid assuming a
* newly-created table is really small, even if it currently is, because
* that may not be true once some data gets loaded into it. Once a vacuum
* or analyze cycle has been done on it, it's more reasonable to believe
* the size is somewhat stable.
*
* (Note that this is only an issue if the plan gets cached and used again
* after the table has been filled. What we're trying to avoid is using a
* nestloop-type plan on a table that has grown substantially since the
* plan was made. Normally, autovacuum/autoanalyze will occur once enough
* inserts have happened and cause cached-plan invalidation; but that
* doesn't happen instantaneously, and it won't happen at all for cases
* such as temporary tables.)
*
* We approximate "never vacuumed" by "has relpages = 0", which means this
* will also fire on genuinely empty relations. Not great, but
* fortunately that's a seldom-seen case in the real world, and it
* shouldn't degrade the quality of the plan too much anyway to err in
* this direction.
*
* If the table has inheritance children, we don't apply this heuristic.
* Totally empty parent tables are quite common, so we should be willing
* to believe that they are empty.
The code which decides if the table has been vacuumed here assumes it
has not if pg_class.relpages == 0. So even if you were to manually
vacuum the table the code here would think it's not yet been vacuumed.
David
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Row estimates for empty tables
2020-07-24 04:01 Row estimates for empty tables Christophe Pettus <xof@thebuild.com>
2020-07-24 04:56 ` Re: Row estimates for empty tables David Rowley <dgrowleyml@gmail.com>
@ 2020-07-24 13:48 ` Tom Lane <tgl@sss.pgh.pa.us>
2020-07-24 14:38 ` Re: Row estimates for empty tables Christophe Pettus <xof@thebuild.com>
0 siblings, 1 reply; 11+ messages in thread
From: Tom Lane @ 2020-07-24 13:48 UTC (permalink / raw)
To: David Rowley <dgrowleyml@gmail.com>; +Cc: Christophe Pettus <xof@thebuild.com>; postgre <pgsql-general@lists.postgresql.org>
David Rowley <dgrowleyml@gmail.com> writes:
> On Fri, 24 Jul 2020 at 16:01, Christophe Pettus <xof@thebuild.com> wrote:
>> I realize I've never quite known this; where does the planner get the row estimates for an empty table? Example:
> We just assume there are 10 pages if the relation has not yet been
> vacuumed or analyzed. The row estimates you see are the number of
> times 1 tuple is likely to fit onto a single page multiplied by the
> assumed 10 pages. If you had made your table wider then the planner
> would have assumed fewer rows
Yeah. Also note that since we have no ANALYZE stats in this scenario,
the row width estimate is going to be backed into via some guesses
based on column data types. (It's fine for fixed-width types, much
less fine for var-width.)
There's certainly not a lot besides tradition to justify the exact
numbers used in this case. However, we do have a good deal of
practical experience to justify the principle of "never assume a
table is empty, or even contains just one row, unless you're really
sure of that". Otherwise you tend to end up with nestloop joins that
will perform horrifically if you were wrong. The other join types
are notably less brittle.
regards, tom lane
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Row estimates for empty tables
2020-07-24 04:01 Row estimates for empty tables Christophe Pettus <xof@thebuild.com>
2020-07-24 04:56 ` Re: Row estimates for empty tables David Rowley <dgrowleyml@gmail.com>
2020-07-24 13:48 ` Re: Row estimates for empty tables Tom Lane <tgl@sss.pgh.pa.us>
@ 2020-07-24 14:38 ` Christophe Pettus <xof@thebuild.com>
2020-07-24 19:14 ` Re: Row estimates for empty tables Pavel Stehule <pavel.stehule@gmail.com>
0 siblings, 1 reply; 11+ messages in thread
From: Christophe Pettus @ 2020-07-24 14:38 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: David Rowley <dgrowleyml@gmail.com>; postgre <pgsql-general@lists.postgresql.org>
> On Jul 24, 2020, at 06:48, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>
> There's certainly not a lot besides tradition to justify the exact
> numbers used in this case.
Since we already special-case parent tables for partition sets, would a storage parameter that lets you either tell the planner "no, really, zero is reasonable here" or sets a minimum number of rows to plan for be reasonable? I happened to get bit by this tracking down an issue where several tables in a large query had zero rows, and the planner's assumption of a few pages worth caused some sub-optimal plans. The performance hit wasn't huge, but they were being joined to some *very* large tables, and the differences added up.
--
-- Christophe Pettus
xof@thebuild.com
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Row estimates for empty tables
2020-07-24 04:01 Row estimates for empty tables Christophe Pettus <xof@thebuild.com>
2020-07-24 04:56 ` Re: Row estimates for empty tables David Rowley <dgrowleyml@gmail.com>
2020-07-24 13:48 ` Re: Row estimates for empty tables Tom Lane <tgl@sss.pgh.pa.us>
2020-07-24 14:38 ` Re: Row estimates for empty tables Christophe Pettus <xof@thebuild.com>
@ 2020-07-24 19:14 ` Pavel Stehule <pavel.stehule@gmail.com>
2020-07-24 19:35 ` Re: Row estimates for empty tables Christophe Pettus <xof@thebuild.com>
2020-07-24 21:09 ` Re: Row estimates for empty tables Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 2 replies; 11+ messages in thread
From: Pavel Stehule @ 2020-07-24 19:14 UTC (permalink / raw)
To: Christophe Pettus <xof@thebuild.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; David Rowley <dgrowleyml@gmail.com>; postgre <pgsql-general@lists.postgresql.org>
pá 24. 7. 2020 v 16:38 odesílatel Christophe Pettus <xof@thebuild.com>
napsal:
>
>
> > On Jul 24, 2020, at 06:48, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> >
> > There's certainly not a lot besides tradition to justify the exact
> > numbers used in this case.
>
> Since we already special-case parent tables for partition sets, would a
> storage parameter that lets you either tell the planner "no, really, zero
> is reasonable here" or sets a minimum number of rows to plan for be
> reasonable? I happened to get bit by this tracking down an issue where
> several tables in a large query had zero rows, and the planner's assumption
> of a few pages worth caused some sub-optimal plans. The performance hit
> wasn't huge, but they were being joined to some *very* large tables, and
> the differences added up.
>
I did this patch ten years ago. GoodData application
https://www.gooddata.com/ uses Postgres lot, and this application stores
some results in tables (as guard against repeated calculations). Lot of
these tables have zero or one row.
Although we ran an ANALYZE over all tables - the queries on empty tables
had very bad plans, and I had to fix it by this patch. Another company uses
a fake one row in table - so there is no possibility to have a really empty
table.
It is an issue for special, not typical applications (this situation is
typical for some OLAP patterns) - it is not too often - but some clean
solution (instead hacking postgres) can be nice.
Regards
Pavel
> --
> -- Christophe Pettus
> xof@thebuild.com
>
>
>
>
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Row estimates for empty tables
2020-07-24 04:01 Row estimates for empty tables Christophe Pettus <xof@thebuild.com>
2020-07-24 04:56 ` Re: Row estimates for empty tables David Rowley <dgrowleyml@gmail.com>
2020-07-24 13:48 ` Re: Row estimates for empty tables Tom Lane <tgl@sss.pgh.pa.us>
2020-07-24 14:38 ` Re: Row estimates for empty tables Christophe Pettus <xof@thebuild.com>
2020-07-24 19:14 ` Re: Row estimates for empty tables Pavel Stehule <pavel.stehule@gmail.com>
@ 2020-07-24 19:35 ` Christophe Pettus <xof@thebuild.com>
1 sibling, 0 replies; 11+ messages in thread
From: Christophe Pettus @ 2020-07-24 19:35 UTC (permalink / raw)
To: Pavel Stehule <pavel.stehule@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; David Rowley <dgrowleyml@gmail.com>; postgre <pgsql-general@lists.postgresql.org>
> On Jul 24, 2020, at 12:14, Pavel Stehule <pavel.stehule@gmail.com> wrote:
>
> this application stores some results in tables (as guard against repeated calculations). Lot of these tables have zero or one row.
Yes, that's the situation we encountered, too. It's not very common (and even less common, I would assume, that it results in a bad plan), but it did in this case.
--
-- Christophe Pettus
xof@thebuild.com
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Row estimates for empty tables
2020-07-24 04:01 Row estimates for empty tables Christophe Pettus <xof@thebuild.com>
2020-07-24 04:56 ` Re: Row estimates for empty tables David Rowley <dgrowleyml@gmail.com>
2020-07-24 13:48 ` Re: Row estimates for empty tables Tom Lane <tgl@sss.pgh.pa.us>
2020-07-24 14:38 ` Re: Row estimates for empty tables Christophe Pettus <xof@thebuild.com>
2020-07-24 19:14 ` Re: Row estimates for empty tables Pavel Stehule <pavel.stehule@gmail.com>
@ 2020-07-24 21:09 ` Tom Lane <tgl@sss.pgh.pa.us>
2020-07-24 21:54 ` Re: Row estimates for empty tables Christophe Pettus <xof@thebuild.com>
2020-07-24 22:34 ` Re: Row estimates for empty tables Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 2 replies; 11+ messages in thread
From: Tom Lane @ 2020-07-24 21:09 UTC (permalink / raw)
To: Pavel Stehule <pavel.stehule@gmail.com>; +Cc: Christophe Pettus <xof@thebuild.com>; David Rowley <dgrowleyml@gmail.com>; postgre <pgsql-general@lists.postgresql.org>
Pavel Stehule <pavel.stehule@gmail.com> writes:
> pá 24. 7. 2020 v 16:38 odesílatel Christophe Pettus <xof@thebuild.com>
> napsal:
>> Since we already special-case parent tables for partition sets, would a
>> storage parameter that lets you either tell the planner "no, really, zero
>> is reasonable here" or sets a minimum number of rows to plan for be
>> reasonable?
> It is an issue for special, not typical applications (this situation is
> typical for some OLAP patterns) - it is not too often - but some clean
> solution (instead hacking postgres) can be nice.
The core issue here is "how do we know whether the table is likely to stay
empty?". I can think of a couple of more or less klugy solutions:
1. Arrange to send out a relcache inval when adding the first page to
a table, and then remove the planner hack for disbelieving relpages = 0.
I fear this'd be a mess from a system structural standpoint, but it might
work fairly transparently.
2. Establish the convention that vacuuming or analyzing an empty table
is what you do to tell the system that this state is going to persist.
That's more or less what the existing comments in plancat.c envision,
but we never made a definition for how the occurrence of that event
would be recorded in the catalogs, other than setting relpages > 0.
Rather than adding another pg_class column, I'm tempted to say that
vacuum/analyze should set relpages to a minimum of 1, even if the
relation has zero pages. That does get the job done:
regression=# create table foo(f1 text);
CREATE TABLE
regression=# explain select * from foo;
QUERY PLAN
--------------------------------------------------------
Seq Scan on foo (cost=0.00..23.60 rows=1360 width=32)
(1 row)
regression=# vacuum foo; -- doesn't help
VACUUM
regression=# explain select * from foo;
QUERY PLAN
--------------------------------------------------------
Seq Scan on foo (cost=0.00..23.60 rows=1360 width=32)
(1 row)
regression=# update pg_class set relpages = 1 where relname = 'foo';
UPDATE 1
regression=# explain select * from foo;
QUERY PLAN
----------------------------------------------------
Seq Scan on foo (cost=0.00..0.00 rows=1 width=32)
(1 row)
(We're still estimating one row, but that's as a result of different
decisions that I'm not nearly as willing to compromise on...)
regards, tom lane
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Row estimates for empty tables
2020-07-24 04:01 Row estimates for empty tables Christophe Pettus <xof@thebuild.com>
2020-07-24 04:56 ` Re: Row estimates for empty tables David Rowley <dgrowleyml@gmail.com>
2020-07-24 13:48 ` Re: Row estimates for empty tables Tom Lane <tgl@sss.pgh.pa.us>
2020-07-24 14:38 ` Re: Row estimates for empty tables Christophe Pettus <xof@thebuild.com>
2020-07-24 19:14 ` Re: Row estimates for empty tables Pavel Stehule <pavel.stehule@gmail.com>
2020-07-24 21:09 ` Re: Row estimates for empty tables Tom Lane <tgl@sss.pgh.pa.us>
@ 2020-07-24 21:54 ` Christophe Pettus <xof@thebuild.com>
1 sibling, 0 replies; 11+ messages in thread
From: Christophe Pettus @ 2020-07-24 21:54 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Pavel Stehule <pavel.stehule@gmail.com>; David Rowley <dgrowleyml@gmail.com>; postgre <pgsql-general@lists.postgresql.org>
> On Jul 24, 2020, at 14:09, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> Rather than adding another pg_class column, I'm tempted to say that
> vacuum/analyze should set relpages to a minimum of 1, even if the
> relation has zero pages.
If there's not an issue about relpages != actual pages on disk, that certain seems straight-forward, and no *more* hacky than the current situation.
--
-- Christophe Pettus
xof@thebuild.com
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Row estimates for empty tables
2020-07-24 04:01 Row estimates for empty tables Christophe Pettus <xof@thebuild.com>
2020-07-24 04:56 ` Re: Row estimates for empty tables David Rowley <dgrowleyml@gmail.com>
2020-07-24 13:48 ` Re: Row estimates for empty tables Tom Lane <tgl@sss.pgh.pa.us>
2020-07-24 14:38 ` Re: Row estimates for empty tables Christophe Pettus <xof@thebuild.com>
2020-07-24 19:14 ` Re: Row estimates for empty tables Pavel Stehule <pavel.stehule@gmail.com>
2020-07-24 21:09 ` Re: Row estimates for empty tables Tom Lane <tgl@sss.pgh.pa.us>
@ 2020-07-24 22:34 ` Tom Lane <tgl@sss.pgh.pa.us>
2020-07-25 00:37 ` Re: Row estimates for empty tables David Rowley <dgrowleyml@gmail.com>
2020-07-25 03:40 ` Re: Row estimates for empty tables Pavel Stehule <pavel.stehule@gmail.com>
1 sibling, 2 replies; 11+ messages in thread
From: Tom Lane @ 2020-07-24 22:34 UTC (permalink / raw)
To: pgsql-hackers@lists.postgresql.org; +Cc: Pavel Stehule <pavel.stehule@gmail.com>; Christophe Pettus <xof@thebuild.com>; David Rowley <dgrowleyml@gmail.com>
[ redirecting to -hackers ]
I wrote:
> The core issue here is "how do we know whether the table is likely to stay
> empty?". I can think of a couple of more or less klugy solutions:
> 1. Arrange to send out a relcache inval when adding the first page to
> a table, and then remove the planner hack for disbelieving relpages = 0.
> I fear this'd be a mess from a system structural standpoint, but it might
> work fairly transparently.
I experimented with doing this. It's not hard to code, if you don't mind
having RelationGetBufferForTuple calling CacheInvalidateRelcache. I'm not
sure whether that code path might cause any long-term problems, but it
seems to work OK right now. However, this solution causes massive
"failures" in the regression tests as a result of plans changing. I'm
sure that's partly because we use so many small tables in the tests.
Nonetheless, it's not promising from the standpoint of not causing
unexpected problems in the real world.
> 2. Establish the convention that vacuuming or analyzing an empty table
> is what you do to tell the system that this state is going to persist.
> That's more or less what the existing comments in plancat.c envision,
> but we never made a definition for how the occurrence of that event
> would be recorded in the catalogs, other than setting relpages > 0.
> Rather than adding another pg_class column, I'm tempted to say that
> vacuum/analyze should set relpages to a minimum of 1, even if the
> relation has zero pages.
I also tried this, and it seems a lot more promising: no existing
regression test cases change. So perhaps we should do the attached
or something like it.
regards, tom lane
Attachments:
set-relpages-nonzero-after-vacuum-or-analyze.patch (text/x-diff, 2.5K) download
diff --git a/src/backend/access/heap/vacuumlazy.c b/src/backend/access/heap/vacuumlazy.c
index 1bbc4598f7..243cdaa409 100644
--- a/src/backend/access/heap/vacuumlazy.c
+++ b/src/backend/access/heap/vacuumlazy.c
@@ -594,6 +594,19 @@ heap_vacuum_rel(Relation onerel, VacuumParams *params,
new_frozen_xid = scanned_all_unfrozen ? FreezeLimit : InvalidTransactionId;
new_min_multi = scanned_all_unfrozen ? MultiXactCutoff : InvalidMultiXactId;
+ /*
+ * Another corner case is that if the relation is physically zero-length,
+ * we don't want to record relpages=reltuples=0 in pg_class; what we want
+ * to record is relpages=1, reltuples=0. This tells the planner that the
+ * relation has been seen to be empty by VACUUM or ANALYZE, so it should
+ * not override a small measured table size.
+ */
+ if (new_rel_pages == 0)
+ {
+ Assert(new_live_tuples == 0);
+ new_rel_pages = 1;
+ }
+
vac_update_relstats(onerel,
new_rel_pages,
new_live_tuples,
diff --git a/src/backend/access/table/tableam.c b/src/backend/access/table/tableam.c
index 4b2bb29559..0a92450e8a 100644
--- a/src/backend/access/table/tableam.c
+++ b/src/backend/access/table/tableam.c
@@ -585,11 +585,9 @@ table_block_relation_estimate_size(Relation rel, int32 *attr_widths,
* doesn't happen instantaneously, and it won't happen at all for cases
* such as temporary tables.)
*
- * We approximate "never vacuumed" by "has relpages = 0", which means this
- * will also fire on genuinely empty relations. Not great, but
- * fortunately that's a seldom-seen case in the real world, and it
- * shouldn't degrade the quality of the plan too much anyway to err in
- * this direction.
+ * We test "never vacuumed" by seeing whether relpages = 0. VACUUM and
+ * ANALYZE will never set relpages to less than 1, even if the relation is
+ * in fact zero length.
*
* If the table has inheritance children, we don't apply this heuristic.
* Totally empty parent tables are quite common, so we should be willing
diff --git a/src/backend/commands/analyze.c b/src/backend/commands/analyze.c
index 924ef37c81..f0247e350f 100644
--- a/src/backend/commands/analyze.c
+++ b/src/backend/commands/analyze.c
@@ -620,6 +620,16 @@ do_analyze_rel(Relation onerel, VacuumParams *params,
visibilitymap_count(onerel, &relallvisible, NULL);
+ /*
+ * As in VACUUM, replace relpages=reltuples=0 with relpages=1,
+ * reltuples=0.
+ */
+ if (relpages == 0)
+ {
+ Assert(totalrows == 0);
+ relpages = 1;
+ }
+
vac_update_relstats(onerel,
relpages,
totalrows,
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Row estimates for empty tables
2020-07-24 04:01 Row estimates for empty tables Christophe Pettus <xof@thebuild.com>
2020-07-24 04:56 ` Re: Row estimates for empty tables David Rowley <dgrowleyml@gmail.com>
2020-07-24 13:48 ` Re: Row estimates for empty tables Tom Lane <tgl@sss.pgh.pa.us>
2020-07-24 14:38 ` Re: Row estimates for empty tables Christophe Pettus <xof@thebuild.com>
2020-07-24 19:14 ` Re: Row estimates for empty tables Pavel Stehule <pavel.stehule@gmail.com>
2020-07-24 21:09 ` Re: Row estimates for empty tables Tom Lane <tgl@sss.pgh.pa.us>
2020-07-24 22:34 ` Re: Row estimates for empty tables Tom Lane <tgl@sss.pgh.pa.us>
@ 2020-07-25 00:37 ` David Rowley <dgrowleyml@gmail.com>
1 sibling, 0 replies; 11+ messages in thread
From: David Rowley @ 2020-07-25 00:37 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: PostgreSQL Developers <pgsql-hackers@lists.postgresql.org>; Pavel Stehule <pavel.stehule@gmail.com>; Christophe Pettus <xof@thebuild.com>
On Sat, 25 Jul 2020 at 10:34, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> I wrote:
> > 1. Arrange to send out a relcache inval when adding the first page to
> > a table, and then remove the planner hack for disbelieving relpages = 0.
> > I fear this'd be a mess from a system structural standpoint, but it might
> > work fairly transparently.
>
> I experimented with doing this. It's not hard to code, if you don't mind
> having RelationGetBufferForTuple calling CacheInvalidateRelcache. I'm not
> sure whether that code path might cause any long-term problems, but it
> seems to work OK right now. However, this solution causes massive
> "failures" in the regression tests as a result of plans changing. I'm
> sure that's partly because we use so many small tables in the tests.
> Nonetheless, it's not promising from the standpoint of not causing
> unexpected problems in the real world.
I guess all these changes would be the planner moving towards a plan
that suits having fewer rows for the given table better. If so, that
does seem quite scary as we already have enough problems from the
planner choosing poor plans when it thinks there are fewer rows than
there actually are. Don't we need to keep something like the 10-page
estimate there so safer plans are produced before auto-vacuum gets in
and gathers some proper stats?
I think if anything we'd want to move in the direction of producing
more cautious plans when the estimated number of rows is low. Perhaps
especially so for when the planner opts to do things like perform a
non-parameterized nested loop join when it thinks the RelOptInfo with,
say 3, unbeknown-to-the-planner, correlated, base restrict quals that
are thought to produce just 1 row, but actually produce many more.
> > 2. Establish the convention that vacuuming or analyzing an empty table
> > is what you do to tell the system that this state is going to persist.
> > That's more or less what the existing comments in plancat.c envision,
> > but we never made a definition for how the occurrence of that event
> > would be recorded in the catalogs, other than setting relpages > 0.
> > Rather than adding another pg_class column, I'm tempted to say that
> > vacuum/analyze should set relpages to a minimum of 1, even if the
> > relation has zero pages.
>
> I also tried this, and it seems a lot more promising: no existing
> regression test cases change. So perhaps we should do the attached
> or something like it.
This sounds like a more plausible solution. At least this way there's
an escape hatch for people who suffer due to this.
David
^ permalink raw reply [nested|flat] 11+ messages in thread
* Re: Row estimates for empty tables
2020-07-24 04:01 Row estimates for empty tables Christophe Pettus <xof@thebuild.com>
2020-07-24 04:56 ` Re: Row estimates for empty tables David Rowley <dgrowleyml@gmail.com>
2020-07-24 13:48 ` Re: Row estimates for empty tables Tom Lane <tgl@sss.pgh.pa.us>
2020-07-24 14:38 ` Re: Row estimates for empty tables Christophe Pettus <xof@thebuild.com>
2020-07-24 19:14 ` Re: Row estimates for empty tables Pavel Stehule <pavel.stehule@gmail.com>
2020-07-24 21:09 ` Re: Row estimates for empty tables Tom Lane <tgl@sss.pgh.pa.us>
2020-07-24 22:34 ` Re: Row estimates for empty tables Tom Lane <tgl@sss.pgh.pa.us>
@ 2020-07-25 03:40 ` Pavel Stehule <pavel.stehule@gmail.com>
1 sibling, 0 replies; 11+ messages in thread
From: Pavel Stehule @ 2020-07-25 03:40 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Christophe Pettus <xof@thebuild.com>; David Rowley <dgrowleyml@gmail.com>
so 25. 7. 2020 v 0:34 odesílatel Tom Lane <tgl@sss.pgh.pa.us> napsal:
> [ redirecting to -hackers ]
>
> I wrote:
> > The core issue here is "how do we know whether the table is likely to
> stay
> > empty?". I can think of a couple of more or less klugy solutions:
>
For these special cases is probably possible to ensure ANALYZE before any
SELECT. When the table is created, then it is analyzed, and after that it
is published and used for SELECT. Usually this table is not modified ever.
Because it is a special case, then it is not necessarily too sophisticated
a solution. But for built in solution it can be designed more goneral
> > 1. Arrange to send out a relcache inval when adding the first page to
> > a table, and then remove the planner hack for disbelieving relpages = 0.
> > I fear this'd be a mess from a system structural standpoint, but it might
> > work fairly transparently.
>
> I experimented with doing this. It's not hard to code, if you don't mind
> having RelationGetBufferForTuple calling CacheInvalidateRelcache. I'm not
> sure whether that code path might cause any long-term problems, but it
> seems to work OK right now. However, this solution causes massive
> "failures" in the regression tests as a result of plans changing. I'm
> sure that's partly because we use so many small tables in the tests.
> Nonetheless, it's not promising from the standpoint of not causing
> unexpected problems in the real world.
>
> > 2. Establish the convention that vacuuming or analyzing an empty table
> > is what you do to tell the system that this state is going to persist.
> > That's more or less what the existing comments in plancat.c envision,
> > but we never made a definition for how the occurrence of that event
> > would be recorded in the catalogs, other than setting relpages > 0.
> > Rather than adding another pg_class column, I'm tempted to say that
> > vacuum/analyze should set relpages to a minimum of 1, even if the
> > relation has zero pages.
>
> I also tried this, and it seems a lot more promising: no existing
> regression test cases change. So perhaps we should do the attached
> or something like it.
>
I am sending a patch that is years used in GoodData.
I am not sure if the company uses 0 or 1, but I can ask.
Regards
Pavel
> regards, tom lane
>
>
Attachments:
fakepages.patch (text/x-patch, 1.9K) download
diff --git a/src/backend/access/table/tableam.c b/src/backend/access/table/tableam.c
index c814733b22..d9ca9904e1 100644
--- a/src/backend/access/table/tableam.c
+++ b/src/backend/access/table/tableam.c
@@ -33,6 +33,7 @@
/* GUC variables */
char *default_table_access_method = DEFAULT_TABLE_ACCESS_METHOD;
bool synchronize_seqscans = true;
+int fake_pages = 10;
/* ----------------------------------------------------------------------------
@@ -591,10 +592,10 @@ table_block_relation_estimate_size(Relation rel, int32 *attr_widths,
* Totally empty parent tables are quite common, so we should be willing
* to believe that they are empty.
*/
- if (curpages < 10 &&
+ if (curpages < fake_pages &&
relpages == 0 &&
!rel->rd_rel->relhassubclass)
- curpages = 10;
+ curpages = fake_pages;
/* report estimated # pages */
*pages = curpages;
diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c
index 11b3f050bc..6954274c28 100644
--- a/src/backend/utils/misc/guc.c
+++ b/src/backend/utils/misc/guc.c
@@ -2195,6 +2195,17 @@ static struct config_int ConfigureNamesInt[] =
NULL, NULL, NULL
},
+ {
+ {"fake_pages", PGC_USERSET, QUERY_TUNING_OTHER,
+ gettext_noop("Sets a number of pages for planner when relation has no pages."),
+ NULL,
+ GUC_UNIT_BLOCKS
+ },
+ &fake_pages,
+ 10, 0, INT_MAX,
+ NULL, NULL, NULL
+ },
+
{
{"max_standby_archive_delay", PGC_SIGHUP, REPLICATION_STANDBY,
gettext_noop("Sets the maximum delay before canceling queries when a hot standby server is processing archived WAL data."),
diff --git a/src/include/access/tableam.h b/src/include/access/tableam.h
index eb18739c36..f997f9dbc4 100644
--- a/src/include/access/tableam.h
+++ b/src/include/access/tableam.h
@@ -30,6 +30,8 @@
extern char *default_table_access_method;
extern bool synchronize_seqscans;
+extern int fake_pages;
+
struct BulkInsertStateData;
struct IndexInfo;
^ permalink raw reply [nested|flat] 11+ messages in thread
end of thread, other threads:[~2020-07-25 03:40 UTC | newest]
Thread overview: 11+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-07-24 04:01 Row estimates for empty tables Christophe Pettus <xof@thebuild.com>
2020-07-24 04:56 ` David Rowley <dgrowleyml@gmail.com>
2020-07-24 13:48 ` Tom Lane <tgl@sss.pgh.pa.us>
2020-07-24 14:38 ` Christophe Pettus <xof@thebuild.com>
2020-07-24 19:14 ` Pavel Stehule <pavel.stehule@gmail.com>
2020-07-24 19:35 ` Christophe Pettus <xof@thebuild.com>
2020-07-24 21:09 ` Tom Lane <tgl@sss.pgh.pa.us>
2020-07-24 21:54 ` Christophe Pettus <xof@thebuild.com>
2020-07-24 22:34 ` Tom Lane <tgl@sss.pgh.pa.us>
2020-07-25 00:37 ` David Rowley <dgrowleyml@gmail.com>
2020-07-25 03:40 ` Pavel Stehule <pavel.stehule@gmail.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox