Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fRiw5-0007tn-6B for pgsql-general@arkaria.postgresql.org; Sat, 09 Jun 2018 18:50:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fRiw3-0006At-It for pgsql-general@arkaria.postgresql.org; Sat, 09 Jun 2018 18:49:59 +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.89) (envelope-from ) id 1fRiw3-0006Ae-8a for pgsql-general@lists.postgresql.org; Sat, 09 Jun 2018 18:49:59 +0000 Received: from mail-lf0-x244.google.com ([2a00:1450:4010:c07::244]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fRivv-0005bJ-39 for pgsql-general@lists.postgresql.org; Sat, 09 Jun 2018 18:49:58 +0000 Received: by mail-lf0-x244.google.com with SMTP id 36-v6so24762695lfr.11 for ; Sat, 09 Jun 2018 11:49:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=reply-to:from:subject:to:message-id:date:user-agent:mime-version :content-transfer-encoding:content-language; bh=ULn3sFt/fvB83jlJuZlu369E6bw97ZgpKowdwr2C3U0=; b=oPqdhwI4SSo8v+BD93PxRJs0Y3EPFpPBRxaktNxqJTK6w6ECGt4bl2jajNa61sMj2r GQRtam4pvnqgPYAPW54PwsFi8naGMTnhT2mXqVMxsbnEyAsixy2t10cWJ1HTBAV1gBzL xDAf8sgcBvyhx+36SEXXPEUBgDU+qSRJ1YmVUOfpjA1HP44Gl24FSQpGv7BdOKQaYJzl iIFMeUZEQO9WyAHMrlmw8Xdm2acpDi1GEdb9xL3lSQBmP3Fhk6jqLBBbH1DS/v3ZYDJI Yd4TdWgDiVZfGf60vMmfC1rCOp2JqVJAn+thW+ClByHUJG1iWa2YNrdlZJTKcoP6NXCZ dMJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:from:subject:to:message-id:date :user-agent:mime-version:content-transfer-encoding:content-language; bh=ULn3sFt/fvB83jlJuZlu369E6bw97ZgpKowdwr2C3U0=; b=OFH+eV2p4mY3qz3ep53Hq6SmAZOn1oeH+YmW8tCjODPKaOglKJna5orgUooXLhavvM nhUV7tUc5QVjA6bE99GRc87MWPGP0b8aJWY5NZtOPvJHiEQodl1p+7rkQ5tGPk2CLx/b uZfuNgsxOga+WAbmdQMD9TSZH1qc6umUPQEl0guLicgIZQDL0WMweprG4tAPmP+O/dMy S7FVecRNuv7e6cbDAciGjJTPqZ8NE+eM0iW2rRkhlOdzZCOsw9vHWrI/FrKXNB27b18h 5dygX5xlEpiHGtbOz7wzUROTTGxGh21D7w+NuPaXohsMijNGiI3LaunupCPu7chPbYlb n5GA== X-Gm-Message-State: APt69E3zaMV10fD2FtotmstJTvho4Oey23cO1CiL9pIbJhn64VG7AHZ9 xQMPG04WfSfmGqZNGIJHw3+wa0Zu X-Google-Smtp-Source: ADUXVKL44tuSGWImtN4hyBkMOqMfy/xCSXqLZstXv9dkicOJP1KIOJdQvHyfieQz0yq553o/vDcbiQ== X-Received: by 2002:a2e:2bcb:: with SMTP id r72-v6mr8009552ljr.133.1528570189264; Sat, 09 Jun 2018 11:49:49 -0700 (PDT) Received: from [10.3.1.85] ([185.62.200.137]) by smtp.gmail.com with ESMTPSA id y89-v6sm6874239lje.74.2018.06.09.11.49.47 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 09 Jun 2018 11:49:48 -0700 (PDT) Reply-To: 22136.1528312205@sss.pgh.pa.us From: Maksim Milyutin Subject: Re: Slow planning time for simple query To: pgsql-general@lists.postgresql.org Message-ID: <36adf760-680b-7a4a-e019-64f4eaaf6ff7@gmail.com> Date: Sat, 9 Jun 2018 21:49:47 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On hot standby I faced with the similar problem. The following simple query: SELECT array_to_json(array_agg(t)) from (     select *         from     main.message m             join main.message_instance mi on m.message_id = mi.message_id             join main.channel_type ct on mi.channel_type_id = ct.channel_type_id         where   m.user_id = 2152595             and ct.name = 'android'             and m.user_delete_ts is null         order by             case                 when read_ts is null then 0                 else 1             end,             m.message_id desc         limit 100     ) t; is planned 4.940 ms on master and *254.741* ms on standby. I tried to investigate the reasons of so long planning on replica and determined that index accesses on planning stage come to multiple heap scans. Here is the execution plan statistics of query above:  Aggregate (actual time=0.641..0.642 rows=1 loops=1)    Buffers: shared hit=14    ->  Subquery Scan on t (actual time=0.337..0.360 rows=2 loops=1)          Buffers: shared hit=14          ->  Limit (actual time=0.292..0.301 rows=2 loops=1)                Buffers: shared hit=14                ->  Sort (actual time=0.287..0.291 rows=2 loops=1)                      Sort Key: (CASE WHEN (m.read_ts IS NULL) THEN 0 ELSE 1 END), m.message_id DESC                      Sort Method: quicksort  Memory: 27kB                      Buffers: shared hit=14                      ->  Nested Loop (actual time=0.157..0.219 rows=2 loops=1)                            Buffers: shared hit=14                            ->  Seq Scan on channel_type ct (actual time=0.043..0.048 rows=1 loops=1)                                  Filter: (name = 'android'::text)                                  Rows Removed by Filter: 7                                  Buffers: shared hit=1                            ->  Nested Loop (actual time=0.098..0.148 rows=2 loops=1)                                  Buffers: shared hit=13                                  ->  Index Scan using message_user_id_idx1 on message m (actual time=0.055..0.063 rows=2 loops=1)                                        Index Cond: (user_id = 2152595)                                        Buffers: shared hit=5                                  ->  Index Scan using message_instance_pkey on message_instance mi (actual time=0.026..0.029 rows=1 loops=2)                                        Index Cond: ((message_id = m.message_id) AND (channel_type_id = ct.channel_type_id))                                        Buffers: shared hit=8 The 14 accesses to buffer cache under query execution. But pg_statio_user_tables and pg_statio_user_indexes views show the different picture: select     schemaname, relname, indexrelname, idx_blks_hit, idx_blks_read, pg_size_pretty(pg_relation_size(indexrelid::regclass)) from pg_statio_user_indexes where idx_blks_hit + idx_blks_read > 0;  schemaname |     relname      |     indexrelname      | idx_blks_hit | idx_blks_read | pg_size_pretty ------------+------------------+-----------------------+--------------+---------------+----------------  main       | channel_type     | channel_type_pkey |            2 |             0 | 16 kB  main       | message_instance | message_instance_pkey |          666 |             0 | 345 MB  main       | message          | message_pkey |           56 |             0 | 53 MB  main       | message          | message_user_id_idx1 |            3 |             0 | 17 MB select     schemaname, relname, heap_blks_read, heap_blks_hit from pg_statio_user_tables where heap_blks_read + heap_blks_hit + idx_blks_hit + idx_blks_read > 0;  schemaname |     relname      | heap_blks_read | heap_blks_hit ------------+------------------+----------------+---------------  main       | channel_type     |              0 |             3  main       | message          |              0 |          8682  main       | message_instance |              0 | 114922 (This experiment is carried out on test hot standby without parallel activities and with cleared system statistics) The vacuum on problem tables (main.message and main.message_instance) on master node resolves the problem somehow but its often execution slows down all queries and generally increases IO. Is there any case to overcome the problem or it's fundamental issue and necessary to rewrite the query to simplify planning? -- Regards, Maksim Milyutin