Hi agentzh,
On Tue, Dec 2, 2014 at 10:10 PM, Yichun Zhang (agentzh)
<age...@gmail.com> wrote:
> I used to have some plan to expose the Pg error codes and error
> strings as nginx variables, so you can customize such things easily
> yourself in nginx.conf (possibly with a little help of
> header_filter_by_lua, for example). Are you willing to contribute a
> patch to ngx_postgres?
Yes, I think it would be a good idea for me to contribute a patch, for
a couple of reasons:
1. To get more control over the behavior of ngx_postgres in this
particular sceanrio
2. To improve my understanding of ngx_postgres and openresty in general
That said, I think I need to better understand the phase handling of
the underlying API and how the asynchrony works with the different
directives, so that I what I am producing is not complete garbage.
For example, I'm not 100% sure about what is happening under the
covers when you do something like this:
location ~ /rows {
postgres_pass database;
rds_json on;
postgres_query HEAD GET "SELECT * FROM generate_series(1,5)";
add_header X-rows $postgres_rows;
content_by_lua '
ngx.say(ngx.var.postgres_rows)
';
}
This returns nil in the body of the response.
But if I leave the content directive out and just let the result
stream to the client, the data and the 'X-rows` header is output as
exepcted, i.e.
location ~ /rows {
postgres_pass database;
rds_json on;
postgres_query HEAD GET "SELECT * FROM generate_series(1,5)";
add_header X-rows $postgres_rows;
}
So I'm assuming that in the case of the former, $postgres_rows is
scoped that is not visible via 'ngx.var.postgres_rows' in the content
directive. I assuming further that this handler is getting executed
asynchronously.
I've been looking into hacks using ngx.location.capture to gather
asynchronous results, but this exposes my ignorance of how it is
actually working.
Is there an idiomatic way to do conditional processing based on the
content of $postgres_rows in opensresty, or do I need to dig deeper
into ngx_postgres?
Cheers,
Ben