在 Python 中创建 Postgres 函数

Create Postgres function in Python

我正在尝试在 Python 的 Postgres 中创建一个函数。我与 postgres 数据库的连接使用 Psycopg 2 并在其他实例中成功连接。代码:

pg_cursor.execute('CREATE OR REPLACE FUNCTION %s.fix_geometry(geometry) \
    RETURNS SETOF geometry AS\
    $BODY$\
        SELECT geom the_geom\
        FROM \
                (\
                SELECT (st_dump(st_buffer(st_snaptogrid(st_makevalid(the_geom), 0.5), 0))).geom\
                FROM (SELECT geom the_geom FROM st_dump(st_snaptogrid(, 0.5))) a\
                ) b\
        WHERE geometrytype(geom) = \'POLYGON\' AND st_area(geom) >= 0.01;\

    $BODY$\
       LANGUAGE sql VOLATILE\
       COST 100\
       ROWS 1000;' %(schema)) #line 84

pg_connection.commit()
pg_cursor.execute('ALTER FUNCTION %s.fix_geometry(geometry) OWNER TO analysis' % (schema))
pg_connection.commit()

我收到一个错误:

line 84, in
ROWS 1000;' %(schema))
ProgrammingError: type geometry does not exist

当我运行 pgAdmin 中的代码执行成功。我错过了什么?

Python 2.7,Postgres 9.3

事实证明,错误就在提供的代码片段之外。 Postgre 在 public 模式中有几何类型,当我为这段代码定义搜索路径时,我只定义了我工作的模式; public 未包括在内。所以....

pg_cursor.execute('set search_path =  %s, public' % (schema))
pg_connection.commit()