pg_dumpall prompts for the password twice when "postgres" connection fails

From: Suraj Kharage <suraj(dot)kharage(at)enterprisedb(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Cc: Andrew Dunstan <andrew(at)dunslane(dot)net>
Subject: pg_dumpall prompts for the password twice when "postgres" connection fails
Date: 2026-11-30 12:04:47
Message-ID: CAF1DzPWzcwnHb21+rYb5gwuCD9D3S6Ejf17zevPEA_k17pu2xg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

While reading through src/bin/pg_dump, I noticed that pg_dumpall can ask
for the password twice. It happens when the connection to the "postgres"
database fails and it falls back to "template1".

Cause: before c1da7281060 ("Move common pg_dump code related to connections
to a new file"), connectDatabase() in pg_dumpall.c kept a prompted password
in a static char *password, and the "template1" attempt reused it. Moving
the function to connectdb.c turned password into a by-value parameter, and
pg_dumpall passes NULL. As a result, the comment above ConnectDatabase() no
longer matches the behavior. It says the function will "preserve any
prompted password for the next try", but the prompted password is lost when
the call returns. It looks like an unintended regression.

Below 2 approaches to fix this -

1. Fix the code (attached patch). ConnectDatabase() takes char **password.
pg_dumpall keeps one password across its connection attempts, and
ConnectDatabaseAhx() now frees a password that ConnectDatabase() prompted
for.
2. Fix the comment. Remove the "preserve any prompted password" sentence
and accept the second prompt. This is a smaller change, but it keeps the
regression, and the leak.

I prefer option 1, since it restores the behavior from before the
refactoring.

Thoughts?
--

Thanks & Regards,
Suraj kharage,

enterprisedb.com <https://www.enterprisedb.com/>

Attachment Content-Type Size
v1-0001-Fix-pg_dumpall-re-prompting-for-password-after-fa.patch application/octet-stream 5.9 KB

Browse pgsql-hackers by date

  From Date Subject
Previous Message Mario Karuza 2026-11-30 11:57:14 Re: Improve hash aggregate spilling by writing only the needed columns