我们在 Postgres 中有一个 2.2 GB 的表,其中有 7,801,611 行。我们正在向它添加一个 uuid/guid 列,我想知道填充该列的最佳方法是什么(因为我们想NOT NULL
向它添加约束)。
如果我正确理解 Postgres,更新在技术上是删除和插入,所以这基本上是重建整个 2.2 gb 表。我们还有一个奴隶在运行,所以我们不希望它落后。
有没有比编写一个随着时间慢慢填充它的脚本更好的方法?
我已经获得了我的 PostgreSQL 数据库的转储:
pg_dump -U user-name -d db-name -f dumpfile
Run Code Online (Sandbox Code Playgroud)
然后我继续在另一个数据库中恢复:
psql X -U postgres -d db-name-b -f dumpfile
Run Code Online (Sandbox Code Playgroud)
我的问题是数据库包含引用约束、检查和触发器,其中一些(特别是检查)在恢复过程中失败,因为信息没有按照会导致这些检查得到遵守的顺序加载。例如,在表中插入一行可能与CHECK
调用一个plpgsql
函数相关联,该函数检查某个条件是否在某个其他不相关的表中成立。如果后者没有psql
在前者之前加载,则会发生错误。
下面是一个 SSCCE,它产生了这样一个一旦转储pg_dump
就无法恢复的数据库:
CREATE OR REPLACE FUNCTION fail_if_b_empty () RETURNS BOOLEAN AS $$
SELECT EXISTS (SELECT 1 FROM b)
$$ LANGUAGE SQL;
CREATE TABLE IF NOT EXISTS a (
i INTEGER NOT NULL
);
INSERT INTO a(i) VALUES (0),(1);
CREATE TABLE IF NOT EXISTS b (
i INTEGER NOT NULL
);
INSERT INTO …
Run Code Online (Sandbox Code Playgroud) postgresql database-design postgresql-9.1 pg-dump check-constraints
我有一张桌子articles
:
Table "articles"
Column | Type | Modifiers | Storage | Stats target | Description
----------------+-----------------------------+----------------------------------------------------+----------+--------------+-------------
id | integer | not null default nextval('articles_id_seq'::regclass) | plain | |
user_id | integer | | plain | |
title | character varying(255) | | extended | |
author | character varying(255) | | extended | |
body | text | default '--- [] +| extended | |
| | '::text | | |
created_at | timestamp without time zone | …
Run Code Online (Sandbox Code Playgroud) postgresql performance datatypes postgresql-9.4 query-performance
我有以下错误:
ERROR: check constraint "cc_at_least_one_mapping_needed" is violated by some row
Run Code Online (Sandbox Code Playgroud)
询问:
ALTER TABLE integrations.tax_aggregates
DROP COLUMN IF EXISTS myob_id,
ADD COLUMN myob_id integrations.FOREIGN_IDENTIFIER;
COMMENT ON COLUMN integrations.tax_aggregates.myob_id IS 'Foreign key for MYOB';
ALTER TABLE integrations.tax_aggregates DROP CONSTRAINT IF EXISTS cc_at_least_one_mapping_needed,
ADD CONSTRAINT cc_at_least_one_mapping_needed CHECK ((("qb_id" IS NOT NULL) :: INTEGER +
("xero_id" IS NOT NULL) :: INTEGER +
("freshbooks_id" IS NOT NULL) :: INTEGER +
("myob_id" IS NOT NULL) :: INTEGER +
("ppy_id" IS NOT NULL) :: INTEGER) > 0); …
Run Code Online (Sandbox Code Playgroud)