Postgresql 幻影Postgres表存在,但可以';不能放弃吗?
我似乎在研究生中有某种幻觉表 假设我执行以下操作:Postgresql 幻影Postgres表存在,但可以';不能放弃吗?,postgresql,Postgresql,我似乎在研究生中有某种幻觉表 假设我执行以下操作: select * from information_schema.tables where table_schema = 'public'; 我得到: table_name | table_type | ... phantom_table BASE TABLE ... 因此,我运行: drop table phantom_table cascade; 我得到: ERROR: table "phantom_table" doe
select * from information_schema.tables where table_schema = 'public';
我得到:
table_name | table_type | ...
phantom_table BASE TABLE
...
因此,我运行:
drop table phantom_table cascade;
我得到:
ERROR: table "phantom_table" does not exist
我尝试过的事情:
vacuum
有人对我应该尝试的东西有什么其他想法吗?你可能在名字的末尾有一些空白 最简单的方法是让
format()
函数生成正确的表名和语句:
select format('drop table %I.%I;', table_schema, table_name) as drop_statement
from information_schema.tables
where table_schema = 'public'
and table_name like '%phantom%';
Edit:Windows上的psql
似乎无法在drop
语句中使用新行处理标识符(但是在创建表时会这样做)
要解决此问题,可以使用DO块:
do
$$
declare
l_stmt text;
begin
select format('drop table %I.%I;', table_schema, table_name) as drop_statement
into l_stmt
from information_schema.tables
where table_schema = 'public'
and table_name like '%phantom%';
execute l_stmt;
end;
$$
;
注意:此代码假定只有一个具有该名称的表存在 名称末尾可能有一些空白 最简单的方法是让
format()
函数生成正确的表名和语句:
select format('drop table %I.%I;', table_schema, table_name) as drop_statement
from information_schema.tables
where table_schema = 'public'
and table_name like '%phantom%';
Edit:Windows上的psql
似乎无法在drop
语句中使用新行处理标识符(但是在创建表时会这样做)
要解决此问题,可以使用DO块:
do
$$
declare
l_stmt text;
begin
select format('drop table %I.%I;', table_schema, table_name) as drop_statement
into l_stmt
from information_schema.tables
where table_schema = 'public'
and table_name like '%phantom%';
execute l_stmt;
end;
$$
;
注意:此代码假定只有一个具有该名称的表存在 有趣的。。。我得到的drop语句在表名中确实有一个换行符,但该语句仍然没有运行。@JohnChrysostom:那么名称中确实有一些空白。但是生成的语句应该能够处理这个问题。无法使生成的语句工作-我怀疑我在与Windows/Unix断线差异作斗争。使用此函数的修改版本可以完成此操作,不过。。。谢谢你的帮助。很有趣。。。我得到的drop语句在表名中确实有一个换行符,但该语句仍然没有运行。@JohnChrysostom:那么名称中确实有一些空白。但是生成的语句应该能够处理这个问题。无法使生成的语句工作-我怀疑我在与Windows/Unix断线差异作斗争。使用此函数的修改版本可以完成此操作,不过。。。谢谢你的帮助。