postgresql语句\u超时行为

postgresql语句\u超时行为,postgresql,Postgresql,为了避免长时间运行的查询,我想为某些用户实现语句时间 pgdb=> alter user user1 set statement_timeout=600; ALTER ROLE pgdb=> select * from pg_user where usename = 'user1'; usename | usesysid | usecreatedb | usesuper | userepl | usebypassrls | passwd | valuntil |

为了避免长时间运行的查询,我想为某些用户实现语句时间

pgdb=> alter user user1 set statement_timeout=600;
ALTER ROLE
pgdb=> select * from pg_user where usename = 'user1';
  usename  |  usesysid  | usecreatedb | usesuper | userepl | usebypassrls |  passwd  | valuntil |              useconfig
-----------+------------+-------------+----------+---------+--------------+----------+----------+--------------------------------------
 user1 | 2538084332 | f           | f        | f       | f            | ******** |          | {statement_timeout=600}
(1 row)

以user1身份登录到PSQL,运行非常短的查询会返回,但大多数查询(将在几秒钟内完成,无语句超时限制)正在超时,即使语句超时设置为180秒

pgdb=> select count(*) from schema.tablea limit 10; -- huge table timeout
ERROR:  canceling statement due to statement timeout
Time: 184.118 ms
imaods=> explain select count(*) from schema.tablea limit 10;
                                                                                       QUERY PLAN

-------------------------------------------------------------------------------------------------------------------------------------------------------------------
---------------------
 Limit  (cost=1906894.11..1906894.12 rows=1 width=8)
   ->  Finalize Aggregate  (cost=1906894.11..1906894.12 rows=1 width=8)
         ->  Gather  (cost=1906893.89..1906894.10 rows=2 width=8)
               Workers Planned: 2
               ->  Partial Aggregate  (cost=1905893.89..1905893.90 rows=1 width=8)
                     ->  Parallel Index Only Scan using idx1 on tablea  (cost=0.57..1828328.86 rows=31026013 width=0)
(6 rows)

pgdb=> select count(*) from tableb;
 count
-------
   260
(1 row)

Time: 0.672 ms
pgdb=> select count(*) from tableb a, tableb b;
 count
-------
 67600
(1 row)
Time: 4.405 ms

pgdb=> select count(*) from tableb a, tableb b, tableb c;  -- timeout
ERROR:  canceling statement due to statement timeout

pgdb=> explain select count(*) from tableb a, tableb b, tableb c;                                                                 
                                           QUERY PLAN
--------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=264536.10..264536.11 rows=1 width=8)
   ->  Nested Loop  (cost=0.00..220596.10 rows=17576000 width=0)
         ->  Nested Loop  (cost=0.00..878.85 rows=67600 width=0)
               ->  Seq Scan on tableb a  (cost=0.00..16.60 rows=260 width=0)
               ->  Materialize  (cost=0.00..17.90 rows=260 width=0)
                     ->  Seq Scan on tableb b  (cost=0.00..16.60 rows=260 width=0)
         ->  Materialize  (cost=0.00..17.90 rows=260 width=0)
               ->  Seq Scan on tableb c  (cost=0.00..16.60 rows=260 width=0)
(8 rows)

pgdb=> explain analyze select count(*) from tableb a, tableb b, tableb c;
ERROR:  canceling statement due to statement timeout
pgdb=> explain select count(*) from tableb a, tableb b;
                                               QUERY PLAN
--------------------------------------------------------------------------------------------------------
 Aggregate  (cost=1047.85..1047.86 rows=1 width=8)
   ->  Nested Loop  (cost=0.00..878.85 rows=67600 width=0)
         ->  Seq Scan on tableb a  (cost=0.00..16.60 rows=260 width=0)
         ->  Materialize  (cost=0.00..17.90 rows=260 width=0)
               ->  Seq Scan on tableb b  (cost=0.00..16.60 rows=260 width=0)
(5 rows)

pgdb=> explain select count(*) from tableb;
                                        QUERY PLAN
------------------------------------------------------------------------------------------
 Aggregate  (cost=17.25..17.26 rows=1 width=8)
   ->  Seq Scan on tableb  (cost=0.00..16.60 rows=260 width=0)
(2 rows)

语句超时是按估计时间还是按实际执行时间进行的。通过解释计划,看起来它考虑了估计时间,如果认为估计时间超过了语句超时,则不执行查询。这是预期的行为还是我遗漏了什么?我的理解是,当实际执行时间超过超时限制时,它应该运行查询并超时。

不,它按实际执行时间执行。当语句开始执行时,计时器开始运行。否则,您将如何解释在取消查询之前您有180秒的时间


PostgreSQL不直接估计执行时间。成本度量有些人为,粗略的准则是1对应于顺序扫描期间从磁盘读取一个8kB页面的时间。

“即使语句超时设置为180s。”不,它设置为600ms。到底是什么让你认为它被设定为180秒?我玩的是不同的价值观,所以混淆在一起。我想这是秒,我应该rtfm,它在女士谢谢。