鉴于当前 Postgres 9.4 中的此设置(来自此相关问题):
CREATE TABLE foo (ts, foo) AS
VALUES (1, 'A') -- int, text
, (7, 'B');
CREATE TABLE bar (ts, bar) AS
VALUES (3, 'C')
, (5, 'D')
, (9, 'E');
Run Code Online (Sandbox Code Playgroud)
db<> fiddle here(也来自上一个问题)。
我SELECT用 a写了一个FULL JOIN来实现引用问题的目标。简化:
SELECT ts, f.foo, b.bar
FROM foo f
FULL JOIN bar b USING (ts);Run Code Online (Sandbox Code Playgroud)
As per specifications, the correct way to address the column ts is without table qualification. Either of the …
表t有两个索引:
create table t (a int, b int);
create type int_pair as (a int, b int);
create index t_row_idx on t (((a,b)::int_pair));
create index t_a_b_idx on t (a,b);
insert into t (a,b)
select i, i
from generate_series(1, 100000) g(i)
;
Run Code Online (Sandbox Code Playgroud)
ANY运算符不使用索引:
explain analyze
select *
from t
where (a,b) = any(array[(1,1),(1,2)])
;
QUERY PLAN
---------------------------------------------------------------------------------------------------
Seq Scan on t (cost=0.00..1693.00 rows=1000 width=8) (actual time=0.042..126.789 rows=1 loops=1)
Filter: (ROW(a, b) = ANY (ARRAY[ROW(1, 1), ROW(1, 2)]))
Rows Removed …Run Code Online (Sandbox Code Playgroud) 在 PG 14 的文档中,在 参考资料CREATE FUNCTION部分中,手册指出函数的主体LANGUAGE SQL可以是单个语句:
RETURN expression
Run Code Online (Sandbox Code Playgroud)
或一个块:
BEGIN ATOMIC
statement;
statement;
...
statement;
END
Run Code Online (Sandbox Code Playgroud)
没有对该块的语义给出任何解释。这看起来类似于BEGIN ... END;PL/pgSQL 中的块,但似乎有些不同。
在 内 或 外 写一组语句有什么区别BEGIN ATOMIC ... END?什么时候需要使用这样的块?关键字是ATOMIC强制的吗?
我有以下表格和索引定义:
CREATE TABLE munkalap (
munkalap_id serial PRIMARY KEY,
...
);
CREATE TABLE munkalap_lepes (
munkalap_lepes_id serial PRIMARY KEY,
munkalap_id integer REFERENCES munkalap (munkalap_id),
...
);
CREATE INDEX idx_munkalap_lepes_munkalap_id ON munkalap_lepes (munkalap_id);
Run Code Online (Sandbox Code Playgroud)
为什么在以下查询中没有使用 munkalap_id 上的任何索引?
EXPLAIN ANALYZE SELECT ml.* FROM munkalap m JOIN munkalap_lepes ml USING (munkalap_id);
QUERY PLAN
Hash Join (cost=119.17..2050.88 rows=38046 width=214) (actual time=0.824..18.011 rows=38046 loops=1)
Hash Cond: (ml.munkalap_id = m.munkalap_id)
-> Seq Scan on munkalap_lepes ml (cost=0.00..1313.46 rows=38046 width=214) (actual time=0.005..4.574 rows=38046 loops=1)
-> Hash (cost=78.52..78.52 rows=3252 …Run Code Online (Sandbox Code Playgroud) 鉴于表:
Column | Type
id | integer
latitude | numeric(9,6)
longitude | numeric(9,6)
speed | integer
equipment_id | integer
created_at | timestamp without time zone
Indexes:
"geoposition_records_pkey" PRIMARY KEY, btree (id)
Run Code Online (Sandbox Code Playgroud)
该表有 2000 万条记录,相对而言,这不是一个大数目。但它会使顺序扫描变慢。
我怎样才能获得max(created_at)每个的最后一条记录 ( ) equipment_id?
我已经尝试了以下两个查询,其中有几个变体,我已经阅读了本主题的许多答案:
select max(created_at),equipment_id from geoposition_records group by equipment_id;
select distinct on (equipment_id) equipment_id,created_at
from geoposition_records order by equipment_id, created_at desc;
Run Code Online (Sandbox Code Playgroud)
我也尝试过创建 btree 索引,equipment_id,created_at但 Postgres 发现使用 seqscan 更快。强制enable_seqscan = off也没有用,因为读取索引与 seq 扫描一样慢,可能更糟。
查询必须定期运行,始终返回最后一个。
使用 Postgres …
可以在 PostgreSQL 中创建原子事务吗?
考虑我有这些行的表类别:
id|name
--|---------
1 |'tablets'
2 |'phones'
Run Code Online (Sandbox Code Playgroud)
并且列名具有唯一约束。
如果我尝试:
BEGIN;
update "category" set name = 'phones' where id = 1;
update "category" set name = 'tablets' where id = 2;
COMMIT;
Run Code Online (Sandbox Code Playgroud)
我越来越:
ERROR: duplicate key value violates unique constraint "category_name_key"
DETAIL: Key (name)=(tablets) already exists.
Run Code Online (Sandbox Code Playgroud) 在 PostgreSQL 9.2.2(Windows 32 位)下,我有一个pg_restore命令可以系统地导致有关检查点频率的日志警告,例如:
LOG: checkpoints are occurring too frequently (17 seconds apart)
HINT: Consider increasing the configuration parameter "checkpoint_segments".
Run Code Online (Sandbox Code Playgroud)
该数据库大小约为 3.3 Gb,具有 112 个表/160 个视图,并在大约 14 分钟内恢复。
在 a 期间发生这种情况是否正常pg_restore?
在 PostgreSQL 9.5 中,给定一个使用以下方法创建的简单表:
create table tbl (
id serial primary key,
val integer
);
Run Code Online (Sandbox Code Playgroud)
我运行 SQL 来插入一个值,然后在同一个语句中更新它:
WITH newval AS (
INSERT INTO tbl(val) VALUES (1) RETURNING id
) UPDATE tbl SET val=2 FROM newval WHERE tbl.id=newval.id;
Run Code Online (Sandbox Code Playgroud)
结果是 UPDATE 被忽略:
testdb=> select * from tbl;
????????????
? id ? val ?
????????????
? 1 ? 1 ?
????????????
Run Code Online (Sandbox Code Playgroud)
为什么是这样?这个限制是 SQL 标准的一部分(即存在于其他数据库中),还是特定于 PostgreSQL 可能在未来修复的部分?在使用查询文件说,多次更新不支持,但没有提到INSERT和UPDATE。
我想通过查询得到结果:
SELECT * FROM (
SELECT id, subject
FROM mailboxes
WHERE tsv @@ plainto_tsquery('avail')
) AS t1 ORDER by id DESC;
Run Code Online (Sandbox Code Playgroud)
这有效并返回tsv包含包含的行Available。但是如果我使用avai(dropped lable) 它找不到任何东西。
所有查询都必须在字典中吗?我们不能只查询这样的字母吗?我有一个包含电子邮件正文(内容)的数据库,我希望随着它每秒增长而使其快速增长。目前我正在使用
... WHERE content ~* 'letters`
Run Code Online (Sandbox Code Playgroud) 我们在 Postgres 中有一个 try catch 等价物吗?我编写了一些由触发器调用的用户定义函数。我(不想)想忽略错误,以免流程中断。
postgresql ×10
index ×3
join ×2
performance ×2
plpgsql ×2
checkpoint ×1
cte ×1
functions ×1
optimization ×1
parameter ×1
primary-key ×1
restore ×1
transaction ×1