public inbox for [email protected]
help / color / mirror / Atom feedFrom: Chao Li <[email protected]>
To: PostgreSQL-development <[email protected]>
Cc: Michael Paquier <[email protected]>
Cc: [email protected]
Cc: [email protected]
Subject: SERVICEFILE shows wrong file after servicefile fallback
Date: Tue, 2 Jun 2026 21:42:23 +0800
Message-ID: <[email protected]> (raw)
Hi,
While testing “psql: Add variable SERVICEFILE”, I found a small issue where SERVICEFILE may show the wrong value.
While tracing the code, I noticed there is a fallback path. If the service file specified in the connection string does not contain the requested service, libpq falls back to pg_service.conf. So I tested the following case:
1. Prepare two service files: one empty, and the other containing the service:
```
% mkdir -p /tmp/svc
% : > /tmp/svc/empty.conf
% printf '[actual]\n' > /tmp/svc/pg_service.conf
```
2. Run psql with a connection string and show SERVICEFILE:
```
% PGSYSCONFDIR=/tmp/svc psql "dbname=postgres service=actual servicefile=/tmp/svc/empty.conf"
psql (19beta1)
Type "help" for help.
postgres=# \echo :SERVICE :SERVICEFILE
actual /tmp/svc/empty.conf
```
Here, SERVICEFILE shows the empty file /tmp/svc/empty.conf, which is wrong because the actual service is read from /tmp/svc/pg_service.conf.
I think the bug was actually introduced by the previous commit 092f3c63efc6. In parseServiceFile(), if the service file has already been set in the connection options, it refuses to update the value, so the fallback service file is not synced to the connection options. Then SERVICEFILE is read from the connection options, which still contain the original file specified on the command line. So, SERVICEFILE just makes the bug visible.
The fix is simple, when fallback happens, explicitly store the actual service file into the connection options. See the attached patch for details.
With the fix, SERVICEFILE shows the actual service file:
```
% PGSYSCONFDIR=/tmp/svc psql "dbname=postgres service=actual servicefile=/tmp/svc/empty.conf"
psql (19beta1)
Type "help" for help.
postgres=# \echo :SERVICE :SERVICEFILE
actual /tmp/svc/pg_service.conf
```
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
Attachments:
[application/octet-stream] v1-0001-libpq-Report-correct-service-file-after-fallback.patch (2.2K, 2-v1-0001-libpq-Report-correct-service-file-after-fallback.patch)
download | inline diff:
From 2133f5ea986f057486d5d9cab11a19b0d1d099de Mon Sep 17 00:00:00 2001
From: "Chao Li (Evan)" <[email protected]>
Date: Tue, 2 Jun 2026 16:50:44 +0800
Subject: [PATCH v1] libpq: Report correct service file after fallback
When an explicitly specified service file does not contain the requested
service, libpq falls back to the system-wide pg_service.conf file. If the
service is found there, however, the "servicefile" connection option still
reported the explicitly specified file.
Update the option after a successful fallback so that PQconninfo() and
psql's SERVICEFILE variable report the file that supplied the service.
Add a TAP test for this case.
Author: Chao Li <[email protected]>
---
src/interfaces/libpq/fe-connect.c | 5 +++++
src/interfaces/libpq/t/006_service.pl | 8 ++++++++
2 files changed, 13 insertions(+)
diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index 4272d386e64..d6d5ad14c6a 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -6049,6 +6049,11 @@ next_file:
status = parseServiceFile(serviceFile, service, options, errorMessage, &group_found);
if (status != 0)
return status;
+ /* Update servicefile to the file that actually supplied the service */
+ if (group_found && service_fname != NULL &&
+ conninfo_storeval(options, "servicefile", serviceFile,
+ errorMessage, false, false) == NULL)
+ return 3;
last_file:
if (!group_found)
diff --git a/src/interfaces/libpq/t/006_service.pl b/src/interfaces/libpq/t/006_service.pl
index 7462d21314d..144248b8533 100644
--- a/src/interfaces/libpq/t/006_service.pl
+++ b/src/interfaces/libpq/t/006_service.pl
@@ -143,6 +143,14 @@ local $ENV{PGSERVICEFILE} = "$srvfile_empty";
sql => "SELECT 'connect2_3'",
expected_stdout => qr/connect2_3/);
+ my $srvfile_empty_win_cared = $srvfile_empty;
+ $srvfile_empty_win_cared =~ s/\\/\\\\/g;
+ $dummy_node->connect_ok(
+ q{service=my_srv servicefile='} . $srvfile_empty_win_cared . q{'},
+ 'servicefile option is updated when service is found in default pg_service.conf',
+ sql => '\echo :SERVICEFILE',
+ expected_stdout => qr/^\Q$srvfile_default\E$/);
+
local $ENV{PGSERVICE} = 'undefined-service';
$dummy_node->connect_fails(
'',
--
2.50.1 (Apple Git-155)
view thread (3+ messages) latest in thread
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: [email protected]
Cc: [email protected], [email protected], [email protected], [email protected]
Subject: Re: SERVICEFILE shows wrong file after servicefile fallback
In-Reply-To: <[email protected]>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox