我已经获得了我的 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
我正在尝试对电路板上零件的放置进行建模。没有任何有意义的限制,我的基本架构如下所示:
create table part (
part_id bigserial primary key,
name text not null,
width double precision not null,
height double precision not null
);
create table board (
board_id bigserial primary key,
width double precision not null,
height double precision not null
);
create table board_part (
board_id bigint not null references board,
part_id bigint not null references part,
position point not null
);
Run Code Online (Sandbox Code Playgroud)
对于b
和b2
any board_part
s,我想强制执行以下约束:
b
位于黑板上:
box(b.position, …
Run Code Online (Sandbox Code Playgroud)postgresql database-design referential-integrity spatial exclusion-constraint