Skip to content

Commit

Permalink
Make compatible for all versions 9.5 to 11.
Browse files Browse the repository at this point in the history
Supporting 9.4 would add a lot of mess to the code and it's about to be
EOL anyway so don't bother.  A lot has changed for v12 and it's not out
yet, so do that in a separate update.
  • Loading branch information
Vik Fearing committed Jul 3, 2019
1 parent 1b0c1a5 commit 577807e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 26 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# A `periods` Extension

*compatible 9.5--11*

This extension attemps to recreate the behavior defined in SQL:2016
(originally SQL:2011) around periods and tables with `SYSTEM
VERSIONING`. The idea is to figure out all the rules that PostgreSQL
Expand Down
33 changes: 10 additions & 23 deletions expected/periods.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* Install the extension */
CREATE EXTENSION periods CASCADE;
NOTICE: installing required extension "btree_gist"
CREATE EXTENSION IF NOT EXISTS btree_gist;
CREATE EXTENSION periods;
/*
* Test creating a table, dropping a column, and then dropping the whole thing;
* without any periods.
Expand Down Expand Up @@ -133,29 +133,17 @@ SELECT periods.add_system_time_period('sysver');
t
(1 row)

\d+ sysver
Table "public.sysver"
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
-------------------+--------------------------+-----------+----------+--------------------------------------+----------+--------------+-------------
val | text | | | | extended | |
startname | timestamp with time zone | | not null | 'infinity'::timestamp with time zone | plain | |
system_time_end | timestamp with time zone | | not null | 'infinity'::timestamp with time zone | plain | |
system_time_start | timestamp with time zone | | not null | transaction_timestamp() | plain | |
endname | timestamp with time zone | | not null | transaction_timestamp() | plain | |
Check constraints:
"sysver_system_time_check" CHECK (system_time_start < system_time_end)
"sysver_system_time_end_infinity_check" CHECK (system_time_end = 'infinity'::timestamp with time zone)
Triggers:
sysver_system_time_generated_always BEFORE INSERT OR UPDATE ON sysver FOR EACH ROW EXECUTE PROCEDURE periods.generated_always_as_row_start_end()
sysver_system_time_write_history AFTER INSERT OR DELETE OR UPDATE ON sysver FOR EACH ROW EXECUTE PROCEDURE periods.write_history()
sysver_truncate AFTER TRUNCATE ON sysver FOR EACH STATEMENT EXECUTE PROCEDURE periods.truncate_system_versioning()

DROP TABLE sysver;
TABLE periods.periods;
table_name | period_name | start_column_name | end_column_name | range_type | bounds_check_constraint
------------+-------------+-------------------+-----------------+------------+-------------------------
(0 rows)

TABLE periods.system_time_periods;
table_name | period_name | infinity_check_constraint | generated_always_trigger | write_history_trigger | truncate_trigger
------------+-------------+---------------------------+--------------------------+-----------------------+------------------
(0 rows)

/* Basic SYSTEM VERSIONING */
CREATE TABLE sysver (val text);
SELECT periods.add_system_time_period('sysver');
Expand Down Expand Up @@ -343,10 +331,9 @@ TABLE periods.periods;
------------+-------------+-------------------+-----------------+------------+-------------------------
(0 rows)

\d
List of relations
Schema | Name | Type | Owner
--------+------+------+-------
TABLE periods.system_time_periods;
table_name | period_name | infinity_check_constraint | generated_always_trigger | write_history_trigger | truncate_trigger
------------+-------------+---------------------------+--------------------------+-----------------------+------------------
(0 rows)

/* Clean up */
Expand Down
8 changes: 8 additions & 0 deletions periods.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(generated_always_as_row_start_end);
PG_FUNCTION_INFO_V1(write_history);

/* Define some SQLSTATEs that might not exist */
#if (PG_VERSION_NUM < 100000)
#define ERRCODE_GENERATED_ALWAYS MAKE_SQLSTATE('4','2','8','C','9')
#endif
#define ERRCODE_INVALID_ROW_VERSION MAKE_SQLSTATE('2','2','0','1','H')

void GetPeriodColumnNames(Relation rel, char *period_name, char **start_name, char **end_name);
Expand Down Expand Up @@ -183,7 +187,11 @@ generated_always_as_row_start_end(PG_FUNCTION_ARGS)
columns[1] = end_num;
values[1] = TimestampTzGetDatum(DT_NOEND);
nulls[1] = false;
#if (PG_VERSION_NUM < 100000)
new_row = SPI_modifytuple(rel, new_row, 2, columns, values, nulls);
#else
new_row = heap_modify_tuple_by_cols(new_row, new_tupdesc, 2, columns, values, nulls);
#endif

return PointerGetDatum(new_row);
}
Expand Down
7 changes: 4 additions & 3 deletions sql/periods.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* Install the extension */
CREATE EXTENSION periods CASCADE;
CREATE EXTENSION IF NOT EXISTS btree_gist;
CREATE EXTENSION periods;

/*
* Test creating a table, dropping a column, and then dropping the whole thing;
Expand Down Expand Up @@ -44,9 +45,9 @@ SELECT periods.drop_system_time_period('sysver', drop_behavior => 'CASCADE', pur
SELECT periods.add_system_time_period('sysver', 'endname', 'startname');
SELECT periods.drop_system_time_period('sysver', drop_behavior => 'CASCADE', purge => true);
SELECT periods.add_system_time_period('sysver');
\d+ sysver
DROP TABLE sysver;
TABLE periods.periods;
TABLE periods.system_time_periods;

/* Basic SYSTEM VERSIONING */

Expand Down Expand Up @@ -116,7 +117,7 @@ SELECT periods.drop_system_versioning('sysver', drop_behavior => 'CASCADE', purg
TABLE periods.system_versioning;
DROP TABLE sysver;
TABLE periods.periods;
\d
TABLE periods.system_time_periods;


/* Clean up */
Expand Down

0 comments on commit 577807e

Please sign in to comment.