Skip to content

Instantly share code, notes, and snippets.

@pilgrim2go
pilgrim2go / plpythonu.sql
Created October 27, 2016 11:16 — forked from rturowicz/plpythonu.sql
postgresql: example use of python procedural language
-- query with stored plan
CREATE or replace FUNCTION pybench1(id int) RETURNS text AS '
if (SD.has_key("plan")):
plan = SD["plan"]
else:
plan = plpy.prepare("SELECT * FROM pagetimer pt, pagebrowser pb WHERE pt.idtimer = $1 and pt.idtimer = pb.idtimer", ["int4"])
SD["plan"] = plan
rec = plpy.execute(plan, [id])
if (rec.nrows() > 0):
@pilgrim2go
pilgrim2go / turnstile_funct.py
Created October 27, 2016 11:18 — forked from sethc23/turnstile_funct.py
f(x) z_get_string_dist
def z_get_string_dist(self):
cmd="""
DROP TYPE IF EXISTS string_dist_results cascade;
CREATE TYPE string_dist_results as (
idx integer,
orig_str text,
jaro double precision,
jaro_b text,
leven integer,
create or replace function update_asset_json(text, text, integer) returns setof integer as $$
import simplejson
from datetime import datetime
now = datetime.now()
updated_data = simplejson.loads(args[1])
rs = plpy.execute("select id, data from assets where id in (%s)" % args[0])
resp = []
for r in rs:
data = simplejson.loads(r['data'])
data.update(updated_data)
@pilgrim2go
pilgrim2go / set_sequence_values.py
Created October 27, 2016 11:26 — forked from BookLaugh/set_sequence_values.py
PostgreSQL procedure for updating all sequences used as default values for table columns. Once called, it will update values of sequences to max value present in column plus 5 (why 5? So you can find out which sequences were altered). Function returns list of names of updated sequences.
CREATE OR REPLACE FUNCTION set_seq_vals(schema_name TEXT)
RETURNS TEXT[]
AS $$
schemaname = schema_name if schema_name else 'public'
plpy.execute("SET SEARCH_PATH=%s,public" % schemaname)
tables = plpy.execute("SELECT tablename FROM pg_tables WHERE schemaname = '%s'" % schemaname);
altered_seqs = []
for t in tables:
cols = plpy.execute("SELECT column_name, column_default, data_type, is_nullable FROM information_schema.columns WHERE table_name = '%s' AND data_type in ('integer', 'bigint') AND column_default IS NOT NULL" % t["tablename"])
for c in cols:
@pilgrim2go
pilgrim2go / run_server.pl.py
Created October 27, 2016 11:27 — forked from joshwilliams/run_server.pl.py
An httpd-like thing in a PL/Python function
CREATE OR REPLACE FUNCTION run_server() RETURNS text
LANGUAGE plpythonu STRICT
AS $$
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
class PGHandler(BaseHTTPRequestHandler):
def do_GET(self):
plpy.notice("Path: %s" % self.path)
if self.path.count('/') == 2 and self.path.split('/')[1] != "" and self.path.split('/')[2] != "":
nsp = plpy.quote_ident(self.path.split('/')[1])
@pilgrim2go
pilgrim2go / pgmail.sql
Created October 27, 2016 11:28 — forked from mikaelhg/pgmail.sql
PostgreSQL PL/Python stored procedures for email handling
CREATE OR REPLACE FUNCTION download(url TEXT) RETURNS TEXT
AS $$
import urllib2
try:
result = urllib2.urlopen(url)
rawdata = result.read()
info = result.info()
try:
content_type, encoding = info['Content-Type'].split('charset=')
except:
@pilgrim2go
pilgrim2go / plpython_returning_record.py
Created October 27, 2016 11:29 — forked from happysundar/plpython_returning_record.py
Example of a PLPythonU method that returns a record
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
@pilgrim2go
pilgrim2go / haproxy.cfg
Created November 8, 2016 07:35 — forked from fumin/haproxy.cfg
Haproxy config file for SSL and basic authentication
# Haproxy config file for SSL and basic authentication
#
# Since ssl is needed, we need to use
# `make TARGET=linux26 USE_OPENSSL=1 ADDLIB=-lz` to install haproxy.
# Also, haproxy version must be haproxy-1.5-dev19 or above.
#
# An example curl command that queryies pic-collage.com/api/collages/feed is
# `curl -k -v -u 'collages:password' 'https://ec2-ip.compute-1.amazonaws.com/api/collages/feed'`
userlist collages_dyno
@pilgrim2go
pilgrim2go / filerotator.py
Created November 9, 2016 07:54 — forked from adejones/filerotator.py
Python class and utility for file rotation. Configurable daily, weekly and monthly retention; can use hard links to save space; supports `argparse` config files.
#!/usr/bin/env python3
# vim: ft=python ts=4 sw=4 expandtab
#
# Copyright (c) 2013 Reed Kraft-Murphy <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
@pilgrim2go
pilgrim2go / install-psycopg2.sh
Created November 9, 2016 10:23 — forked from whyvez/install-psycopg2.sh
install psycopg2 on aws linux
sudo yum install gcc python27 python27-devel postgresql-devel
sudo curl https://bootstrap.pypa.io/ez_setup.py -o - | sudo python27
sudo /usr/bin/easy_install-2.7 pip
sudo pip2.7 install psycopg2