Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gej7T-0006ar-Ob for pgsql-hackers@arkaria.postgresql.org; Wed, 02 Jan 2019 16:11:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gej7S-00067p-3o for pgsql-hackers@arkaria.postgresql.org; Wed, 02 Jan 2019 16:11:46 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gej7R-00067d-TC; Wed, 02 Jan 2019 16:11:45 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gej7P-0007xL-OE; Wed, 02 Jan 2019 16:11:45 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x02GBdKW002026; Wed, 2 Jan 2019 11:11:39 -0500 From: Tom Lane To: Mark cc: Jeff Janes , pgsql-general@lists.postgresql.org, pgsql-hackers@lists.postgresql.org Subject: Re: Query planner / Analyse statistics bad estimate rows=1 with maximum statistics 10000 on PostgreSQL 10.2 In-reply-to: References: Comments: In-reply-to Mark message dated "Wed, 02 Jan 2019 12:15:24 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2024.1546445499.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Wed, 02 Jan 2019 11:11:39 -0500 Message-ID: <2025.1546445499@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Mark writes: > I have a long running job which deletes all of the common_student table = and > then repopulates it. It takes long time to load all the other data and > commit the transaction. I didn't think the delete inside the transaction > would have any effect until it is commited or rolled back. > I will have to rewrite the application so it updates the existing rows > rather than deleting all and then inserting. Hmm ... I'm not sure if that will actually make things better. The root of the issue is what analyze.c does with DELETE_IN_PROGRESS tuples: * We count delete-in-progress rows as still live, usi= ng * the same reasoning given above; but we don't bother= to * include them in the sample. The "reasoning given above" is a reference to what happens for INSERT_IN_PROGRESS tuples: * Insert-in-progress rows are not counted. We assume * that when the inserting transaction commits or abor= ts, * it will send a stats message to increment the prope= r * count. This works right only if that transaction e= nds * after we finish analyzing the table; if things happ= en * in the other order, its stats update will be * overwritten by ours. However, the error will be la= rge * only if the other transaction runs long enough to * insert many tuples, so assuming it will finish afte= r us * is the safer option. Now the problem with this, from your perspective, is that *neither* INSERT_IN_PROGRESS nor DELETE_IN_PROGRESS tuples are included in ANALYZE's data sample. So a long-running update transaction will cause all the rows it changes to be excluded from the sample until commit. This seems like a bad thing, and it definitely means that adjusting your app as you're contemplating won't help. In order to handle the bulk-update scenario sanely, it seems like we ought to allow one of INSERT_IN_PROGRESS and DELETE_IN_PROGRESS tuples to be included. And it looks like, for consistency with the row-counting logic, the one that's included needs to be DELETE_IN_PROGRESS. This is a slightly annoying conclusion, because it means we're accumulating stats that we know are likely to soon be obsolete, but I think sampling INSERT_IN_PROGRESS tuples instead would lead to very strange results in some cases. In short, I think we want to do this to the DELETE_IN_PROGRESS logic: if (TransactionIdIsCurrentTransactionId(HeapTupleHeade= rGetUpdateXid(targtuple.t_data))) deadrows +=3D 1; else + { + sample_it =3D true; liverows +=3D 1; + } with suitable adjustment of the adjacent comment. Thoughts? regards, tom lane