If I recall Postgresql 9.6 established an option to kill "Idle In Transaction" connections after a certain amount of time.
#idle_in_transaction_session_timeout = 0 # in milliseconds, 0 is disabled
May also what to check what happens to the connection after your Pascal program gets the error. See if the connection is still visible in postgres.
The session is closed in Postgresql too, even much before Lazarus actually shows the Pqconnection error.