[Patch] New pg_stat_tablespace view

Поиск
Список
Период
Сортировка
Искать

[Patch] New pg_stat_tablespace view

От:
shihao zhong <zhong950419@gmail.com>
Дата:
Вложения:
Hi hackers,

I’ve been working on extending the cumulative statistics system to
provide better visibility into tablespace-level workloads, and I'd
like to propose a patch to add a new system view: pg_stat_tablespace.

Currently, PostgreSQL provides statistics per database (e.g.,
pg_stat_database) and per relation (e.g., pg_statio_user_tables).
However, because tablespaces can span multiple databases, it is
difficult for DBAs to analyze storage hotspots across the cluster or
verify if a specific tablespace (such as a high-performance SSD vs a
slow HDD array) is experiencing I/O bottlenecks or excessive temporary
file usage.

The pg_stat_tablespace view bridges this gap by providing an aggregate
view of block I/O and temporary file usage grouped by tablespace,
making it easier to optimize storage architectures.

Thanks,
Shihao

Re: [Patch] New pg_stat_tablespace view

От:
shihao zhong <zhong950419@gmail.com>
Дата:
Вложения:
On Mon, Mar 23, 2026 at 3:08 PM shihao zhong  wrote:
>
> Hi hackers,
>
> I’ve been working on extending the cumulative statistics system to
> provide better visibility into tablespace-level workloads, and I'd
> like to propose a patch to add a new system view: pg_stat_tablespace.
>
> Currently, PostgreSQL provides statistics per database (e.g.,
> pg_stat_database) and per relation (e.g., pg_statio_user_tables).
> However, because tablespaces can span multiple databases, it is
> difficult for DBAs to analyze storage hotspots across the cluster or
> verify if a specific tablespace (such as a high-performance SSD vs a
> slow HDD array) is experiencing I/O bottlenecks or excessive temporary
> file usage.
>
> The pg_stat_tablespace view bridges this gap by providing an aggregate
> view of block I/O and temporary file usage grouped by tablespace,
> making it easier to optimize storage architectures.
>
> Thanks,
> Shihao

New version fix the CI/CD

Re: [Patch] New pg_stat_tablespace view

От:
Zsolt Parragi <zsolt.parragi@percona.com>
Дата:
Hello!

I get assertion failures with the patch, all it takes is a simple select:

SELECT * FROM pg_stat_tablespace;
  TRAP: failed Assert("tupdesc->firstNonCachedOffsetAttr >= 0"),
  File: execTuples.c, Line: 2341

The test suite also fails with similar errors.


Re: [Patch] New pg_stat_tablespace view

От:
songjinzhou <tsinghualucky912@foxmail.com>
Дата:
在 2026/3/24 10:49, shihao zhong 写道:
> On Mon, Mar 23, 2026 at 3:08 PM shihao zhong  wrote:
>>
>> Hi hackers,
>>
>> I’ve been working on extending the cumulative statistics system to
>> provide better visibility into tablespace-level workloads, and I'd
>> like to propose a patch to add a new system view: pg_stat_tablespace.
>>
>> Currently, PostgreSQL provides statistics per database (e.g.,
>> pg_stat_database) and per relation (e.g., pg_statio_user_tables).
>> However, because tablespaces can span multiple databases, it is
>> difficult for DBAs to analyze storage hotspots across the cluster or
>> verify if a specific tablespace (such as a high-performance SSD vs a
>> slow HDD array) is experiencing I/O bottlenecks or excessive temporary
>> file usage.
>>
>> The pg_stat_tablespace view bridges this gap by providing an aggregate
>> view of block I/O and temporary file usage grouped by tablespace,
>> making it easier to optimize storage architectures.
>>
>> Thanks,
>> Shihao
> 
> New version fix the CI/CD

Hello, shihao

I applied it on master and did a simple test. Here are some minor review 
comments:

1. The type of temp_bytes in monitoring.sgml should be bigint, but it 
was written as numeric here.

2. The pgstat_drop_tablespace function doesn't seem to be called.

Thank you.

-- 
regards,
songjinzhou




Re: [Patch] New pg_stat_tablespace view

От:
shihao zhong <zhong950419@gmail.com>
Дата:
Вложения:
On Tue, Mar 24, 2026 at 3:22 AM songjinzhou
 wrote:
>
> 在 2026/3/24 10:49, shihao zhong 写道:
> > On Mon, Mar 23, 2026 at 3:08 PM shihao zhong  wrote:
> >>
> >> Hi hackers,
> >>
> >> I’ve been working on extending the cumulative statistics system to
> >> provide better visibility into tablespace-level workloads, and I'd
> >> like to propose a patch to add a new system view: pg_stat_tablespace.
> >>
> >> Currently, PostgreSQL provides statistics per database (e.g.,
> >> pg_stat_database) and per relation (e.g., pg_statio_user_tables).
> >> However, because tablespaces can span multiple databases, it is
> >> difficult for DBAs to analyze storage hotspots across the cluster or
> >> verify if a specific tablespace (such as a high-performance SSD vs a
> >> slow HDD array) is experiencing I/O bottlenecks or excessive temporary
> >> file usage.
> >>
> >> The pg_stat_tablespace view bridges this gap by providing an aggregate
> >> view of block I/O and temporary file usage grouped by tablespace,
> >> making it easier to optimize storage architectures.
> >>
> >> Thanks,
> >> Shihao
> >
> > New version fix the CI/CD
>
> Hello, shihao
>
> I applied it on master and did a simple test. Here are some minor review
> comments:
>
> 1. The type of temp_bytes in monitoring.sgml should be bigint, but it
> was written as numeric here.
>
> 2. The pgstat_drop_tablespace function doesn't seem to be called.
>
> Thank you.
>
> --
> regards,
> songjinzhou
Hi SongJin,

Thanks for your reviewing, the v2 patch addresses both 1 and 2.

Thanks,
Shihao

Re: [Patch] New pg_stat_tablespace view

От:
Zsolt Parragi <zsolt.parragi@percona.com>
Дата:
Hello!

blk_read_time and blk_write_time doesn't seem to work, they show 0 to
me even after some workloads, and I don't see any assignments in the
code. The testcase also checks for "blk_read_time >= 0" which
trivially succeeds.

blocks_fetched is also misleading, it includes both reads and cache
hits. pg_stat_database calls this column blocks_read, and properly
substracts blocks_hit from it.

+ rel->pgstat_info->reltablespace = rel->rd_locator.spcOid;

Shouldn't this be included in TwoPhasePgStatRecord / pgstat_twophase_postcommit?


Re: [Patch] New pg_stat_tablespace view

От:
shihao zhong <zhong950419@gmail.com>
Дата:
Вложения:
On Tue, Mar 24, 2026 at 6:11 PM Zsolt Parragi  wrote:
>
> Hello!
>
> blk_read_time and blk_write_time doesn't seem to work, they show 0 to
> me even after some workloads, and I don't see any assignments in the
> code. The testcase also checks for "blk_read_time >= 0" which
> trivially succeeds.
>
> blocks_fetched is also misleading, it includes both reads and cache
> hits. pg_stat_database calls this column blocks_read, and properly
> substracts blocks_hit from it.
>
> + rel->pgstat_info->reltablespace = rel->rd_locator.spcOid;
>
> Shouldn't this be included in TwoPhasePgStatRecord / pgstat_twophase_postcommit?
>
>

Hi Zsolt and Jian,

Thanks for the feedback. I've attached v3, addressing all comments.
Notably, I've included tuple-level stats in the pg_stat_tablespace
view to align with the addition of SpaceOid in TwoPhasePgStatRecord.

Thanks,
Shihao

Re: [Patch] New pg_stat_tablespace view

От:
shihao zhong <zhong950419@gmail.com>
Дата:
Вложения:
On Fri, Mar 27, 2026 at 2:05 PM shihao zhong  wrote:
>
> On Tue, Mar 24, 2026 at 6:11 PM Zsolt Parragi  wrote:
> >
> > Hello!
> >
> > blk_read_time and blk_write_time doesn't seem to work, they show 0 to
> > me even after some workloads, and I don't see any assignments in the
> > code. The testcase also checks for "blk_read_time >= 0" which
> > trivially succeeds.
> >
> > blocks_fetched is also misleading, it includes both reads and cache
> > hits. pg_stat_database calls this column blocks_read, and properly
> > substracts blocks_hit from it.
> >
> > + rel->pgstat_info->reltablespace = rel->rd_locator.spcOid;
> >
> > Shouldn't this be included in TwoPhasePgStatRecord / pgstat_twophase_postcommit?
> >
> >
>
> Hi Zsolt and Jian,
>
> Thanks for the feedback. I've attached v3, addressing all comments.
> Notably, I've included tuple-level stats in the pg_stat_tablespace
> view to align with the addition of SpaceOid in TwoPhasePgStatRecord.
>
> Thanks,
> Shihao

Rebase with head.

Re: [Patch] New pg_stat_tablespace view

От:
Bernd Reiß <bd_reiss@gmx.at>
Дата:
Hi Shihao,

On 14/09/2026 04:46, shihao zhong wrote:
> Hi Ahmed,
> 
> Thanks for reviewing my patch!
> 
>  > I am having a problem building the v5 patch locally. I get the 
> following compile errors:
> You are right: 3f2f5e7c4c moved those counters to
> PgStat_TableCountsXact and my v5 missed it. Fixed in v6.
> 
> While testing that fix I found a worse bug, which v6 also fixes.
> 
> The buffer manager reports block I/O timings from the checkpointer and the
> background writer too. Those two processes never call pgstat_report_stat(),
> so the pending entry they created was never flushed. That caused two
> problems.
> 
> First, an unflushed pending entry keeps its shared entry alive, because
> pgstat_gc_entry_refs() skips refs that still have pending data. So after
> DROP TABLESPACE the entry could not be freed, and the checkpointer crashed
> while writing the stats file at shutdown:
> 
> TRAP: failed Assert("!ps->dropped"), File: "pgstat.c", Line: 1776
> LOG: checkpointer process (PID ...) was terminated by signal 6
> 
> To reproduce, on an assert build with track_io_timing=on: create a
> tablespace, insert enough rows that the checkpointer writes to it,
> CHECKPOINT, DROP TABLE, DROP TABLESPACE, then pg_ctl stop -m fast.
> 
> Second, blk_write_time stayed at zero while the server ran. In one test the
> checkpointer wrote 161 buffers in 3 ms and the view did not move at all.
> The value only appeared after a restart. Most writes come from the
> checkpointer, so the column was close to useless.
> 
> v6 keeps these timings in process-local memory and flushes them through
> flush_static_cb, the same way PGSTAT_KIND_BACKEND handles its own data.
> Backends flush through pgstat_report_stat(). The checkpointer and the
> background writer call pgstat_flush_tablespace_times() from
> pgstat_report_checkpointer() and pgstat_report_bgwriter().
> 
> v6 also adds pgstat_create_tablespace(), called from CreateTableSpace().
> v5 had a drop but no create, and this clears old stats if a tablespace OID
> is ever reused.
> 
> 
> Thanks,
> 
> Shihao
> 

thanks for the updated patch.

I noticed that one test still checks for >= 0 for some columns. As Zsolt
Parragi mentioned, this condition is trivially always true. This might 
still need to be addressed.

I watched the numbers in the view after some workload on a database that 
is completely in one tablespace that only contains this database. They 
add up to what pg_stat_database reports. One thing worth mentioning, 
though, is that there is some skew in blk_read_time and blk_write_time. 
The following queries compare the database (first row) to the tablespace 
(second row).

test=# select
     d.blks_read,
     d.blks_hit,
     d.blk_read_time,
     d.blk_write_time
from
     pg_stat_database d where datname = 'test'
union all
select
     t.blks_read,
     t.blks_hit,
     t.blk_read_time,
     t.blk_write_time
from pg_stat_tablespace t where tablespace_name = 'tbs_alt';

  blks_read | blks_hit | blk_read_time | blk_write_time
-----------+----------+---------------+----------------
     129030 | 50180798 |         3.317 |        457.384
     129030 | 50180798 |         3.442 |         467.34
(2 rows)

I attribute the difference to pgstat_count_tablespace_blk_*_time being 
called after pgstat_count_io_op_time and therefore registering the time 
spent a little later.

The tuples add up fine:

test=# select
     d.tup_returned,
     d.tup_fetched,
     d.tup_inserted,
     d.tup_updated,
     d.tup_deleted
from
     pg_stat_database d where datname = 'test'
union all
select
     t.tup_returned,
     t.tup_fetched,
     t.tup_inserted,
     t.tup_updated,
     t.tup_deleted
from pg_stat_tablespace t where tablespace_name = 'tbs_alt';

  tup_returned | tup_fetched | tup_inserted | tup_updated | tup_deleted
--------------+-------------+--------------+-------------+-------------
      20000926 |          32 |     10000000 |    10000001 |    10000000
      20000926 |          32 |     10000000 |    10000001 |    10000000
(2 rows)

The temp file stats also add up to what pg_stat_database reports:

test=# select
     d.temp_files,
     d.temp_bytes
from pg_stat_database d where datname='test'
union all
select
     t.temp_files,
     t.temp_bytes
from pg_stat_tablespace t where tablespace_name ='tbs_alt';

  temp_files | temp_bytes
------------+------------
           2 | 1202405376
           2 | 1202405376

One problem I came across while testing was a mismatch in numbers after 
a server restart when there is an open session in the database.

The setup involves the following:

postgres=# create tablespace tbs_alt location '/var/lib/postgresql/tbs_alt';
CREATE TABLESPACE
postgres=# create database test tablespace tbs_alt;
CREATE DATABASE
postgres=# \c test
You are now connected to database "test" as user "postgres".
test=# create table test (id bigint);
CREATE TABLE
test=#

I ran the following several times (the FATAL message represents pg_ctl 
restart):

test=# truncate test;
TRUNCATE TABLE
test=# select pg_stat_reset();
  pg_stat_reset
---------------

(1 row)

test=# select pg_stat_reset_shared();
  pg_stat_reset_shared
----------------------

(1 row)

test=# insert into test select generate_series(1,10000);
INSERT 0 10000

test=# select 1;

FATAL:  terminating connection due to administrator command
server closed the connection unexpectedly
         This probably means the server terminated abnormally
         before or while processing the request.
The connection to the server was lost. Attempting reset: Succeeded.

test=# select
     datname,
     d.blks_read,
     d.blks_hit
from
     pg_stat_database d where datname = 'test'
union all
select
     tablespace_name,
     t.blks_read,
     t.blks_hit
from pg_stat_tablespace t where tablespace_name = 'tbs_alt';

Examples of results from 3 different (non-consecutive) runs:

  datname | blks_read | blks_hit
---------+-----------+----------
  test    |       105 |    11713
  tbs_alt |       102 |    11706
(2 rows)

  datname | blks_read | blks_hit
---------+-----------+----------
  test    |       112 |    12055
  tbs_alt |       115 |    12062
(2 rows)

  datname | blks_read | blks_hit
---------+-----------+----------
  test    |        99 |    11692
  tbs_alt |        99 |    11692
(2 rows)

I am not quite sure why these numbers differ, but whenever they are not 
the same, blks_read differs exactly by 3 and blks_hit by 7. The 
difference stays constant at 3 and 7 as more activity accumulates. This 
might be worth investigating.

All in all this is a very interesting patch. It could be very useful for
monitoring and day-to-day DBA workflows.

Best
Bernd


Re: [Patch] New pg_stat_tablespace view

От:
shihao zhong <zhong950419@gmail.com>
Дата:
Вложения:
Hi Bernd,

Thanks for testing.

> blks_read differs exactly by 3 and blks_hit by 7

I can reproduce this, and it does not need a restart. pg_stat_reset()
clears the database entry and pg_stat_reset_shared() clears the tablespace
entry. If more than a second passes between them, the backend flushes its
pending counts in the gap, and the entry that was reset first keeps them.
Swapping the two resets flips the sign. With both resets in one statement
the numbers always match.

> there is some skew in blk_read_time and blk_write_time

Part of it was the second clock read. v7 reuses the time that
pgstat_count_io_op_time() already computed.


> one test still checks for >= 0

Fixed.

v7 attached.

Thanks,
Shihao 



Re: [Patch] New pg_stat_tablespace view

От:
solai v <solai.cdac@gmail.com>
Дата:
Hi all,

On Mon, Sep 21, 2026 at 10:45 AM shihao zhong  wrote:
>
> Hi Bernd,
>
> Thanks for testing.
>
> > blks_read differs exactly by 3 and blks_hit by 7
>
> I can reproduce this, and it does not need a restart. pg_stat_reset()
> clears the database entry and pg_stat_reset_shared() clears the tablespace
> entry. If more than a second passes between them, the backend flushes its
> pending counts in the gap, and the entry that was reset first keeps them.
> Swapping the two resets flips the sign. With both resets in one statement
> the numbers always match.
>
> > there is some skew in blk_read_time and blk_write_time
>
> Part of it was the second clock read. v7 reuses the time that
> pgstat_count_io_op_time() already computed.
>
>
> > one test still checks for >= 0
>
> Fixed.
>
> v7 attached.
>

Thank you for the updated patch. I tested the v7 patch and verified
that the regression suite completed successfully, with all 239 tests
passing, including the stats and tablespace tests. I also performed
targeted testing using a dedicated  tablespace. The view reported
tuple activity for inserts, updates, and deletes, along with nonzero
block-write timing when I/O timing was enabled. The checkpoint
completed successfully, and the server log showed no errors in the
inspected output. After testing, I dropped the test table and
tablespace and shut down the separate test server cleanly. One concern
is that this targeted workload did not demonstrate nonzero
physical-read counters or read timing. Apart from this limitation, I
did not observe any functional or regression errors in the tests I
performed.

Regards,
Solai


Re: [Patch] New pg_stat_tablespace view

От:
shihao zhong <zhong950419@gmail.com>
Дата:
Hi Solai,

Thanks for testing.

> One concern is that this targeted workload did not demonstrate nonzero
> physical-read counters or read timing.

That is expected. The pages you just wrote are still in shared buffers, so
later scans count as blks_hit and there is nothing to read from disk. Restart
the server and scan the table again, and blks_read and blk_read_time should
go up.

The regression test covers this path.

Thanks,
Shihao

Re: [Patch] New pg_stat_tablespace view

От:
Andres Freund <andres@anarazel.de>
Дата:
Hi,

On 2026-09-18 23:07:07 -0400, shihao zhong wrote:
> @@ -1824,6 +1824,7 @@ WaitReadBuffers(ReadBuffersOperation *operation)
>  				!pgaio_wref_check_done(&operation->io_wref))
>  			{
>  				instr_time	io_start = pgstat_prepare_io_time(track_io_timing);
> +				instr_time	io_time;
>  
>  				pgaio_wref_wait(&operation->io_wref);
>  				needed_wait = true;
> @@ -1833,8 +1834,10 @@ WaitReadBuffers(ReadBuffersOperation *operation)
>  				 * itself was already counted earlier in AsyncReadBuffers() --
>  				 * either by us or by another backend if this is a foreign IO.
>  				 */
> -				pgstat_count_io_op_time(io_object, io_context, IOOP_READ,
> -										io_start, 0, 0);
> +				io_time = pgstat_count_io_op_time(io_object, io_context, IOOP_READ,
> +												  io_start, 0, 0);
> +				pgstat_count_tablespace_blk_read_time(operation->smgr->smgr_rlocator.locator.spcOid,
> +													  io_time);
>  			}
>  			else
>  			{

I am not on board to add duplicate counting to add these places, particularly
not if that requires a hash table lookup every single time.

*If* we want this, it really needs to be designed to avoid all this duplicate
work, both in the amount of code added to a bunch of places, and in the amount
of work that's needed for stats lookups.

Greetings,

Andres Freund


Re: [Patch] New pg_stat_tablespace view

От:
shihao zhong <zhong950419@gmail.com>
Дата:
Hi Andres,

Thanks for looking.

> I am not on board to add duplicate counting to add these places

The clock is already read only once, but you are right that each site still
makes a second call. That should go.

What I would like to do instead is count it in one place, inside
pgstat_count_io_op_time(), right next to pgstat_count_backend_io_op_time().
I would add a variant of that function that takes the tablespace OID, and
keep the current one as a wrapper, so the WAL callers are not touched.

> particularly not if that requires a hash table lookup every single time.

The lookup is in a backend local table, no lock and no shared memory. It only
runs when track_io_timing is on and a real read or write happened, so it sits
next to two clock reads and a syscall. If that is still too much, I can keep a
pointer to the last tablespace used, and the common case becomes one compare.

> *If* we want this

pg_stat_io cannot tell you which device is slow. With tablespaces on
different storage, that is the first question people ask, and today the only
answer is to go to OS tools and map files back by hand.

Would that shape work for you? I would rather agree on it before sending v8.

Thanks,
Shihao

Re: [Patch] New pg_stat_tablespace view

От:
shihao zhong <zhong950419@gmail.com>
Дата:
Вложения:
Hi Andres,

v8 counts the time in pgstat_count_io_op_time(), next to the per-backend
time. bufmgr.c and localbuf.c call a variant that takes the tablespace OID
as one more argument, and that is their whole change. The last tablespace
entry used is cached, so the usual case is one compare and an add, with no
hash lookup. The times are flushed with the IO stats, so the checkpointer
and bgwriter changes are gone.

It also fixes an assertion failure at shutdown in v7, after dropping a
tablespace the checkpointer had written to. There is a test for it in
029_stats_restart.pl.

As for why, here is one tablespace on a RAM disk and one on the SSD, same
table, same 5000 random lookups, with debug_io_direct=data to keep the OS
cache out:

 tablespace_name | blks_read | blk_read_time | us_per_block
-----------------+-----------+---------------+--------------
 ts_ssd          |      9645 |         667.8 |         69.2
 ts_ram          |      9645 |         170.1 |         17.6

pg_stat_io and pg_stat_database only show the sum.

Regards,
Shihao

Re: [Patch] New pg_stat_tablespace view

От:
Bernd Reiß <bd_reiss@gmx.at>
Дата:
Hi Shihao,

On 19/09/2026 05:07, shihao zhong wrote:
> Hi Bernd,
> 
> Thanks for testing.
> 
>  > blks_read differs exactly by 3 and blks_hit by 7
> 
> I can reproduce this, and it does not need a restart. pg_stat_reset()
> clears the database entry and pg_stat_reset_shared() clears the tablespace
> entry. If more than a second passes between them, the backend flushes its
> pending counts in the gap, and the entry that was reset first keeps them.
> Swapping the two resets flips the sign. With both resets in one statement
> the numbers always match.
> 

Yes, this explains it.

>  > there is some skew in blk_read_time and blk_write_time
> 
> Part of it was the second clock read. v7 reuses the time that
> pgstat_count_io_op_time() already computed.
> 
> 
>  > one test still checks for >= 0
> 
> Fixed.
>

I like this: Having the test being strictly bigger and comparing to 
beforehand values makes much sense to me.


> v7 attached.
> 
> Thanks,
> Shihao
> 
> 
> 

Best
Bernd


Re: [Patch] New pg_stat_tablespace view

От:
shihao zhong <zhong950419@gmail.com>
Дата:
Вложения:
Hi,

cfbot fails on v8 now and then. The stats test checks that
blk_read_time grows after reading the table back, and that is not
guaranteed.

The read time only counts how long the backend waits for a read. If
the read is already done when the backend looks, nothing is added.
pg_stat_database behaves the same way on master.

v9 drops that one check. No code changes. I also shortened the commit
message.

Thanks,
Shihao

Re: [Patch] New pg_stat_tablespace view

От:
Zsolt Parragi <zsolt.parragi@percona.com>
Дата:
Hello

I see two more issue in v9.

1: there's a window for a permanent statistics leak when a tablespace
is dropped.
Maybe entries should be created eagerly during create tablespace and
only looked up with create = false for aggregation?

setup { SET allow_in_place_tablespaces = on; }
setup { CREATE TABLESPACE ts_res LOCATION ''; }
setup
{
	CREATE TABLE t_res (a int) TABLESPACE ts_res;
	INSERT INTO t_res SELECT generate_series(1, 100);
	CREATE TABLE ts_oid AS SELECT oid FROM pg_tablespace WHERE spcname = 'ts_res';
}

# Session teardowns run before this, so t_res is already gone.
teardown { DROP TABLESPACE IF EXISTS ts_res; }

session s1
setup
{
	SET debug_parallel_query = off;
	SELECT count(*) FROM t_res;
	SELECT pg_stat_force_next_flush();
}
step s1_read	{ SELECT count(*) FROM t_res; }
step s1_flush	{ SELECT pg_stat_force_next_flush(); }

session s2
step s2_drop_table	{ DROP TABLE t_res; }
step s2_drop_ts		{ DROP TABLESPACE ts_res; }
step s2_check
{
	SELECT EXISTS (SELECT FROM pg_tablespace t WHERE t.oid = o.oid) AS in_catalog,
		   pg_stat_have_stats('tablespace', 0, o.oid::int8) AS have_stats
	FROM ts_oid o;
}
teardown
{
	DROP TABLE IF EXISTS t_res;
	DROP TABLE ts_oid;
}

# s1's pending counts flush after the drop
permutation s2_check s1_read s2_drop_table s2_drop_ts s2_check s1_flush s2_check


2: Are you sure the transaction move behavior is correct? The test
case even documents it:

+-- A relation moved to another tablespace must be credited to the new one in
+-- pg_stat_tablespace.  pgstat_info is preserved across a relcache rebuild, so
+-- doing this while the relation is already in use exercises
+-- pgstat_relation_update_tablespace().
+CREATE TABLE tablespace_stats_move (a int);
+INSERT INTO tablespace_stats_move SELECT generate_series(1, 10);
+SELECT pg_stat_force_next_flush();
+SELECT tup_inserted AS stats_move_before FROM pg_stat_tablespace
+  WHERE tablespace_name = 'regress_tblspace' \gset
+
+BEGIN;
+SELECT count(*) > 0 FROM tablespace_stats_move;
+ALTER TABLE tablespace_stats_move SET TABLESPACE regress_tblspace;
+INSERT INTO tablespace_stats_move SELECT generate_series(1, 10);
+COMMIT;
+SELECT pg_stat_force_next_flush();

But looking at this test case, the table was read in the old
tablespace, not in the new, so attributing this to the new tablespace
doesn't seem right to me?


Re: [Patch] New pg_stat_tablespace view

От:
shihao zhong <zhong950419@gmail.com>
Дата:
Вложения:
Hi Zsolt,

Thanks, both are real. v10 is attached.

> 1: there's a window for a permanent statistics leak when a tablespace
> is dropped.

Fixed. A flush only looks up the entry now. If it is gone, the counts
are dropped.

CREATE TABLESPACE creates the entry, as you suggested. That alone
misses pg_default, standbys, and everything after a crash. So a
process also creates it the first time it counts something for a
tablespace. A relation or a temp file in the tablespace is in use at
that moment, so it cannot be dropped.

Your spec is in the patch as an isolation test.

> 2: Are you sure the transaction move behavior is correct?

No, it was wrong. Counts from before the move now go to the old
tablespace. The test checks both tablespaces.

Andres, counting a block time is now an add into a small local array.
v9 did a hash lookup each time the tablespace changed. The only lookup
now is when a tablespace is added to that array, once per tablespace
between two flushes.

Thanks,
Shihao

Re: [Patch] New pg_stat_tablespace view

От:
Andres Freund <andres@anarazel.de>
Дата:
Hi,

On 2026-10-09 00:49:20 -0400, shihao zhong wrote:
> @@ -113,6 +113,20 @@ pgstat_prepare_io_time(bool track_io_guc)
>  void
>  pgstat_count_io_op_time(IOObject io_object, IOContext io_context, IOOp io_op,
>  						instr_time start_time, uint32 cnt, uint64 bytes)
> +{
> +	pgstat_count_io_op_time_ext(io_object, io_context, io_op, start_time,
> +								cnt, bytes, InvalidOid);
> +}


If everything uses pgstat_count_io_op_time_ext(), there's no point in keeping
pgstat_count_io_op_time(). I.e. we should not introduce _ext().


> +/*
> + * Like pgstat_count_io_op_time() except the time is also credited to
> + * tablespace "spcoid", for pg_stat_tablespace.  The buffer manager uses this
> + * for relation I/O; everything else passes InvalidOid.
> + */
> +void
> +pgstat_count_io_op_time_ext(IOObject io_object, IOContext io_context,
> +							IOOp io_op, instr_time start_time,
> +							uint32 cnt, uint64 bytes, Oid spcoid)

One thing worth noting is that by adding another parameter to
pgstat_count_io_op_time() you pushed it into passing parameters via stack on
x86-64. That sometimes is noticeable.


>  {
>  	if (!INSTR_TIME_IS_ZERO(start_time))
>  	{
> @@ -147,6 +161,10 @@ pgstat_count_io_op_time(IOObject io_object, IOContext io_context, IOOp io_op,
>  		/* Add the per-backend count */
>  		pgstat_count_backend_io_op_time(io_object, io_context, io_op,
>  										io_time);
> +
> +		/* Add the per-tablespace count */
> +		if (OidIsValid(spcoid))
> +			pgstat_count_tablespace_io_op_time(spcoid, io_op, io_time);
>  	}
>  
>  	pgstat_count_io_op(io_object, io_context, io_op, cnt, bytes);
> @@ -162,11 +180,16 @@ pgstat_fetch_stat_io(void)

I'm not on board with adding an external function call to every invocation of
pgstat_count_backend_io_op_time(), particularly not one that then will also
often have to do a hash table lookup.


> +/*
> + * Fill in the counts of a relation or index that are kept per tablespace.
> + *
> + * Rows changed by a transaction that is still open are not in here yet.  They
> + * are added when the transaction ends, see AtEOXact_PgStat_Relations().
> + */
> +static void
> +pgstat_relation_tablespace_counts(const PgStat_RelationStatus *ps,
> +								  PgStat_StatTabspaceEntry *counts)
> +{
> +	memset(counts, 0, sizeof(*counts));
> +
> +	if (ps->kind == PGSTAT_KIND_INDEX)
> +	{
> +		counts->tuples_returned = ps->idx.tuples_returned;
> +		counts->tuples_fetched = ps->idx.tuples_fetched;
> +		counts->blocks_fetched = ps->idx.blocks_fetched;
> +		counts->blocks_hit = ps->idx.blocks_hit;
> +	}
> +	else
> +	{
> +		counts->tuples_returned = ps->tab.counts.tuples_returned;
> +		counts->tuples_fetched = ps->tab.counts.tuples_fetched;
> +		counts->tuples_inserted = ps->tab.counts_xact.tuples_inserted;
> +		counts->tuples_updated = ps->tab.counts_xact.tuples_updated;
> +		counts->tuples_deleted = ps->tab.counts_xact.tuples_deleted;
> +		counts->blocks_fetched = ps->tab.counts.blocks_fetched;
> +		counts->blocks_hit = ps->tab.counts.blocks_hit;
> +	}
> +}

Uh, what is the justification for keeping all of these also for tablespaces?
This could just as well be done on the querying side, no?

Greetings,

Andres Freund


Re: [Patch] New pg_stat_tablespace view

От:
shihao zhong <zhong950419@gmail.com>
Дата:
Вложения:
Hi Andres,

Thanks for looking at v10 and provide valuable feedback, I 
appreciate it.

You are right about the function. In the x86-64 code for v10, every
caller in bufmgr.c stores the tablespace OID on the stack, and the
old name cannot tail call the new one, so the WAL callers pay too.

v11 leaves pgstat_count_io_op_time() alone, its code is the same as
on master. Relation block IO calls a second function,
pgstat_count_smgr_io_op_time(), which takes the SMgrRelation in place
of the IOObject, so still six arguments. It reads the tablespace slot
cached in the SMgrRelation and adds the time to that slot. No lookup,
no new call. The slot is assigned on the first timed IO on a
relation, once per SMgrRelation per process, and that is also where
the shared entry gets created. Both functions share one inlined body,
and the generic one compiles to the same code as on master. No caller
touches the stack.

On the per relation counters. The reason is the one pg_stat_database
has for the same columns. Relation stats live in one database and
vanish with the relation, while a tablespace spans databases, and the
block counts are the denominator of the block times. So v11 keeps
them. If you still want fewer columns, say which ones.

v11 is attached. 

Thanks,
Shihao

Re: [Patch] New pg_stat_tablespace view

От:
Zsolt Parragi <zsolt.parragi@percona.com>
Дата:
> and everything after a crash

There's still an issue with tablespaces no backend touched after
resetting stats (crashing):

* Create two tablespaces (ts_a,ts_b)
* Create a table with some data in ts_a
* Kill and restart the server
* In one transaction: read the table, move table to ts_b, insert some
data, commit
* check stats, ts_a read is there, but ts_b write is missing


Re: [Patch] New pg_stat_tablespace view

От:
shihao zhong <zhong950419@gmail.com>
Дата:
Вложения:
Hi Zoslot,

Thanks for reviewing my patch.

> There's still an issue with tablespaces no backend touched after
> resetting stats (crashing):

v12 creates the entry when the tablespace of a relation changes, in
the same place that credits the old tablespace with what was counted
before the move. The table lives in the new tablespace at that point,
so it cannot be dropped underneath us. The recovery test now has your
scenario,.

Thanks,
Shihao
FAQ