【问题标题】:Renaming postgres table will drop existing indexes?重命名 postgres 表会删除现有索引吗?
【发布时间】:2017-12-01 09:47:21
【问题描述】:

我正在开发一个 ETL,我们从 hive 获取数据并将其转储到 Postgres。为了确保数据没有损坏,我首先将数据存储在临时表中(创建为主表,包含所有索引和约束),如果数据经过验证,则将其复制到主表中。 但是,只要数据量很大,它就需要很长时间。 验证数据后,我现在正在考虑删除主表,然后将临时表重命名为主表。

在 Postgres 中重命名表会删除其上定义的索引、约束和默认值吗?

【问题讨论】:

  • 您可以采取的另一种方法是保留两个表并使用视图(我认为 PostgreSQL 不支持同义词)作为指向临时数据的正确表和实时数据的正确表的指针。跨度>

标签: postgresql database-design ddl table-rename


【解决方案1】:

一个词 - 不,它不会删除任何索引、约束或默认值。这是一个快速演示:

db=> CREATE TABLE mytab (
id INT PRIMARY KEY,
col_uniq INT UNIQUE,
col_not_null INT NOT NULL DEFAULT 123
);
CREATE TABLE
db=> \d mytab
             Table "public.mytab"
    Column    |  Type   |      Modifiers       
--------------+---------+----------------------
 id           | integer | not null
 col_uniq     | integer | 
 col_not_null | integer | not null default 123
Indexes:
    "mytab_pkey" PRIMARY KEY, btree (id)
    "mytab_col_uniq_key" UNIQUE CONSTRAINT, btree (col_uniq)

db=> ALTER TABLE mytab RENAME TO mytab_renamed;
ALTER TABLE
db=> \d mytab_renamed
         Table "public.mytab_renamed"
    Column    |  Type   |      Modifiers       
--------------+---------+----------------------
 id           | integer | not null
 col_uniq     | integer | 
 col_not_null | integer | not null default 123
Indexes:
    "mytab_pkey" PRIMARY KEY, btree (id)
    "mytab_col_uniq_key" UNIQUE CONSTRAINT, btree (col_uniq)

【讨论】:

    猜你喜欢
    • 2015-11-06
    • 1970-01-01
    • 2013-03-14
    • 1970-01-01
    • 2023-02-16
    • 2016-12-29
    • 2019-07-05
    • 2016-03-04
    • 2022-07-20
    相关资源
    最近更新 更多