I have a function written in PL/Python. It is a database function that runs in Python, which is permitted because of a procedural language installed via:
CREATE PROCEDURAL LANGUAGE 'plpythonu' HANDLER plpython_call_handler
(I found a nice trick, to allow non-admin users permission to run, by using a unique name, though it has not much to do with my question, I'm sure some of you will wonder how I am doing this, so below is the answer)
CREATE TRUSTED PROCEDURAL LANGUAGE 'plpythonu2' HANDLER plpython_call_handler
GRANT USAGE ON LANGUAGE plpythonu2 TO admin;
Now to the question at hand, my "hack" above works for me, but if I want to use Amazon's RDS service, I cannot install languages, and PL/Python is not available. SQL however, is.
Therefore, I need help translating the following function, written in Python into pure SQL.
CREATE OR REPLACE FUNCTION "public"."human_readable_bits" (
"b" bigint = 0
)
RETURNS varchar AS
$body$
import math
if b:
exponent = math.floor(math.log(b)/math.log(1024))
val = b/pow(1024, math.floor(exponent))
val = round(val*2)/2 -- This rounds to the nearest HALF (X.5) B, Kb, Mb, Gb, etc.
return "%.2f %s" % (val, ('B','Kb','Mb','Gb','Tb','Pb','Eb','Zb','Yb')[int(exponent)])
else:
return "0 Gb"
$body$
LANGUAGE 'plpythonu2'
VOLATILE
RETURNS NULL ON NULL INPUT
SECURITY INVOKER
COST 100;
This function allows me to perform queries such as:
=> SELECT human_readable_bits(3285824466906);
human_readable_bits
---------------------
3.00 Tb
(1 row)
OR
=> SELECT human_readable_bits(5920466906);
human_readable_bits
---------------------
5.50 Gb
(1 row)
Also, as a side-note/secondary question, after I created the function, when I look at the DDL, it has a line in it that says "SECURITY INVOKER," does anyone know what that means/does?