While investigating a customer issue (details here) one of the replies suggested to use pre_auth_delay to add a delay before a session is authenticated. Even if this was not useful for the actual issue, this can be very useful for other scenarios, and this is the reason for this short blog post.

Let’s assume you know that some of your sessions are doing something unexpected and you want to strace that process from the operating system. The issue is, that those session are very short living and it is hard to know beforehand which session is causing issues. What you can do using pre_auth_delay is to add a delay before the session is authenticated so you’re able to identify the process and then attach strace to it. Another use case would be to troubleshoot authentication itself.

The parameter can be changed online (context=sighup), so let’s change it to 20 seconds:

postgres=# \dconfig+ pre_auth_delay
                List of configuration parameters
   Parameter    | Value |  Type   | Context | Access privileges 
----------------+-------+---------+---------+-------------------
 pre_auth_delay | 10s   | integer | sighup  | 
(1 row)
postgres=# alter system set pre_auth_delay = '10s';
ALTER SYSTEM
postgres=# select pg_reload_conf();
 pg_reload_conf 
----------------
 t
(1 row)
postgres=# \q

In another session identify the current psql sessions (as we’re going to use psql to connect), which returns nothing in my case:

postgres@debian12-pg:/home/postgres/ [pgdev] ps -ef | grep psql | grep -v grep
postgres@debian12-pg:/home/postgres/ [pgdev] 

Start a new psql session, which will block because of the delay we’ve set:

postgres@debian12-pg:/home/postgres/ [pgdev] psql

In the other session identify the connection:

postgres@debian12-pg:/home/postgres/ [pgdev] ps -ef | grep psql | grep -v grep
postgres    9750    8637  0 16:57 pts/1    00:00:00 psql

… and then attach strace to it:

postgres@debian12-pg:/home/postgres/ [pgdev] strace -fp 9750
strace: Process 9750 attached
pselect6(1, [0], NULL, NULL, NULL, {sigmask=[], sigsetsize=8}

Done. For completeness, there is also post_auth_delay:


postgres=# \dconfig+ post_auth_delay
                List of configuration parameters
    Parameter    | Value |  Type   | Context | Access privileges 
-----------------+-------+---------+---------+-------------------
 post_auth_delay | 0     | integer | backend | 
(1 row)