Re: PGSERVICEFILE as part of a normal connection string

Michael Paquier <michael@paquier.xyz>

From: Michael Paquier <michael@paquier.xyz>
To: Torsten Förtsch <tfoertsch123@gmail.com>
Cc: pgsql-hackers@lists.postgresql.org
Date: 2024-11-20T06:22:11Z
Lists: pgsql-hackers
On Mon, Nov 18, 2024 at 09:21:56PM +0100, Torsten Förtsch wrote:
> I like to bundle all my database connections in a .pg_service.conf. Over
> time I collected a bunch of such service files. A while back I discovered
> that the service file can only be specified as an environment variable. It
> cannot be given as part of the connection string like

-    if ((env = getenv("PGSERVICEFILE")) != NULL)
+    if (service_fname != NULL)
+        strlcpy(serviceFile, service_fname, sizeof(serviceFile));
+    else if ((env = getenv("PGSERVICEFILE")) != NULL)
         strlcpy(serviceFile, env, sizeof(serviceFile));

That should be right, the connection parameter takes priority over the
environment variable.  The comment at the top of this code block
becomes incorrect.

     else
     {
@@ -5678,6 +5684,16 @@ parseServiceFile(const char *serviceFile,
                     goto exit;
                 }
 
+                if (strcmp(key, "servicefile") == 0)
+                {
+                    libpq_append_error(errorMessage,
+                                       "nested servicefile specifications not supported in service file \"%s\", line %d",
+                                       serviceFile,
+                                       linenr);
+                    result = 3;
+                    goto exit;
+                }

Interesting.  We've never had tests for that even for "service".
Perhaps it would be the time to add some tests for the existing case
and the one you are adding?  Your test suite should make that easy to
add.

+# This tests "service" and "servicefile"

You are introducing tests for the existing "service", as well as tests
for the new "servicefile".  Could it be possible to split that into
two patches for clarity?  You'd want one to provide coverage for the
existing features (PGSERVICEFILE, PGSERVICE and connection parameter
"service"), then add tests for the new feature "servicename" with its
libpq implementation.  That would make your main patch simpler, as
well.

+open my $fh, '>', $srvfile or die $!;
+print $fh "[my_srv]\n";
+print $fh +($node->connstr =~ s/ /\n/gr), "\n";
+close $fh;

Sure that's OK on Windows where we have CRLFs, not just LFs?
--
Michael

Commits

Same data as JSON: GET /api/v1/messages/:b64id/commits the thread's linked commits as JSON, with link sources. API reference →
  1. psql: Add variable SERVICEFILE

  2. libpq: Add "servicefile" connection option

  3. libpq: Add TAP test for nested service file

  4. libpq: Fix some issues in TAP tests for service files

  5. docs: Fix column count attribute in table

  6. libpq: Add TAP tests for service files and names