由于命名冲突,无法创建表。

3

我尝试在数据库中创建一张表格,但出现了以下错误。

ERROR:  type "tbl_last_alert" already exists
HINT:  A relation has an associated type of the same name, so you must use a name that doesn't conflict with any existing type.

然后我认为表格一定存在,所以我运行了以下查询:

select * from pg_tables;

但是没有找到任何东西。然后我尝试了:

select * from tbl_last_alert;
ERROR:  relation "tbl_last_alert" does not exist

你有什么想法可以解决这个问题吗?

我正在尝试通过

重命名类型。

ALTER TYPE v4report.tbl_last_alert RENAME TO tbl_last_alert_old;
ERROR:  v4report.tbl_last_alert is a table's row type
HINT:  Use ALTER TABLE instead.

并且遇到了错误。

4个回答

7
Postgres为每个表创建了同名的复合(行)类型。这就是为什么错误消息提到“类型”而不是“表”的原因。实际上,表名不能与来自pg_class手册中的此列表发生冲突:

r = 普通表,i = 索引,S = 序列,t = TOAST表, v = 视图,m = 材料化视图,c = 复合类型,f = 外部表,p = 分区表,I = 分区索引

我加粗了强调。因此,您可以使用以下查询找到任何冲突的条目:
SELECT n.nspname AS schemaname, c.relname, c.relkind
FROM   pg_class c
JOIN   pg_namespace n ON n.oid = c.relnamespace
WHERE  relname = 'tbl_last_alert';

这涵盖了所有可能的竞争对手,不仅仅是类型。请注意,相同的名称可以在多个模式中多次存在 - 但不能在同一模式中存在。

解决方法

如果您发现有冲突的组合类型,则可以将其重命名或删除以腾出空间 - 如果您不需要它!

DROP TYPE tbl_last_alert;

确保类型的架构是搜索路径中的第一个匹配项,或将名称架构化。我已将架构添加到上面的查询中。如下:

DROP TYPE public.tbl_last_alert;

删除类型 v4report.tbl_last_alert; 错误:缓存查找关系19108失败。有任何想法吗? - smn_onrocks
schemaname | relname | relkind ------------+---------+--------- (0 行) - smn_onrocks
1
@smn_onrocks:我担心你的数据库出现了严重故障。通过重新索引系统表,您可能能够修复它。有关详细信息,请参见此相关答案。 这可能是根源于硬件问题。 - Erwin Brandstetter
2
@smn_onrocks 在你做任何其他事情之前,请遵循以下建议:http://wiki.postgresql.org/wiki/Corruption - Craig Ringer
@Erwin Brandstetter,我已尝试了您给的所有建议,但仍然出现错误。 - smn_onrocks

3

如果你无法删除类型,请从pg_type中删除它:

DELETE FROM pg_type where typname~'tbl_last_alert';

0
错误信息称其为“类型”; 你可能在某个地方有一个同名的类型。
请在psql 中使用此命令来查找任何内容:
\d tbl_last_alert

0
你可以在 pg_type 表中进行检查:
select * from pg_type where typname = 'tbl_last_alert'

选择 * 从 pg_type 中,其中 typname = 'tbl_last_alert'; tbl_last_alert | 16716 | 10 | -1 | f | c | C | f | t | , | 19108 | 0 | 19109 | recor d_in | record_out | record_recv | record_send | - | - | - | d | x | f | \x1A | 0 | -1 | 0 | 0 | | 运行查询后,现在我想删除表,怎么做? - smn_onrocks
@smn_onrocks drop type tbl_last_alert - xdazz
删除类型 v4report.tbl_last_alert; 错误:缓存查找关系19108失败。有任何想法吗? - smn_onrocks

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接