How to set statement timeout per user?

All we need is an easy explanation of the problem, so here it is.

I have multiple users in Postgres. I would like to set up different statement timeouts for different users.
Eg: Guest 5 minutes and Admin 10 minutes.

Is it possible in Postgres 11.11?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Yes, Postgres allows settings per user or even per database and user, including statement_timeout:

ALTER ROLE foo SET statement_timeout = 12345;  -- milliseconds

Related:

To see the currently active setting for the session:

SHOW statement_timeout;

Or get more details from the view pg_settings (including how it was set):

SELECT *
FROM   pg_settings
WHERE  name = 'statement_timeout';

To check current settings for a role:

SELECT rolname, rolconfig
FROM   pg_roles
WHERE  rolname = 'foo';

rolconfig is an array, unnest it to get one setting per row:

SELECT rolname, unnest(rolconfig) AS setting
FROM   pg_roles
WHERE  rolname = 'foo';

Method 2

Found that we can set the timeout on Role.

ALTER ROLE guest SET statement_timeout='5m';
ALTER ROLE admin SET statement_timeout='10m';

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply