RefLib

Reference library for AI

Back

postgresql_16:6.9.2.2 Null, None

PostgreSQL 16 > Server Programming > PL/Python — Python Procedural Language > Data Values > Null, None

If an SQL null value is passed to a function, the argument value will appear as None in Python. For example, the function definition of pymax shown in (see postgresql_16:6.9.1) will return the wrong answer for null inputs. We could add STRICT to the function definition to make PostgreSQL do something more reasonable: if a null value is passed, the function will not be called at all, but will just return a null result automatically. Alternatively, we could check for null inputs in the function body: CREATE FUNCTION pymax (a integer, b integer) RETURNS integer AS $$ if (a is None) or (b is None): return None if a > b: return a return b $$ LANGUAGE plpython3u; As shown above, to return an SQL null value from a PL/Python function, return the value None. This can be done whether the function is strict or not.

Back