Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eLbbP-0005pP-O5 for pgsql-performance@arkaria.postgresql.org; Sun, 03 Dec 2017 21:15:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eLbbO-0003vM-Jc for pgsql-performance@arkaria.postgresql.org; Sun, 03 Dec 2017 21:15:06 +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_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eLbbO-0003v9-A9 for pgsql-performance@lists.postgresql.org; Sun, 03 Dec 2017 21:15:06 +0000 Received: from mail-lf0-x230.google.com ([2a00:1450:4010:c07::230]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1eLbbL-0007vf-Uo for pgsql-performance@lists.postgresql.org; Sun, 03 Dec 2017 21:15:05 +0000 Received: by mail-lf0-x230.google.com with SMTP id r143so16943770lfe.13 for ; Sun, 03 Dec 2017 13:15:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language; bh=LG0l4Xw+q0/G/LKGYMUlJpB0jdE3Wmzah9HHUvuz6+g=; b=vVdqsz6mPUrUG/kgwhTRUp8Sn0k5+Vox6uFXpejSg09X9MFbDUzGS/4w7I+Uu9KglR hYUwkd0/SAoAwwQosAChKg2mBdq7KuVIC6r2vOQA6R2cIXqVcLya+0UQkTAtxSG6mshY VknI66x7xGYH5c1kO8KYsZMBzdaX0rX5NP1WnyZfnBHWHC5IuBwBtb5pxbU7785sVqsd rGhrl9WkvFft2/RSNR/MuUnvA+Z06O8NvAkwlewnrFYnWcHYO8WfULZorY+p+E/2wUu9 +J5JhX1LcxidjVBljBEu6A88KmN8hvYqqJYCEoPPuf6BcTw75VfRdmwFwi50GzAP1K2r wvIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=LG0l4Xw+q0/G/LKGYMUlJpB0jdE3Wmzah9HHUvuz6+g=; b=G5x8Lgr7WafV3Q1MxkSW78q7glFdKYyP+HBy25Nvo4P3eo3uiM0a09IjY1FaXH60XZ 1yitNbX3HkwHGZ9C1FPht/a5p0BtWqNi5u/97ca8JAOMJFrvEKa3BvfaBjgqAqBCh0bT sK7M6b0+LyitusTD6BD6L/mKVk/xSQAps5++NhqdjCctraWZMtC9jl7zrNpsapvRY+u3 8qDczv9sfOPdqI6kRKNuSteOUvXIHLu5UxGL3v1/9Y91PehzFdLZ1hiT2NwGZzKaqDBo 4xUsi+y0jRgPunE4JFc79C12xMjV7EMCOw5jbFdrTPqeaM/f3s8lCu9FibFWO31NXbvL gjpA== X-Gm-Message-State: AJaThX7W9kINzvVTOfwAkbW2oWpy03xd8Jf6015nIwh50XfTOtwxk0iz wOGtXwTeXb3nW2FjBMZcRAo6UChY X-Google-Smtp-Source: AGs4zMbluF05dDXU7nuehKr2HrBTf+ra2NlLcb+lhJIivxbMIS1iOlmjcoQCYMnDptSorLbhGiv+3Q== X-Received: by 10.46.41.150 with SMTP id p22mr7591377ljp.5.1512335701781; Sun, 03 Dec 2017 13:15:01 -0800 (PST) Received: from [192.168.1.151] ([178.54.41.83]) by smtp.gmail.com with ESMTPSA id f78sm2438257lje.20.2017.12.03.13.15.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 03 Dec 2017 13:15:00 -0800 (PST) Subject: Re: Bitmap scan is undercosted? To: Jeff Janes Cc: Justin Pryzby , pgsql-performance@lists.postgresql.org References: <73a4936d-2814-dc08-ed0c-978f76f435b0@gmail.com> <20171201183427.GK18413@telsasoft.com> <20171201231104.GN18413@telsasoft.com> From: Vitaliy Garnashevich Message-ID: <501bd53b-bc66-bf58-7590-939dc5cba170@gmail.com> Date: Sun, 3 Dec 2017 23:15:01 +0200 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.5.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------2C800F711CF8673C16790551" 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. --------------2C800F711CF8673C16790551 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 02/12/2017 23:17, Jeff Janes wrote: > Right, so there is a cpu costing problem (which could only be fixed by > hacking postgresql and recompiling it), but it is much smaller of a > problem than the IO cost not being accurate due to the high hit rate. > Fixing the CPU costing problem is unlikely to make a difference to > your real query.  If you set the page costs to zero, what happens to > your real query? I can't reproduce the exact issue on the real database any more. The query started to use the slow bitmap scan recently, and had been doing so for some time lately, but now it's switched back to use the index scan. The table involved in the query gets modified a lot. It has hundreds of millions of rows. Lots of new rows are appended to it every day, the oldest rows are sometimes removed. The table is analyzed at least daily. It's possible that statistics was updated and that caused the query to run differently. But I still would like to understand why that issue happened, and how to properly fix it, in case the issue returns. > > But I doubt that the settings seq_page_cost = random_page_cost = > 0.0 should actually be used. > > > Why not?  If your production server really has everything in memory > during normal operation, that is the correct course of action.  If you > ever restart the server, then you could have some unpleasant time > getting it back up to speed again, but pg_prewarm could help with that. In the real database, not everything is in memory. There are 200GB+ of RAM, but DB is 500GB+. The table involved in the query itself is 60GB+ of data and 100GB+ of indexes. I'm running the test case in a way where all reads are done from RAM, only to make it easier to reproduce and to avoid unrelated effects. As far as know, costs in Postgres were designed to be relative to seq_page_cost, which for that reason is usually defined as 1.0. Even if everything would be in RAM, accesses to the pages would still not have zero cost. Setting 0.0 just seems too extreme, as all other non-zero costs would become infinitely bigger. > If you really want to target the plan with the BitmapAnd, you should > increase cpu_index_tuple_cost and/or cpu_operator_cost but not > increase cpu_tuple_cost.  That is because the  unselective bitmap > index scan does not incur any cpu_tuple_cost, but does incur > index_tuple and operator costs.  Unfortunately all other index scans > in the system will also be skewed by such a change if you make the > change system-wide. Exactly. I'd like to understand why the worse plan is being chosen, and 1) if it's fixable by tuning costs, to figure out the right settings which could be used in production, 2) if there is a bug in Postgres optimizer, then to bring some attention to it, so that it's eventually fixed in one of future releases, 3) if Postgres is supposed to work this way, then at least I (and people who ever read this thread) would understand it better. Regards, Vitaliy --------------2C800F711CF8673C16790551 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit
On 02/12/2017 23:17, Jeff Janes wrote:
Right, so there is a cpu costing problem (which could only be fixed by hacking postgresql and recompiling it), but it is much smaller of a problem than the IO cost not being accurate due to the high hit rate.  Fixing the CPU costing problem is unlikely to make a difference to your real query.  If you set the page costs to zero, what happens to your real query?
I can't reproduce the exact issue on the real database any more. The query started to use the slow bitmap scan recently, and had been doing so for some time lately, but now it's switched back to use the index scan. The table involved in the query gets modified a lot. It has hundreds of millions of rows. Lots of new rows are appended to it every day, the oldest rows are sometimes removed. The table is analyzed at least daily. It's possible that statistics was updated and that caused the query to run differently. But I still would like to understand why that issue happened, and how to properly fix it, in case the issue returns.
 
But I doubt that the settings seq_page_cost = random_page_cost = 0.0 should actually be used.

Why not?  If your production server really has everything in memory during normal operation, that is the correct course of action.  If you ever restart the server, then you could have some unpleasant time getting it back up to speed again, but pg_prewarm could help with that. 
In the real database, not everything is in memory. There are 200GB+ of RAM, but DB is 500GB+. The table involved in the query itself is 60GB+ of data and 100GB+ of indexes. I'm running the test case in a way where all reads are done from RAM, only to make it easier to reproduce and to avoid unrelated effects.

As far as know, costs in Postgres were designed to be relative to seq_page_cost, which for that reason is usually defined as 1.0. Even if everything would be in RAM, accesses to the pages would still not have zero cost. Setting 0.0 just seems too extreme, as all other non-zero costs would become infinitely bigger.
If you really want to target the plan with the BitmapAnd, you should increase  cpu_index_tuple_cost and/or cpu_operator_cost but not increase cpu_tuple_cost.  That is because the  unselective bitmap index scan does not incur any cpu_tuple_cost, but does incur index_tuple and operator costs.  Unfortunately all other index scans in the system will also be skewed by such a change if you make the change system-wide.
Exactly. I'd like to understand why the worse plan is being chosen, and 1) if it's fixable by tuning costs, to figure out the right settings which could be used in production, 2) if there is a bug in Postgres optimizer, then to bring some attention to it, so that it's eventually fixed in one of future releases, 3) if Postgres is supposed to work this way, then at least I (and people who ever read this thread) would understand it better.

Regards,
Vitaliy


--------------2C800F711CF8673C16790551--