如何判断Postgresql表空间中的内容?

时间:2011-02-11 15:39:57

标签: postgresql tablespace

我创建了一个名为indexes的新表空间,我正在尝试删除过去包含一些表和索引的旧表空间indexes_old。当我尝试删除表空间时,我得到:

=> drop tablespace indexes_old;
ERROR:  tablespace "indexes_old" is not empty

但是当我试图看到那里有什么内容时,似乎没有表格存在于该表空间中:

=> select * from pg_tables where tablespace = 'indexes_old';
schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers
------------+-----------+------------+------------+------------+----------+-------------
(0 rows)

=> select * from pg_indexes where tablespace = 'indexes_old';
schemaname | tablename | indexname | tablespace | indexdef
------------+-----------+-----------+------------+----------
(0 rows)

那个表空间中阻止我丢弃它的是什么?

如果重要,我只是使用pg_upgrade工具从Pg 8.4迁移到Pg 9.0。

表空间如下所示:

    Name     |  Owner   |    Location     | Access privileges | Description 
-------------+----------+-----------------+-------------------+-------------
 indexes     | nobody   | /data/pgindex90 |                   | 
 indexes_old | nobody   | /data/pgindex84 |                   | 

并且/ data / pgindex84的内容包括所有旧的8.4索引,以及pg_upgrade自动创建的新的9.0索引

# sudo ls -al /data/pgindex84/PG_9.0_201008051/11874
total 8280
drwx------ 2 postgres postgres    4096 Feb  9 14:58 .
drwx------ 3 postgres postgres    4096 Feb 11 09:28 ..
-rw------- 1 postgres postgres   40960 Feb  9 14:58 10462602
-rw------- 1 postgres postgres   40960 Feb  9 14:58 10462604
-rw------- 1 postgres postgres 4644864 Feb  9 14:58 10462614
-rw------- 1 postgres postgres 3727360 Feb  9 14:58 10462616

6 个答案:

答案 0 :(得分:13)

检查pg_class以查看位于:

的位置
SELECT 
  c.relname, 
  t.spcname 
FROM 
  pg_class c 
    JOIN pg_tablespace t ON c.reltablespace = t.oid 
WHERE 
  t.spcname = 'indexes_old';

答案 1 :(得分:9)

在PostgreSQL中,任何PostgreSQL数据库都可以使用表空间。 (只要请求用户具有足够的权限,即。)我认为这个查询

SELECT spcname, spclocation FROM pg_tablespace;

将显示index_old在PostgreSQL版本到9.1中的文件系统中使用的目录。在那里徘徊,看看是否有真实的东西在你的方式。不过,除了使用PostgreSQL的界面之外,我会非常谨慎地尝试删除任何内容。

在9.2+中,尝试

select spcname, pg_tablespace_location(oid) from pg_tablespace;

答案 2 :(得分:3)

在PG 10中,可能还有一点,这似乎已经转变为:

SELECT tablename from pg_tables WHERE tablespace = 'foo';

答案 3 :(得分:1)

不幸的是,有"全球"查看所有数据库。但是,这可以使用dblink扩展名以及以下函数来完成:

create or replace function show_tablespace_objects(p_tablespace text, p_user text, p_password text) 
  returns table (db_name text, schema_name text, object_name text, object_type text, tablespace_name text)
as
$func$
declare
  l_stmt text;
  l_con_name text := 'tbs_check_conn';
  l_con_string text;
  l_rec record;  
begin
  l_stmt := $query$SELECT current_database(), 
           n.nspname as schema_name, 
           c.relname as object_name,
           case c.relkind 
             when 'r' then 'table'
             when 'i' then 'index'
             when 't' then 'TOAST table'
             when 'm' then 'materialized view'
             when 'f' then 'foreign table'
             when 'p' then 'partitioned table'
             else c.relkind::text
           end as object_type,
           t.spcname as tablespace_name
    FROM pg_class c 
      JOIN pg_namespace n on n.oid = c.relnamespace
      JOIN pg_tablespace t ON c.reltablespace = t.oid$query$;

  if p_tablespace is not null then 
    l_stmt := l_stmt || format(' WHERE t.spcname=%L', p_tablespace);
  end if;

  for l_rec in (select * from pg_database where datallowconn) loop

     l_con_string := format('dbname=%L user=%L password=%L',
                             l_rec.datname, p_user, p_password);
     return query 
        select * 
        from dblink(l_con_string, l_stmt) 
             as t(db_name text, schema_name text, object_name text, object_type text, tablespace_name text);
  end loop;
end;
$func$
language plpgsql;

该函数接受表空间名称以及对当前服务器中的所有数据库有效的用户名和密码。

如果表空间名称作为null传递,则列出所有不在默认表空间中的对象(在默认安装中为pg_global而没有任何其他表空间)

可以这样使用:

select *
from show_tablespace_objects('indexes_old', 'postgres', 'verysecretpassword');

答案 4 :(得分:1)

十年后,我遇到了这个问题,上面的一些小评论帮助我找到了解决方法。 select * from pg_tables where tablespace = 'my_tablespace';仅列出使用表空间的当前数据库中的表 。您必须遍历尝试该命令的每个数据库,以找到使用该表空间的数据库。

答案 5 :(得分:-5)

我们是在谈论PgSQL接口吗?

列出这样的模式(表空间):

\dn

列出架构(表空间)中的所有表,如下所示:

\dn <table_space>.*

使用

\?

了解更多选项

相关问题