Come posso sapere che cos'è Postgresql tablespace?
-
12-11-2019 - |
Domanda
Ho creato un nuovo spazio di tabella chiamata indexes
, e sto cercando di rimuovere il vecchio tablespace indexes_old
, che usato per contenere alcune tabelle e indici.Quando provo a goccia tablespace, ottengo:
=> drop tablespace indexes_old;
ERROR: tablespace "indexes_old" is not empty
Ma quando provo a vedere cosa succede, sembra che le tabelle non vivere in quello spazio di tabella:
=> 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)
Così che cosa è che lo spazio tabella che mi impedisce di farlo cadere?
In caso di questioni, ho appena migrato da Pg 8,4 a Pg 9.0 utilizzando il pg_upgrade strumento.
Gli spazi tabella simile a questa:
Name | Owner | Location | Access privileges | Description
-------------+----------+-----------------+-------------------+-------------
indexes | nobody | /data/pgindex90 | |
indexes_old | nobody | /data/pgindex84 | |
e il contenuto di /dati/pgindex84 includere tutti i vecchi 8.4 indici, oltre a questo nuovo 9.0 indice che pg_upgrade creato automaticamente
# 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
Soluzione
Controllare pg_class per vedere cosa si trova dove:
SELECT
c.relname,
t.spcname
FROM
pg_class c
JOIN pg_tablespace t ON c.reltablespace = t.oid
WHERE
t.spcname = 'indexes_old';
Altri suggerimenti
In PostgreSQL, uno spazio di tabella può essere utilizzato da qualsiasi database PostgreSQL.(Fintanto che l'utente richiedente dispone di privilegi sufficienti, che è.) Penso che questa query
SELECT spcname, spclocation FROM pg_tablespace;
mostra la directory che index_old utilizza il file system in versione di PostgreSQL attraverso 9.1.Si aggirano intorno per vedere se qualcosa di reale, è nel vostro senso.Sarei molto cauto nel cercare di eliminare nulla, a parte utilizzando PostgreSQL interfaccia, però.
9.2+, provare
select spcname, pg_tablespace_location(oid) from pg_tablespace;
In PG 10 e forse un po ' prima, questo sembra essere trasformata a:
SELECT tablename from pg_tables WHERE tablespace = 'foo';
Purtroppo non c'è visione "globale" in tutti i database.Tuttavia, questo può essere fatto utilizzando il dblink estensione insieme con la seguente funzione:
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;
La funzione accetta un tablespace nome e un nome utente e una password che è valido per tutti i database del server corrente.
Se il tablespace nome è passato come null
tutti gli oggetti che non sono di default tablespace sono elencati (che sarebbe pg_global
in una installazione di default, senza ulteriori spazi)
Questo può essere usato come questa:
select *
from show_tablespace_objects('indexes_old', 'postgres', 'verysecretpassword');
Stiamo parlando del PgSQL Interfaccia?
Elenco di schemi (spazi) come questo:
\dn
Elenco di tutte le tabelle all'interno di uno schema (tablespace) come questo:
\dn <table_space>.*
Utilizzare
\?
per ulteriori opzioni