Re: First query on each connection is too slow

From: Andres Freund <andres(at)anarazel(dot)de>
To: Vadim Nevorotin <nevorotin(at)gmail(dot)com>
Cc: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: First query on each connection is too slow
Date: 2018-06-13 14:40:54
Message-ID: 20180613144054.v7bi6dwnq5wywgpd@alap3.anarazel.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2018-06-13 14:49:39 Re: First query on each connection is too slow
Previous Message Andres Freund 2018-06-13 14:38:17 Re: First query on each connection is too slow