Re: proposal: function pg_setting_value_split() to parse shared_preload_libraries etc.

2020-12-02 Thread Ian Lawrence Barwick
2020年11月30日(月) 14:53 Craig Ringer :
>
> On Mon, 30 Nov 2020 at 03:24, Anastasia Lubennikova
>  wrote:
> >
> > On 23.10.2020 05:06, Ian Lawrence Barwick wrote:
> > > Having just submitted this, I realised I'm focussing on the GUCs which 
> > > call
> > > "SplitDirectoriesString()" (as my specific uses case is for
> > > "shared_preload_libraries")
> > > but the patch does not consider the other GUC_LIST_INPUT settings, which
> > > call "SplitIdentifierString()", so as is, it might produce unexpected
> > > results for those.
> > >
> > > I'll rethink and submit an updated version.
> >
> > Status update for a commitfest entry.
> >
> > This entry was "Waiting on author" during this CF. As I see, the patch
> > needs more work before review, so I changed it to "Withdrawn".
> > Feel free to resubmit an updated version to a future commitfest.
>
>
> FWIW I was looking for this functionality just the other day, for
> parsing synchronous_standby_names . So I'd definitely welcome it.

Thanks, useful to know someone else has a use-case for this I'll
resubmit for the next CF.


Regards

Ian Barwick

-- 
EnterpriseDB: https://www.enterprisedb.com




Re: proposal: function pg_setting_value_split() to parse shared_preload_libraries etc.

2020-11-29 Thread Craig Ringer
On Mon, 30 Nov 2020 at 03:24, Anastasia Lubennikova
 wrote:
>
> On 23.10.2020 05:06, Ian Lawrence Barwick wrote:
> > Having just submitted this, I realised I'm focussing on the GUCs which call
> > "SplitDirectoriesString()" (as my specific uses case is for
> > "shared_preload_libraries")
> > but the patch does not consider the other GUC_LIST_INPUT settings, which
> > call "SplitIdentifierString()", so as is, it might produce unexpected
> > results for those.
> >
> > I'll rethink and submit an updated version.
>
> Status update for a commitfest entry.
>
> This entry was "Waiting on author" during this CF. As I see, the patch
> needs more work before review, so I changed it to "Withdrawn".
> Feel free to resubmit an updated version to a future commitfest.


FWIW I was looking for this functionality just the other day, for
parsing synchronous_standby_names . So I'd definitely welcome it.




Re: proposal: function pg_setting_value_split() to parse shared_preload_libraries etc.

2020-11-29 Thread Anastasia Lubennikova

On 23.10.2020 05:06, Ian Lawrence Barwick wrote:

Having just submitted this, I realised I'm focussing on the GUCs which call
"SplitDirectoriesString()" (as my specific uses case is for
"shared_preload_libraries")
but the patch does not consider the other GUC_LIST_INPUT settings, which
call "SplitIdentifierString()", so as is, it might produce unexpected
results for those.

I'll rethink and submit an updated version.


Status update for a commitfest entry.

This entry was "Waiting on author" during this CF. As I see, the patch 
needs more work before review, so I changed it to "Withdrawn".

Feel free to resubmit an updated version to a future commitfest.

--
Anastasia Lubennikova
Postgres Professional: http://www.postgrespro.com
The Russian Postgres Company





Re: proposal: function pg_setting_value_split() to parse shared_preload_libraries etc.

2020-10-22 Thread Ian Lawrence Barwick
2020年10月23日(金) 9:53 Ian Lawrence Barwick :
>
> Hi
>
> From time to time I find myself in a situation where it would be very useful 
> to
> be able to programatically determine whether a particular library is included 
> in
> "shared_preload_libraries", which accepts a comma-separated list of values.
>
> Unfortunately it's not as simple as splitting the list on the commas, as while
> that will *usually* work, the following is also valid:
>
> shared_preload_libraries = 'foo,bar,"baz ,"'
>
> and reliably splitting it up into its constituent parts would mean 
> re-inventing
> a wheel (and worse possibly introducing some regular expressions into the
> process, cf. https://xkcd.com/1171/ ).
>
> Now, while it's highly unlikely someone will go to the trouble of creating a
> library name with commas and spaces in it, "highly unlikely" is not the same 
> as
> "will definitely never ever happen". So it would be very handy to be able to 
> use
> the same function PostgreSQL uses internally ("SplitDirectoriesString()") to
> produce the guaranteed same result.
>
> Attached patch provides a new function "pg_setting_value_split()" which does
> exactly this, i.e. called with a string containing such a list, it calls
> "SplitDirectoriesString()" and returns the result as a set of text, e.g.:
>
> postgres# SELECT setting FROM pg_setting_value_split('foo,bar,"baz ,"');
>
>  setting
> -
>  foo
>  bar
>  baz ,
> (3 rows)
>
> though a more likely use would be:
>
>SELECT setting FROM
> pg_setting_value_split(current_setting('shared_preload_libraries'));
>
> Other GUCs this applies to:
>
>   - local_preload_libraries
>   - session_preload_libraries
>   - unix_socket_directories

Having just submitted this, I realised I'm focussing on the GUCs which call
"SplitDirectoriesString()" (as my specific uses case is for
"shared_preload_libraries")
but the patch does not consider the other GUC_LIST_INPUT settings, which
call "SplitIdentifierString()", so as is, it might produce unexpected
results for those.

I'll rethink and submit an updated version.


Regards

Ian Barwick




proposal: function pg_setting_value_split() to parse shared_preload_libraries etc.

2020-10-22 Thread Ian Lawrence Barwick
Hi

>From time to time I find myself in a situation where it would be very useful to
be able to programatically determine whether a particular library is included in
"shared_preload_libraries", which accepts a comma-separated list of values.

Unfortunately it's not as simple as splitting the list on the commas, as while
that will *usually* work, the following is also valid:

shared_preload_libraries = 'foo,bar,"baz ,"'

and reliably splitting it up into its constituent parts would mean re-inventing
a wheel (and worse possibly introducing some regular expressions into the
process, cf. https://xkcd.com/1171/ ).

Now, while it's highly unlikely someone will go to the trouble of creating a
library name with commas and spaces in it, "highly unlikely" is not the same as
"will definitely never ever happen". So it would be very handy to be able to use
the same function PostgreSQL uses internally ("SplitDirectoriesString()") to
produce the guaranteed same result.

Attached patch provides a new function "pg_setting_value_split()" which does
exactly this, i.e. called with a string containing such a list, it calls
"SplitDirectoriesString()" and returns the result as a set of text, e.g.:

postgres# SELECT setting FROM pg_setting_value_split('foo,bar,"baz ,"');

 setting
-
 foo
 bar
 baz ,
(3 rows)

though a more likely use would be:

   SELECT setting FROM
pg_setting_value_split(current_setting('shared_preload_libraries'));

Other GUCs this applies to:

  - local_preload_libraries
  - session_preload_libraries
  - unix_socket_directories

I will add this to the next CF.

Regards

Ian Barwick

-- 
EnterpriseDB: https://www.enterprisedb.com
diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml
index c99499e52b..263863ca72 100644
--- a/doc/src/sgml/func.sgml
+++ b/doc/src/sgml/func.sgml
@@ -23917,6 +23917,55 @@ SELECT collation for ('foo' COLLATE "de_DE");
 

 
+   
+  shows the function
+ available to parse run-time configuration parameters.
+   
+
+   
+Configuration Parameter Parsing Function
+
+ 
+  Name Return Type Description
+ 
+ 
+  
+   
+
+ pg_setting_value_split
+
+pg_setting_value_split(setting)
+   
+   setof text
+   split a parameter value into a set of text columns
+  
+ 
+
+   
+   
+ The function pg_setting_value_split takes a comma-separated list of values
+ (as would be provided as library names for the configuration parameters
+ local_preload_libraries, session_preload_libraries and
+ shared_preload_libraries, or as directory names for the configuration
+ parameter unix_socket_directories) and returns them as a set of
+ text columns, in the order they occur in the list. An example:
+
+SELECT setting FROM pg_setting_value_split('foo,bar,"baz ,"');
+
+ setting
+-
+ foo
+ bar
+ baz ,
+(3 rows)
+
+
+This function parses the comma-separated list as it would be when PostgreSQL parses
+the configuration file, and is a reliable way of determining the presence of a
+particular item in the list.
+   
+
+
   
 
   
diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c
index a62d64eaa4..d80dea4bbe 100644
--- a/src/backend/utils/misc/guc.c
+++ b/src/backend/utils/misc/guc.c
@@ -9784,6 +9784,93 @@ show_all_file_settings(PG_FUNCTION_ARGS)
 	return (Datum) 0;
 }
 
+/*
+ * setting_value_split
+ *
+ * Given a GUC such as "shared_preload_libraries" which may contain a
+ * comma-separated list of values, split these into individual items
+ * using SplitDirectoriesString and return as a list.
+ */
+Datum
+setting_value_split(PG_FUNCTION_ARGS)
+{
+#define NUM_PG_SETTINGS_VALUE_SPLIT_ATTS 1
+	text	   *setting;
+	char	   *rawstring;
+	List	   *elemlist;
+	ListCell   *l;
+	MemoryContext per_query_ctx;
+	MemoryContext oldcontext;
+	TupleDesc	tupdesc;
+	Tuplestorestate *tupstore;
+	ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
+
+	/* Check to see if caller supports us returning a tuplestore */
+	if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
+		ereport(ERROR,
+(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("set-valued function called in context that cannot accept a set")));
+
+	if (!(rsinfo->allowedModes & SFRM_Materialize))
+		ereport(ERROR,
+(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("materialize mode required, but it is not " \
+		"allowed in this context")));
+
+	if (PG_ARGISNULL(0))
+		PG_RETURN_NULL();
+
+	setting = PG_GETARG_TEXT_PP(0);
+	rawstring = pstrdup(TextDatumGetCString(setting));
+
+	/* Parse string into list of filename paths */
+	if (!SplitDirectoriesString(rawstring, ',', ))
+	{
+		/* syntax error in list */
+		list_free_deep(elemlist);
+		pfree(rawstring);
+
+		ereport(ERROR,
+(errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg("invalid list syntax")));
+	}
+
+	/* Switch into long-lived context to construct returned data structures */
+	per_query_ctx