periods
periods : Provide Standard SQL functionality for PERIODs and SYSTEM VERSIONING
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 1030 | periods | periods | 1.2.3 |
TIME | PostgreSQL | C |
| Attribute | Has Binary | Has Library | Need Load | Has DDL | Relocatable | Trusted |
|---|---|---|---|---|---|---|
| --s-d-- | No | Yes | No | Yes | no | no |
| Relationships | |
|---|---|
| Requires | btree_gist |
| See Also | temporal_tables table_version data_historization pgmemento table_log emaj ddl_historization |
Packages
| Type | Repo | Version | PG Major Compatibility | Package Pattern | Dependencies |
|---|---|---|---|---|---|
| EXT | PIGSTY | 1.2.3 |
18 17 16 15 14 | periods |
btree_gist |
| RPM | PIGSTY | 1.2.3 |
18 17 16 15 14 | periods_$v |
- |
| DEB | PGDG | 1.2.3 |
18 17 16 15 14 | postgresql-$v-periods |
- |
| Linux / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
| el8.x86_64 | PIGSTY 1.2.3 | PIGSTY 1.2.3 | PIGSTY 1.2.3 | PIGSTY 1.2.3 | PIGSTY 1.2.3 |
| el8.aarch64 | PIGSTY 1.2.3 | PIGSTY 1.2.3 | PIGSTY 1.2.3 | PIGSTY 1.2.3 | PIGSTY 1.2.3 |
| el9.x86_64 | PIGSTY 1.2.3 | PIGSTY 1.2.3 | PIGSTY 1.2.3 | PIGSTY 1.2.3 | PIGSTY 1.2.3 |
| el9.aarch64 | PIGSTY 1.2.3 | PIGSTY 1.2.3 | PIGSTY 1.2.3 | PIGSTY 1.2.3 | PIGSTY 1.2.3 |
| el10.x86_64 | PIGSTY 1.2.3 | PIGSTY 1.2.3 | PIGSTY 1.2.3 | PIGSTY 1.2.3 | PIGSTY 1.2.3 |
| el10.aarch64 | PIGSTY 1.2.3 | PIGSTY 1.2.3 | PIGSTY 1.2.3 | PIGSTY 1.2.3 | PIGSTY 1.2.3 |
| d12.x86_64 | PGDG 1.2.3 | PGDG 1.2.3 | PGDG 1.2.3 | PGDG 1.2.3 | PGDG 1.2.3 |
| d12.aarch64 | PGDG 1.2.3 | PGDG 1.2.3 | PGDG 1.2.3 | PGDG 1.2.3 | PGDG 1.2.3 |
| d13.x86_64 | PGDG 1.2.3 | PGDG 1.2.3 | PGDG 1.2.3 | PGDG 1.2.3 | PGDG 1.2.3 |
| d13.aarch64 | PGDG 1.2.3 | PGDG 1.2.3 | PGDG 1.2.3 | PGDG 1.2.3 | PGDG 1.2.3 |
| u22.x86_64 | PGDG 1.2.3 | PGDG 1.2.3 | PGDG 1.2.3 | PGDG 1.2.3 | PGDG 1.2.3 |
| u22.aarch64 | PGDG 1.2.3 | PGDG 1.2.3 | PGDG 1.2.3 | PGDG 1.2.3 | PGDG 1.2.3 |
| u24.x86_64 | PGDG 1.2.3 | PGDG 1.2.3 | PGDG 1.2.3 | PGDG 1.2.3 | PGDG 1.2.3 |
| u24.aarch64 | PGDG 1.2.3 | PGDG 1.2.3 | PGDG 1.2.3 | PGDG 1.2.3 | PGDG 1.2.3 |
| u26.x86_64 | PGDG 1.2.3 | PGDG 1.2.3 | PGDG 1.2.3 | PGDG 1.2.3 | PGDG 1.2.3 |
| u26.aarch64 | PGDG 1.2.3 | PGDG 1.2.3 | PGDG 1.2.3 | PGDG 1.2.3 | PGDG 1.2.3 |
Source
github.com/xocolatl/periods
periods-1.2.3.tar.gz
Install
Make sure PGDG and PIGSTY repo available:
Install this extension with pig:
Create this extension with:
Usage
This extension recreates the behavior defined in SQL:2016 (originally in SQL:2011) around periods and tables with SYSTEM VERSIONING. The idea is to figure out all the rules that PostgreSQL would like to adopt and to allow earlier versions of PostgreSQL to simulate the behavior once the feature is finally integrated.
What is a period?
A period is a definition on a table which specifies a name and two columns. The period’s name cannot be the same as any column name of the table.
Since extensions cannot modify PostgreSQL’s grammar, we use functions, views, and triggers to get as close to the same thing as possible.
Defining a period constrains the two columns such that the start column’s value must be strictly inferior to the end column’s value, and that both columns be non-null. The period’s value includes the start value but excludes the end value.
Unique Constraints
Periods may be part of PRIMARY KEYs and UNIQUE constraints.
The extension will create a unique constraint over all of the columns specified and the two columns of the period given. It will also create an exclusion constraint using gist to implement the WITHOUT OVERLAPS part of the constraint.
Foreign Keys
If you can have unique keys with periods, you can also have foreign keys pointing at them.
Portions
The SQL standard allows syntax for updating or deleting just a portion of a period. Rows are inserted as needed for the portions not being updated or deleted.
This extension uses a view with an INSTEAD OF trigger to figure out what portion of the period you would like to modify:
In order to use this feature, the table must have a primary key.
Predicates
The SQL standard provides for several predicates on periods, implemented as inlined functions:
System-Versioned Tables
SYSTEM_TIME
If the period is named SYSTEM_TIME, then special rules apply. The type of the columns must be date, timestamp without time zone, or timestamp with time zone; and they are not modifiable by the user. This extension uses triggers to set the start column to transaction_timestamp() and the end column is always 'infinity'.
Note: It is generally unwise to use anything but timestamp with time zone because changes in the TimeZone configuration parameter or Daylight Savings Time changes can distort the history.
The columns need not exist — they will be created by the extension.
Excluding Columns
It might be desirable to prevent some columns from updating the SYSTEM_TIME values:
Excluded columns can be defined after the fact as well:
WITH SYSTEM VERSIONING
This special SYSTEM_TIME period can be used to keep track of changes in the table.
This instructs the system to keep a record of all changes in the table. A separate history table is used. You can create the history table yourself and instruct the extension to use it if you want to do things like add partitioning.
Temporal Querying
The SQL standard extends the FROM and JOIN clauses to allow specifying a point in time, or a range of time. This extension implements them through inlined functions:
Access Control
The history table as well as the helper functions all follow the ownership and access privileges of the base table. The history data is read-only. In order to trim old data, SYSTEM VERSIONING must be suspended:
Altering a Table with System Versioning
This extension prevents you from dropping objects while system versioning is active. The suggested way to make changes is:
It is up to you to make sure you alter the history table in a way that is compatible with the main table. Re-activating system versioning will verify this.