@@ -77,9 +77,11 @@ typedef struct Counters
7777 int64 rows ; /* total # of retrieved or affected rows */
7878 int64 shared_blks_hit ; /* # of shared buffer hits */
7979 int64 shared_blks_read ; /* # of shared disk blocks read */
80+ int64 shared_blks_dirtied ; /* # of shared disk blocks dirtied */
8081 int64 shared_blks_written ; /* # of shared disk blocks written */
8182 int64 local_blks_hit ; /* # of local buffer hits */
8283 int64 local_blks_read ; /* # of local disk blocks read */
84+ int64 local_blks_dirtied ; /* # of local disk blocks dirtied */
8385 int64 local_blks_written ; /* # of local disk blocks written */
8486 int64 temp_blks_read ; /* # of temp blocks read */
8587 int64 temp_blks_written ; /* # of temp blocks written */
@@ -652,12 +654,16 @@ pgss_ProcessUtility(Node *parsetree, const char *queryString,
652654 pgBufferUsage .shared_blks_hit - bufusage .shared_blks_hit ;
653655 bufusage .shared_blks_read =
654656 pgBufferUsage .shared_blks_read - bufusage .shared_blks_read ;
657+ bufusage .shared_blks_dirtied =
658+ pgBufferUsage .shared_blks_dirtied - bufusage .shared_blks_dirtied ;
655659 bufusage .shared_blks_written =
656660 pgBufferUsage .shared_blks_written - bufusage .shared_blks_written ;
657661 bufusage .local_blks_hit =
658662 pgBufferUsage .local_blks_hit - bufusage .local_blks_hit ;
659663 bufusage .local_blks_read =
660664 pgBufferUsage .local_blks_read - bufusage .local_blks_read ;
665+ bufusage .local_blks_dirtied =
666+ pgBufferUsage .local_blks_dirtied - bufusage .local_blks_dirtied ;
661667 bufusage .local_blks_written =
662668 pgBufferUsage .local_blks_written - bufusage .local_blks_written ;
663669 bufusage .temp_blks_read =
@@ -766,9 +772,11 @@ pgss_store(const char *query, double total_time, uint64 rows,
766772 e -> counters .rows += rows ;
767773 e -> counters .shared_blks_hit += bufusage -> shared_blks_hit ;
768774 e -> counters .shared_blks_read += bufusage -> shared_blks_read ;
775+ e -> counters .shared_blks_dirtied += bufusage -> shared_blks_dirtied ;
769776 e -> counters .shared_blks_written += bufusage -> shared_blks_written ;
770777 e -> counters .local_blks_hit += bufusage -> local_blks_hit ;
771778 e -> counters .local_blks_read += bufusage -> local_blks_read ;
779+ e -> counters .local_blks_dirtied += bufusage -> local_blks_dirtied ;
772780 e -> counters .local_blks_written += bufusage -> local_blks_written ;
773781 e -> counters .temp_blks_read += bufusage -> temp_blks_read ;
774782 e -> counters .temp_blks_written += bufusage -> temp_blks_written ;
@@ -793,7 +801,8 @@ pg_stat_statements_reset(PG_FUNCTION_ARGS)
793801 PG_RETURN_VOID ();
794802}
795803
796- #define PG_STAT_STATEMENTS_COLS 14
804+ #define PG_STAT_STATEMENTS_COLS_V1_0 14
805+ #define PG_STAT_STATEMENTS_COLS 16
797806
798807/*
799808 * Retrieve statement statistics.
@@ -810,6 +819,7 @@ pg_stat_statements(PG_FUNCTION_ARGS)
810819 bool is_superuser = superuser ();
811820 HASH_SEQ_STATUS hash_seq ;
812821 pgssEntry * entry ;
822+ bool sql_supports_dirty_counters = true;
813823
814824 if (!pgss || !pgss_hash )
815825 ereport (ERROR ,
@@ -830,6 +840,8 @@ pg_stat_statements(PG_FUNCTION_ARGS)
830840 /* Build a tuple descriptor for our result type */
831841 if (get_call_result_type (fcinfo , NULL , & tupdesc ) != TYPEFUNC_COMPOSITE )
832842 elog (ERROR , "return type must be a row type" );
843+ if (tupdesc -> natts == PG_STAT_STATEMENTS_COLS_V1_0 )
844+ sql_supports_dirty_counters = false;
833845
834846 per_query_ctx = rsinfo -> econtext -> ecxt_per_query_memory ;
835847 oldcontext = MemoryContextSwitchTo (per_query_ctx );
@@ -887,14 +899,19 @@ pg_stat_statements(PG_FUNCTION_ARGS)
887899 values [i ++ ] = Int64GetDatumFast (tmp .rows );
888900 values [i ++ ] = Int64GetDatumFast (tmp .shared_blks_hit );
889901 values [i ++ ] = Int64GetDatumFast (tmp .shared_blks_read );
902+ if (sql_supports_dirty_counters )
903+ values [i ++ ] = Int64GetDatumFast (tmp .shared_blks_dirtied );
890904 values [i ++ ] = Int64GetDatumFast (tmp .shared_blks_written );
891905 values [i ++ ] = Int64GetDatumFast (tmp .local_blks_hit );
892906 values [i ++ ] = Int64GetDatumFast (tmp .local_blks_read );
907+ if (sql_supports_dirty_counters )
908+ values [i ++ ] = Int64GetDatumFast (tmp .local_blks_dirtied );
893909 values [i ++ ] = Int64GetDatumFast (tmp .local_blks_written );
894910 values [i ++ ] = Int64GetDatumFast (tmp .temp_blks_read );
895911 values [i ++ ] = Int64GetDatumFast (tmp .temp_blks_written );
896912
897- Assert (i == PG_STAT_STATEMENTS_COLS );
913+ Assert (i == sql_supports_dirty_counters ? \
914+ PG_STAT_STATEMENTS_COLS : PG_STAT_STATEMENTS_COLS_V1_0 );
898915
899916 tuplestore_putvalues (tupstore , tupdesc , values , nulls );
900917 }
0 commit comments