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 1fT5jz-0000SN-Nn for pgsql-general@arkaria.postgresql.org; Wed, 13 Jun 2018 13:23:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fT5jy-0008Io-6f for pgsql-general@arkaria.postgresql.org; Wed, 13 Jun 2018 13:23:10 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fT5j9-0002tC-Fm for pgsql-general@lists.postgresql.org; Wed, 13 Jun 2018 13:22:21 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fT5j5-0002HP-Bt for pgsql-general@lists.postgresql.org; Wed, 13 Jun 2018 13:22:17 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id AAADC21C2C; Wed, 13 Jun 2018 09:22:13 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Wed, 13 Jun 2018 09:22:13 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; bh=0hoz1yj8TkpekYIgGdFeqMdVCkkdZ vFE/Uz1AMUOJ9Y=; b=rFClaWhL8W09Y0xyA4+dluckc912Rik9iIK5/qOQZSVlf pQKSxtW2KZHDtDdS0INRPCharaSZvkSsmLx2qcwUZqvRIVUOu1SfMMwLPpDaPyBR H1VDH8Fi/cSqFBrTUwH/ynFVgtIUE//Mz8MxGuz6fvTCpUsnITI1WYRgL/YgnCu1 xzzsGFaFefeFnLlNHSP6M4wbnOsUQe1SKclEbmaegDE7jGXxUijGy4UH1mFSE1/X 9U5NtSjiFkRvhbElygOM9CGEb+2vDhSSbV1Q0YjlRsObAbTwB0B6kXNnEpMsyMTB 5QQXaOC8v//aMFu/qe+MKNRc/D8M2g+9NG4W5l5aA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; bh=0hoz1y j8TkpekYIgGdFeqMdVCkkdZvFE/Uz1AMUOJ9Y=; b=ad14eym1wPBAW7QKquJucJ kEkSKskmL6DRCUhdX0YP8OeucRL3xMLAffn0PoZq/+uhw3FHPTKhnuBzO2lR2zB/ 4OF8rp+J5pmA2tsupDqOBab8RC6I9UTVCVVg/Z7xz9Or5tpyM33ecNBAB5l0sIoW OkZsgNNTpXqzZrlN9zoVgd1ruYvPsfD8DbBzC2NftVuk/TRxT2oBhr33xGADJwtD P0TYwYh2djbWmOXtmZxlkoYkhDy6t3gurAftWKgIhihSopdeu5Kak8d4RNtMM24p j73AhgsurAqibaLTvJCPQqZflpumbK36cqdN8XW7d2QDA/razBpoEG4X72qLgFCQ == X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Sender: Received: from [192.168.1.3] (unknown [71.212.159.209]) by mail.messagingengine.com (Postfix) with ESMTPA id BFA7810278; Wed, 13 Jun 2018 09:22:12 -0400 (EDT) Subject: Re: First query on each connection is too slow To: Vadim Nevorotin , pgsql-general@lists.postgresql.org References: From: Adrian Klaver Message-ID: Date: Wed, 13 Jun 2018 06:22:11 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 06/13/2018 02:55 AM, Vadim Nevorotin wrote: > I have a very strange problem. I'm using PostgreSQL 9.6 with PostGIS 2.3 > (both from Debian Strecth repos) to store DB for OSM server (but > actually it doesn't matter). And I've noticed, that on each new > connection to DB first query is much slower (10x) than all others. E.g.: > > $ psql test_gis > psql (9.6.7) > Type "help" for help. > test_gis=# \timing > Timing is on. > test_gis=# SELECT srid FROM geometry_columns WHERE > f_table_name='planet_osm_polygon' AND f_geometry_column='way'; >  srid > ------ >  3857 > (1 row) > Time: 52.889 ms > test_gis=# SELECT srid FROM geometry_columns WHERE > f_table_name='planet_osm_polygon' AND f_geometry_column='way'; >  srid > ------ >  3857 > (1 row) > Time: 2.070 ms > test_gis=# > > If I run others instances of psql in parallel, when the first is active > - they has absolutely the same problem. In one instance of psql query is > fast (if it's not the first query), in others - first query is slow, but > all others is fast. The EXPLAIN plan below is for the exact query above, correct? What is the EXPLAIN ANALYZE for the second run of the query? Does the Postgres log show anything around the time you make the query that might relate? > > EXPLAIN (ANALYZE, BUFFERS) for the first queryshows very strange timings: > >               QUERY PLAN > > -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- > >  Nested Loop Left Join  (cost=0.82..26.41 rows=1 width=4) *(actual > time=49.290..49.297 rows=1 loops=1)* >    Join Filter: ((s_2.connamespace = n.oid) AND (s_2.conrelid = c.oid) > AND (a.attnum = ANY (s_2.conkey))) >    Buffers: shared hit=18 >    ->  Nested Loop Left Join  (cost=0.82..25.28 rows=1 width=14) > (actual time=0.135..0.141 rows=1 loops=1) >          Join Filter: ((s_1.connamespace = n.oid) AND (s_1.conrelid = > c.oid) AND (a.attnum = ANY (s_1.conkey))) >          Buffers: shared hit=17 >          ->  Nested Loop Left Join  (cost=0.82..24.16 rows=1 width=14) > (actual time=0.126..0.132 rows=1 loops=1) >                Join Filter: ((s.connamespace = n.oid) AND (s.conrelid = > c.oid) AND (a.attnum = ANY (s.conkey))) >                Buffers: shared hit=16 >                ->  Nested Loop  (cost=0.82..23.05 rows=1 width=14) > (actual time=0.083..0.089 rows=1 loops=1) >                      Buffers: shared hit=15 >                      ->  Nested Loop  (cost=0.55..17.75 rows=1 > width=18) (actual time=0.078..0.082 rows=1 loops=1) >                            Join Filter: (c.relnamespace = n.oid) >                            Rows Removed by Join Filter: 5 >                            Buffers: shared hit=12 >                            ->  Nested Loop  (cost=0.55..16.61 rows=1 > width=18) (actual time=0.072..0.074 rows=1 loops=1) >                                  Buffers: shared hit=11 >                                  ->  Index Scan using > pg_class_relname_nsp_index on pg_class c  (cost=0.27..8.30 rows=1 > width=8) (actual time=0.049..0.050 rows=1 loops=1) >                                        Index Cond: (relname = > 'planet_osm_polygon'::name) >                                        Filter: ((NOT > pg_is_other_temp_schema(relnamespace)) AND has_table_privilege(oid, > 'SELECT'::text) AND (relkind = ANY ('{r,v,m,f}'::"char"[]))) >                                        Buffers: shared hit=8 >                                  ->  Index Scan using > pg_attribute_relid_attnam_index on pg_attribute a  (cost=0.28..8.30 > rows=1 width=14) (actual time=0.021..0.021 rows=1 loops=1) >                                        Index Cond: ((attrelid = c.oid) > AND (attname = 'way'::name)) >                                        Filter: (NOT attisdropped) >                                        Buffers: shared hit=3 >                            ->  Seq Scan on pg_namespace n >  (cost=0.00..1.06 rows=6 width=4) (actual time=0.003..0.004 rows=6 loops=1) >                                  Buffers: shared hit=1 >                      ->  Index Scan using pg_type_oid_index on pg_type > t  (cost=0.27..5.29 rows=1 width=4) (actual time=0.005..0.007 rows=1 > loops=1) >                            Index Cond: (oid = a.atttypid) >                            Filter: (typname = 'geometry'::name) >                            Buffers: shared hit=3 >                ->  Seq Scan on pg_constraint s  (cost=0.00..1.09 rows=1 > width=31) (actual time=0.041..0.041 rows=0 loops=1) >                      Filter: (consrc ~~* '%geometrytype(% = %'::text) >                      Rows Removed by Filter: 7 >                      Buffers: shared hit=1 >          ->  Seq Scan on pg_constraint s_1  (cost=0.00..1.09 rows=1 > width=31) (actual time=0.009..0.009 rows=0 loops=1) >                Filter: (consrc ~~* '%ndims(% = %'::text) >                Rows Removed by Filter: 7 >                Buffers: shared hit=1 >    ->  Seq Scan on pg_constraint s_2  (cost=0.00..1.09 rows=1 width=77) > (actual time=0.011..0.011 rows=0 loops=1) >          Filter: (consrc ~~* '%srid(% = %'::text) >          Rows Removed by Filter: 7 >          Buffers: shared hit=1 > * Planning time: 2.589 ms >  Execution time: 49.467 ms* > > (45 rows) > > The same problem is when connecting to PostgreSQL from Mapnik, so it's > not a psql problem. We have a lot of memory (shared_buffers = 20GB, > work_mem = 100MB) and a very small db (we've upload to empty db data > only for one very small county). So I'm absolutely sure that it's the > problem of main PostgreSQL. > > And there are two questions: why this problem occurs and how can I fix it? -- Adrian Klaver adrian.klaver@aklaver.com