正在寻找一种在执行Postgresql查询之前重写它的方法

正在寻找一种在执行Postgresql查询之前重写它的方法,postgresql,proxy,middleware,Postgresql,Proxy,Middleware,我有一个Postgres客户端发送如下查询 SELECT ... FROM "public"."mycontent" "mycontent" WHERE (strpos(substring("mycontent"."summary" from 1), 'search_string') + 1 - 1 > 0) 到我们的Postgres服务器。我希望客户端使用我的全文搜索功能,但我无法访问客户端的代码。因此,我正在寻找一种方法,将上述表单中的所有传入查询重写为如下内容: SELECT ..

我有一个Postgres客户端发送如下查询

SELECT ... FROM "public"."mycontent" "mycontent"
WHERE (strpos(substring("mycontent"."summary" from 1), 'search_string') + 1 - 1 > 0)
到我们的Postgres服务器。我希望客户端使用我的全文搜索功能,但我无法访问客户端的代码。因此,我正在寻找一种方法,将上述表单中的所有传入查询重写为如下内容:

SELECT ... FROM "public"."mycontent" "mycontent"
WHERE id in full_text_search('search_string')

注意“search_string”的提取,因此这里不能使用Postgres规则,因为它们不进行这种提取。我希望任何人都知道可以进行查询重写的postgres中间件或代理,或者有其他更好的想法吗?谢谢。

如果您能将全文搜索重写为在数据库服务器上执行的SQL函数(遵循postgres SQL方言),我不会这么做


实际上,postgres提供了一整套管理字符串的功能,我想我必须回答我自己的问题。我使用python gevent套接字编程实现了一个用于重写查询的postgres代理服务器。注意,如果连接使用SSL,则这不起作用

from gevent import socket, server, Greenlet, joinall

def pipe(source_socket, destination_socket, modify=False):
    while True:
        try:
            data = source_socket.recv(1024)
        except socket.error, e:
            break
        else:
            if data:
                if modify: data = data.replace("limit 10", "limit 1 ")
                destination_socket.send(data)
            else:
                break

def pg_proxy(client_socket, address):
    pg_socket = socket.create_connection(("localhost", 5432))
    pg_socket.settimeout(300.0)
    client_socket.settimeout(300.0)
    joinall((
        Greenlet.spawn(pipe, client_socket, pg_socket, modify=True),
        Greenlet.spawn(pipe, pg_socket, client_socket, modify=False)
    ))
    pg_socket.close()
    client_socket.close()

if __name__ == '__main__':
    s = server.StreamServer(("localhost", 5433), pg_proxy)
    s.serve_forever()

这并不能解决客户端使用其他函数的问题。理论上有重写语句的规则,但我认为他们无法查看SQL文本来确定是否应该重写语句。