我在列a和b. 我需要这样的东西:
insert into my_table (a, b) values (1, 2), (1, 2)
on conflict (a) do update set c = 'a_violation'
on conflict (b) do update set c = 'b_violation'
Run Code Online (Sandbox Code Playgroud)
所以一般我想根据冲突目标进行不同的更新 - 不支持上面的语法(只支持一个on conflict语句)。有没有其他方法可以做到这一点?
SELECT json_array_elements('["one", "two"]'::json)
Run Code Online (Sandbox Code Playgroud)
给出结果
| json_array_elements | | :------------------ | | “一个” | | “两个” |
我想要相同但没有引号:
one
two
Run Code Online (Sandbox Code Playgroud)
看起来我不能->>在这里使用,因为我在 JSON 中没有字段名称。它只是一个字符串数组。
Postgres 版本:PostgreSQL 10.0 on x86_64-apple-darwin,由 i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (基于 Apple Inc. build 5658) (LLVM build 2336.11.00) 编译,64-少量
我正处于一个十字路口,我需要决定是否要坚持bigserial作为我的主键,或者更改为uuid(非自动生成\xe2\x80\x94)我的 API 服务器将使用 uuid v4 生成 ID并插入)。
我花了几个小时研究bigserial主uuid键,似乎没有人能就其缺点uuid(如果有的话)达成一致。jsonb我的数据库并没有那么复杂:它是一系列具有非常基本关系的表,我通常一次只插入一行,我到处使用一些字段。写入速度/频率只会在一张表上特别高。
我开始研究 UUID 的原因并不是因为我认为我会用完bigint密钥(如果我没记错的话,有 9 千万亿),更多的是从混淆的角度来看。现在我必须在前端对 ID 进行哈希处理,以避免在 URL 中显示用户数据库 ID(例如/things/2732)。使用Hashids,我可以使用类似 的 URL /things/To2jZP13dG。但我认为我可以更进一步,只使用 UUID,它不会提供有关记录数的任何线索。我不喜欢的是,在将 ID 传递到后端并在那里解码之前必须对其进行编码,然后在查询 50-100 个项目的批次以返回给客户端时,必须对所有这些进行批量编码,这会增加额外的工作量在将 ID 返回给客户之前。
支持随机 UUID(uuid v4)的一个论据是:
\n\n\n如果您的主键是递增 ID,则它们在物理上彼此相邻存储。由于许多人正在写入该数据库页面,因此该数据库页面可能会发生争用。随机 ID 通过将写入分散到整个数据库来防止争用
\n
但后来我发现这里有一个矛盾的说法有一个矛盾的说法:
\n\n\n常规随机 UUID 在整个可能值范围内均匀分布。这会导致在将数据插入索引时局部性较差 - 所有索引叶页都同样可能被命中,从而迫使整个索引进入内存。对于小索引来说这没什么问题,但是一旦索引大小超过共享缓冲区(或 RAM),缓存命中率就会迅速下降。
\n
我知道 Heroku 的人们热衷于使用 UUID 作为主键。不管它的价值如何,我根本不打算让 …
描述Postgres 10 新功能的页面提到了“触发器的转换表”。
触发器的转换表
此功能
AFTER STATEMENT通过适当地向查询公开旧行和新行,使触发器既实用又高效。在此功能之前,AFTER STATEMENT触发器无法直接访问这些,并且变通方法是拜占庭式的并且性能很差。现在可以将许多触发器逻辑编写为AFTER STATEMENT,从而避免需要在 FOR EACH ROW 触发器所需的每一行进行昂贵的上下文切换。
什么是过渡表?
Postgres 系统列记录在第 5 章。数据定义 > 5.4。系统列。
该页面提到oid值“是 32 位数量”。该页面对交易标识符也有同样的说法。所以我假设这意味着oid, tableoid, xmin, cmin, xmax, 和cmax都是 32 位整数。
但这离开了ctid系统列。
行版本在其表中的物理位置。请注意,尽管 ctid 可用于非常快速地定位行版本,但如果行的 ctid 被 VACUUM FULL 更新或移动,则该行的 ctid 将更改。因此 ctid 作为长期行标识符是无用的。OID,或者更好的是用户定义的序列号,应该用于标识逻辑行。
? ctid列的数据类型是什么?
具体来说,我对 Postgres 10.3 版本感兴趣,但如果它在过去的版本中发生了变化,那会很高兴知道。
I have the following tables (taken from the Sakila database):
I am selecting a particular film. For this film, I also want all actors participating in that film. I have two queries for this: one with a LEFT JOIN and one with a LEFT JOIN LATERAL.
select film.film_id, film.title, a.actors
from film
left join
(
select film_actor.film_id, array_agg(first_name) as actors
from actor
inner …Run Code Online (Sandbox Code Playgroud) postgresql performance join execution-plan postgresql-10 postgresql-performance
PostgreSQL 使用默认值,加上
default_statistics_target=1000
random_page_cost=1.5
Run Code Online (Sandbox Code Playgroud)
版本
PostgreSQL 10.4 on x86_64-pc-linux-musl, compiled by gcc (Alpine 6.4.0) 6.4.0, 64-bit
Run Code Online (Sandbox Code Playgroud)
我已经抽真空并分析过。查询非常简单:
SELECT r.price
FROM account_payer ap
JOIN account_contract ac ON ap.id = ac.account_payer_id
JOIN account_schedule "as" ON ac.id = "as".account_contract_id
JOIN schedule s ON "as".id = s.account_schedule_id
JOIN rate r ON s.id = r.schedule_id
WHERE ap.account_id = 8
Run Code Online (Sandbox Code Playgroud)
每id列都是主键,所有被连接的都是外键关系,每个外键都有一个索引。加上一个索引account_payer.account_id。
返回 76k 行需要 3.93s。
Merge Join (cost=8.06..83114.08 rows=3458267 width=6) (actual time=0.228..3920.472 rows=75548 loops=1)
Merge Cond: (s.account_schedule_id = "as".id)
-> Nested Loop …Run Code Online (Sandbox Code Playgroud) 我正在查看7/01为 PostgreSQL安排的commit-fest,我看到 Pg 可能很快就会获得“身份列”。
我在information_schema.columns 中发现了一些提及但没什么
is_identity yes_or_no Applies to a feature not available in PostgreSQL
identity_generation character_data Applies to a feature not available in PostgreSQL
identity_start character_data Applies to a feature not available in PostgreSQL
identity_increment character_data Applies to a feature not available in PostgreSQL
identity_maximum character_data Applies to a feature not available in PostgreSQL
identity_minimum character_data Applies to a feature not available in PostgreSQL
identity_cycle yes_or_no Applies to a feature …Run Code Online (Sandbox Code Playgroud) 使用PostgreSQL 10.5。我正在尝试创建一个分页系统,用户可以在其中来回切换各种结果。
为了不使用OFFSET,我id在名为p(prevId)的参数中从上一页的最后一行传递了。然后我选择id高于p参数中传递的数字的前三行。(如本文所述)
例如,如果id上一页的最后一行是 5,我会选择前 3 行的 anid大于 5:
SELECT
id,
firstname,
lastname
FROM
people
WHERE
firstname = 'John'
AND id > 5
ORDER BY
ID ASC
LIMIT
3;
Run Code Online (Sandbox Code Playgroud)
这很好用,而且时机也不错:
Limit (cost=0.00..3.37 rows=3 width=17) (actual time=0.046..0.117 rows=3 loops=1)
-> Seq Scan on people (cost=0.00..4494.15 rows=4000 width=17) (actual time=0.044..0.114 rows=3 loops=1)
Filter: ((id > 5) AND (firstname = 'John'::text))
Rows Removed by Filter: …Run Code Online (Sandbox Code Playgroud) postgresql performance index paging postgresql-10 query-performance
清理 WAL postgres 的正确方法是什么?我有一个超过 100 GB 的数据库,它在 pg_wal 中有大约 600 GB。我还设置了 2 个逻辑复制。
主从 - Postgres 10。
Master 和 Slaves 有评论wal_keep_segments和max_wal_size。
pg_archivecleanup没有使用%r选项,然后我通过在pg_controldata最新检查点的 REDO WAL 文件中搜索并删除了日志来获取存档名称,但随后一个副本因错误而停止
无法从 WAL 流接收数据:致命:请求的 WAL 段 xxx 已被删除
为了解决这个问题,我删除并重新创建了副本。
postgresql ×10
postgresql-10 ×10
index ×2
join ×2
performance ×2
datatypes ×1
duplication ×1
identity ×1
json ×1
paging ×1
primary-key ×1
table ×1
trigger ×1
upsert ×1