Advertisement
the4thdoctor

View to get dead files

Jul 16th, 2013
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*  At row 71 the directory PG_9.2_201204301 is used to build the tablespace's path.
  2.     This is specific for postgresql 9.2. To build a correct path for previous
  3.      versions the name need to be adapted.
  4.     A case construct over the SELECT substr(version(),12,3); can automate the path's build
  5. */
  6. DROP VIEW IF EXISTS v_dead_files;
  7. CREATE OR REPLACE VIEW v_dead_files AS
  8. SELECT
  9.     t_file,
  10.     t_node,
  11.     t_db_path,
  12.     t_rel_node,
  13.     (pg_stat_file(t_db_path||'/'||t_file)).modification AS ts_modification,
  14.     pg_size_pretty((pg_stat_file(t_db_path||'/'||t_file)).size) as t_size
  15. FROM
  16. (
  17.     SELECT
  18.         t_file,
  19.         CASE
  20.             WHEN
  21.                     split_part(t_file,'.',2)<>''
  22.                 THEN
  23.                     'Extent number '||split_part(t_file,'.',2)||' for file '||split_part(t_file,'.',1)
  24.             WHEN
  25.                     split_part(t_file,'_',2)='fsm'
  26.                 THEN
  27.                     'Free space map  for file '||split_part(t_file,'_',1)
  28.             WHEN
  29.                     split_part(t_file,'_',2)='vm'
  30.                 THEN
  31.                     'Visibility map for file '||split_part(t_file,'_',1)
  32.             WHEN
  33.                     split_part(t_file,'_',2)=''
  34.                 OR  split_part(t_file,'.',2)=''
  35.                 THEN
  36.                     t_file
  37.         END AS t_node,
  38.         t_db_path,
  39.         CASE
  40.             WHEN
  41.                     split_part(t_file,'.',2)<>''
  42.                 THEN
  43.                     split_part(t_file,'.',1)
  44.             WHEN
  45.                     split_part(t_file,'_',2)<>''
  46.                 THEN
  47.                     split_part(t_file,'_',1)
  48.             WHEN
  49.                     split_part(t_file,'_',2)=''
  50.                 OR  split_part(t_file,'.',2)=''
  51.                 THEN
  52.                     t_file
  53.         END AS t_rel_node
  54.     FROM
  55.     (
  56.         SELECT
  57.             pg_ls_dir(t_db_path) AS t_file,
  58.             t_db_path
  59.         FROM
  60.         (
  61.             SELECT
  62.                 CASE
  63.                     WHEN t_dir='base'
  64.                     THEN
  65.                         t_dir||'/'||t_dat_dir
  66.                     WHEN t_dir='global'
  67.                     THEN
  68.                         t_dir
  69.                    
  70.                     ELSE
  71.                         'pg_tblspc/'||t_dir||'/PG_9.2_201204301/'||t_dat_dir
  72.                    
  73.                 END AS t_db_path,
  74.                 t_dat_dir,
  75.                 t_dir
  76.             FROM
  77.             (
  78.                 SELECT
  79.                     oid::text as t_dat_dir
  80.                 FROM
  81.                     pg_database
  82.                 WHERE
  83.                     datname=current_database()
  84.  
  85.             ) t_dat,
  86.             (
  87.  
  88.                 SELECT
  89.                     CASE
  90.                         WHEN
  91.                             spcname='pg_default'
  92.                         THEN
  93.                             'base'::text
  94.                         WHEN
  95.                             spcname='pg_global'
  96.                         THEN
  97.                             'global'::text
  98.                         ELSE
  99.                             oid::text
  100.                     END AS t_dir
  101.                 FROM
  102.                     pg_tablespace
  103.                 WHERE
  104.                     oid IN
  105.                             (
  106.                                 SELECT DISTINCT reltablespace FROM pg_class
  107.                             )
  108.  
  109.                
  110.             ) t_dir
  111.         ) t_lsdir  
  112.     ) t_files
  113. ) t_file_list
  114.  
  115. WHERE      
  116.     t_rel_node NOT IN
  117.             (
  118.                 SELECT pg_relation_filenode(oid)::text FROM pg_class WHERE pg_relation_filenode(oid) IS NOT NULL
  119.               )
  120.     AND t_file NOT IN (
  121.                 'pg_filenode.map',
  122.                 'PG_VERSION',
  123.                 'pg_internal.init',
  124.                 'pg_control'
  125.                 )
  126. ORDER BY
  127.     t_rel_node::integer DESC
  128.    
  129.     ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement