[pgpool-committers: 3404] pgpool: Handling of pg_terminate_backend for simple query protocol

Muhammad Usama m.usama at gmail.com
Wed Aug 17 00:58:30 JST 2016

Handling of pg_terminate_backend for simple query protocol

Since the pg_terminate_backend function in PostgreSQL is used to terminate the
backend connection, So what happens is, when this function kills a PostgreSQL
backend that is connected to the pgpool-II, This disconnection of backend by
pg_terminate_backend function is appeared as a backend node failure
to the pgpool-II.
But the problem here is, PostgreSQL does not give any information to the
client program that the connection is going to be killed because of the
pg_terminate_backend call and on the client side, it looks similar
to the backend node failure.

Now to solve this in pgpool-II we need two things.
First is to identify the pg_terminate_backend function in the query and the
pgpool-II child process that hosts the particular backend connection which will
be killed by that pg_terminate_backend function call, so that we get a heads up
in advance about the backend termination, and secondly the routing of the query
containing pg_terminate_backend also needs a new logic so that the query should
be sent to the correct PostgreSQL node that hosts the backend with the
PID referred by the pg_terminate_bakend()

So how does this commit handles pg_terminate_backend()??
In the SimpleQuery() function which is the work horse of simple query processing
in the pgpool-II we start with the search of the pg_terminate_backend()
function call in the query parse tree and if the search comes out to be
successful, the next step is to locate the pgpool-II child process and a
backend node of that connection whose PID is specified in pg_terminate_backend
function's argument. Once the connection and the pgpool-II child process
is identified, we just set the swallow_termination flag(added by this commit in
ConnectionInfo structure) for that connection in the shared memory, and also
set the query destination node to the backend node that hosts that particular
connection and does not call pool_where_to_send() for this query so that the
query should be sent to the correct backend node.

Now when the query is routed to the correct node and consequently the backend
gets killed, that results in the communication error on pgpool-II side, the
pgpool-II already knows that this disconnection is due the pg_terminate_backend
and not because of node failure as the swallow_termination flag is already
set for the connection.


-- pg_terminate_backend is not handled with extended query protocol

-- Currently we only support pg_terminate_backend(constant number) function
calls. If the expression or sub query is used in the argument of
pg_terminate_backend then it would not be handled
    pgpool=# select pg_terminate_backend(1025); -- Supported
    pgpool=# select pg_terminate_backend( 2 +1); -- NOT SUPPORTED
    pgpool=# select pg_terminate_backend((select 1)); -- NOT SUPPORTED

 -- Currently only one pg_terminate_backend call in a query is handled.



Modified Files
src/auth/pool_auth.c                       |  2 +
src/context/pool_process_context.c         | 81 ++++++++++++++++++++++++++++++
src/context/pool_query_context.c           | 46 ++++++++++++++++-
src/include/context/pool_process_context.h |  5 ++
src/include/context/pool_query_context.h   |  9 +++-
src/include/pcp/libpcp_ext.h               | 11 +++-
src/include/pool.h                         |  3 ++
src/include/utils/pool_select_walker.h     |  2 +
src/protocol/pool_connection_pool.c        |  1 +
src/protocol/pool_process_query.c          | 14 +++++-
src/protocol/pool_proto_modules.c          | 80 +++++++++++++++++++++++++++--
src/utils/pool_select_walker.c             | 34 +++++++++++++
src/utils/pool_stream.c                    | 42 +++++++++++++++-
13 files changed, 318 insertions(+), 12 deletions(-)

More information about the pgpool-committers mailing list