SoFunction
Updated on 2025-04-08

Postgresql dynamically counts the number of times a certain value appears in a column

Example analysis:

select to_char(, 'yyyy-MM-dd HH24') as hour,
  , sum(case  when '1' then 1 else 0 end) as "1",
  sum(case  when '2' then 1 else 0 end) as "2",
  sum(case  when '3' then 1 else 0 end) as "3",
  sum(case  when '4' then 1 else 0 end) as "4",
  sum(case  when '5' then 1 else 0 end) as "5",
   from iface_satisfaction_investigation as log 
where  >= '2017-08-03 00:00:00' and  < '2017-08-04 00:00:00' 
group by hour,, order by hour,, asc

to_char: used for querying time formatting, to_char(, 'yyyy-MM-dd HH24'), the approximate result is: 2017-08-03 13

sum(): It is undoubtedly used to calculate the sum.

sum(case when '1' then 1 else 0 end) What is the calculation?

What he means is:

Calculate how many rows there are when the value of grade column is 1, and the sum(...) afterwards is analogous.

There's nothing else to say

Supplement: Commonly used statistics in PostgreSQL

I won't say much nonsense, let's just read the code~

/* Calculate the space size of the table*/
select oid,table_schema as "model",
	table_name as "Table Name",
	row_estimate::bigint as "Number of rows in the table(Estimated value)",
	pg_size_pretty(total_bytes) as "Total Size",
  pg_size_pretty(table_bytes) as "Table Size",
	pg_size_pretty(index_bytes) as "Index Size",
  pg_size_pretty(toast_bytes) as "Toast table total size"  
from (
	select *, total_bytes-index_bytes-coalesce(toast_bytes,0) as table_bytes 
		from (
			select
				,
				nspname as table_schema,
				relname as table_name,
				 as row_estimate,
				pg_total_relation_size() as total_bytes,
				pg_indexes_size() as index_bytes,
				pg_total_relation_size(reltoastrelid) as toast_bytes
			from pg_class c
			left join pg_namespace n on  = 
			where relkind = 'r'
 ) t1 
) t2 order by 2,3;
/*Statistics of user table information*/
select 
	schemaname as "model",
	relname as "Table Name",
	seq_scan as "Number of sequential scans",
	seq_tup_read as "Sequentially scan to get the number of active rows",
	idx_scan as "Number of Index Scans",
	idx_tup_fetch as "Index scan gets the number of active rows",
	n_tup_ins as "Accumulated number of inserted rows",
	n_tup_upd as "Cumulative number of updated rows(IncludeHOT Updated rows)",	
	n_tup_del as "Accumulated number of deleted rows",
	n_live_tup as "The estimated number of current active rows",
	n_dead_tup as "The estimated number of current death rows",
	n_mod_since_analyze as "The number of rows that were modified after the last analysis",
	last_vacuum as "The last time I was manually cleaned up(No statisticsVACUUM FULL)",
	last_autovacuum as "The last time of automatic cleaning",
	last_analyze as "The last time of manual analysis",
	last_autoanalyze as "The last time of automatic cleaning analysis",
	vacuum_count as "Number of times manual cleaning",
	autovacuum_count as "Number of times automatic cleaning",
	analyze_count as "Number of manual analysis",
	autoanalyze_count as "Number of automatic analysis",
	pg_size_pretty(pg_table_size(relid)) as "Table size(不Include索引)"
from pg_stat_user_tables
order by 1;
/* Statistics of user table IO information*/
select
	schemaname as "model",
	relname as "Table Name",
	heap_blks_read as "Number of disk blocks read",
	heap_blks_hit as "Number of buffer hits",
	idx_blks_read as "Number of disk blocks read by all indexes on the table",
	idx_blks_hit as "surface上的所有索引Number of buffer hits",
	toast_blks_read as "TOASTsurface(If there is)Number of disk blocks read",
	toast_blks_hit as "TOASTsurface(If there is)Number of buffer hits",
	tidx_blks_read as "TOASTsurface索引(If there is)Number of disk blocks read",
	tidx_blks_hit as "TOASTsurface索引(If there is)Number of buffer hits"
from pg_statio_user_tables
order by 1;
/*Statistics of user index information*/
select 
	indexrelid,
	schemaname as "model",
	relname as "索引所在的surface名称",
	indexrelname as "Index name",
	idx_scan as "Number of Index Scans",
	idx_tup_read as "Number of index items returned by index scan",
	idx_tup_fetch as "Number of active rows obtained by a simple index scan",
	pg_size_pretty(pg_relation_size(indexrelid)) as "Index Size"
from pg_stat_user_indexes
order by 1,2;
/*Tracking function, you need to open the track_functions parameter (default closed)*/
select * from pg_stat_user_functions;

The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.