This is an automated email from the ASF dual-hosted git repository.

yjhjstz pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudberry.git

commit 16ff096c3eea3911fe2dcbfd0b0c8d3eb8a7224b
Author: Huansong Fu <fuhuans...@gmail.com>
AuthorDate: Tue Jun 20 12:42:23 2023 -0700

    Fix gp_stat_bgwriter which shows incomplete/incorrect results
    
    The gp_stat_bgwriter view which is supposed to show cluster-wide view
    of pg_stat_bgwriter but only shows part of the segments, e.g.:
    
    ```sql
    postgres=# select * from gp_stat_bgwriter;
     segment_id | checkpoints_timed | checkpoints_req | checkpoint_write_time | 
checkpoint_sync_time | buffers_checkpoint | buffers_clean | maxwritten_clean | 
buffers_backend | buffers_backend_fsync | buffers_alloc |          stats_reset
    
------------+-------------------+-----------------+-----------------------+----------------------+--------------------+---------------+------------------+-----------------+-----------------------+---------------+-------------------------------
              0 |              1097 |               0 |                 37946 | 
                  15 |                378 |             0 |                0 |  
          5133 |                     0 |           387 | 2023-04-28 
12:46:24.938582-07
             -1 |              1097 |               0 |                 37946 | 
                  15 |                378 |             0 |                0 |  
          5133 |                     0 |           387 | 2023-04-28 
12:46:24.938582-07
    (2 rows)
    ```
    And even the result of the only primary segment is not correct - it is
    just repeating the result from the coordinator.
    
    The root cause is that because pg_stat_bgwriter is selecting from a bunch
    of STABLE functions, they are being evaluated and simplied in planning 
phase.
    As a result, the coordinator's results are incorrectly used for the primary.
    In addition, the subquery SELECT * FROM pg_stat_bgwriter is given a locus
    CdbLocusType_General which results in direct dispatch and only one primary
    is being dispatched to.
    
    The initial fix that I thought was to not simplify stable functions at
    all, but that would be an overkill. And it's not easy to do something like
    in #14499 where we skip the simpifying just for the functions that
    pg_stat_bgwriter needs - there are too many functions and no clear pattern 
to
    distinguish them.
    
    So just do a simpler fix which is to have a volatile function to select the
    results, and let pg_stat_bgwriter select from that function.
    
    Also modified the regress test to be able to catch such issue in future.
    Found a new issue with pg_replication_slot which is unrelated to the current
    one. Added a FIXME for it.
    
    Fix #15455
---
 src/backend/catalog/system_views.sql | 46 ++++++++++++++++++++++++++----------
 1 file changed, 34 insertions(+), 12 deletions(-)

diff --git a/src/backend/catalog/system_views.sql 
b/src/backend/catalog/system_views.sql
index 561f89de8f..7ec1293af3 100644
--- a/src/backend/catalog/system_views.sql
+++ b/src/backend/catalog/system_views.sql
@@ -1545,19 +1545,41 @@ CREATE VIEW pg_stat_archiver AS
         s.stats_reset
     FROM pg_stat_get_archiver() s;
 
-CREATE VIEW pg_stat_bgwriter AS
+-- Internal function for pg_stat_bgwriter. It needs to be VOLATILE in order
+-- for pg_stat_bgwriter to work correctly with gp_dist_random.
+CREATE OR REPLACE FUNCTION pg_stat_bgwriter_func()
+RETURNS TABLE (
+    checkpoints_timed BIGINT,
+    checkpoints_req BIGINT,
+    checkpoint_write_time FLOAT,
+    checkpoint_sync_time FLOAT,
+    buffers_checkpoint BIGINT,
+    buffers_clean BIGINT,
+    maxwritten_clean BIGINT,
+    buffers_backend BIGINT,
+    buffers_backend_fsync BIGINT,
+    buffers_alloc BIGINT,
+    stats_reset TIMESTAMPTZ
+)
+AS
+$$
     SELECT
-        pg_stat_get_bgwriter_timed_checkpoints() AS checkpoints_timed,
-        pg_stat_get_bgwriter_requested_checkpoints() AS checkpoints_req,
-        pg_stat_get_checkpoint_write_time() AS checkpoint_write_time,
-        pg_stat_get_checkpoint_sync_time() AS checkpoint_sync_time,
-        pg_stat_get_bgwriter_buf_written_checkpoints() AS buffers_checkpoint,
-        pg_stat_get_bgwriter_buf_written_clean() AS buffers_clean,
-        pg_stat_get_bgwriter_maxwritten_clean() AS maxwritten_clean,
-        pg_stat_get_buf_written_backend() AS buffers_backend,
-        pg_stat_get_buf_fsync_backend() AS buffers_backend_fsync,
-        pg_stat_get_buf_alloc() AS buffers_alloc,
-        pg_stat_get_bgwriter_stat_reset_time() AS stats_reset;
+        pg_stat_get_bgwriter_timed_checkpoints(),
+        pg_stat_get_bgwriter_requested_checkpoints(),
+        pg_stat_get_checkpoint_write_time(),
+        pg_stat_get_checkpoint_sync_time(),
+        pg_stat_get_bgwriter_buf_written_checkpoints(),
+        pg_stat_get_bgwriter_buf_written_clean(),
+        pg_stat_get_bgwriter_maxwritten_clean(),
+        pg_stat_get_buf_written_backend(),
+        pg_stat_get_buf_fsync_backend(),
+        pg_stat_get_buf_alloc(),
+        pg_stat_get_bgwriter_stat_reset_time();
+$$
+LANGUAGE SQL;
+
+CREATE VIEW pg_stat_bgwriter AS
+    SELECT * FROM pg_stat_bgwriter_func();
 
 CREATE VIEW pg_stat_wal AS
     SELECT


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@cloudberry.apache.org
For additional commands, e-mail: commits-h...@cloudberry.apache.org

Reply via email to