BitemporalData Making it happened in Postgres · BitemporalData Making it happened in Postgres...

35
Bitemporal Data Making it happened in Postgres Henrietta Dombrovskaya Braviant Holding Chad Slaughter Scale Genius, Inc.

Transcript of BitemporalData Making it happened in Postgres · BitemporalData Making it happened in Postgres...

Page 1: BitemporalData Making it happened in Postgres · BitemporalData Making it happened in Postgres Henrietta Dombrovskaya Braviant Holding Chad Slaughter Scale Genius, Inc.

Bitemporal DataMakingithappenedinPostgres

HenriettaDombrovskayaBraviant HoldingChadSlaughterScaleGenius,Inc.

Page 2: BitemporalData Making it happened in Postgres · BitemporalData Making it happened in Postgres Henrietta Dombrovskaya Braviant Holding Chad Slaughter Scale Genius, Inc.

IncludingtimeintodatamodelsWhywemayneedtoincludetimeintoourdatamodel?

• Whenwasachangemade?Whatexactlythechangewas?Wasitanactualupdateoracorrectionofamistakepreviouslymade?

• Howdidthedatalookatsomemomentinthepast?Howthedatawilllookatsomemomentinthefuture?

• WhendidattributeXhasvalue‘a’?WhatwasthevalueofattributeY,whenthevalueofattributeXwas‘a’?

Page 3: BitemporalData Making it happened in Postgres · BitemporalData Making it happened in Postgres Henrietta Dombrovskaya Braviant Holding Chad Slaughter Scale Genius, Inc.

References• C.J.Date,HughDarwen,andNikosLorentzos.2014. TimeandRelational

Theory,SecondEdition:TemporalDatabasesintheRelationalModelandSQL(2nded.).MorganKaufmannPublishersInc.,SanFrancisco,CA,USA.

• TomJohnston.2014. Bitemporal Data:TheoryandPractice(1sted.).MorganKaufmannPublishersInc.,SanFrancisco,CA,USA.

• TomJohnstonandRandallWeis.2010.ManagingTimeinRelationalDatabases:HowtoDesign,UpdateandQueryTemporalData.MorganKaufmannPublishersInc.,SanFrancisco,CA,USA.

• KrishnaKulkarniandJan-EikeMichels.2012.TemporalfeaturesinSQL:2011. SIGMODRec.41,3(October2012),34-43.

• RichardThomasSnodgrass.1999. DevelopingTime-OrientedDatabaseApplicationsinSQL.MorganKaufmannPublishersInc.,SanFrancisco,CA,USA.

• ISO/IEC9075-2:2011,Informationtechnology— Databaselanguages—SQL,2011

Page 4: BitemporalData Making it happened in Postgres · BitemporalData Making it happened in Postgres Henrietta Dombrovskaya Braviant Holding Chad Slaughter Scale Genius, Inc.

TheLinguisticProblemToomanycommontermswithoverlappingmeaning.

• ValidTime• TransactionTime• SystemTime• StateTime• InscriptionTime• ApplicationTime• SpeechActTime• AssertiveTime• EffectiveTime

Page 5: BitemporalData Making it happened in Postgres · BitemporalData Making it happened in Postgres Henrietta Dombrovskaya Braviant Holding Chad Slaughter Scale Genius, Inc.

WhyBitemporal?• Whydoweneedasecondtimedimension?Whyisn'tatimestampgood

enough?Whyisn'tmyhistorytableordatachangestablesufficient?• Howdoyoucapturechangestothepast,present,andfuture?Second

timedimensionisneededtocapturechangesforthepast,present,andfuturewhilestillallowingforcorrectionstothepastandpresent.

• Howdoyoucorrectbaddatathatalreadyhastimeassociatedwithit?Howdoyoudoitwithafullhistoryofcorrectsandnormalupdates?Howdoyouauditthecorrections?

• Howdoyouinsertdataaboutthepastandpresentbutthatwillonlybevisibleinthefuture?

• Howcanyoumergeactive,Terabyte-sizeddatabaseswithainstanceswitchover?

• Howdoyouhandleallthischangedataovertimewithoutchangingyourquery?

• Howdoyouhandleaquarterlyfinancialreportwithcorrectionsandnoquerycharges?

Page 6: BitemporalData Making it happened in Postgres · BitemporalData Making it happened in Postgres Henrietta Dombrovskaya Braviant Holding Chad Slaughter Scale Genius, Inc.

Regular,TemporalandBitemporaltables

Page 7: BitemporalData Making it happened in Postgres · BitemporalData Making it happened in Postgres Henrietta Dombrovskaya Braviant Holding Chad Slaughter Scale Genius, Inc.

Bitemporal insert

Page 8: BitemporalData Making it happened in Postgres · BitemporalData Making it happened in Postgres Henrietta Dombrovskaya Braviant Holding Chad Slaughter Scale Genius, Inc.

Bitemporal update

Page 9: BitemporalData Making it happened in Postgres · BitemporalData Making it happened in Postgres Henrietta Dombrovskaya Braviant Holding Chad Slaughter Scale Genius, Inc.

Bitemporal correction

Page 10: BitemporalData Making it happened in Postgres · BitemporalData Making it happened in Postgres Henrietta Dombrovskaya Braviant Holding Chad Slaughter Scale Genius, Inc.

Bitemporal inactivation

Page 11: BitemporalData Making it happened in Postgres · BitemporalData Making it happened in Postgres Henrietta Dombrovskaya Braviant Holding Chad Slaughter Scale Genius, Inc.

Bitemporal deletion

Page 12: BitemporalData Making it happened in Postgres · BitemporalData Making it happened in Postgres Henrietta Dombrovskaya Braviant Holding Chad Slaughter Scale Genius, Inc.

Bitemporal constraints

• TemporalEntityIntegrity(TEI)– theprimarykeyanduniqueconstraintsfortemporaltablesaredifferent– how?

• TemporalReferentialIntegrity(TRI)– theforeignkeysshouldbedefineddifferently–how?

Page 13: BitemporalData Making it happened in Postgres · BitemporalData Making it happened in Postgres Henrietta Dombrovskaya Braviant Holding Chad Slaughter Scale Genius, Inc.

Howtosupportbitemporal datainPG9.4andup

Rangessupportandgistindexesmakesitsoeasy!

CREATETABEbi_temporal.customers(cust-nbr INTEGER,cust-nmTEXT,cust-typeTEXT,effective_range TSRANGE,asserted_range TSRANGE,row_created_at timestampz,EXCLUDEUSINGgist(cust-nbr WITH=,effective_range WITH&&,asserted_range WITH&&))

Page 14: BitemporalData Making it happened in Postgres · BitemporalData Making it happened in Postgres Henrietta Dombrovskaya Braviant Holding Chad Slaughter Scale Genius, Inc.

Tableexample:database_versionCREATE TABLE database_versions(

release_version_key serial NOT NULL,

release_version_id integer,

release_version numeric(3,1),

effective temporal_relationships.timeperiod,

asserted temporal_relationships.timeperiod, row_created_at timestamp with time zone NOT NULL

DEFAULT now(),

CONSTRAINT release_version_id_asserted_effective_exclEXCLUDE USING gist

(release_version_id WITH =, asserted WITH &&, effective WITH &&)

)

Page 15: BitemporalData Making it happened in Postgres · BitemporalData Making it happened in Postgres Henrietta Dombrovskaya Braviant Holding Chad Slaughter Scale Genius, Inc.

Tableexample:postgres_clustersCREATE TABLE bi_temp_tables.postgres_clusters(

postgres_cluster_key serial NOT NULL

,postgres_cluster_id integer NOT NULL

,port integer,name character varying(16)

,postgres_version integer

,archive boolean NOT NULL DEFAULT false

,preferred_auth_method text

,effective temporal_relationships.timeperiod,asserted temporal_relationships.timeperiod

,row_created_at timestamp with time zone NOT NULL DEFAULT now(), CONSTRAINT postgres_clusters_preferred_auth_method_fkey FOREIGN KEY (preferred_auth_method) REFERENCES bi_temp_tables.postgres_auth_methods (auth_method)

,CONSTRAINT postgres_cluster_id_asserted_effective_excl EXCLUDE USING gist (postgres_cluster_id WITH =, asserted WITH &&, effective WITH &&))

Page 16: BitemporalData Making it happened in Postgres · BitemporalData Making it happened in Postgres Henrietta Dombrovskaya Braviant Holding Chad Slaughter Scale Genius, Inc.

AllenRelationshipsJamesF.Allen.1983.Maintainingknowledgeabouttemporalintervals.Commun.ACM26,11(November1983),832-843.

Page 17: BitemporalData Making it happened in Postgres · BitemporalData Making it happened in Postgres Henrietta Dombrovskaya Braviant Holding Chad Slaughter Scale Genius, Inc.

FunctionsrepresentingAllenrelationships

has_starts

has_finishes

equals

is_during

is_contained_in

has_during

is_overlaps

has_overlaps

is_before

is_after

has_before

is_meets

has_meets

has_includes

has_contains

has_aligns_with

has_encloses

has_excludes

Page 18: BitemporalData Making it happened in Postgres · BitemporalData Making it happened in Postgres Henrietta Dombrovskaya Braviant Holding Chad Slaughter Scale Genius, Inc.

Definingdomainsandranges

create domain timeperiod as tstzrange;

create domain time_endpoint as timestamptz;

create or replace function timeperiod(

p_range_start time_endpoint,

p_range_end time_endpoint)

RETURNS timeperiod

language sql IMMUTABLE

as $func$

select tstzrange(p_range_start, p_range_end,'[)')::timeperiod;

$func$;

Page 19: BitemporalData Making it happened in Postgres · BitemporalData Making it happened in Postgres Henrietta Dombrovskaya Braviant Holding Chad Slaughter Scale Genius, Inc.

Createbitemporal table

bitemporal_internal.ll_create_bitemporal_table(p_table text, p_table_definition text, p_business_key text)

RETURNS BOOLEAN

Page 20: BitemporalData Making it happened in Postgres · BitemporalData Making it happened in Postgres Henrietta Dombrovskaya Braviant Holding Chad Slaughter Scale Genius, Inc.

Examplesselect bitemporal_internal.ll_create_bitemporal_table(

'bi_temp_tables.database_versions’

,'release_version_key serial

,release_version_id integer --business key

,release_version numeric(3,1)' -- temporal unique

,'release_version_id’);

select bitemporal_internal.ll_create_bitemporal_table(

'bi_temp_tables.postgres_clusters’

,'postgres_cluster_key serial

,postgres_cluster_id integer NOT NULL

,port integer -- bitemporal unique

,name varchar(16)

,postgres_version integer -- bitemporal fk

,archive boolean DEFAULT false NOT NULL

,preferred_auth_method text references bi_temp_tables.postgres_auth_methods ( auth_method )'

,'postgres_cluster_id')

Page 21: BitemporalData Making it happened in Postgres · BitemporalData Making it happened in Postgres Henrietta Dombrovskaya Braviant Holding Chad Slaughter Scale Genius, Inc.

Check,whetherthetableinquestionisbitemporal

ll_is_bitemporal_table(p_table text) RETURNS boolean

Page 22: BitemporalData Making it happened in Postgres · BitemporalData Making it happened in Postgres Henrietta Dombrovskaya Braviant Holding Chad Slaughter Scale Genius, Inc.

Bitemporal insert

Page 23: BitemporalData Making it happened in Postgres · BitemporalData Making it happened in Postgres Henrietta Dombrovskaya Braviant Holding Chad Slaughter Scale Genius, Inc.

Bitemporal correction

Page 24: BitemporalData Making it happened in Postgres · BitemporalData Making it happened in Postgres Henrietta Dombrovskaya Braviant Holding Chad Slaughter Scale Genius, Inc.

Bitemporal update

Page 25: BitemporalData Making it happened in Postgres · BitemporalData Making it happened in Postgres Henrietta Dombrovskaya Braviant Holding Chad Slaughter Scale Genius, Inc.

Bitemporal inactivate

Page 26: BitemporalData Making it happened in Postgres · BitemporalData Making it happened in Postgres Henrietta Dombrovskaya Braviant Holding Chad Slaughter Scale Genius, Inc.

Bitemporal delete

Page 27: BitemporalData Making it happened in Postgres · BitemporalData Making it happened in Postgres Henrietta Dombrovskaya Braviant Holding Chad Slaughter Scale Genius, Inc.

Howtodefineconstraints

• Weneedtosupportthefollowingconstrainttypes:– Primarykey– Unique– Check– nodifferencefromregulartables– IS/ISNOTNULL– nodifferencefromregulartables– Foreignkey

• Metacode– Howwerecordthepresenceofthebitemporalconstraints?

Page 28: BitemporalData Making it happened in Postgres · BitemporalData Making it happened in Postgres Henrietta Dombrovskaya Braviant Holding Chad Slaughter Scale Genius, Inc.

DefinePKWedefineabitemporal primarykeywhenwecreateabitemporaltable,i.e.therecan’tbeatemporaltablewithoutabitemporal PK:

,CONSTRAINT postgres_cluster_id_asserted_effective_excl EXCLUDE USING gist (postgres_cluster_id WITH =, asserted WITH &&, effective WITH &&))

Inadditionwedefineacorrespondingcheckconstraint:

,CONSTRAINT "bitemporal pk postgres_cluster_id" check(true or 'pk' <> '@postgres_cluster_id@')

Function:

select bitemporal_internal.pk_constraint('postgres_cluster_id');

Page 29: BitemporalData Making it happened in Postgres · BitemporalData Making it happened in Postgres Henrietta Dombrovskaya Braviant Holding Chad Slaughter Scale Genius, Inc.

DefineuniqueconstraintDefininguniqueconstraintisnodifferentfromdefiningthePK,wejustnamethemdifferently:

CONSTRAINT "postgres_port_asserted_effective_excl” EXCLUDE USING gist (port WITH =, asserted WITH &&, effective WITH &&)

Inadditionwedefineacorrespondingcheckconstraint:,CONSTRAINT "bitemporal pk postgres_cluster_id" check(true or ’unique' <> '@postgres_port@')

Function:

select bitemporal_internal.unique_constraint('port');

Page 30: BitemporalData Making it happened in Postgres · BitemporalData Making it happened in Postgres Henrietta Dombrovskaya Braviant Holding Chad Slaughter Scale Genius, Inc.

Defineforeignkeyconstraint(bi-temporalreferentialintegrity)

Thedifficultyofverifyingthebi-temporalFKisthatthePK/UQintheparenttableshouldbeeffectiveandassertedallthetimewhenadependentrecordiseffective/assertedCONSTRAINT "bitemporal fkpostgres_version_database_versionsrelease_version" check (true or 'fk' <> '@postgres_version -> database_versions(release_version)@'));

Function:select bitemporal_internal.fk_constraint('postgres_version’,'database_versions’,'release_version');

Page 31: BitemporalData Making it happened in Postgres · BitemporalData Making it happened in Postgres Henrietta Dombrovskaya Braviant Holding Chad Slaughter Scale Genius, Inc.

FKconstraintvalidationinternals

Whathappens,whenanewFKisbeingcreated:

bitemporal_internal.ll_add_fk(p_schema_name text,p_table_name text,p_column_name text,p_source_schema_name text,p_source_table_name text,p_source_column_name text)

returnstext

Page 32: BitemporalData Making it happened in Postgres · BitemporalData Making it happened in Postgres Henrietta Dombrovskaya Braviant Holding Chad Slaughter Scale Genius, Inc.

FKcreationsteps• CheckwhetherthereferencingfieldisaPK/UQ

– validate_bitemporal_pk_uq

• Createcheckconstraint– fk_constraint

• Checkwhetherthevalidationontheparenttablefieldalreadyexists– ll_lookup_validation_function

• Ifnot,createit– ll_generate_fk_validate

• createtriggeroninsert/updateandatriggerfunction

Page 33: BitemporalData Making it happened in Postgres · BitemporalData Making it happened in Postgres Henrietta Dombrovskaya Braviant Holding Chad Slaughter Scale Genius, Inc.

FKvalidationoninsert/update

WhenanewrecordisinsertedoraFKattriibuteisupdated:• ForeachattributewhichisaFK,runamatchingvalidationfunction,andinsert/updaterecordonlyifallarevalid.

Page 34: BitemporalData Making it happened in Postgres · BitemporalData Making it happened in Postgres Henrietta Dombrovskaya Braviant Holding Chad Slaughter Scale Genius, Inc.

Repo:

https://github.com/scalegenius/pg_bitemporal

Page 35: BitemporalData Making it happened in Postgres · BitemporalData Making it happened in Postgres Henrietta Dombrovskaya Braviant Holding Chad Slaughter Scale Genius, Inc.

Futurework

• CompleteBitemporal ForeignKeysSupport• DeployinaProductionApplication• ConverttheCodefromPL/PgSQL toCandpackageasanExtension

• ConvertConstraintstousetheCatalog• BetterIndexingStrategy