On Thu, Apr 15, 2021 at 03:43:59PM +0800, Julien Rouhaud wrote:
> On Mon, Apr 12, 2021 at 02:56:59PM +0800, Julien Rouhaud wrote:
> > I think we should simply document that %Q is not compatible with
> > log_statements.
> 
> Hearing no objection I documented that limitation.
> 
> > 
> > > While making the feature run on some test server, I have noticed that
> > > %Q would log some garbage query ID for autovacuum workers that's kept
> > > around.  That looks wrong.
> > 
> > I've not been able to reproduce it, do you have some hint on how to do it?
> > 
> > Maybe setting a zero queryid at the beginning of AutoVacWorkerMain() could 
> > fix
> > the problem?
> 
> It turns out that the problem was simply that some process can inherit a
> PgBackendStatus for which a previous backend reported a queryid.  For 
> processes
> like autovacuum process, they will never report a new identifier so they
> reported the previous one.  Resetting the field like the other ones in
> pgstat_bestart() fixes the problem for autovacuum and any similar process.

I slightly adjusted the patch and applied it.  Thanks.  I think this
closes all the open issues around query_id.  :-)

-- 
  Bruce Momjian  <br...@momjian.us>        https://momjian.us
  EDB                                      https://enterprisedb.com

  If only the physical world exists, free will is an illusion.

diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
index 776ab1a8c8..dd7ebe7a9d 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -7139,6 +7139,16 @@ log_line_prefix = '%m [%p] %q%u@%d/%a '
 </programlisting>
         </para>
        </tip>
+
+       <note>
+        <para>
+         The <literal>%Q</literal> escape always reports a zero identifier
+         for lines output by <xref linkend="guc-log-statement"/> because
+         <varname>log_statement</varname> generates output before an
+         identifier can be calculated, including invalid statements for
+         which an identifier cannot be calculated.
+        </para>
+       </note>
       </listitem>
      </varlistentry>
 
diff --git a/src/backend/utils/activity/backend_status.c b/src/backend/utils/activity/backend_status.c
index 787f062f9c..a368101103 100644
--- a/src/backend/utils/activity/backend_status.c
+++ b/src/backend/utils/activity/backend_status.c
@@ -398,6 +398,7 @@ pgstat_bestart(void)
 	lbeentry.st_state = STATE_UNDEFINED;
 	lbeentry.st_progress_command = PROGRESS_COMMAND_INVALID;
 	lbeentry.st_progress_command_target = InvalidOid;
+	lbeentry.st_query_id = UINT64CONST(0);
 
 	/*
 	 * we don't zero st_progress_param here to save cycles; nobody should

Reply via email to