标签: postgresql-10

多个冲突目标

我在列ab. 我需要这样的东西:

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语句)。有没有其他方法可以做到这一点?

postgresql duplication upsert postgresql-10

16
推荐指数
1
解决办法
1万
查看次数

取消引用 JSON 字符串;打印不带引号的 JSON 字符串

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-少量

postgresql json string-representation postgresql-10

16
推荐指数
3
解决办法
2万
查看次数

在 Postgres 10.12 中使用 uuid 作为主键对性能有何影响?(需要规范的答案)

我正处于一个十字路口,我需要决定是否要坚持bigserial作为我的主键,或者更改为uuid(非自动生成\xe2\x80\x94)我的 API 服务器将使用 uuid v4 生成 ID并插入)。

\n

我花了几个小时研究bigserialuuid键,似乎没有人能就其缺点uuid(如果有的话)达成一致。jsonb我的数据库并没有那么复杂:它是一系列具有非常基本关系的表,我通常一次只插入一行,我到处使用一些字段。写入速度/频率只会在一张表上特别高。

\n

我开始研究 UUID 的原因并不是因为我认为我会用完bigint密钥(如果我没记错的话,有 9 千万亿),更多的是从混淆的角度来看。现在我必须在前端对 ID 进行哈希处理,以避免在 URL 中显示用户数据库 ID(例如/things/2732)。使用Hashids,我可以使用类似 的 URL /things/To2jZP13dG。但我认为我可以更进一步,只使用 UUID,它不会提供有关记录数的任何线索。我不喜欢的是,在将 ID 传递到后端并在那里解码之前必须对其进行编码,然后在查询 50-100 个项目的批次以返回给客户端时,必须对所有这些进行批量编码,这会增加额外的工作量在将 ID 返回给客户之前。

\n

支持随机 UUID(uuid v4)的一个论据是:

\n
\n

如果您的主键是递增 ID,则它们在物理上彼此相邻存储。由于许多人正在写入该数据库页面,因此该数据库页面可能会发生争用。随机 ID 通过将写入分散到整个数据库来防止争用

\n
\n

但后来我发现这里有一个矛盾的说法有一个矛盾的说法:

\n
\n

常规随机 UUID 在整个可能值范围内均匀分布。这会导致在将数据插入索引时局部性较差 - 所有索引叶页都同样可能被命中,从而迫使整个索引进入内存。对于小索引来说这没什么问题,但是一旦索引大小超过共享缓冲区(或 RAM),缓存命中率就会迅速下降。

\n
\n

我知道 Heroku 的人们热衷于使用 UUID 作为主键。不管它的价值如何,我根本不打算让 …

postgresql index primary-key postgresql-10

16
推荐指数
2
解决办法
2万
查看次数

Postgres 中的“转换表”是什么?

描述Postgres 10 新功能的页面提到了“触发器的转换表”。

触发器的转换表

此功能AFTER STATEMENT通过适当地向查询公开旧行和新行,使触发器既实用又高效。在此功能之前,AFTER STATEMENT触发器无法直接访问这些,并且变通方法是拜占庭式的并且性能很差。现在可以将许多触发器逻辑编写为AFTER STATEMENT,从而避免需要在 FOR EACH ROW 触发器所需的每一行进行昂贵的上下文切换。

什么是过渡表?

postgresql trigger table postgresql-10

15
推荐指数
2
解决办法
3774
查看次数

Postgres 中“ctid”系统列的数据类型是什么?

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 版本感兴趣,但如果它在过去的版本中发生了变化,那会很高兴知道。

postgresql uniqueidentifier datatypes postgresql-10

14
推荐指数
1
解决办法
1万
查看次数

Why does this LEFT JOIN perform so much worse than LEFT JOIN LATERAL?

I have the following tables (taken from the Sakila database):

  • film: film_id is pkey
  • actor: actor_id is pkey
  • film_actor: film_id and actor_id are fkeys to film/actor

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

14
推荐指数
1
解决办法
4050
查看次数

为什么 PostgreSQL 选择更昂贵的连接顺序?

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)

postgresql join execution-plan postgresql-10

13
推荐指数
1
解决办法
1417
查看次数

什么是身份列?

我正在查看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 identity postgresql-10

12
推荐指数
2
解决办法
8548
查看次数

大表的高效分页

使用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

10
推荐指数
1
解决办法
1万
查看次数

如何清理 Postgres WAL?

清理 WAL postgres 的正确方法是什么?我有一个超过 100 GB 的数据库,它在 pg_wal 中有大约 600 GB。我还设置了 2 个逻辑复制。

主从 - Postgres 10。

Master 和 Slaves 有评论wal_keep_segmentsmax_wal_size

pg_archivecleanup没有使用%r选项,然后我通过在pg_controldata最新检查点的 REDO WAL 文件中搜索并删除了日志来获取存档名称,但随后一个副本因错误而停止

无法从 WAL 流接收数据:致命:请求的 WAL 段 xxx 已被删除

为了解决这个问题,我删除并重新创建了副本。

postgresql postgresql-10

9
推荐指数
1
解决办法
2万
查看次数