The Python code I'm writing makes a psycopg2
connection to a PostgreSQL database. I need to build some reports out of the data in this database, so I have a couple Python procs that periodically run and create a csv file out of some tables and nice queries.
The problem I'm facing here is that I need to include a column in my csv report which is the result of a function stored in the PostgreSQL database. This database is managed by another group of persons so I can't write to it. I could easily see the content of that function and emulate the behaviour on Python and have the column values I need to calculate, but in this case this function is periodically changing and it won't make sense to continually update the Python function.
So my question here is if it's possible to somehow load the database function into Python code, every time my code connects to the database. I could make actual use of the function on the database itself, but imagine making 900K calls to a database function from Python code to calculate a value, simply does not scale.
edit: Adding sql function
CREATE OR REPLACE FUNCTION public.p_start(integer, integer)
RETURNS numeric
LANGUAGE sql
IMMUTABLE
AS $function$
SELECT CASE WHEN $1 = 0 AND $2 = 0 THEN 0.2760
WHEN $1 = 0 AND $2 = 1 THEN 0.0684
WHEN $1 = 0 AND $2 = 2 THEN 0.0277
WHEN $1 = 0 AND $2 = 3 THEN 0.0189
WHEN $1 = 0 AND $2 = 4 THEN 0.0038
WHEN $1 = 0 AND $2 = 5 THEN 0.0098
WHEN $1 = 1 AND $2 = 1 THEN 0.5501
WHEN $1 = 1 AND $2 = 2 THEN 0.2264
WHEN $1 = 1 AND $2 = 3 THEN 0.1203
WHEN $1 = 1 AND $2 = 4 THEN 0.0804
WHEN $1 = 1 AND $2 = 5 THEN 0.0839
ELSE 0.1 END;
$function$
Thanks