FRiCKLE / ngx_postgres

upstream module that allows nginx to communicate directly with PostgreSQL database.
http://labs.frickle.com/nginx_ngx_postgres/
BSD 2-Clause "Simplified" License
544 stars 123 forks source link

on-connected hook for custom query #18

Open clkao opened 11 years ago

clkao commented 11 years ago

It seems there's no such hook in ngx_postgres to allow custom queries to be sent upon upstream connection establishment.

I am trying out ngx_postgres with pgrest (http://github.com/clkao/pgrest). However for each connection, we need to call a postgresql function via select, something like: SELECT pgrest_boot('{...bootstrap_json}').

I also tried calling the bootstrap function along with other queries mapped from GET requests, but ngx_postgres is not handling multiple queries very well.

agentzh commented 11 years ago

Multiple result sets support is still a TODO.

However, you can use ngx_echo module's echo_location or echo_subrequest directives to do the combinations yourself:

http://wiki.nginx.org/HttpEchoModule#echo_location

clkao commented 11 years ago

Thanks @agentzh! I gave it a quick try, it seems postgres_pass can't be used with echo_location, so i have to create a wrapper route that contains two echo_location to the to routes for doing bootstrap and the actual query.

I then use postgres_output none for the first location so only the second one is rendered. however it seems I now get a response where the body is flushed before the http headers.

agentzh commented 11 years ago

@clkao echo_location should work with ngx_postgres but you need separate locations because you're doing Nginx subrequests here. For a single location, you cannot enable two Nginx module's content handlers at the same time, that is, you cannot use echo_location and proxy_pass in a single location.