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 1jz4wp-0005k8-4x for pgsql-general@arkaria.postgresql.org; Fri, 24 Jul 2020 21:09:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jz4wn-0006vP-LN for pgsql-general@arkaria.postgresql.org; Fri, 24 Jul 2020 21:09:41 +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 1jz4wn-0006vI-DW for pgsql-general@lists.postgresql.org; Fri, 24 Jul 2020 21:09:41 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jz4wj-0006Zh-83 for pgsql-general@lists.postgresql.org; Fri, 24 Jul 2020 21:09:41 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 06OL9XrT578095; Fri, 24 Jul 2020 17:09:33 -0400 From: Tom Lane To: Pavel Stehule cc: Christophe Pettus , David Rowley , postgre Subject: Re: Row estimates for empty tables In-reply-to: References: <373968.1595598516@sss.pgh.pa.us> Comments: In-reply-to Pavel Stehule message dated "Fri, 24 Jul 2020 21:14:04 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <578093.1595624973.1@sss.pgh.pa.us> Content-Transfer-Encoding: 8bit Date: Fri, 24 Jul 2020 17:09:33 -0400 Message-ID: <578094.1595624973@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Pavel Stehule writes: > pá 24. 7. 2020 v 16:38 odesílatel Christophe Pettus > 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