What's a good way to provide versioned rows in PostgreSQL? How to query them?

daj*_*ood 6 sql postgresql ddl

I want to store different versions of different texts and other data in a table. For the texts, my table looks like this:

id BigSerial, PRIMARY KEY  
version Integer  
text Text  
origin BigInt
Run Code Online (Sandbox Code Playgroud)

Now I want to store different versions of texts in this table like this:

1,0,"My Text, first Version",null  
2,1,"My Text, second Version",1  
3,0,"My 2nd Text v1",null  
4,1,"My 2nd Text v2",3
Run Code Online (Sandbox Code Playgroud)

I don't know yet how to query for the row with the highest version number for each set of texts.

Mik*_*ll' 5

Bigserial ID 号没有任何用处。

create temp table my_table (
  id integer not null,
  version integer not null check(version > 0),
  -- Give a lot of thought to whether text should also be unique. *I* think
  -- it probably should, but it's really application-dependent.
  text Text not null unique,
  primary key (id, version)
);

insert into my_table values 
(1, 1, 'My Text, first Version'),
(1, 2, 'My Text, second Version'),
(2, 1, 'My 2nd text v1'),
(2, 2, 'My 2nd text v2')
Run Code Online (Sandbox Code Playgroud)

每个 id 的版本数。

select id, count(*)
from my_table
group by id;
Run Code Online (Sandbox Code Playgroud)

每个 id 的当前版本。

with current_ver as (
  select id, max(version) as version
  from my_table
  group by id
)
select m.* from my_table m
inner join current_ver c on c.id = m.id and c.version = m.version
Run Code Online (Sandbox Code Playgroud)

尽管我使用公用表表达式编写了该内容,但您可能希望创建当前版本的视图。我认为访问此数据的大多数应用程序都需要当前版本。