跳转到主要内容

periods

为 PERIODs 和 SYSTEM VERSIONING 提供标准 SQL 功能

periods · 为 PERIODs 和 SYSTEM VERSIONING 提供标准 SQL 功能

概览

ID扩展软件包版本类别许可证编程语言
1030periodsperiods1.2.3TIMEPostgreSQLC
属性可执行文件动态库需要预加载需要建扩展可迁移可信
--s-d--否是否是否否

软件包

类型仓库版本兼容的 PostgreSQL 大版本包名模式依赖
EXTPIGSTY1.2.319 18 17 16 15periodsbtree_gist
RPMPIGSTY1.2.319 18 17 16 15periods_$v-
DEBPGDG1.2.319 18 17 16 15postgresql-$v-periods-
Linux / PGPG19PG18PG17PG16PG15
el8.x86_64N/APIGSTY 1.2.3PIGSTY 1.2.3PIGSTY 1.2.3PIGSTY 1.2.3
el8.aarch64N/APIGSTY 1.2.3PIGSTY 1.2.3PIGSTY 1.2.3PIGSTY 1.2.3
el9.x86_64PIGSTY 1.2.3PIGSTY 1.2.3PIGSTY 1.2.3PIGSTY 1.2.3PIGSTY 1.2.3
el9.aarch64PIGSTY 1.2.3PIGSTY 1.2.3PIGSTY 1.2.3PIGSTY 1.2.3PIGSTY 1.2.3
el10.x86_64PIGSTY 1.2.3PIGSTY 1.2.3PIGSTY 1.2.3PIGSTY 1.2.3PIGSTY 1.2.3
el10.aarch64PIGSTY 1.2.3PIGSTY 1.2.3PIGSTY 1.2.3PIGSTY 1.2.3PIGSTY 1.2.3
d12.x86_64PIGSTY 1.2.3PGDG 1.2.3PGDG 1.2.3PGDG 1.2.3PGDG 1.2.3
d12.aarch64PIGSTY 1.2.3PGDG 1.2.3PGDG 1.2.3PGDG 1.2.3PGDG 1.2.3
d13.x86_64PIGSTY 1.2.3PGDG 1.2.3PGDG 1.2.3PGDG 1.2.3PGDG 1.2.3
d13.aarch64PIGSTY 1.2.3PGDG 1.2.3PGDG 1.2.3PGDG 1.2.3PGDG 1.2.3
u22.x86_64PIGSTY 1.2.3PGDG 1.2.3PGDG 1.2.3PGDG 1.2.3PGDG 1.2.3
u22.aarch64PIGSTY 1.2.3PGDG 1.2.3PGDG 1.2.3PGDG 1.2.3PGDG 1.2.3
u24.x86_64PIGSTY 1.2.3PGDG 1.2.3PGDG 1.2.3PGDG 1.2.3PGDG 1.2.3
u24.aarch64PIGSTY 1.2.3PGDG 1.2.3PGDG 1.2.3PGDG 1.2.3PGDG 1.2.3
u26.x86_64PIGSTY 1.2.3PGDG 1.2.3PGDG 1.2.3PGDG 1.2.3PGDG 1.2.3
u26.aarch64PIGSTY 1.2.3PGDG 1.2.3PGDG 1.2.3PGDG 1.2.3PGDG 1.2.3
软件包版本平台仓库大小下载
periods_191.2.3el9.x86_64pigsty55.5 KiBperiods_19-1.2.3-1PGSTY.el9.x86_64.rpm
periods_191.2.3el9.aarch64pigsty55.1 KiBperiods_19-1.2.3-1PGSTY.el9.aarch64.rpm
periods_191.2.3el10.x86_64pigsty55.7 KiBperiods_19-1.2.3-1PGSTY.el10.x86_64.rpm
periods_191.2.3el10.aarch64pigsty55.3 KiBperiods_19-1.2.3-1PGSTY.el10.aarch64.rpm
postgresql-19-periods1.2.3d12.x86_64pigsty47.1 KiBpostgresql-19-periods_1.2.3-1PGSTY~bookworm_amd64.deb
postgresql-19-periods1.2.3d12.aarch64pigsty46.4 KiBpostgresql-19-periods_1.2.3-1PGSTY~bookworm_arm64.deb
postgresql-19-periods1.2.3d13.x86_64pigsty47.3 KiBpostgresql-19-periods_1.2.3-1PGSTY~trixie_amd64.deb
postgresql-19-periods1.2.3d13.aarch64pigsty46.6 KiBpostgresql-19-periods_1.2.3-1PGSTY~trixie_arm64.deb
postgresql-19-periods1.2.3u22.x86_64pigsty48.1 KiBpostgresql-19-periods_1.2.3-1PGSTY~jammy_amd64.deb
postgresql-19-periods1.2.3u22.aarch64pigsty47.9 KiBpostgresql-19-periods_1.2.3-1PGSTY~jammy_arm64.deb
postgresql-19-periods1.2.3u24.x86_64pigsty47.6 KiBpostgresql-19-periods_1.2.3-1PGSTY~noble_amd64.deb
postgresql-19-periods1.2.3u24.aarch64pigsty47.0 KiBpostgresql-19-periods_1.2.3-1PGSTY~noble_arm64.deb
postgresql-19-periods1.2.3u26.x86_64pigsty47.1 KiBpostgresql-19-periods_1.2.3-1PGSTY~resolute_amd64.deb
postgresql-19-periods1.2.3u26.aarch64pigsty46.7 KiBpostgresql-19-periods_1.2.3-1PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
periods_171.2.3el8.x86_64pigsty56.6 KiBperiods_17-1.2.3-1PGSTY.el8.x86_64.rpm
periods_171.2.2el8.x86_64pgdg45.4 KiBperiods_17-1.2.2-10PGDG.rhel8.10.x86_64.rpm
periods_171.2.2el8.x86_64pgdg44.4 KiBperiods_17-1.2.2-3PGDG.rhel8.x86_64.rpm
periods_171.2.3el8.aarch64pigsty56.2 KiBperiods_17-1.2.3-1PGSTY.el8.aarch64.rpm
periods_171.2.2el8.aarch64pgdg45.1 KiBperiods_17-1.2.2-10PGDG.rhel8.10.aarch64.rpm
periods_171.2.2el8.aarch64pgdg44.1 KiBperiods_17-1.2.2-3PGDG.rhel8.aarch64.rpm
periods_171.2.3el9.x86_64pigsty55.4 KiBperiods_17-1.2.3-1PGSTY.el9.x86_64.rpm
periods_171.2.2el9.x86_64pgdg43.0 KiBperiods_17-1.2.2-10PGDG.rhel9.9.x86_64.rpm
periods_171.2.2el9.x86_64pgdg43.0 KiBperiods_17-1.2.2-10PGDG.rhel9.8.x86_64.rpm
periods_171.2.2el9.x86_64pgdg43.0 KiBperiods_17-1.2.2-10PGDG.rhel9.7.x86_64.rpm
periods_171.2.2el9.x86_64pgdg42.3 KiBperiods_17-1.2.2-6PGDG.rhel9.8.x86_64.rpm
periods_171.2.2el9.x86_64pgdg42.4 KiBperiods_17-1.2.2-3PGDG.rhel9.x86_64.rpm
periods_171.2.3el9.aarch64pigsty55.0 KiBperiods_17-1.2.3-1PGSTY.el9.aarch64.rpm
periods_171.2.2el9.aarch64pgdg42.8 KiBperiods_17-1.2.2-10PGDG.rhel9.9.aarch64.rpm
periods_171.2.2el9.aarch64pgdg42.8 KiBperiods_17-1.2.2-10PGDG.rhel9.8.aarch64.rpm
periods_171.2.2el9.aarch64pgdg42.7 KiBperiods_17-1.2.2-10PGDG.rhel9.7.aarch64.rpm
periods_171.2.2el9.aarch64pgdg42.1 KiBperiods_17-1.2.2-6PGDG.rhel9.8.aarch64.rpm
periods_171.2.2el9.aarch64pgdg42.1 KiBperiods_17-1.2.2-3PGDG.rhel9.aarch64.rpm
periods_171.2.3el10.x86_64pigsty55.8 KiBperiods_17-1.2.3-1PGSTY.el10.x86_64.rpm
periods_171.2.2el10.x86_64pgdg43.2 KiBperiods_17-1.2.2-10PGDG.rhel10.3.x86_64.rpm
periods_171.2.2el10.x86_64pgdg43.2 KiBperiods_17-1.2.2-10PGDG.rhel10.2.x86_64.rpm
periods_171.2.2el10.x86_64pgdg43.2 KiBperiods_17-1.2.2-10PGDG.rhel10.1.x86_64.rpm
periods_171.2.2el10.x86_64pgdg42.5 KiBperiods_17-1.2.2-6PGDG.rhel10.2.x86_64.rpm
periods_171.2.2el10.x86_64pgdg42.7 KiBperiods_17-1.2.2-4PGDG.rhel10.x86_64.rpm
periods_171.2.3el10.aarch64pigsty55.2 KiBperiods_17-1.2.3-1PGSTY.el10.aarch64.rpm
periods_171.2.2el10.aarch64pgdg42.9 KiBperiods_17-1.2.2-10PGDG.rhel10.3.aarch64.rpm
periods_171.2.2el10.aarch64pgdg42.9 KiBperiods_17-1.2.2-10PGDG.rhel10.2.aarch64.rpm
periods_171.2.2el10.aarch64pgdg42.9 KiBperiods_17-1.2.2-10PGDG.rhel10.1.aarch64.rpm
periods_171.2.2el10.aarch64pgdg42.3 KiBperiods_17-1.2.2-6PGDG.rhel10.2.aarch64.rpm
periods_171.2.2el10.aarch64pgdg42.5 KiBperiods_17-1.2.2-4PGDG.rhel10.aarch64.rpm
postgresql-17-periods1.2.3d12.x86_64pgdg47.0 KiBpostgresql-17-periods_1.2.3-2.pgdg12+1_amd64.deb
postgresql-17-periods1.2.3d12.aarch64pgdg46.3 KiBpostgresql-17-periods_1.2.3-2.pgdg12+1_arm64.deb
postgresql-17-periods1.2.3d13.x86_64pgdg47.2 KiBpostgresql-17-periods_1.2.3-2.pgdg13+1_amd64.deb
postgresql-17-periods1.2.3d13.aarch64pgdg46.5 KiBpostgresql-17-periods_1.2.3-2.pgdg13+1_arm64.deb
postgresql-17-periods1.2.3u22.x86_64pgdg50.1 KiBpostgresql-17-periods_1.2.3-2.pgdg22.04+1_amd64.deb
postgresql-17-periods1.2.3u22.aarch64pgdg49.6 KiBpostgresql-17-periods_1.2.3-2.pgdg22.04+1_arm64.deb
postgresql-17-periods1.2.3u24.x86_64pgdg46.1 KiBpostgresql-17-periods_1.2.3-2.pgdg24.04+1_amd64.deb
postgresql-17-periods1.2.3u24.aarch64pgdg45.3 KiBpostgresql-17-periods_1.2.3-2.pgdg24.04+1_arm64.deb
postgresql-17-periods1.2.3u26.x86_64pgdg46.0 KiBpostgresql-17-periods_1.2.3-2.pgdg26.04+1_amd64.deb
postgresql-17-periods1.2.3u26.aarch64pgdg45.4 KiBpostgresql-17-periods_1.2.3-2.pgdg26.04+1_arm64.deb
软件包版本平台仓库大小下载
periods_161.2.3el8.x86_64pigsty56.6 KiBperiods_16-1.2.3-1PGSTY.el8.x86_64.rpm
periods_161.2.2el8.x86_64pgdg45.4 KiBperiods_16-1.2.2-10PGDG.rhel8.10.x86_64.rpm
periods_161.2.2el8.x86_64pgdg44.2 KiBperiods_16-1.2.2-1.rhel8.1.x86_64.rpm
periods_161.2.3el8.aarch64pigsty56.2 KiBperiods_16-1.2.3-1PGSTY.el8.aarch64.rpm
periods_161.2.2el8.aarch64pgdg45.1 KiBperiods_16-1.2.2-10PGDG.rhel8.10.aarch64.rpm
periods_161.2.2el8.aarch64pgdg43.9 KiBperiods_16-1.2.2-1.rhel8.1.aarch64.rpm
periods_161.2.3el9.x86_64pigsty55.4 KiBperiods_16-1.2.3-1PGSTY.el9.x86_64.rpm
periods_161.2.2el9.x86_64pgdg43.0 KiBperiods_16-1.2.2-10PGDG.rhel9.9.x86_64.rpm
periods_161.2.2el9.x86_64pgdg43.0 KiBperiods_16-1.2.2-10PGDG.rhel9.8.x86_64.rpm
periods_161.2.2el9.x86_64pgdg43.0 KiBperiods_16-1.2.2-10PGDG.rhel9.7.x86_64.rpm
periods_161.2.2el9.x86_64pgdg42.3 KiBperiods_16-1.2.2-6PGDG.rhel9.8.x86_64.rpm
periods_161.2.2el9.x86_64pgdg42.1 KiBperiods_16-1.2.2-1.rhel9.1.x86_64.rpm
periods_161.2.3el9.aarch64pigsty55.0 KiBperiods_16-1.2.3-1PGSTY.el9.aarch64.rpm
periods_161.2.2el9.aarch64pgdg42.8 KiBperiods_16-1.2.2-10PGDG.rhel9.9.aarch64.rpm
periods_161.2.2el9.aarch64pgdg42.8 KiBperiods_16-1.2.2-10PGDG.rhel9.8.aarch64.rpm
periods_161.2.2el9.aarch64pgdg42.8 KiBperiods_16-1.2.2-10PGDG.rhel9.7.aarch64.rpm
periods_161.2.2el9.aarch64pgdg42.1 KiBperiods_16-1.2.2-6PGDG.rhel9.8.aarch64.rpm
periods_161.2.2el9.aarch64pgdg41.6 KiBperiods_16-1.2.2-1.rhel9.1.aarch64.rpm
periods_161.2.3el10.x86_64pigsty55.7 KiBperiods_16-1.2.3-1PGSTY.el10.x86_64.rpm
periods_161.2.2el10.x86_64pgdg43.2 KiBperiods_16-1.2.2-10PGDG.rhel10.3.x86_64.rpm
periods_161.2.2el10.x86_64pgdg43.2 KiBperiods_16-1.2.2-10PGDG.rhel10.2.x86_64.rpm
periods_161.2.2el10.x86_64pgdg43.2 KiBperiods_16-1.2.2-10PGDG.rhel10.1.x86_64.rpm
periods_161.2.2el10.x86_64pgdg42.5 KiBperiods_16-1.2.2-6PGDG.rhel10.2.x86_64.rpm
periods_161.2.2el10.x86_64pgdg42.7 KiBperiods_16-1.2.2-4PGDG.rhel10.x86_64.rpm
periods_161.2.3el10.aarch64pigsty55.2 KiBperiods_16-1.2.3-1PGSTY.el10.aarch64.rpm
periods_161.2.2el10.aarch64pgdg42.9 KiBperiods_16-1.2.2-10PGDG.rhel10.3.aarch64.rpm
periods_161.2.2el10.aarch64pgdg42.9 KiBperiods_16-1.2.2-10PGDG.rhel10.2.aarch64.rpm
periods_161.2.2el10.aarch64pgdg43.0 KiBperiods_16-1.2.2-10PGDG.rhel10.1.aarch64.rpm
periods_161.2.2el10.aarch64pgdg42.3 KiBperiods_16-1.2.2-6PGDG.rhel10.2.aarch64.rpm
periods_161.2.2el10.aarch64pgdg42.5 KiBperiods_16-1.2.2-4PGDG.rhel10.aarch64.rpm
postgresql-16-periods1.2.3d12.x86_64pgdg47.0 KiBpostgresql-16-periods_1.2.3-2.pgdg12+1_amd64.deb
postgresql-16-periods1.2.3d12.aarch64pgdg46.3 KiBpostgresql-16-periods_1.2.3-2.pgdg12+1_arm64.deb
postgresql-16-periods1.2.3d13.x86_64pgdg47.2 KiBpostgresql-16-periods_1.2.3-2.pgdg13+1_amd64.deb
postgresql-16-periods1.2.3d13.aarch64pgdg46.5 KiBpostgresql-16-periods_1.2.3-2.pgdg13+1_arm64.deb
postgresql-16-periods1.2.3u22.x86_64pgdg49.7 KiBpostgresql-16-periods_1.2.3-2.pgdg22.04+1_amd64.deb
postgresql-16-periods1.2.3u22.aarch64pgdg49.2 KiBpostgresql-16-periods_1.2.3-2.pgdg22.04+1_arm64.deb
postgresql-16-periods1.2.3u24.x86_64pgdg46.0 KiBpostgresql-16-periods_1.2.3-2.pgdg24.04+1_amd64.deb
postgresql-16-periods1.2.3u24.aarch64pgdg45.4 KiBpostgresql-16-periods_1.2.3-2.pgdg24.04+1_arm64.deb
postgresql-16-periods1.2.3u26.x86_64pgdg46.0 KiBpostgresql-16-periods_1.2.3-2.pgdg26.04+1_amd64.deb
postgresql-16-periods1.2.3u26.aarch64pgdg45.4 KiBpostgresql-16-periods_1.2.3-2.pgdg26.04+1_arm64.deb
软件包版本平台仓库大小下载
periods_151.2.3el8.x86_64pigsty56.6 KiBperiods_15-1.2.3-1PGSTY.el8.x86_64.rpm
periods_151.2.2el8.x86_64pgdg45.5 KiBperiods_15-1.2.2-10PGDG.rhel8.10.x86_64.rpm
periods_151.2.2el8.x86_64pgdg44.2 KiBperiods_15-1.2.2-1.rhel8.x86_64.rpm
periods_151.2el8.x86_64pgdg60.8 KiBperiods_15-1.2-2.rhel8.x86_64.rpm
periods_151.2.3el8.aarch64pigsty56.2 KiBperiods_15-1.2.3-1PGSTY.el8.aarch64.rpm
periods_151.2.2el8.aarch64pgdg45.2 KiBperiods_15-1.2.2-10PGDG.rhel8.10.aarch64.rpm
periods_151.2.2el8.aarch64pgdg43.9 KiBperiods_15-1.2.2-1.rhel8.aarch64.rpm
periods_151.2el8.aarch64pgdg60.4 KiBperiods_15-1.2-2.rhel8.aarch64.rpm
periods_151.2.3el9.x86_64pigsty55.5 KiBperiods_15-1.2.3-1PGSTY.el9.x86_64.rpm
periods_151.2.2el9.x86_64pgdg43.0 KiBperiods_15-1.2.2-10PGDG.rhel9.9.x86_64.rpm
periods_151.2.2el9.x86_64pgdg43.0 KiBperiods_15-1.2.2-10PGDG.rhel9.8.x86_64.rpm
periods_151.2.2el9.x86_64pgdg43.0 KiBperiods_15-1.2.2-10PGDG.rhel9.7.x86_64.rpm
periods_151.2.2el9.x86_64pgdg42.4 KiBperiods_15-1.2.2-6PGDG.rhel9.8.x86_64.rpm
periods_151.2.2el9.x86_64pgdg42.1 KiBperiods_15-1.2.2-1.rhel9.x86_64.rpm
periods_151.2el9.x86_64pgdg59.8 KiBperiods_15-1.2-2.rhel9.x86_64.rpm
periods_151.2.3el9.aarch64pigsty55.0 KiBperiods_15-1.2.3-1PGSTY.el9.aarch64.rpm
periods_151.2.2el9.aarch64pgdg42.8 KiBperiods_15-1.2.2-10PGDG.rhel9.9.aarch64.rpm
periods_151.2.2el9.aarch64pgdg42.8 KiBperiods_15-1.2.2-10PGDG.rhel9.8.aarch64.rpm
periods_151.2.2el9.aarch64pgdg42.8 KiBperiods_15-1.2.2-10PGDG.rhel9.7.aarch64.rpm
periods_151.2.2el9.aarch64pgdg42.1 KiBperiods_15-1.2.2-6PGDG.rhel9.8.aarch64.rpm
periods_151.2.2el9.aarch64pgdg41.6 KiBperiods_15-1.2.2-1.rhel9.aarch64.rpm
periods_151.2el9.aarch64pgdg59.2 KiBperiods_15-1.2-2.rhel9.aarch64.rpm
periods_151.2.3el10.x86_64pigsty55.8 KiBperiods_15-1.2.3-1PGSTY.el10.x86_64.rpm
periods_151.2.2el10.x86_64pgdg43.2 KiBperiods_15-1.2.2-10PGDG.rhel10.3.x86_64.rpm
periods_151.2.2el10.x86_64pgdg43.2 KiBperiods_15-1.2.2-10PGDG.rhel10.2.x86_64.rpm
periods_151.2.2el10.x86_64pgdg43.2 KiBperiods_15-1.2.2-10PGDG.rhel10.1.x86_64.rpm
periods_151.2.2el10.x86_64pgdg42.5 KiBperiods_15-1.2.2-6PGDG.rhel10.2.x86_64.rpm
periods_151.2.2el10.x86_64pgdg42.7 KiBperiods_15-1.2.2-4PGDG.rhel10.x86_64.rpm
periods_151.2.3el10.aarch64pigsty55.2 KiBperiods_15-1.2.3-1PGSTY.el10.aarch64.rpm
periods_151.2.2el10.aarch64pgdg43.0 KiBperiods_15-1.2.2-10PGDG.rhel10.3.aarch64.rpm
periods_151.2.2el10.aarch64pgdg43.0 KiBperiods_15-1.2.2-10PGDG.rhel10.2.aarch64.rpm
periods_151.2.2el10.aarch64pgdg43.0 KiBperiods_15-1.2.2-10PGDG.rhel10.1.aarch64.rpm
periods_151.2.2el10.aarch64pgdg42.3 KiBperiods_15-1.2.2-6PGDG.rhel10.2.aarch64.rpm
periods_151.2.2el10.aarch64pgdg42.5 KiBperiods_15-1.2.2-4PGDG.rhel10.aarch64.rpm
postgresql-15-periods1.2.3d12.x86_64pgdg46.9 KiBpostgresql-15-periods_1.2.3-2.pgdg12+1_amd64.deb
postgresql-15-periods1.2.3d12.aarch64pgdg46.3 KiBpostgresql-15-periods_1.2.3-2.pgdg12+1_arm64.deb
postgresql-15-periods1.2.3d13.x86_64pgdg47.2 KiBpostgresql-15-periods_1.2.3-2.pgdg13+1_amd64.deb
postgresql-15-periods1.2.3d13.aarch64pgdg46.5 KiBpostgresql-15-periods_1.2.3-2.pgdg13+1_arm64.deb
postgresql-15-periods1.2.3u22.x86_64pgdg49.7 KiBpostgresql-15-periods_1.2.3-2.pgdg22.04+1_amd64.deb
postgresql-15-periods1.2.3u22.aarch64pgdg49.3 KiBpostgresql-15-periods_1.2.3-2.pgdg22.04+1_arm64.deb
postgresql-15-periods1.2.3u24.x86_64pgdg46.0 KiBpostgresql-15-periods_1.2.3-2.pgdg24.04+1_amd64.deb
postgresql-15-periods1.2.3u24.aarch64pgdg45.3 KiBpostgresql-15-periods_1.2.3-2.pgdg24.04+1_arm64.deb
postgresql-15-periods1.2.3u26.x86_64pgdg46.0 KiBpostgresql-15-periods_1.2.3-2.pgdg26.04+1_amd64.deb
postgresql-15-periods1.2.3u26.aarch64pgdg45.4 KiBpostgresql-15-periods_1.2.3-2.pgdg26.04+1_arm64.deb

源码

pig build pkg periods;		# build rpm/deb

安装

启用 PGDG 和 PIGSTY 仓库并更新缓存。

pig repo add pgsql -u   # add both repo and update cache

使用 pig 安装扩展。

pig install periods;		# install by package name

pig install periods -v 19;   # install for PG 19
pig install periods -v 18;   # install for PG 18
pig install periods -v 17;   # install for PG 17
pig install periods -v 16;   # install for PG 16
pig install periods -v 15;   # install for PG 15

在数据库中创建扩展。

CREATE EXTENSION periods CASCADE; -- requires btree_gist

用法

periods: PostgreSQL 的时间段与系统版本控制

本扩展实现了 SQL:2016 标准(最初于 SQL:2011 引入)中关于时间段(Period)和 SYSTEM VERSIONING 系统版本控制的行为。

什么是时间段?

时间段是表上的一个定义,指定一个名称和两个列。时间段的值包含起始值,但不包含结束值。

-- 标准 SQL 语法
CREATE TABLE example (
    id bigint,
    start_date date,
    end_date date,
    PERIOD FOR validity (start_date, end_date)
);

由于扩展无法修改 PostgreSQL 的语法,我们通过函数、视图和触发器来尽可能模拟相同的行为:

CREATE TABLE example (
    id bigint,
    start_date date,
    end_date date
);
SELECT periods.add_period('example', 'validity', 'start_date', 'end_date');

定义时间段后,会约束两列:起始值必须严格小于结束值,且两列均不能为空。

唯一约束

时间段可以作为 PRIMARY KEY 和 UNIQUE 约束的一部分:

CREATE TABLE example (
    id bigint,
    start_date date,
    end_date date
);
SELECT periods.add_period('example', 'validity', 'start_date', 'end_date');
SELECT periods.add_unique_key('example', ARRAY['id'], 'validity');

扩展会创建一个覆盖所有指定列和时间段两列的唯一约束,并通过 GiST 排他约束实现 WITHOUT OVERLAPS 语义。

外键

有了带时间段的唯一键,就可以创建指向它们的外键:

SELECT periods.add_foreign_key('example2', 'ARRAY[ex_id]', 'validity', 'example_id_validity');

部分操作

SQL 标准允许对时间段的一部分进行更新或删除,扩展通过 INSTEAD OF 触发器的视图来实现:

UPDATE example__for_portion_of_validity
SET ...,
    start_date = ...,
    end_date = ...
WHERE ...;

使用此功能前,表必须有主键。

谓词函数

SQL 标准定义了多种时间段谓词,以内联函数形式实现:

-- "t" 和 "u" 是具有时间段 "p" 和 "q" 的表
-- 两个时间段的底层列为 "s" 和 "e"

WHERE periods.contains(t.s, t.e, 42)            -- t.p CONTAINS 42
WHERE periods.contains(t.s, t.e, u.s, u.e)      -- t.p CONTAINS u.q
WHERE periods.equals(t.s, t.e, u.s, u.e)        -- t.p EQUALS u.q
WHERE periods.overlaps(t.s, t.e, u.s, u.e)      -- t.p OVERLAPS u.q
WHERE periods.precedes(t.s, t.e, u.s, u.e)      -- t.p PRECEDES u.q
WHERE periods.succeeds(t.s, t.e, u.s, u.e)      -- t.p SUCCEEDS u.q
WHERE periods.immediately_precedes(t.s, t.e, u.s, u.e)  -- t.p IMMEDIATELY PRECEDES u.q
WHERE periods.immediately_succeeds(t.s, t.e, u.s, u.e)  -- t.p IMMEDIATELY SUCCEEDS u.q

系统版本控制表

SYSTEM_TIME

如果时间段名为 SYSTEM_TIME,则适用特殊规则。列类型必须是 date、timestamp without time zone 或 timestamp with time zone,且用户不可修改。扩展使用触发器将起始列设为 transaction_timestamp(),结束列始终为 'infinity'。

注意: 一般建议使用 timestamp with time zone,因为时区配置参数或夏令时变更可能导致历史记录失真。

CREATE TABLE example (
    id bigint PRIMARY KEY,
    value text
);
SELECT periods.add_system_time_period('example', 'row_start', 'row_end');

这些列无需预先存在——扩展会自动创建。

排除列

可以阻止某些列的更新触发 SYSTEM_TIME 值变化:

SELECT periods.add_system_time_period(
            'example',
            excluded_column_names => ARRAY['foo', 'bar']);

启用系统版本控制

SELECT periods.add_system_time_period('example', 'row_start', 'row_end');
SELECT periods.add_system_versioning('example');

系统会将所有变更记录到一张独立的历史表中。你也可以自行创建历史表(例如添加分区)并指定扩展使用它。

时态查询

SQL 标准扩展了 FROM 和 JOIN 子句以支持时间点或时间范围查询,扩展通过内联函数实现:

SELECT * FROM t__as_of('...');                       -- FOR system_time AS OF '...'
SELECT * FROM t__from_to('...', '...');              -- FOR system_time FROM '...' TO '...'
SELECT * FROM t__between('...', '...');              -- FOR system_time BETWEEN '...' AND '...'
SELECT * FROM t__between_symmetric('...', '...');    -- FOR system_time BETWEEN SYMMETRIC '...' AND '...'

访问控制

历史表及辅助函数遵循基表的所有权和访问权限。历史数据为只读。如需清理旧数据,必须先暂停系统版本控制:

BEGIN;
SELECT periods.drop_system_versioning('t');
GRANT DELETE ON TABLE t TO CURRENT_USER;
DELETE FROM t_history WHERE system_time_end < now() - interval '1 year';
SELECT periods.add_system_versioning('t');
COMMIT;

表格阅读器