
declare
v_seq   bigint;
v_db    name;
msg     text;
v_version text;
v_stats_cnt	numeric :=0;
begin
select nextval('snapshot_num_seq') into v_seq;
select current_database() into v_db;
select version() into v_version;
/*
select count(*) into v_stats_cnt from pg_settings
	where name in ('stats_block_level','stats_row_level')
	and setting = 'on';
if v_stats_cnt < 2 then
	msg := 'Stats_block_level and stats_row_level parameters should be set = on.';
	return msg;
	exit;
end if;
*/
insert into edb$snap
values (v_seq,
        v_db,
     now(),
     null      ,
     null,
     null,
     'N')
;
if v_version like '%EnterpriseDB Advanced Server%' then
	insert into edb$system_waits
		select v_seq,v_db,
		wait_name  ,
		wait_count  ,
		avg_wait  ,
		max_wait  ,
		total_wait
		from system_waits;
 
	insert into edb$session_waits
		select v_seq, v_db,
		backend_id   ,
		wait_name   ,
		wait_count     ,
		avg_wait_time    ,
		max_wait_time    ,
		total_wait_time  
		, usename, query
			from session_waits LEFT OUTER JOIN pg_stat_activity 
			on backend_id = pg_stat_activity.pid;
	insert into edb$session_wait_history
		select v_seq,
		v_db,
		backend_id   ,
		seq          ,
		wait_name    ,
		elapsed      ,
		p1          ,
		p2           ,
		p3           
		from session_wait_history;
end if;
------------------------------ NEW ------------------------------
insert into edb$stat_all_tables
select v_seq,v_db,
relid              ,                     
 schemaname         ,                 
 relname            ,                
 seq_scan            ,                  
 seq_tup_read        ,                  
 idx_scan            ,                  
 idx_tup_fetch       ,                  
 n_tup_ins           ,                  
 n_tup_upd          ,                 
 n_tup_del         -- ,                 
 -- last_vacuum         ,
 -- last_autovacuum     ,
 -- last_analyze        ,
 -- last_autoanalyze   
from pg_stat_all_tables;
insert into edb$stat_all_indexes
select v_seq,v_db,
relid           ,   
 indexrelid       ,   
 schemaname       ,
 relname          ,
 indexrelname     ,
 idx_scan         ,
 idx_tup_read     ,
 idx_tup_fetch   
from pg_stat_all_indexes;
insert into edb$statio_all_tables
select v_seq,v_db,
relid             ,  
 schemaname        ,
 relname            ,
 heap_blks_read     ,
 heap_blks_hit     , 
 idx_blks_read      ,
 idx_blks_hit       ,
 toast_blks_read    ,
 toast_blks_hit     ,
 tidx_blks_read     ,
 tidx_blks_hit
from pg_statio_all_tables;
insert into edb$statio_all_indexes
select v_seq,v_db,
relid           ,  
 indexrelid        ,  
 schemaname      , 
 relname          ,
 indexrelname     ,
 idx_blks_read    ,
 idx_blks_hit
from pg_statio_all_indexes;
insert into edb$stat_database
select v_seq,v_db,
datid           ,    
 datname          ,
 numbackends      ,
 xact_commit      ,
 xact_rollback   , 
 blks_read         ,
 blks_hit
from pg_stat_database;
------------------------------ END OF NEW -----------------------
 
msg := 'Statement processed.';
return msg;
/* 
exception
when others then
msg := 'Unsupported version.';
return msg;
*/ 
end;
  