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 1fT75o-0007OO-Qb for pgsql-general@arkaria.postgresql.org; Wed, 13 Jun 2018 14:49:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fT75n-0007yu-2E for pgsql-general@arkaria.postgresql.org; Wed, 13 Jun 2018 14:49:47 +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 1fT75m-0007yj-NL for pgsql-general@lists.postgresql.org; Wed, 13 Jun 2018 14:49:46 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fT75k-0004Fs-8W for pgsql-general@lists.postgresql.org; Wed, 13 Jun 2018 14:49:45 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id w5DEndZp029036; Wed, 13 Jun 2018 10:49:39 -0400 From: Tom Lane To: Andres Freund cc: Vadim Nevorotin , pgsql-general@lists.postgresql.org Subject: Re: First query on each connection is too slow In-reply-to: <20180613144054.v7bi6dwnq5wywgpd@alap3.anarazel.de> References: <20180613144054.v7bi6dwnq5wywgpd@alap3.anarazel.de> Comments: In-reply-to Andres Freund message dated "Wed, 13 Jun 2018 07:40:54 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <29034.1528901379.1@sss.pgh.pa.us> Date: Wed, 13 Jun 2018 10:49:39 -0400 Message-ID: <29035.1528901379@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Andres Freund writes: > On 2018-06-13 12:55:27 +0300, 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.: >> test_gis=# SELECT srid FROM geometry_columns WHERE >> f_table_name='planet_osm_polygon' AND f_geometry_column='way'; > What you're seeing is likely a mix of > a) Operating system overhead of doing copy-on-write the first time > memory is touched. This can be reduced to some degree by configuring > huge pages. > b) Postgres' caches over catalog contents (i.e. how your tables look > like) having to be filled on the first access. There's not really > much you can do about it. Seeing that this query seems to involve PostGIS, I suspect that there might be a third cause: time to load the PostGIS shared library. If so, you could probably alleviate the issue by adding postgis to shared_preload_libraries. If that doesn't fix it, (b) could perhaps be alleviated by adopting connection pooling, though that has costs of its own. regards, tom lane