Skip to content

Commit 5209b68

Browse files
JelteFCommitfest Bot
authored andcommitted
Add report_parameters protocol parameter
Connection poolers use the ParameterStatus message to track changes in session level parameters. Before assinging a server connection to a client, the pooler will first restore the server parameters that the client expects. This is done to emulate session level SET commands in transaction pooling mode. Previously this could only be done for a hard-coded set of backend parameters, but with this change a connection pooler can choose for which additional backend parameters it wants to receive status changes. It can do this by setting the newly added report_parameters protocol parameter to a list of parameter names.
1 parent 6f335a0 commit 5209b68

File tree

14 files changed

+531
-14
lines changed

14 files changed

+531
-14
lines changed

doc/src/sgml/libpq.sgml

Lines changed: 132 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2425,6 +2425,22 @@ postgresql://%2Fvar%2Flib%2Fpostgresql/dbname
24252425
</para>
24262426
</listitem>
24272427
</varlistentry>
2428+
2429+
<varlistentry id="libpq-connect-report-parameters" xreflabel="report_parameters">
2430+
<term><literal>report_parameters</literal></term>
2431+
<listitem>
2432+
<para>
2433+
Specifies the value that is requested at connection startup for the
2434+
<xref linkend="protocol-parameter-report-parameters"/> protocol
2435+
parameter. The connection attempt is not canceled if the server does
2436+
not support this protocol parameter, or if it only supports part of
2437+
the requested value.
2438+
Use the <xref linkend="libpq-PQreportParameters"/> function after
2439+
connection has completed to find out what actual value the server uses.
2440+
</para>
2441+
2442+
</listitem>
2443+
</varlistentry>
24282444
</variablelist>
24292445
</para>
24302446
</sect2>
@@ -2742,7 +2758,7 @@ const char *PQparameterStatus(const PGconn *conn, const char *paramName);
27422758
</para>
27432759

27442760
<para>
2745-
Parameters reported as of the current release include:
2761+
The parameters that are reported by default as of the current release include:
27462762
<simplelist type="vert" columns="2">
27472763
<member><varname>application_name</varname></member>
27482764
<member><varname>client_encoding</varname></member>
@@ -2770,6 +2786,10 @@ const char *PQparameterStatus(const PGconn *conn, const char *paramName);
27702786
<varname>server_encoding</varname> and
27712787
<varname>integer_datetimes</varname>
27722788
cannot change after startup.
2789+
It's possible to choose additional parameters for which the values should
2790+
be reported by using the
2791+
<xref linkend="protocol-parameter-report-parameters"/> protocol
2792+
parameter.
27732793
</para>
27742794

27752795
<para>
@@ -7707,6 +7727,117 @@ PGContextVisibility PQsetErrorContextVisibility(PGconn *conn, PGContextVisibilit
77077727
</listitem>
77087728
</varlistentry>
77097729

7730+
<varlistentry id="libpq-PQreportParameters">
7731+
<term><function>PQreportParameters</function><indexterm><primary>PQreportParameter</primary></indexterm></term>
7732+
7733+
<listitem>
7734+
<para>
7735+
Returns the server value of the
7736+
<xref linkend="protocol-parameter-report-parameters"/> protocol parameter.
7737+
<synopsis>
7738+
const char *PQreportParameters(const PGconn *<replaceable>conn</replaceable>);
7739+
</synopsis>
7740+
7741+
This returns a string with a comma separated list of server parameter
7742+
names. If the protocol parameter is not supported by the server this
7743+
returns NULL instead. Checking this value for NULL is the recommended way
7744+
to check for server support of report_parameters.
7745+
</para>
7746+
</listitem>
7747+
</varlistentry>
7748+
7749+
<varlistentry id="libpq-PQreportParametersSupport">
7750+
<term><function>PQreportParametersSupport</function><indexterm><primary>PQreportParametersSupport</primary></indexterm></term>
7751+
7752+
<listitem>
7753+
<para>
7754+
Returns the "supported values" of the
7755+
<xref linkend="protocol-parameter-report-parameters"/> protocol parameter.
7756+
<synopsis>
7757+
const char *PQreportParametersSupport(const PGconn *<replaceable>conn</replaceable>);
7758+
</synopsis>
7759+
7760+
This returns a string indicating what features report_parameters supports,
7761+
on future protocol versions this might be more useful, but for now this
7762+
always returns '<literal>L</literal>' if the report_parameters protocol
7763+
parameter is supported by the server. If the protocol parameter is not
7764+
supported by the server this returns NULL instead.
7765+
</para>
7766+
</listitem>
7767+
</varlistentry>
7768+
7769+
<varlistentry id="libpq-PQsetReportParameters">
7770+
<term><function>PQsetReportParameters</function><indexterm><primary>PQsetReportParameters</primary></indexterm></term>
7771+
7772+
<listitem>
7773+
<para>
7774+
Sends a request to set the value of the
7775+
<xref linkend="protocol-parameter-report-parameters"/> protocol parameter.
7776+
<synopsis>
7777+
PGresult *PQsetReportParameters(PGconn *<replaceable>conn</replaceable>, const char *<replaceable>params</replaceable>);
7778+
</synopsis>
7779+
7780+
<replaceable>conn</replaceable> is a connection to the server,
7781+
and <replaceable>params</replaceable> is a comma separated list of
7782+
parameter names for which the server should report their values.
7783+
use. If the request was not even sent, the function returns NULL;
7784+
The error message can be retrieved using
7785+
<xref linkend="libpq-PQerrorMessage"/>.
7786+
</para>
7787+
7788+
<para>
7789+
Returns a <structname>PGresult</structname> pointer representing the
7790+
result of the command, or a null pointer if the routine failed before
7791+
issuing any command.
7792+
The <xref linkend="libpq-PQresultStatus"/> function should be called
7793+
to check the return value for any errors (including the value of a null
7794+
pointer, in which case it will return
7795+
<symbol>PGRES_FATAL_ERROR</symbol>). Use
7796+
<xref linkend="libpq-PQerrorMessage"/> to get more information about
7797+
such errors.
7798+
</para>
7799+
7800+
<para>
7801+
The status can be <literal>PGRES_COMMAND_OK</literal> if the command
7802+
succeeded, <literal>PGRES_FATAL_ERROR</literal> if it failed, or
7803+
<literal>PGRES_NONFATAL_ERROR</literal> if it failed but not in a way
7804+
that required aborting a running transaction. Use
7805+
<xref linkend="libpq-PQerrorMessage"/> to get more information about the
7806+
error when the status is <literal>PGRES_FATAL_ERROR</literal>.
7807+
</para>
7808+
7809+
<para>
7810+
In case of success, use <xref linkend="libpq-PQreportParameters"/> to
7811+
find the new value of the report_parameters protocol parameter. This new
7812+
value might be different than the value that was sent, due to e.g.
7813+
unknown parameters.
7814+
</para>
7815+
</listitem>
7816+
</varlistentry>
7817+
7818+
<varlistentry id="libpq-PQsendSetReportParameters">
7819+
<term><function>PQsendSetReportParameters</function><indexterm><primary>PQsendSetReportParameters</primary></indexterm></term>
7820+
7821+
<listitem>
7822+
<para>
7823+
Sends a request to set the value of the
7824+
<xref linkend="protocol-parameter-report-parameters"/> protocol
7825+
parameter, without waiting for completion.
7826+
<synopsis>
7827+
int PQsendSetReportParameters(PGconn *conn, const char *params);
7828+
</synopsis>
7829+
7830+
This is an asynchronous version of
7831+
<xref linkend="libpq-PQsetReportParameters"/>: it
7832+
returns 1 if it was able to dispatch the request, and 0 if not.
7833+
After a successful call, call <xref linkend="libpq-PQgetResult"/> to
7834+
determine whether the server successfully set report_parameters. The
7835+
function's parameters are handled identically to
7836+
<xref linkend="libpq-PQsetReportParameters"/>.
7837+
</para>
7838+
</listitem>
7839+
</varlistentry>
7840+
77107841
<varlistentry id="libpq-PQtrace">
77117842
<term><function>PQtrace</function><indexterm><primary>PQtrace</primary></indexterm></term>
77127843

doc/src/sgml/protocol.sgml

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,9 +1335,8 @@ SELCT 1/0;<!-- this typo is intentional -->
13351335
effective value.
13361336
</para>
13371337

1338-
<para>
1339-
At present there is a hard-wired set of parameters for which
1340-
ParameterStatus will be generated. They are:
1338+
<para id="protocol-parameter-status-default">
1339+
The default parameters for which ParameterStatus will be generated are:
13411340
<simplelist type="vert" columns="2">
13421341
<member><varname>application_name</varname></member>
13431342
<member><varname>client_encoding</varname></member>
@@ -1365,7 +1364,11 @@ SELCT 1/0;<!-- this typo is intentional -->
13651364
<varname>server_encoding</varname> and
13661365
<varname>integer_datetimes</varname>
13671366
are pseudo-parameters that cannot change after startup.
1368-
This set might change in the future, or even become configurable.
1367+
It's possible to choose additional parameters for which the values should
1368+
be reported using the
1369+
<xref linkend="protocol-parameter-report-parameters"/> protocol parameter.
1370+
Since this set is configurable, a frontend can not assume that it only
1371+
gets these specific defaults.
13691372
Accordingly, a frontend should simply ignore ParameterStatus for
13701373
parameters that it does not understand or care about.
13711374
</para>
@@ -1736,6 +1739,33 @@ SELCT 1/0;<!-- this typo is intentional -->
17361739
</note>
17371740

17381741
<variablelist>
1742+
<varlistentry id="protocol-parameter-report-parameters" xreflabel="report_parameters">
1743+
<term><varname>report_parameters</varname>
1744+
<indexterm>
1745+
<primary><varname>report_parameters</varname> configuration parameter</primary>
1746+
</indexterm>
1747+
</term>
1748+
<listitem>
1749+
<para>
1750+
This protocol parameter can be used to add parameters to the default
1751+
list of parameters that the server reports values for using the
1752+
ParameterStatus message.
1753+
This <literal>report_parameters</literal> can be used to
1754+
to report changes for additional parameters that are not reported by
1755+
default. The parameters that are in this list by default can be found in
1756+
<xref linkend="protocol-parameter-status-default"/>.
1757+
</para>
1758+
1759+
<para>
1760+
The value of this parameter is a comma separated list of parameter
1761+
names. Unknown parameters, and parameters that require the
1762+
<literal>pg_read_all_settings</literal> to be viewed are ignored. The
1763+
default value is the empty string. The server will use the literal value
1764+
'<literal>L</literal>' as the supported version string in the
1765+
NegotiateProtocolParameter message.
1766+
</para>
1767+
</listitem>
1768+
</varlistentry>
17391769

17401770
</variablelist>
17411771
</sect2>

src/backend/libpq/protocol-parameters.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,20 @@
1818
#include "libpq/pqformat.h"
1919
#include "tcop/tcopprot.h"
2020
#include "utils/memutils.h"
21+
#include "utils/guc.h"
2122

2223
static void SendSetProtocolParameterComplete(ProtocolParameter *param, bool error);
2324

2425

2526
static MemoryContext ProtocolParameterMemoryContext;
2627

2728
static struct ProtocolParameter SupportedProtocolParameters[] = {
29+
{
30+
"report_parameters",
31+
"",
32+
report_parameters_handler,
33+
.supported_string = "L",
34+
}
2835
};
2936

3037
ProtocolParameter *

0 commit comments

Comments
 (0)