Implement TODO item:

Fix log_line_prefix to display the transaction id (%x) for statements not in a transaction block
    Currently it displays zero.


Check that the XID has been assigned at the location where the statement log is now printed. If not, no statement log is output. And then before finish_xact_command. If the statement has not been output to the log. Here the log can get XID.

DML that does not manipulate any data still does not get XID.

[32718][788] LOG:  statement: insert into t1 values(1,0,'');
[32718][789] LOG:  statement: delete from t1;
[32718][0] LOG:  statement: delete from t1;


--
Quan Zongliang
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index 21b9763183..b73676ab9d 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -1063,8 +1063,9 @@ exec_simple_query(const char *query_string)
         */
        parsetree_list = pg_parse_query(query_string);
 
-       /* Log immediately if dictated by log_statement */
-       if (check_log_statement(parsetree_list))
+       /* Log immediately if dictated by log_statement and XID assigned. */
+       if (GetTopTransactionIdIfAny() != InvalidTransactionId &&
+                       check_log_statement(parsetree_list))
        {
                ereport(LOG,
                                (errmsg("statement: %s", query_string),
@@ -1281,6 +1282,16 @@ exec_simple_query(const char *query_string)
 
                PortalDrop(portal, false);
 
+               /* Log if dictated by log_statement and has not been logged. */
+               if (!was_logged && check_log_statement(parsetree_list))
+               {
+                       ereport(LOG,
+                                       (errmsg("statement: %s", query_string),
+                                       errhidestmt(true),
+                                       errdetail_execute(parsetree_list)));
+                       was_logged = true;
+               }
+
                if (lnext(parsetree_list, parsetree_item) == NULL)
                {
                        /*

Reply via email to