是否可以根据另一列的值更改 postgres 中列的约束?例如(伪代码):
CREATE TABLE transactions(
id SERIAL PRIMARY KEY,
type TXN_TYPE NOT NULL,
amount BIGINT,
. . . .,
refunded boolean DEFAULT FALSE,
refund_id DEFAULT NULL if (CONSTRAINT link_refund CHECK (refunded=TRUE))=TRUE REFERENCES transactions(id)
);
Run Code Online (Sandbox Code Playgroud) 正如在stackoverflow 上被问到的那样,但是对于 MySQL,我想知道它在 PostgreSQL 中是如何工作的。当我将“FOR UPDATE”与“LIMIT”、“ORDER BY”和一些“WHERE”组合在一起时,锁定了多少行。他们更新返回的行。我希望“FOR UPDATE”查询只会锁定一行,但也许我错过了一些实现问题。(我打算写一些压力测试来更可靠地重现这一点)
这些查询导致我的系统出现死锁:
(session1) select field1, field2, ... from documents where transaction_path='some/path' for update
(session2) select field1, field2, ... from documents where (transaction_path is null) and queue_id=2 and next_pickup_ts<now() order by next_pickup_ts limit 1 for update
(session3) select field1, field2, ... from documents where (transaction_path is null) and queue_id=2 and next_pickup_ts<now() order by next_pickup_ts limit 1 for update
Run Code Online (Sandbox Code Playgroud)
但我不明白为什么。(transaction_path 上有一个唯一索引。)
我有一个从表格中选择一些数据的功能。我想返回所选数据和该表中的总行数。
我怎样才能做到这一点,或者我怎样才能以最有效的方式获得相同的结果?
我尝试了几件事,最后得到了下面的代码,现在这是我想要的格式,但count(*) over () as total_count会一直返回 1,我需要它返回的是该records选择的总行数。
SELECT
row_to_json(selected_records) as data
FROM
(
SELECT
count(*) over () as total_count,
array_to_json(array_agg(row_to_json(records))) as data
FROM (
SELECT
sum(entrances) as entrances
FROM report_la
WHERE profile_id = 3777614
GROUP BY landing_path_id
limit 10 offset 0
) records
) as selected_records
Run Code Online (Sandbox Code Playgroud)
更新,下面的代码产生了我想要的结果,如果我可以total_count从records选择中隐藏该列就好了
SELECT
row_to_json(selected_records) as data
FROM
(
SELECT
min(total_count) as total_count
,array_to_json(array_agg(row_to_json(records))) as data
FROM (
SELECT
sum(entrances) as entrances
,count(*) over () as total_count …Run Code Online (Sandbox Code Playgroud) Postgresdate_trunc对timestampor进行操作interval,并且:
日期和时间类型的值分别自动转换为时间戳或间隔。
换句话说,我们可以使用date_trunc强制转换的date值:
select date_trunc('month',current_date)::date;
??????????????
? date_trunc ?
??????????????
? 2014-12-01 ?
??????????????
Run Code Online (Sandbox Code Playgroud)
但是这个时区是否安全——如果当前日期是夏令时而月份的开始不是,反之亦然会得到正确的日期吗?
我有点不清楚如何使用pg_dump/ pg_restoreduo 移动大型数据库。
这是我用来转储数据库的命令(主机和用户名取自环境变量):
pg_dump --dbname=mydb --format=custom > mydb.dump
Run Code Online (Sandbox Code Playgroud)
我使用这种custom格式是因为我通过网络执行此操作并希望使用压缩。
当我现在尝试恢复它时,我尝试了以下操作:
pg_restore --dbname=mydb --no-owner mydb.dump
Run Code Online (Sandbox Code Playgroud)
但我得到:
pg_restore: [archiver (db)] connection to database "mydb" failed: FATAL: database "mydb" does not exist
Run Code Online (Sandbox Code Playgroud)
我接下来尝试使用该--create选项,但这会产生相同的错误消息。
最后我尝试通过template1作为一个--dbname选项:
pg_restore --dbname=template1 --no-owner --create mydb.dump
Run Code Online (Sandbox Code Playgroud)
但这是我得到的:
pg_restore: [archiver (db)] Error while PROCESSING TOC:
pg_restore: [archiver (db)] Error from TOC entry 2125; 1262 16395 DATABASE mydb mydbuser
pg_restore: [archiver (db)] could not execute query: ERROR: invalid locale name: …Run Code Online (Sandbox Code Playgroud) 我有一个未使用现有索引的查询,我不明白为什么。
桌子:
mustang=# \d+ bss.amplifier_saturation
Table "bss.amplifier_saturation"
Column | Type | Modifiers | Storage | Description
--------+--------------------------+-------------------------------------------------------------------+---------+-------------
value | integer | not null | plain |
target | integer | not null | plain |
start | timestamp with time zone | not null | plain |
end | timestamp with time zone | not null | plain |
id | integer | not null default nextval('amplifier_saturation_id_seq'::regclass) | plain |
lddate | timestamp with time zone | not null default …Run Code Online (Sandbox Code Playgroud) 我有一个表继承设置,可以像这样简化:
CREATE TABLE p (
id BIGSERIAL PRIMARY KEY,
type_id BIGINT,
approved BOOLEAN
);
CREATE INDEX ON p(approved);
CREATE TABLE a (
a_field VARCHAR,
PRIMARY KEY (id),
CHECK(type_id = 1::BIGINT)
) INHERITS (p);
CREATE TABLE b (
b_field INT,
PRIMARY KEY (id),
CHECK(type_id = 2::BIGINT)
) INHERITS (p);
CREATE INDEX ON b(approved);
CREATE TABLE c (
c_field NUMERIC,
PRIMARY KEY (id)
-- this table is missing the check constraint (for no good reason)
) INHERITS (p);
Run Code Online (Sandbox Code Playgroud)
我现在有一个锁定 table 的长期运行事务a …
我们有一个规则的一般概念,其中特定的实现细节存储在他们自己的表中。我们目前有以下架构:
Rule
* id
* name
* ruleAId
* ruleBId
* ....
* ruleNId
* created
* updated
RuleA
* id
* ...
RuleB
* id
* ...
....
RuleC
* id
* ...
Run Code Online (Sandbox Code Playgroud)
我们不喜欢的部分是每个特定规则实现都有一个可为空的列。
我们有两种可能的解决方案,但都有明显的缺点:
将规则 ID 存储在具体的实现表上,所以我们的模式变成:
RuleA
* id
* ruleId
* ...
Rule
* id
* name
* created
* updated
Run Code Online (Sandbox Code Playgroud)
正如标题所暗示的那样,这意味着每次需要查找规则实现时都要进行 N 次查询。估计对性能不利。
如果我们从 Rule 表中删除对参照完整性的需求,我们只需要一个列来存储特定的实现主键和一个枚举来告诉我们 id 用于哪个表,给我们:
Rule
* id
* name
* implementationType
* implementationId
* created
* …Run Code Online (Sandbox Code Playgroud) 我在 Debian x86 上安装了 PG 服务器(9.0.4)。
在我的一张桌子中,我使用numeric(6,1):
CREATE TABLE mytable(name text, numfield numeric(6,1))
Run Code Online (Sandbox Code Playgroud)
还为此字段创建了索引:
CREATE INDEX mytable_numfield_idx ON mytable USING btree (numfield);
Run Code Online (Sandbox Code Playgroud)
如果我将此字段与实数值一起使用,一切都会很好。正如我看到的查询使用索引:
EXPLAIN ANALYZE SELECT * FROM kodiall WHERE kodgo=123.0
Run Code Online (Sandbox Code Playgroud)
在 mytable 上使用 mytable_numfield_idx 进行索引扫描(cost=0.00..8.27 rows=1 width=193)(实际时间=0.085..0.087 rows=1 loops=1)
索引条件:(numfield = 123.0)
总运行时间:0.131 毫秒
但是如果我使用 0.0 或 NULL 作为条件值,由于某种原因索引被忽略:
EXPLAIN ANALYZE SELECT * FROM kodiall WHERE numfield=0.0
--EXPLAIN ANALYZE SELECT * FROM kodiall WHERE numfield=NULL
Run Code Online (Sandbox Code Playgroud)
mytable 上的 Seq Scan (cost=0.00..57.80 rows=1080 width=193) (实际时间=0.033..1.853 rows=1088 loops=1)
过滤器:(numfield = …
我在 PostgreSQL 9.4 中有一个表,我对其进行了分区,以便对父分区的所有插入实际上都进入其中一个子分区。问题是在创建分区之前,我的父表中已经有数亿行数据。
父级和子级存在于不同的模式中:
家长:public.history_uint
孩子:partitions.history_uint_p2015_mm_dd
TRUNCATE父母如何而不是孩子?
postgresql ×10
index ×2
locking ×2
partitioning ×2
aggregate ×1
condition ×1
constraint ×1
deadlock ×1
migration ×1
order-by ×1
performance ×1
timezone ×1
truncate ×1