总有一天,我要把这样的文章,删掉,通通删掉,I Hate PG!
安装呢是在编译的过程中选择
./configure --with-python [root@laptop]# ./configure --help |grep python --with-python build Python modules (PL/Python)[root@]# 具体可以使用如下命令论证./configure --help |grep python (Linux 环境中多使用 help, 真个人生都是升华的,不多说了,技术的梗,离开技术希望你也能活)
然后使用psql 吧,类似安装模块的语法,安装语言
postgres=# CREATE PROCEDURAL LANGUAGE plpythonu;CREATE LANGUAGEpostgres=# \?postgres=# \dL List of languages Name | Owner | Trusted | Description -----------+----------+---------+------------------------------ plpgsql | postgres | t | PL/pgSQL procedural language plpythonu | postgres | f | (2 rows)postgres=# \dL+postgres=# CREATE OR REPLACE FUNCTION hello ()postgres-# RETURNS TEXTpostgres-# AS $$postgres$# import ospostgres$# return '你好,我当前目录为:'+ os.getcwd()postgres$# $$ LANGUAGE plpythonu;CREATE FUNCTIONpostgres=# SELECT hello(); hello ------------------------------------ 你好,我当前目录为:/dev/shm/hello(1 row)postgres=#
开篇一: 在greenplum(@pivotal) 中使用PL/Python
在下面的语句中加入了 try{} catch {} 是加入了异常处理
CREATE TABLE test(id int, name varchar(20));INSERT INTO test(id, name) VALUES(1, 'hire'),(2, 'me');CREATE OR REPLACE FUNCTION query_test(a text) RETURNS text AS $$try: query = "SELECT name FROM test" r = plpy.execute(query) return r[0]except Exception, ex: plpy.notice(str(ex)) return str(ex)$$LANGUAGE plpythonu ; SELECT query_test('nothing');SELECT * FROM test;
定义查询计划 使用prepare
CREATE OR REPLACE FUNCTION query_test(a text) RETURNS text AS $$try: query = "SELECT id FROM test where name = $1 " plan = plpy.prepare(query, ["text"]) r = plpy.execute(plan , [a]) return r[0]except Exception, ex: plpy.notice(str(ex)) return str(ex)$$LANGUAGE plpythonu ; SELECT query_test('me');
参考资料:
- 猪脑子