Re: Postgres chooses slow query plan from time to time

2021-09-16 Thread Kristjan Mustkivi
Understood. Thank you so much for looking into this! Best regards, Kristjan On Wed, Sep 15, 2021 at 5:34 PM Tom Lane wrote: > > Kristjan Mustkivi writes: > > On Wed, Sep 15, 2021 at 3:16 PM Tom Lane wrote: > >> Note the lack of any visible cast on the varchar column, in each one of > >> thes

Re: Postgres chooses slow query plan from time to time

2021-09-15 Thread Tom Lane
Kristjan Mustkivi writes: > On Wed, Sep 15, 2021 at 3:16 PM Tom Lane wrote: >> Note the lack of any visible cast on the varchar column, in each one of >> these queries, even where I tried to force one to appear. There is >> something happening in your database that is not happening in mine. > T

Re: Postgres chooses slow query plan from time to time

2021-09-15 Thread Kristjan Mustkivi
On Wed, Sep 15, 2021 at 3:16 PM Tom Lane wrote: > Note the lack of any visible cast on the varchar column, in each one of > these queries, even where I tried to force one to appear. There is > something happening in your database that is not happening in mine. > > My mind is now running to the p

Re: Postgres chooses slow query plan from time to time

2021-09-15 Thread Tom Lane
Kristjan Mustkivi writes: > Both are of type varchar(30). Ah, right, you showed that back at the top of the thread. > So is this something odd: Filter: (((product_code)::text = ($1)::text) > AND ((balance_type)::text = ($4)::text)) ? Yes, that is very darn odd. When I try this I get: regressi

Re: Postgres chooses slow query plan from time to time

2021-09-14 Thread Kristjan Mustkivi
Hello! Both are of type varchar(30). So is this something odd: Filter: (((product_code)::text = ($1)::text) AND ((balance_type)::text = ($4)::text)) ? But why does it do the type-cast if both product_code and balance_type are of type text (although with constraint 30) and the values are also of

Re: Postgres chooses slow query plan from time to time

2021-09-14 Thread Tom Lane
Kristjan Mustkivi writes: >>> Filter: (((product_code)::text = ($1)::text) AND >>> ((balance_type)::text = ($4)::text)) > But the Primary Key is defined as btree (cage_code, cage_player_id, > product_code, balance_type, version) so this should be exactly that > (apart from the extra "version" col

Re: Postgres chooses slow query plan from time to time

2021-09-14 Thread Kristjan Mustkivi
On Tue, Sep 14, 2021 at 5:15 PM Tom Lane wrote: > > Kristjan Mustkivi writes: > > -> Index Scan Backward using player_balance_history_idx2 on > > mytable pbh (cost=0.70..21639.94 rows=3885 width=66) (actual > > time=5934.153..5934.153 rows=1 loops=1) > > Index Cond: ((

Re: Postgres chooses slow query plan from time to time

2021-09-14 Thread Tom Lane
Kristjan Mustkivi writes: > -> Index Scan Backward using player_balance_history_idx2 on > mytable pbh (cost=0.70..21639.94 rows=3885 width=66) (actual > time=5934.153..5934.153 rows=1 loops=1) > Index Cond: ((cage_code = $3) AND (cage_player_id = > $2) AND (modified_tim

Re: Postgres chooses slow query plan from time to time

2021-09-14 Thread Kristjan Mustkivi
I am very sorry, I indeed copy-pasted an incomplete plan. Here it is in full: 2021-09-14 06:55:33 UTC, pid=27576 db=mydb, usr=myuser, client=ip, app=PostgreSQL JDBC Driver, line=55 LOG: duration: 5934.165 ms plan: Query Text: SELECT * FROM myschema.mytable pbh WHERE pbh.product_code =

Re: Postgres chooses slow query plan from time to time

2021-09-14 Thread Jeff Janes
On Tue, Sep 14, 2021 at 3:55 AM Kristjan Mustkivi wrote: > Hello Tomas, > > The auto explain analyze caught this: > > 2021-09-14 06:55:33 UTC, pid=12345 db=mydb, usr=myuser, client=ip, > app=PostgreSQL JDBC Driver, line=55 LOG: duration: 5934.165 ms plan: > Query Text: SELECT * FROM mysche

Re: Postgres chooses slow query plan from time to time

2021-09-14 Thread Laurenz Albe
On Tue, 2021-09-14 at 10:55 +0300, Kristjan Mustkivi wrote: > 2021-09-14 06:55:33 UTC, pid=12345  db=mydb, usr=myuser, client=ip, > app=PostgreSQL JDBC Driver, line=55 LOG:  duration: 5934.165 ms  plan: >   Query Text: SELECT *   FROM myschema.mytable pbh WHERE > pbh.product_code = $1   AND pbh.cag

Re: Postgres chooses slow query plan from time to time

2021-09-14 Thread Kristjan Mustkivi
Hi Jeff, The specialized index is present due to some other queries and the index is used frequently (according to the statistics). I do agree that in this particular case the index btree (cage_code, cage_player_id, product_code, balance_type, modified_time) would solve the problem but at the mome

Re: Postgres chooses slow query plan from time to time

2021-09-14 Thread Kristjan Mustkivi
Hello Tomas, The auto explain analyze caught this: 2021-09-14 06:55:33 UTC, pid=12345 db=mydb, usr=myuser, client=ip, app=PostgreSQL JDBC Driver, line=55 LOG: duration: 5934.165 ms plan: Query Text: SELECT * FROM myschema.mytable pbh WHERE pbh.product_code = $1 AND pbh.cage_player_id = $

Re: Postgres chooses slow query plan from time to time

2021-09-13 Thread Jeff Janes
On Mon, Sep 13, 2021 at 9:25 AM Kristjan Mustkivi wrote: > > I have caught this with AUTOEXPLAIN: > > Index Cond: ((cage_code = $3) AND (cage_player_id = $2) AND > (modified_time < $5)) > Filter: (((product_code)::text = ($1)::text) AND > ((balance_type)::text = ($4)::text)) >

Re: Postgres chooses slow query plan from time to time

2021-09-13 Thread Jeff Janes
On Mon, Sep 13, 2021 at 9:25 AM Kristjan Mustkivi wrote: > SELECT > * > FROM > myschema.mytable pbh > WHERE > pbh.product_code = $1 > AND pbh.cage_player_id = $2 > AND pbh.cage_code = $3 > AND balance_type = $4 > AND pbh.modified_time < $5 > ORDER BY > pbh.modifie

Re: Postgres chooses slow query plan from time to time

2021-09-13 Thread Justin Pryzby
On Mon, Sep 13, 2021 at 08:19:40AM -0600, Michael Lewis wrote: > Autovacuum will only run for freezing, right? Insert only tables don't get > autovacuumed/analyzed until PG13 if I remember right. Tomas is talking about autovacuum running *analyze*, not vacuum. It runs for analyze, except on parti

Re: Postgres chooses slow query plan from time to time

2021-09-13 Thread Michael Lewis
Autovacuum will only run for freezing, right? Insert only tables don't get autovacuumed/analyzed until PG13 if I remember right.

Re: Postgres chooses slow query plan from time to time

2021-09-13 Thread Tomas Vondra
On 9/13/21 3:24 PM, Kristjan Mustkivi wrote: > Dear community, > > I have a query that most of the time gets executed in a few > milliseconds yet occasionally takes ~20+ seconds. The difference, as > far as I am able to tell, comes whether it uses the table Primary Key > (fast) or an additional in

Postgres chooses slow query plan from time to time

2021-09-13 Thread Kristjan Mustkivi
Dear community, I have a query that most of the time gets executed in a few milliseconds yet occasionally takes ~20+ seconds. The difference, as far as I am able to tell, comes whether it uses the table Primary Key (fast) or an additional index with smaller size. The table in question is INSERT ON