public inbox for [email protected]  
help / color / mirror / Atom feed
SERVICEFILE shows wrong file after servicefile fallback
3+ messages / 2 participants
[nested] [flat]

* SERVICEFILE shows wrong file after servicefile fallback
@ 2026-06-02 13:42 Chao Li <[email protected]>
  2026-06-04 04:47 ` Re: SERVICEFILE shows wrong file after servicefile fallback Michael Paquier <[email protected]>
  0 siblings, 1 reply; 3+ messages in thread

From: Chao Li @ 2026-06-02 13:42 UTC (permalink / raw)
  To: pgsql-hackers; +Cc: Michael Paquier <[email protected]>; [email protected]; [email protected]

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)



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: SERVICEFILE shows wrong file after servicefile fallback
  2026-06-02 13:42 SERVICEFILE shows wrong file after servicefile fallback Chao Li <[email protected]>
@ 2026-06-04 04:47 ` Michael Paquier <[email protected]>
  2026-06-04 05:47   ` Re: SERVICEFILE shows wrong file after servicefile fallback Chao Li <[email protected]>
  0 siblings, 1 reply; 3+ messages in thread

From: Michael Paquier @ 2026-06-04 04:47 UTC (permalink / raw)
  To: Chao Li <[email protected]>; +Cc: Postgres hackers <[email protected]>; [email protected]; [email protected]

On Tue, Jun 02, 2026 at 09:42:23PM +0800, Chao Li wrote:
> 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:

Right, as of the default file in PGSYSCONFDIR.  This qualifies as an
open item for v19.

> 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. 

Yep, it looks like you are right here.  It does not make sense to show
in SERVICEFILE the file that has been skipped in favor of the second
default in PGSYSCONFDIR.  We need to show the latter.  An \echo of 
SERVICEFILE is an interesting way to show your point.  Why not.

Thanks for the report, will fix.
--
Michael


Attachments:

  [application/pgp-signature] signature.asc (833B, 2-signature.asc)
  download

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: SERVICEFILE shows wrong file after servicefile fallback
  2026-06-02 13:42 SERVICEFILE shows wrong file after servicefile fallback Chao Li <[email protected]>
  2026-06-04 04:47 ` Re: SERVICEFILE shows wrong file after servicefile fallback Michael Paquier <[email protected]>
@ 2026-06-04 05:47   ` Chao Li <[email protected]>
  0 siblings, 0 replies; 3+ messages in thread

From: Chao Li @ 2026-06-04 05:47 UTC (permalink / raw)
  To: Michael Paquier <[email protected]>; +Cc: Postgres hackers <[email protected]>; [email protected]; [email protected]



> On Jun 4, 2026, at 12:47, Michael Paquier <[email protected]> wrote:
> 
> On Tue, Jun 02, 2026 at 09:42:23PM +0800, Chao Li wrote:
>> 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:
> 
> Right, as of the default file in PGSYSCONFDIR.  This qualifies as an
> open item for v19.
> 
>> 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.
> 
> Yep, it looks like you are right here.  It does not make sense to show
> in SERVICEFILE the file that has been skipped in favor of the second
> default in PGSYSCONFDIR.  We need to show the latter.  An \echo of 
> SERVICEFILE is an interesting way to show your point.  Why not.
> 
> Thanks for the report, will fix.
> --
> Michael

Thanks for confirming. I just added it to the Open Item list.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/










^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2026-06-04 05:47 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-06-02 13:42 SERVICEFILE shows wrong file after servicefile fallback Chao Li <[email protected]>
2026-06-04 04:47 ` Michael Paquier <[email protected]>
2026-06-04 05:47   ` Chao Li <[email protected]>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox