Proper way to clean-up connection for reuse (`DISCARD ALL` and default role)

From: Logan MAUZAIZE <logan(dot)mauzaize(at)gmail(dot)com>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Proper way to clean-up connection for reuse (`DISCARD ALL` and default role)
Date: 2025-01-20 16:16:14
Message-ID: CAC+peWh9jTUWmNDA_nQNc-jbrAh3r9XC_EQmKPXScHBu6baVBQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hello!

We encountered an issue with connection pooling from .Net driver (
https://github.com/npgsql/npgsql)

A few weeks ago, in order to give same access privileges for multi-service
access (service/API, https://sqitch.org/, satellite services, ...), we
enabled default role (`ALTER ROLE <specific role> SET ROLE <shared role>`).
Feature has been validated with "scripting" (and then with a POC):

```sql
select current_user;

create table ...;
-- check table owner
```

However, when using .Net apps we figured out owner wasn't expected one (the
connection role instead of default one). After investigating our (and
driver) code base. We find out that .Net driver use `DISCARD ALL` statement
in order to clean-up pooled connection.

While cleaning-up reused/pooled connection before they are offered back to
the pool is expected in order to avoid side effects, it seems `DISCARD ALL`
isn't currenty the proper way to achieve clean-up as it results in
different behavior than a fresh connection.

Here's test statements to check behavior of: fresh connection, `DISCARD
ALL`, `RESET ROLE`, `SET SESSION AUTHORIZATION DEFAULT` and `RESET ALL`:

```sql
select current_user;
-- result: default role

discard all; select current_user;
-- result: connection role

reset role; select current_user;
-- result: default role

set session authorization default; select current_user;
-- result: connection role

reset role; select current_user;
-- result: default role

reset all; select current_user;
-- result: default role

set session authorization default; select current_user;
-- result: connection role

reset all; select current_user;
-- result: connection role
```

Can you please:

1. clarify if `DISCARD ALL` behavior is the expected one or is it a bug?
1. If not a bug, specify proper way to clean-up connection for reuse?

Thank you very much for your help!

Note: discussion is coming from https://github.com/npgsql/npgsql/issues/5997

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Umar Hayat 2025-01-20 16:19:06 Re: Add XMLNamespaces to XMLElement
Previous Message Ivan Kush 2025-01-20 16:14:07 Re: pg_stat_statements: improve loading and saving routines for the dump file