Обсуждение: pgbench without dbname worked differently with psql and pg_dump

Поиск
Список
Период
Сортировка

pgbench without dbname worked differently with psql and pg_dump

От
"Hayato Kuroda (Fujitsu)"
Дата:
Dear hackers,

Recently the 'd' option of pgbench is unified with other applications like psql and pg_dump,
but I found further difference. pgbebch uses an OS user as the dbanme even when the '-U' is
specified. Please see below experiments.

```
# Assuming the OS user is "hayato", whereas the database admin is "postgres"
$ initdb -U postgres -D data
...
Success. You can now start the database server using:

    pg_ctl -D data -l logfile start

$pg_ctl -D data -l logfile start
waiting for server to start.... done
server started

# psql can connect to the database "postgres"
$ psql -U postgres -c "SELECT current_database();"
 current_database
------------------
 postgres
(1 row)

# pg_dump can connect as well
$ pg_dump -U postgres
--
-- PostgreSQL database dump
--
....

# ... but pgbench cannot
$ pgbench -U postgres
pgbench: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL:  database "hayato" does not exist
pgbench: error: could not create connection for setup
```

Also, I found that dbname parameter for both pgbench and pg_dump is described by
the same sentences [1] [2]. This is also confusing.

Is it an expected behavior?


[1]: https://www.postgresql.org/docs/devel/pgbench.html#PGBENCH-OPTION-DBNAME
```
[-d] dbname
[--dbname=]dbname
Specifies the name of the database to test in. If this is not specified, the environment variable PGDATABASE is used.
If that is not set, the user name specified for the connection is used.
```

[2]: https://www.postgresql.org/docs/devel/app-pgdump.html#:~:text=of%20the%20output.-,dbname,-Specifies%20the%20name
```
dbname
Specifies the name of the database to be dumped. If this is not specified, the environment variable PGDATABASE is used.
If that is not set, the user name specified for the connection is used.
```

Best regards,
Hayato Kuroda
FUJITSU LIMITED




Re: pgbench without dbname worked differently with psql and pg_dump

От
Ashutosh Bapat
Дата:
On Tue, Jan 21, 2025 at 12:33 PM Hayato Kuroda (Fujitsu)
<kuroda.hayato@fujitsu.com> wrote:
>
> Dear hackers,
>
> Recently the 'd' option of pgbench is unified with other applications like psql and pg_dump,
> but I found further difference. pgbebch uses an OS user as the dbanme even when the '-U' is
> specified. Please see below experiments.
>
> ```
> # Assuming the OS user is "hayato", whereas the database admin is "postgres"
> $ initdb -U postgres -D data
> ...
> Success. You can now start the database server using:
>
>     pg_ctl -D data -l logfile start
>
> $pg_ctl -D data -l logfile start
> waiting for server to start.... done
> server started
>
> # psql can connect to the database "postgres"
> $ psql -U postgres -c "SELECT current_database();"
>  current_database
> ------------------
>  postgres
> (1 row)
>
> # pg_dump can connect as well
> $ pg_dump -U postgres
> --
> -- PostgreSQL database dump
> --
> ....
>
> # ... but pgbench cannot
> $ pgbench -U postgres
> pgbench: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL:  database "hayato" does not exist
> pgbench: error: could not create connection for setup
> ```
>
> Also, I found that dbname parameter for both pgbench and pg_dump is described by
> the same sentences [1] [2]. This is also confusing.
>
> Is it an expected behavior?
>
>
> [1]: https://www.postgresql.org/docs/devel/pgbench.html#PGBENCH-OPTION-DBNAME
> ```
> [-d] dbname
> [--dbname=]dbname
> Specifies the name of the database to test in. If this is not specified, the environment variable PGDATABASE is used.
> If that is not set, the user name specified for the connection is used.
> ```
>
> [2]: https://www.postgresql.org/docs/devel/app-pgdump.html#:~:text=of%20the%20output.-,dbname,-Specifies%20the%20name
> ```
> dbname
> Specifies the name of the database to be dumped. If this is not specified, the environment variable PGDATABASE is
used.
> If that is not set, the user name specified for the connection is used.
> ```

I see the same behaviour. The code in pgbench which decided the
database to connect to, looks similar to the relevant code in
createdb. But createdb documentation is in agreement with the code.
[1]
```
dbname

Specifies the name of the database to be created. The name must be
unique among all PostgreSQL databases in this cluster. The default is
to create a database with the same name as the current system user.
```

I couldn't locate the code in psql which calculates the name of the
database from a quick read of psql/startup.c. I think pgbench should
behave like psql, not like createdb.

[1] https://www.postgresql.org/docs/17/app-createdb.html

--
Best Wishes,
Ashutosh Bapat



Re: pgbench without dbname worked differently with psql and pg_dump

От
"David G. Johnston"
Дата:
On Tue, Jan 21, 2025 at 6:54 PM Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com> wrote:

IIUC createdb has the good description [1] and it can be re-used
everywhere.

As none of those programs "create" a database the wording "The default is to create a database" seems quite a poor choice to standardize on.

The wording you suggest replacing seems fine as-is.

ISTM the following is the only issue that probably needs to be addressed.  Either by fixing the apparent bug in the code or documenting the correct behavior in the pgbench docs.

# ... but pgbench cannot
$ pgbench -U postgres
pgbench: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL:  database "hayato" does not exist
pgbench: error: could not create connection for setup

Your patch 0001 doesn't really seem to do either.
"connection to default database failed" is a strictly worse error message than the existing one used unconditionally.
There probably should still be the existing "else" block in the other hunk - we do default to OS user name - but to handle -U shouldn't we just add an "else if (username) ...block before it?

David J.