Re: First query on each connection is too slow - Mailing list pgsql-general

From Andres Freund
Subject Re: First query on each connection is too slow
Date
Msg-id 20180613144054.v7bi6dwnq5wywgpd@alap3.anarazel.de
Whole thread Raw
In response to First query on each connection is too slow  (Vadim Nevorotin <nevorotin@gmail.com>)
Responses Re: First query on each connection is too slow  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-general
Hi,

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.:
> 
> $ 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.

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.

Is the overhead of this prohibitive for you, or are you merely curious?

- Andres


pgsql-general by date:

Previous
From: Andres Freund
Date:
Subject: Re: First query on each connection is too slow
Next
From: Tom Lane
Date:
Subject: Re: First query on each connection is too slow