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 1geh8P-0001ci-Fr for pgsql-general@arkaria.postgresql.org; Wed, 02 Jan 2019 14:04:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1geh8M-0005hY-NR for pgsql-general@arkaria.postgresql.org; Wed, 02 Jan 2019 14:04:34 +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 1geh8M-0005hQ-BT for pgsql-general@lists.postgresql.org; Wed, 02 Jan 2019 14:04:34 +0000 Received: from mail-ot1-x330.google.com ([2607:f8b0:4864:20::330]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1geh8J-00052m-Oa for pgsql-general@lists.postgresql.org; Wed, 02 Jan 2019 14:04:33 +0000 Received: by mail-ot1-x330.google.com with SMTP id k98so26758466otk.3 for ; Wed, 02 Jan 2019 06:04:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language; bh=CvNH6v1VZRZcnnnlvOrD54VVC4gqGdynZkjqE8vDLkI=; b=CHTkOqNwVYprGcWV+y9ardHvXls/j4TFJWCG2594/k/pp4q3yzXXq1ppavOcNSmH8b aJ1DOlTnfTl6+BYN3XmcRfFEArlsu2854njN6Ovpk1ConWXqyydEwP2hTQ/2qHAN5RXW d01gqdFpl7n7ubpk7wQnxY4eczAtXHC2rTFCQWRrlw//Yey7s6Q7GH0iUtugaRUcq/zV Hz/N2KlNx4jgy0mBy/kRQrCujebKKGfCOz0/MCtJlGNLEEpv+aptdA47x9XtFwWjUdVz D/RRSnV1op+AS7tO139vCyYiCbJePyNQft0ymPJFEuXtRwXNt4DH/f3VeqQ5LmsnUhoM Ns5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=CvNH6v1VZRZcnnnlvOrD54VVC4gqGdynZkjqE8vDLkI=; b=cJmH3M66Azq4xxG2QtttGuvi/N7GjAUnmyJAEH8wIU6wiFb2LDa00Mur2jK3/tBPmU nJlb2FA/ZHeHi0DIEf17SZYOKgxi1i6ErxGl438h+ALi3CaZbaNA1SgAyGIkf0jZGGxM g6a+3cB5rqrjTJMYnFo7lmsJU8KKw33efgoP5smaqlLplKhSo0c35xvsp2YGbfOH3jbK glUxk6OUCLStoLuR2D+WcMxydVH09PDjMYOaRvC7/hN8x1qQzSaSwkD7/WyZ2yUmKCNz nf3qhixbitlq6mMHYZhc65x7RzNziol53RJCat841Z3cY7OWyAamqRVwSUFrNKMlQ0q6 xIFw== X-Gm-Message-State: AJcUukf8NW8rbaShnyJM1GSgnjRZKztVGXRq5rc1xhsNZC+lLhTHjwrx Z3ncyg20bQd0y81cobOH+i2roOkC X-Google-Smtp-Source: ALg8bN74DVU8YvmdC5zjfFB3zYJPqpaZE24UVKpaYrtlEp3w500/pd2B53ox4/bJ17sQXGXEYj0XIw== X-Received: by 2002:a9d:172e:: with SMTP id i46mr29037783ota.322.1546437868404; Wed, 02 Jan 2019 06:04:28 -0800 (PST) Received: from [192.168.1.10] (ip70-171-116-89.no.no.cox.net. [70.171.116.89]) by smtp.googlemail.com with ESMTPSA id v3sm47838889oib.57.2019.01.02.06.04.27 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 02 Jan 2019 06:04:27 -0800 (PST) Subject: Re: Query planner / Analyse statistics bad estimate rows=1 with maximum statistics 10000 on PostgreSQL 10.2 To: pgsql-general@lists.postgresql.org References: From: Ron Message-ID: <9e353edc-b04e-8273-f0b4-23bbd586fed1@gmail.com> Date: Wed, 2 Jan 2019 08:04:26 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------79F68E8DEB9300C43A455DA4" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------79F68E8DEB9300C43A455DA4 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit But Jeff said "left open indefinitely without either committing or rolling back".  Your process is committing the transaction. On 1/2/19 6:15 AM, Mark wrote: > Hi Jeff, > > Thanks for your help. That is exactly what is happening. > > 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. > > Thanks again for helping me understand what's happening here. > > Proof: > > db=> explain analyze select * from common_student where school_id = 36; >     QUERY PLAN > -------------------------------------------------------------------------------------------------------------------------------------- >  Bitmap Heap Scan on common_student  (cost=88.30..3846.49 rows=1533 > width=384) (actual time=4.852..7.065 rows=1388 loops=1) >    Recheck Cond: (school_id = 36) >    Heap Blocks: exact=67 >    ->  Bitmap Index Scan on idx_common_student_sid (cost=0.00..87.91 > rows=1533 width=0) (actual time=4.817..4.817 rows=1388 loops=1) >          Index Cond: (school_id = 36) >  Planning time: 0.097 ms >  Execution time: 8.084 ms > (7 rows) > > db=> /* At this point I have started a long running transaction that > deletes all of common_student for school_id 36  */ ; > > db=> analyse verbose common_student(school_id); > INFO:  analyzing "public.common_student" > INFO:  "common_student": scanned 7322 of 7322 pages, containing 65431 live > rows and 8060 dead rows; 56818 rows in sample, 65431 estimated total rows > ANALYZE > db=> explain analyze select * from common_student where school_id = 36; >        QUERY PLAN > -------------------------------------------------------------------------------------------------------------------------------------------- >  Index Scan using idx_common_student_sid on common_student  > (cost=0.41..8.43 rows=1 width=384) (actual time=0.017..1.498 rows=1388 > loops=1) >    Index Cond: (school_id = 36) >  Planning time: 0.098 ms >  Execution time: 2.583 ms > (4 rows) > > db=> /* At this point I have killed the long running transaction that > deletes all of common_student for school_id 36  */ ; > db=> vacuum analyze common_student; > VACUUM > db=> explain analyze select * from common_student where school_id = 36; >     QUERY PLAN > -------------------------------------------------------------------------------------------------------------------------------------- >  Bitmap Heap Scan on common_student  (cost=79.17..3357.79 rows=1388 > width=383) (actual time=0.088..1.302 rows=1388 loops=1) >    Recheck Cond: (school_id = 36) >    Heap Blocks: exact=67 >    ->  Bitmap Index Scan on idx_common_student_sid (cost=0.00..78.83 > rows=1388 width=0) (actual time=0.077..0.077 rows=1388 loops=1) >          Index Cond: (school_id = 36) >  Planning time: 0.327 ms >  Execution time: 2.311 ms > (7 rows) > > > On Sun, 23 Dec 2018 at 02:57 Jeff Janes > wrote: > > > - Does the analyse output below mean that it only scanned 51538 of > 65463 rows in the table? Is school_id 36 just being missed in the > sample? (This happens when the analyse is repeated ) > > > Is there a transaction which had deleted all of school_id=36, and then > was just left open indefinitely without either committing or rolling back? > > That would explain it, and I don't know of anything else that could.  > The deleted but not committed tuples are still live, but don't get > sampled. > > Cheers, > > Jeff > -- Angular momentum makes the world go 'round. --------------79F68E8DEB9300C43A455DA4 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit But Jeff said "left open indefinitely without either committing or rolling back".  Your process is committing the transaction.

On 1/2/19 6:15 AM, Mark wrote:
Hi Jeff, 

Thanks for your help. That is exactly what is happening.

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.

Thanks again for helping me understand what's happening here. 

Proof:

db=> explain analyze select * from common_student where school_id = 36;
                                                              QUERY PLAN                                                              
--------------------------------------------------------------------------------------------------------------------------------------
 Bitmap Heap Scan on common_student  (cost=88.30..3846.49 rows=1533 width=384) (actual time=4.852..7.065 rows=1388 loops=1)
   Recheck Cond: (school_id = 36)
   Heap Blocks: exact=67
   ->  Bitmap Index Scan on idx_common_student_sid  (cost=0.00..87.91 rows=1533 width=0) (actual time=4.817..4.817 rows=1388 loops=1)
         Index Cond: (school_id = 36)
 Planning time: 0.097 ms
 Execution time: 8.084 ms
(7 rows)

db=> /* At this point I have started a long running transaction that deletes all of common_student for school_id 36  */ ;

db=> analyse verbose common_student(school_id); 
INFO:  analyzing "public.common_student"
INFO:  "common_student": scanned 7322 of 7322 pages, containing 65431 live rows and 8060 dead rows; 56818 rows in sample, 65431 estimated total rows
ANALYZE
db=> explain analyze select * from common_student where school_id = 36;
                                                                 QUERY PLAN                                                                 
--------------------------------------------------------------------------------------------------------------------------------------------
 Index Scan using idx_common_student_sid on common_student  (cost=0.41..8.43 rows=1 width=384) (actual time=0.017..1.498 rows=1388 loops=1)
   Index Cond: (school_id = 36)
 Planning time: 0.098 ms
 Execution time: 2.583 ms
(4 rows)

db=> /* At this point I have killed the long running transaction that deletes all of common_student for school_id 36  */ ;
db=> vacuum analyze common_student; 
VACUUM
db=> explain analyze select * from common_student where school_id = 36;
                                                              QUERY PLAN                                                              
--------------------------------------------------------------------------------------------------------------------------------------
 Bitmap Heap Scan on common_student  (cost=79.17..3357.79 rows=1388 width=383) (actual time=0.088..1.302 rows=1388 loops=1)
   Recheck Cond: (school_id = 36)
   Heap Blocks: exact=67
   ->  Bitmap Index Scan on idx_common_student_sid  (cost=0.00..78.83 rows=1388 width=0) (actual time=0.077..0.077 rows=1388 loops=1)
         Index Cond: (school_id = 36)
 Planning time: 0.327 ms
 Execution time: 2.311 ms
(7 rows)


On Sun, 23 Dec 2018 at 02:57 Jeff Janes <jeff.janes@gmail.com> wrote:

- Does the analyse output below mean that it only scanned 51538 of 65463 rows in the table? Is school_id 36 just being missed in the sample? (This happens when the analyse is repeated )

Is there a transaction which had deleted all of school_id=36, and then was just left open indefinitely without either committing or rolling back?

That would explain it, and I don't know of anything else that could.  The deleted but not committed tuples are still live, but don't get sampled.

Cheers,

Jeff

--
Angular momentum makes the world go 'round.
--------------79F68E8DEB9300C43A455DA4--