May 17, 2026, 11:40 PM
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
-- Analyze stats for a table testanalyze(schema is public)
|
||||
|
||||
dbaclass=# analyze testanalyze;
|
||||
ANALYZE
|
||||
|
||||
-- For analyzing selected columns for emptab table ( schema is dbatest)
|
||||
|
||||
dbaclass=# analyze dbatest.emptab (datname,datdba);
|
||||
ANALYZE
|
||||
|
||||
dbaclass=# select relname,reltuples from pg_class where relname in ('testanalyze','emptab');
|
||||
relname | reltuples
|
||||
-------------+-----------
|
||||
testanalyze | 4
|
||||
emptab | 4
|
||||
(2 rows)
|
||||
|
||||
dbaclass=# select schemaname,relname,analyze_count,last_analyze,last_autoanalyze from pg_stat_user_tables where relname in ('testanalyze','emptab');
|
||||
schemaname | relname | analyze_count | last_analyze | last_autoanalyze
|
||||
------------+-------------+---------------+----------------------------------+------------------
|
||||
public | testanalyze | 1 | 2020-07-21 17:00:49.687053+05:30 |
|
||||
dbatest | emptab | 1 | 2020-07-21 17:10:01.111517+05:30 |
|
||||
(2 rows)
|
||||
|
||||
---Analyze command with verbose command
|
||||
|
||||
dbaclass=# analyze verbose dbatest.emptab (datname,datdba);
|
||||
|
||||
INFO: analyzing "dbatest.emptab"
|
||||
INFO: "emptab": scanned 1 of 1 pages, containing 4 live rows and 0 dead rows; 4 rows in sample, 4 estimated total rows
|
||||
ANALYZE
|
||||
|
||||
---Analyze tables in the current schema that the user has access to.
|
||||
|
||||
|
||||
|
||||
dbaclass=# analyze ;
|
||||
@@ -0,0 +1,13 @@
|
||||
-- Finding statistics level of a column ( orders.orderdate)
|
||||
-- statistics level range is 1-10000 ( where 100 means 1 percent,10000 means 100 percent)
|
||||
|
||||
edbstore=> SELECT attname as column_name , attstattarget as stats_level FROM pg_attribute WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = 'orders') and attname='orderdate';
|
||||
|
||||
column_name | stats_level
|
||||
-------------+-------------
|
||||
orderdate | 1000
|
||||
(1 row)
|
||||
|
||||
-- To change statistics level of a column:
|
||||
|
||||
edbstore=> alter table orders alter column orderdate set statistics 1000;
|
||||
@@ -0,0 +1,15 @@
|
||||
dbaclass=# SELECT a.query,p.phase, p.blocks_total,p.blocks_done,p.tuples_total, p.tuples_done FROM pg_stat_progress_create_index p JOIN pg_stat_activity a ON p.pid = a.pid;
|
||||
|
||||
-[ RECORD 1 ]+-------------------------------
|
||||
query | reindex index test_idx;
|
||||
phase | building index: scanning table
|
||||
blocks_total | 61281
|
||||
blocks_done | 15331
|
||||
tuples_total | 0
|
||||
tuples_done | 0
|
||||
|
||||
(or)
|
||||
|
||||
dbaclass=# select pid,datname,command,phase,tuples_total,tuples_done,partitions_total,partitions_done from pg_stat_progress_create_index;
|
||||
|
||||
-[ RECORD 1 ]-
|
||||
@@ -0,0 +1 @@
|
||||
select * from pg_stat_progress_vacuum;
|
||||
@@ -0,0 +1,41 @@
|
||||
REINDEX rebuilds an index using the data stored in the index's table, replacing the old copy of the index. There are several scenarios in which to use REINDEX:
|
||||
|
||||
- Rebuild particular index:
|
||||
|
||||
postgres=# REINDEX INDEX TEST_IDX2;
|
||||
REINDEX
|
||||
|
||||
-- Rebuild all indexes on a table:
|
||||
|
||||
postgres=# REINDEX TABLE TEST;
|
||||
REINDEX
|
||||
|
||||
-- Rebuild all indexes of tables in a schema:
|
||||
postgres=# reindex schema public;
|
||||
REINDEX
|
||||
|
||||
-- Rebuild all indexes in a database :
|
||||
|
||||
postgres=# reindex database dbaclass;
|
||||
REINDEX
|
||||
|
||||
-- Reindex with verbose option:
|
||||
|
||||
postgres=# reindex (verbose) table test;
|
||||
INFO: index "test_idx" was reindexed
|
||||
DETAIL: CPU: user: 5.44 s, system: 2.72 s, elapsed: 11.96 s
|
||||
INFO: index "test_idx2" was reindexed
|
||||
DETAIL: CPU: user: 3.34 s, system: 1.01 s, elapsed: 5.49 s
|
||||
INFO: index "pg_toast_17395_index" was reindexed
|
||||
DETAIL: CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s
|
||||
REINDEX
|
||||
|
||||
Rebuild index without causing lock on the table:( using concurrently option)
|
||||
|
||||
postgres=# REINDEX ( verbose) table concurrently test;
|
||||
INFO: index "public.test_idx" was reindexed
|
||||
INFO: index "public.test_idx2" was reindexed
|
||||
INFO: index "pg_toast.pg_toast_17395_index" was reindexed
|
||||
INFO: table "public.test" was reindexed
|
||||
DETAIL: CPU: user: 11.09 s, system: 6.23 s, elapsed: 24.63 s.
|
||||
REINDEX
|
||||
@@ -0,0 +1,25 @@
|
||||
VACUUM - > REMOVES DEAD ROWS, AND MARK THEM FOR REUSE, BUT IT DOESN’T RETURN THE SPACE TO ORACLE,. IT DOESN'T NEED EXCLUSIVE LOCK ON THE TABLE.
|
||||
|
||||
-------------------------------------------------------------------------
|
||||
|
||||
vacuum a table:
|
||||
|
||||
dbaclass=# vacuum dbatest.emptab;
|
||||
VACUUM
|
||||
|
||||
both vacuum and analyze:
|
||||
|
||||
dbaclass=# vacuum analyze dbatest.emptab;
|
||||
VACUUM
|
||||
|
||||
with verbose:
|
||||
|
||||
dbaclass# vacuum verbose analyze dbatest.emptab;
|
||||
|
||||
Monitor vacuum process( if vacuum process runs for a long time)
|
||||
|
||||
dbaclass#select * from pg_stat_progress_vacuum;
|
||||
|
||||
Check vacuum related information for the table
|
||||
|
||||
dbaclass=# select schemaname,relname,last_vacuum,vacuum_count from pg_stat_user_tables where relname='emptab';
|
||||
@@ -0,0 +1,35 @@
|
||||
VACUUM FULL - > JUST LIKE MOVE COMMAND IN ORACLE . IT TAKES MORE TIME, BUT IT RETURNS THE SPACE TO OS BECAUSE OF ITS COMPLEX ALGORITHM. IT also requires additional disk space , which can store the new copy of the table., until the activity is completed. Also it locks the table exclusively, which block all operations on the table .
|
||||
|
||||
-- Command to run vacuum full command for table:
|
||||
|
||||
dbaclass=# VACUUM FULL dbatest.emptab;
|
||||
VACUUM
|
||||
|
||||
|
||||
|
||||
DEMO TO CHECK HOW IT RECLAIMS SPACE:
|
||||
|
||||
-- Check existing space and delete some data:
|
||||
dbaclass=# select pg_size_pretty(pg_relation_size('dbatest.emptab'));
|
||||
pg_size_pretty
|
||||
----------------
|
||||
114 MB
|
||||
(1 row)
|
||||
|
||||
dbaclass=# delete from dbatest.emptab where oid=13634;
|
||||
DELETE 131072
|
||||
|
||||
-- We can observe size is still same:
|
||||
|
||||
dbaclass=# select pg_size_pretty(pg_relation_size('dbatest.emptab'));
|
||||
pg_size_pretty
|
||||
----------------
|
||||
114 MB
|
||||
(1 row)
|
||||
|
||||
-- Run vacuum full and observe the space usage:
|
||||
|
||||
dbaclass=# VACUUM FULL dbatest.emptab;
|
||||
VACUUM
|
||||
|
||||
dbaclass=# select pg_size_pretty(pg_relation_size('dbatest.emptab'));
|
||||
@@ -0,0 +1,24 @@
|
||||
Autovacuum methods automates the executions vacuum,freeze and analyze commands.
|
||||
|
||||
-- Find whether autovacuum is enabled or not:
|
||||
|
||||
dbaclass=# select name,setting,short_desc,boot_val,pending_restart from pg_settings where name in ('autovacuum','track_counts');
|
||||
name. | setting | short_desc | boot_val | pending_restart
|
||||
--------------+---------+-------------------------------------------+----------+-----------------
|
||||
autovacuum | on | Starts the autovacuum subprocess. | on | f
|
||||
track_counts | on | Collects statistics on database activity. | on | f
|
||||
(2 rows)
|
||||
|
||||
-- Find other autovacuum related parameter settings
|
||||
dbaclass=# select name,setting,short_desc,min_val,max_val,enumvals,boot_val,pending_restart from pg_settings where category like 'Autovacuum';
|
||||
|
||||
|
||||
- Change autovacuum settings:( they need restart)
|
||||
|
||||
dbaclass=# alter system set autovacuum_max_workers=10 ;
|
||||
ALTER SYSTEM
|
||||
|
||||
Now restart :
|
||||
|
||||
pg_ctl stop
|
||||
pg_ctl start
|
||||
Reference in New Issue
Block a user