From 3b3b5e9b7ad709a03c5b704ed74b9bfcf007380c Mon Sep 17 00:00:00 2001
From: Jacob Champion <jacob.champion@enterprisedb.com>
Date: Fri, 10 Oct 2025 11:53:49 -0700
Subject: [PATCH 5/6] WIP: pg_service: implement forwards compatibility

To allow defaults from multiple major versions of libpq to coexist in
the PGSERVICEFILE, add the ability to ignore unknown keywords in the
defaults section by prefixing them with '?':

  [my-defaults-section]
  +=defaults
  ?amazing_pg30_feature=on
  sslrootcert=system
  sslmode=verify-full
  ...
---
 src/interfaces/libpq/fe-connect.c     | 13 ++++++++++++-
 src/interfaces/libpq/t/006_service.pl |  1 +
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index 4fce5f393e1..4fbaf4727ef 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -6138,6 +6138,7 @@ parseServiceFile(const char *serviceFile,
 				char	   *key,
 						   *val;
 				bool		found_keyword;
+				bool		skip_unknown = false;
 
 #ifdef USE_LDAP
 
@@ -6177,6 +6178,16 @@ parseServiceFile(const char *serviceFile,
 				}
 				*val++ = '\0';
 
+				/*
+				 * Inside a defaults section, unknown options may be marked as
+				 * skippable by the user for forwards compatibility purposes.
+				 */
+				if (service == NULL && key[0] == '?')
+				{
+					skip_unknown = true;
+					key++;
+				}
+
 				/*
 				 * A default service setting may be specified, but they're not
 				 * allowed to be nested inside other services.
@@ -6224,7 +6235,7 @@ parseServiceFile(const char *serviceFile,
 					}
 				}
 
-				if (!found_keyword)
+				if (!found_keyword && !skip_unknown)
 				{
 					/*
 					 * "unknown keyword" is unhelpful, if the actual problem
diff --git a/src/interfaces/libpq/t/006_service.pl b/src/interfaces/libpq/t/006_service.pl
index 0826add30fd..973035aac9b 100644
--- a/src/interfaces/libpq/t/006_service.pl
+++ b/src/interfaces/libpq/t/006_service.pl
@@ -137,6 +137,7 @@ unknown-setting=1
 +=defaults
 service=my_srv
 options=-O
+?unknown-setting=1  # should be ignored
 
 [my_srv]
 });
-- 
2.34.1

