This blog post shows an example of how to create a immutable_concat function in Pg:
CREATE OR REPLACE FUNCTION immutable_concat(VARIADIC "any")
RETURNS text AS 'text_concat'
LANGUAGE internal IMMUTABLE
I'd like to do the same with concat_ws and the corresponding text_concat_ws does exist, however, the following just crashes the process:
CREATE OR REPLACE FUNCTION immutable_concat_ws(VARIADIC "any")
RETURNS text AS 'text_concat_ws'
LANGUAGE internal IMMUTABLE
Update: The siguature of immutable_concat_ws should be (glue, *parts), one glue (text or varchar) and one or more parts (text, varchar or null).
What am I missing here?
Firstly, the function requires two parameters in the definition, like Richard already suggested, and you updated your question accordingly.
Secondly, you can create that function with
"any"input usingLANGUAGE internal. Does not mean that you should, though.concat_ws()is onlySTABLEfor a reason. Among others, the text representation ofdateortimestampdepends on locale / datestyle settings, so the result is not immutable. Indexes building on this could silently break. Restricted totextinput, it's safe to declare itIMMUTABLE. Since you only needtextinput (orvarchar, which has an implicit cast totext), limit it to your use case and be safe:Crating a
LANGUAGE internalfunction requires superuser privileges. If that's not an option, the next best thing would be an SQL function like:Mark it as
PARALLEL SAFEin Postgres 9.6 or later (it qualifies!) to enable parallelism when involving this function. The manual:Resist the temptation to do things like this
immutable_concat_ws('|', now()::text, 'foo'). This would reintroduce said dependencies in the call.Related: