Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1b90xw-0004Mm-GY for pgsql-performance@arkaria.postgresql.org; Sat, 04 Jun 2016 02:05:32 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1b90xv-00007I-02 for pgsql-performance@arkaria.postgresql.org; Sat, 04 Jun 2016 02:05:31 +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 1b90xt-00006n-2O for pgsql-performance@postgresql.org; Sat, 04 Jun 2016 02:05:29 +0000 Received: from mail-oi0-x22e.google.com ([2607:f8b0:4003:c06::22e]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1b90xl-0005dE-1U for pgsql-performance@postgresql.org; Sat, 04 Jun 2016 02:05:28 +0000 Received: by mail-oi0-x22e.google.com with SMTP id k23so153481710oih.0 for ; Fri, 03 Jun 2016 19:05:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=B35AcwqeKXAX4M8bpago0vpVTj0wlaWz7fbFr9wIvLo=; b=jV2p+pxStz1TdSRv4KNAeIJPNEy8tQXmr62vTKzBbVeMl36eFhJ2uiFYx3gqbNxS7G f1Owh+/LjUQvNpnSfjshgh/hqnIXt1Uly/OjCPD6rNBe6LB9m+jS7GBiUjFX7qbF8RlB R5yr/g1uar3dFVTF5x/S1rOcc5IBbJTgQay4VpcKz0WAVYvDYvkgbhWi3qnPMASTgi+S yLtpgqN+A3Z1mIP8r7O7ivzn8Swq1iV0hwSdGZwjpwQZpX7m5z0utuT2bxJ1K+4xwVSo /vkNMZwJIwNI07ITyM7x4pwelsmebZLdtErRGrOqc843kHQ5pQFI+vR8Db4yVHQEz4IS ke8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=B35AcwqeKXAX4M8bpago0vpVTj0wlaWz7fbFr9wIvLo=; b=imXB12iTlptrIOmU2qQxXzzSS0Ak5ke9vzIAcstY0k52CmLF5iK1hwptHUJ9xR4bFB mY0p8LgyzrJJe1rRTXE6J7fGdfue2KDw7fk93UF/R7xVi3QnQwjlxgr2e8iV1qxwaNIK p6AEBej4M44vVLM2zvOHBRd/nNZbcTOmXQRrhgbKUiwUrNxA7YbO+sHHA9+0Krg0rMSG 5he1/5tw4qcIebjrsVofhLSovy/aJbuD0UHh8oNdpHtXyXbRexh9j+iduSqvFdNnFmeq GvCt+4F+eanguzMvJKluoR2ec3PipC850HfzPffiuA3jprQ8euen0o/Aa8JR1QL3zodb RtAw== X-Gm-Message-State: ALyK8tI6qcI3MeTjbWgN1RB333L28Qs1Ej+oEpKoG6AOWeFFcHlQqHdn1oqQ5JyNpPaaB5zdljmP92Ne7SJemQ== X-Received: by 10.202.91.7 with SMTP id p7mr3542584oib.16.1465005918498; Fri, 03 Jun 2016 19:05:18 -0700 (PDT) MIME-Version: 1.0 Received: by 10.202.204.2 with HTTP; Fri, 3 Jun 2016 19:05:17 -0700 (PDT) In-Reply-To: <20160603235406.GN23616@telsasoft.com> References: <20160524173914.GA11880@telsasoft.com> <20160525140034.GB21220@telsasoft.com> <20160603235406.GN23616@telsasoft.com> From: Claudio Freire Date: Fri, 3 Jun 2016 23:05:17 -0300 Message-ID: Subject: Re: index fragmentation on insert-only table with non-unique column To: Justin Pryzby Cc: postgres performance list Content-Type: text/plain; charset=UTF-8 X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org On Fri, Jun 3, 2016 at 8:54 PM, Justin Pryzby wrote: > As a test, I did SET effective_cache_size='1MB', before running explain, and > still does: > > | -> Index Scan using cdrs_huawei_pgwrecord_2016_05_29_recordopeningtime_idx on cdrs_huawei_pgwrecord_2016_05_29 (cost=0.44..1526689.49 rows=8342796 width=355) > | Index Cond: ((recordopeningtime >= '2016-05-29 00:00:00'::timestamp without time zone) AND (recordopeningtime < '2016-05-30 00:00:00'::timestamp without time zone)) > > I Set enable_indexscan=0, and got: > > | -> Bitmap Heap Scan on cdrs_huawei_pgwrecord_2016_05_29 (cost=168006.10..4087526.04 rows=8342796 width=355) > | Recheck Cond: ((recordopeningtime >= '2016-05-29 00:00:00'::timestamp without time zone) AND (recordopeningtime < '2016-05-30 00:00:00'::timestamp without time zone)) > | -> Bitmap Index Scan on cdrs_huawei_pgwrecord_2016_05_29_recordopeningtime_idx (cost=0.00..165920.40 rows=8342796 width=0) > | Index Cond: ((recordopeningtime >= '2016-05-29 00:00:00'::timestamp without time zone) AND (recordopeningtime < '2016-05-30 00:00:00'::timestamp without time zone)) > > Here's a minimal query which seems to isolate the symptom: > > ts=# explain (analyze,buffers) SELECT sum(duration) FROM cdrs_huawei_pgwrecord_2016_05_22 WHERE recordopeningtime>='2016-05-22' AND recordopeningtime<'2016-05-23'; > | Aggregate (cost=2888731.67..2888731.68 rows=1 width=8) (actual time=388661.892..388661.892 rows=1 loops=1) > | Buffers: shared hit=4058501 read=1295147 written=35800 > | -> Index Scan using cdrs_huawei_pgwrecord_2016_05_22_recordopeningtime_idx on cdrs_huawei_pgwrecord_2016_05_22 (cost=0.56..2867075.33 rows=8662534 w > |idth=8) (actual time=0.036..379332.910 rows=8575673 loops=1) > | Index Cond: ((recordopeningtime >= '2016-05-22 00:00:00'::timestamp without time zone) AND (recordopeningtime < '2016-05-23 00:00:00'::timestamp > | without time zone)) > | Buffers: shared hit=4058501 read=1295147 written=35800 > | Planning time: 0.338 ms > | Execution time: 388661.947 ms > > And here's an older one to avoid cache, with enable_indexscan=0 > |ts=# explain (analyze,buffers) SELECT sum(duration) FROM cdrs_huawei_pgwrecord_2016_05_08 WHERE recordopeningtime>='2016-05-08' AND recordopeningtime<'2016-05-09'; > | Aggregate (cost=10006286.58..10006286.59 rows=1 width=8) (actual time=44219.156..44219.156 rows=1 loops=1) > | Buffers: shared hit=118 read=1213887 written=50113 > | -> Bitmap Heap Scan on cdrs_huawei_pgwrecord_2016_05_08 (cost=85142.24..9985848.96 rows=8175048 width=8) (actual time=708.024..40106.062 rows=8179338 loops=1) > | Recheck Cond: ((recordopeningtime >= '2016-05-08 00:00:00'::timestamp without time zone) AND (recordopeningtime < '2016-05-09 00:00:00'::timestamp without time zone)) > | Rows Removed by Index Recheck: 74909 > | Heap Blocks: lossy=1213568 > | Buffers: shared hit=118 read=1213887 written=50113 > | -> Bitmap Index Scan on cdrs_huawei_pgwrecord_2016_05_08_recordopeningtime_idx1 (cost=0.00..83098.48 rows=8175048 width=0) (actual time=706.557..706.557 rows=12135680 loops=1) > | Index Cond: ((recordopeningtime >= '2016-05-08 00:00:00'::timestamp without time zone) AND (recordopeningtime < '2016-05-09 00:00:00'::timestamp without time zone)) > | Buffers: shared hit=117 read=320 > | Planning time: 214.786 ms > | Execution time: 44228.874 ms > |(12 rows) Correct me if I'm wrong, but this looks like the planner not accounting for correlation when using bitmap heap scans. Checking the source, it really doesn't. So correlated index scans look extra favourable vs bitmap index scans because bitmap heap scans consider random page costs sans correlation effects (even though correlation applies to bitmap heap scans as well). While that sounds desirable a priori, it seems it's hurting this case quite badly. I'm not sure there's any simple way of working around that. -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance