Skip to content

PostgreSQL: Delaying authentication to debug a process

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:

Questions about your own setup? A dbi services consultant can look at your case.

Talk to an expert (opens in a new tab)
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)

Let’s work together

Ready to operate at
the next level?

Tell us about your challenge. We’ll bring the depth, the expertise, and the Swiss precision.

Open source Team

Open source Team

Posts published under the Open source Team account are collective contributions by the dbi services open infrastructure consultants. The practice covers open-source databases and platforms, PostgreSQL first of all, along with MySQL, MariaDB, and Linux, from architecture, replication, and high availability to performance tuning and operations under service level agreements, and contributes actively to the PostgreSQL community.View Author posts

721 posts since September 2011

Most read in Database

The Database posts our readers open most: the all-time favourites, and what is trending right now.

Leave a Comment

Your email address will not be published. Required fields are marked *