Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1khjHl-000405-LQ for pgsql-hackers@arkaria.postgresql.org; Wed, 25 Nov 2020 01:07:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1khjHj-0003pv-AP for pgsql-hackers@arkaria.postgresql.org; Wed, 25 Nov 2020 01:07:51 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1khjHi-0003pn-VB for pgsql-hackers@lists.postgresql.org; Wed, 25 Nov 2020 01:07:51 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1khjHg-00076h-Hh for pgsql-hackers@postgresql.org; Wed, 25 Nov 2020 01:07:49 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 0AP17EYb388251; Tue, 24 Nov 2020 20:07:14 -0500 From: Tom Lane To: Alvaro Herrera cc: Anastasia Lubennikova , Greg Nancarrow , Peter Smith , "Smith, Peter" , David Steele , "Tsunakawa, Takayuki" , Haribabu Kommi , Robert Haas , Laurenz Albe , Dave Cramer , Jing Wang , PostgreSQL-development , Elvis Pranskevichus Subject: Re: Libpq support to connect to standby server as priority In-reply-to: <325979.1606259987@sss.pgh.pa.us> References: <20201124225917.GA21884@alvherre.pgsql> <325979.1606259987@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Tue, 24 Nov 2020 18:19:47 -0500" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <388233.1606266426.0@sss.pgh.pa.us> Date: Tue, 24 Nov 2020 20:07:14 -0500 Message-ID: <388250.1606266434@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <388233.1606266426.1@sss.pgh.pa.us> I wrote: > Alvaro Herrera writes: >> Agreed. If this is just a few hundred bytes of server-side local memory >> per backend, it seems definitely worth it. > Yeah, given the current set of GUC_REPORT variables, it's hard to see > the storage for their last-reported values amounting to much. The need > for an extra pointer field in each GUC variable record might eat more > space than the actually-live values :-( Here's a v2 that does it like that. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="0001-report-guc-changes-at-query-end-2.patch"; charset="us-ascii" Content-ID: <388233.1606266426.2@sss.pgh.pa.us> Content-Description: 0001-report-guc-changes-at-query-end-2.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c index 7c5f7c775b..34ed0e7558 100644 --- a/src/backend/tcop/postgres.c +++ b/src/backend/tcop/postgres.c @@ -4229,6 +4229,9 @@ PostgresMain(int argc, char *argv[], pgstat_report_activity(STATE_IDLE, NULL); } = + /* Report any recently-changed GUC options */ + ReportChangedGUCOptions(); + ReadyForQuery(whereToSendOutput); send_ready_for_query =3D false; } diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c index bb34630e8e..245a3472bc 100644 --- a/src/backend/utils/misc/guc.c +++ b/src/backend/utils/misc/guc.c @@ -4822,6 +4822,8 @@ static bool guc_dirty; /* true if need to do commi= t/abort work */ = static bool reporting_enabled; /* true to enable GUC_REPORT */ = +static bool report_needed; /* true if any GUC_REPORT reports are needed = */ + static int GUCNestLevel =3D 0; /* 1 when in main transaction */ = = @@ -5452,6 +5454,7 @@ InitializeOneGUCOption(struct config_generic *gconf) gconf->reset_scontext =3D PGC_INTERNAL; gconf->stack =3D NULL; gconf->extra =3D NULL; + gconf->last_reported =3D NULL; gconf->sourcefile =3D NULL; gconf->sourceline =3D 0; = @@ -5828,7 +5831,10 @@ ResetAllOptions(void) gconf->scontext =3D gconf->reset_scontext; = if (gconf->flags & GUC_REPORT) - ReportGUCOption(gconf); + { + gconf->status |=3D GUC_NEEDS_REPORT; + report_needed =3D true; + } } } = @@ -6215,7 +6221,10 @@ AtEOXact_GUC(bool isCommit, int nestLevel) = /* Report new value if we changed it */ if (changed && (gconf->flags & GUC_REPORT)) - ReportGUCOption(gconf); + { + gconf->status |=3D GUC_NEEDS_REPORT; + report_needed =3D true; + } } /* end of stack-popping loop */ = if (stack !=3D NULL) @@ -6257,17 +6266,60 @@ BeginReportingGUCOptions(void) if (conf->flags & GUC_REPORT) ReportGUCOption(conf); } + + report_needed =3D false; +} + +/* + * ReportChangedGUCOptions: report recently-changed GUC_REPORT variables + * + * This is called just before we wait for a new client query. + * + * By handling things this way, we ensure that a ParameterStatus message + * is sent at most once per variable per query, even if the variable + * changed multiple times within the query. That's quite possible when + * using features such as function SET clauses. Function SET clauses + * also tend to cause values to change intraquery but eventually revert + * to their prevailing values; ReportGUCOption is responsible for avoidin= g + * redundant reports in such cases. + */ +void +ReportChangedGUCOptions(void) +{ + /* Quick exit if not (yet) enabled */ + if (!reporting_enabled) + return; + + /* Quick exit if no values have been changed */ + if (!report_needed) + return; + + /* Transmit new values of interesting variables */ + for (int i =3D 0; i < num_guc_variables; i++) + { + struct config_generic *conf =3D guc_variables[i]; + + if ((conf->flags & GUC_REPORT) && (conf->status & GUC_NEEDS_REPORT)) + ReportGUCOption(conf); + } + + report_needed =3D false; } = /* * ReportGUCOption: if appropriate, transmit option value to frontend + * + * We need not transmit the value if it's the same as what we last + * transmitted. However, clear the NEEDS_REPORT flag in any case. */ static void ReportGUCOption(struct config_generic *record) { - if (reporting_enabled && (record->flags & GUC_REPORT)) + char *val =3D _ShowOption(record, false); + + if (record->last_reported =3D=3D NULL || + strcmp(val, record->last_reported) !=3D 0) { - char *val =3D _ShowOption(record, false); StringInfoData msgbuf; = pq_beginmessage(&msgbuf, 'S'); @@ -6275,8 +6327,19 @@ ReportGUCOption(struct config_generic *record) pq_sendstring(&msgbuf, val); pq_endmessage(&msgbuf); = - pfree(val); + /* + * We need a long-lifespan copy. If strdup() fails due to OOM, we'll + * set last_reported to NULL and thereby possibly make a duplicate + * report later. + */ + if (record->last_reported) + free(record->last_reported); + record->last_reported =3D strdup(val); } + + pfree(val); + + record->status &=3D ~GUC_NEEDS_REPORT; } = /* @@ -7695,7 +7758,10 @@ set_config_option(const char *name, const char *val= ue, } = if (changeVal && (record->flags & GUC_REPORT)) - ReportGUCOption(record); + { + record->status |=3D GUC_NEEDS_REPORT; + report_needed =3D true; + } = return changeVal ? 1 : -1; } diff --git a/src/include/utils/guc.h b/src/include/utils/guc.h index 073c8f3e06..6a20a3bcec 100644 --- a/src/include/utils/guc.h +++ b/src/include/utils/guc.h @@ -363,6 +363,7 @@ extern void AtStart_GUC(void); extern int NewGUCNestLevel(void); extern void AtEOXact_GUC(bool isCommit, int nestLevel); extern void BeginReportingGUCOptions(void); +extern void ReportChangedGUCOptions(void); extern void ParseLongOption(const char *string, char **name, char **value= ); extern bool parse_int(const char *value, int *result, int flags, const char **hintmsg); diff --git a/src/include/utils/guc_tables.h b/src/include/utils/guc_tables= .h index 04431d0eb2..7f36e1146f 100644 --- a/src/include/utils/guc_tables.h +++ b/src/include/utils/guc_tables.h @@ -161,6 +161,8 @@ struct config_generic GucContext reset_scontext; /* context that set the reset value */ GucStack *stack; /* stacked prior values */ void *extra; /* "extra" pointer for current actual value */ + char *last_reported; /* if variable is GUC_REPORT, value last sent + * to client (NULL if not yet sent) */ char *sourcefile; /* file current setting is from (NULL if not * set in config file) */ int sourceline; /* line in source file */ @@ -172,7 +174,8 @@ struct config_generic * Caution: the GUC_IS_IN_FILE bit is transient state for ProcessConfigFi= le. * Do not assume that its value represents useful information elsewhere. */ -#define GUC_PENDING_RESTART 0x0002 +#define GUC_PENDING_RESTART 0x0002 /* changed value cannot be applied yet= */ +#define GUC_NEEDS_REPORT 0x0004 /* new value must be reported to client *= / = = /* GUC records for specific variable types */ ------- =_aaaaaaaaaa0--