我有下表:
CREATE TABLE post (
id bigint primary key,
thread_id bigint,
is_notice boolean,
title text,
content text
)
Run Code Online (Sandbox Code Playgroud)
我使用以下查询显示列表:
SELECT * FROM post ORDER BY is_notice desc, thread_id desc, id
Run Code Online (Sandbox Code Playgroud)
然后,给定由 id(ie SELECT * FROM post where id=3
)选择的帖子,我如何检索下一个和上一个帖子?
http://www.postgresql.org/docs/9.2/static/transaction-iso.html
可重复读取模式提供了严格的保证,每个事务都可以看到完全稳定的数据库视图。但是,此视图不一定总是与同一级别的并发事务的某些串行(一次一个)执行一致。例如,即使是此级别的只读事务也可能会看到更新的控制记录以显示批次已完成,但看不到逻辑上属于批次的详细记录之一,因为它读取了控制记录的较早版本. 如果不小心使用显式锁来阻止冲突的事务,那么尝试通过在此隔离级别运行的事务来强制执行业务规则是不可能正常工作的。
这不是幻读,在可重复读取模式下是不可能的吗?
文档说可重复读取事务中的查询看到事务开始时的快照,那么查询怎么可能读取不一致的数据?
我创建了一个表:
CREATE TABLE foo (
id integer primary key,
bar integer NOT NULL
);
Run Code Online (Sandbox Code Playgroud)
并插入一行:
INSERT INTO foo (id, bar) VALUES (1, 1);
Run Code Online (Sandbox Code Playgroud)
并运行以下简单更新语句的 pgbench。
脚本.txt:
update foo set bar=bar+1 where id=1
Run Code Online (Sandbox Code Playgroud)
pgbench 结果:
C:\my-temp>"c:\Program Files\PostgreSQL\9.3\bin\pgbench.exe" -U postgres -c 1 -t 10000 -n -f script.txt testdb
Password:
transaction type: Custom query
scaling factor: 1
query mode: simple
number of clients: 1
number of threads: 1
number of transactions per client: 10000
number of transactions actually processed: 10000/10000
tps = 2052.384567 …
Run Code Online (Sandbox Code Playgroud)