Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I indicate that a postgresql plpython function argument can be any type?

I'm using (or trying to) the following function:

CREATE FUNCTION replace_value(data JSON, key text, value anyelement)
RETURNS JSON AS $$
    import json
    d = json.loads(data)
    d[key] = value
    return json.dumps(d)
$$ LANGUAGE plpython3u;

This, as I did not expect, does not work. Postgres complains:

ERROR:  PL/Python functions cannot accept type anyelement

Well... that's just silly, because native Python functions can accept anything of any type, since variables are just names for things.

And in this case, I could not care what the actual type of the value is, I just want to be able to replace it. How can I do such a thing in Postgres/PLPython?

like image 988
Wayne Werner Avatar asked Aug 03 '26 20:08

Wayne Werner


1 Answers

Define the parameter as text and cast to text when calling the function.

like image 99
Laurenz Albe Avatar answered Aug 07 '26 09:08

Laurenz Albe