RE: Have pg_basebackup write "dbname" in "primary_conninfo"? - Mailing list pgsql-hackers

From Hayato Kuroda (Fujitsu)
Subject RE: Have pg_basebackup write "dbname" in "primary_conninfo"?
Date
Msg-id OS7PR01MB12081DFCBB29FC98B8401336CF52C2@OS7PR01MB12081.jpnprd01.prod.outlook.com
Whole thread Raw
In response to Re: Have pg_basebackup write "dbname" in "primary_conninfo"?  (Masahiko Sawada <sawada.mshk@gmail.com>)
Responses Re: Have pg_basebackup write "dbname" in "primary_conninfo"?
Re: Have pg_basebackup write "dbname" in "primary_conninfo"?
Re: Have pg_basebackup write "dbname" in "primary_conninfo"?
List pgsql-hackers
Dear Sawada-san,

Thanks for giving comments!

> This behavior makes sense to me. But do we want to handle the case of
> using environment variables too? 

Yeah, v5 does not consider which libpq parameters are specified by environment
variables. Such a variable should be used when the dbname is not expressly written
in the connection string.
Such a path was added in the v6 patch. If the dbname is not determined after
parsing the connection string, we call PQconndefaults() to get settings from
environment variables and service files [1], then start to search dbname again.
Below shows an example.

```
PGPORT=5431 PGUSER=kuroda PGDATABASE=postgres pg_basebackup -D data_N2 -R -v
->
primary_conninfo = 'user=kuroda ... port=5431 ... dbname=postgres ... '
```

> IIUC,
>
> pg_basebackup -D tmp -d "user=masahiko dbname=test_db"
>
> is equivalent to:
>
> PGDATABASE="user=masahiko dbname=test_db" pg_basebackup -D tmp

The case won't work. I think You assumed that expanded_dbname like
PQconnectdbParams() [2] can be used for enviroment variables, but it is not correct
- it won't parse as connection string again.

In the libpq layer, connection parameters are parsed in PQconnectStartParams()->conninfo_array_parse().
When expand_dbname is specified, the entry "dbname" is firstly checked and
parsed its value. They are done at fe-connect.c:5846.

The environment variables are checked and parsed in conninfo_add_defaults(), which
is called from conninfo_array_parse(). However, it is done at fe-connect.c:5956 - the
expand_dbname has already been done at that time. This means there is no chance
that PGDATABASE is parsed as an expanded style.

For example, if the pg_basebackup runs like below:

PGDATABASE="user=kuroda dbname=postgres" pg_basebackup -D data_N2 -R -v

The primary_conninfo written in the file will be:

primary_conninfo = 'user=hayato ... dbname=''user=kuroda dbname=postgres'''

[1]: https://www.postgresql.org/docs/devel/libpq-pgservice.html
[2]: https://www.postgresql.org/docs/devel/libpq-connect.html#LIBPQ-PQCONNECTDBPARAMS

Best Regards,
Hayato Kuroda
FUJITSU LIMITED
https://www.fujitsu.com/ 


Attachment

pgsql-hackers by date:

Previous
From: Shlok Kyal
Date:
Subject: Re: speed up a logical replica setup
Next
From: Andrew Dunstan
Date:
Subject: Re: Possibility to disable `ALTER SYSTEM`