This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- ******************************************************************************************** | |
-- General FX utility functions in PL/pgSql (Postgres) | |
-- Released under the MIT license: http://opensource.org/licenses/mit-license.php by Jon Asher | |
-- Download current source at https://gist.github.com/jasher/6026284 | |
-- ******************************************************************************************** | |
CREATE FUNCTION array_contains(p_int_arr integer[], p_value integer) RETURNS boolean | |
LANGUAGE plpgsql | |
AS $$ | |
BEGIN |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE FUNCTION day_begin(dt timestamp with time zone DEFAULT CURRENT_TIMESTAMP) | |
RETURNS timestamp with time zone AS $$ | |
BEGIN | |
RETURN date_trunc('day', dt); | |
END; | |
$$ LANGUAGE plpgsql; | |
CREATE OR REPLACE FUNCTION day_end(dt timestamp with time zone DEFAULT CURRENT_TIMESTAMP) | |
RETURNS timestamp with time zone AS $$ | |
BEGIN |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create extension plpythonu; | |
create type py_environ_type as (name text, value text); | |
create or replace function py_environ(name varchar DEFAULT NULL) | |
returns setof py_environ_type | |
as $$ | |
import os | |
aev = [] | |
if name is None: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create or replace function f_sk_date (ts timestamp ) | |
returns integer | |
stable as $$ | |
if not ts: | |
return None | |
return int(str(ts)[0:10].replace('-','')) | |
$$ language plpythonu; | |
create or replace function f_date (ts timestamp) | |
returns date |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1) instead of returning SETOF composite_type or SETOF RECORD (OUT col type) return TABLE (col type,etc) and just yield. | |
2) For long queries in plpy.execute use triple quotes for multiline queries | |
3) use explain [query] to figure out what is taking so much time on a query then build indexes to remove the "sequential scans" | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 传入轨迹抽样点,获取整条轨迹穿越地区 | |
* 300: 执行时间184.992ms | |
* 1000: 执行时间587.400ms | |
* 5000: 执行时间2834.339ms | |
* 10000:执行时间5526.228ms | |
*/ | |
CREATE OR REPLACE FUNCTION getAddressFromPoints(points_str text) | |
RETURNS text AS $$ | |
import json |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE FUNCTION crop(image bytea, rect box) | |
RETURNS bytea | |
LANGUAGE plpythonu | |
AS $function$ | |
if ('io' in SD) and ('StringIO' in SD) and ('Image' in SD): | |
io = SD['io'] | |
StringIO = SD['StringIO'] | |
Image = SD['Image'] | |
else: | |
import io, StringIO |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE FUNCTION public.geocode_google(IN inaddress text, OUT address text, OUT longitude double precision, OUT latitude double precision) | |
RETURNS record AS | |
$BODY$ | |
from geopy.geocoders import GoogleV3 | |
geolocator = GoogleV3() | |
try: | |
address, (latitude, longitude) = geolocator.geocode(inaddress,timeout=1,exactly_one=True) | |
return address, longitude, latitude | |
except: | |
return None, None, None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- If you are missing the pypythonu extension, install it first | |
--- (Ubuntu): sudo apt-get install python-psycopg2 | |
--- Activate plpython | |
CREATE EXTENSION plpythonu; | |
--- Create the profiling function | |
CREATE OR REPLACE FUNCTION similarity(text, text) RETURNS numeric AS $$ | |
import difflib | |
return difflib.SequenceMatcher(None,args[0], args[1]).ratio() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DROP FUNCTION IF EXISTS get_role_to_actor_and_actor_to_role( INOUT BIGINT, OUT JSONB, OUT JSONB ); | |
CREATE OR REPLACE FUNCTION get_role_to_actor_and_actor_to_role( | |
INOUT program_id BIGINT, | |
OUT actor_to_role JSONB, | |
OUT role_to_actor JSONB) | |
RETURNS RECORD IMMUTABLE | |
AS $plpython_function$ | |
import json | |