Temporal Data

13

Click here to load reader

description

Scott BaileyFew things we model in our databases are as complicated as time. The major database vendors have struggled for years with implementing the base data types to represent time. And the capabilities and functionality vary wildly among databases. Fortunately PostgreSQL has one of the best implementations out there. We will look at PostgreSQL's core functionality, discuss temporal extensions, modeling temporal data, time travel and bitemporal data.

Transcript of Temporal Data

Page 1: Temporal Data

Temporal Data in PostgreSQLTemporal Data in PostgreSQL

Scott BaileyThe Evergreen State CollegePostgreSQL Conference West 2009http://scottrbailey.wordpress.com

Page 2: Temporal Data

AgendaAgenda

✗ Provided data types✗ Extended data types✗ Temporal Databases✗ What is in store for PostgreSQL✗ Tips, tricks and questions

Page 3: Temporal Data

Thinking About TimeThinking About Time

✗ Temporal Quiz✗ How many minutes in a day?✗ How many seconds in a day?✗ How long does it take the Earth to complete one

rotation?✗ Actually, what is a second?✗ What number should be at the top of the clock?

✗ Extremely complicated!

Page 4: Temporal Data

Basic Data TypesBasic Data Types

✗ Timestamp &Timestamp with time zone✗ Granularity 1 microsecond✗ Can only subtract two timestamps and

add/subtract with intervals.✗ Timestamp with out time zone is default for

compliance with SQL specification.✗ Which should you use?

✗ Date ✗ Granularity 1 day✗ Can add/subtract dates & integers

Page 5: Temporal Data

Basic Types Cont.Basic Types Cont.

✗ Interval – a duration of time✗ Granularity 1 microsecond✗ Add/Subtract/Multiply/Divide

✗ Time / Time with time zone✗ Granularity 1 microsecond✗ Acts like both time and a (small) interval✗ Timezone info is useless without date

✗ Depricated types✗ abstime (Unix timestamp), reltime (limited

interval), timespan (interval), tinterval (period)

Page 6: Temporal Data

Period Data TypePeriod Data Type

✗ Period – defines an anchored interval or timespan✗ Often what we are modeling is not an instantaneous

event but a period over which some state is true. ✗ Typically implemented as a start and end timestamp.

Could be implemented with an anchor time and an interval.

✗ Theory has been around for a while. ✗ Current implementations – PgTemporal, Chronos✗ Write down a period representing today.✗ Can be open or closed intervals but are typically half-

open. ✗ [ ] indicates endpoints are contained () not contained.

Page 7: Temporal Data

Period Data TypePeriod Data Type

✗ Why a half open interval?✗ Period functions

✗ Positional✗ before(), after(), adjacent(), overlaps(), overleft(), over-

right(), starts(), ends()✗ Containment

✗ contains(), contained_by()✗ Manipulation

✗ shift(), grow(), shrink()✗ Set functions – periods are contiguous sets

✗ period_union(), period_intersect(), period_minus(), period_exclude()

Page 8: Temporal Data

Period IssuesPeriod Issues✗ When one end point is unknown

✗ Since – period(timestamp, NULL) ✗ Until – period(NULL, timestamp) not as common✗ Hard to do much with infinity.

✗ Referential integrity✗ Primary keys – exclusion, can't be two places at

once.✗ Foreign keys – containment, lifetime of child item

must be contained by parent's lifetime.

Page 9: Temporal Data

Non-contiguous Time SetsNon-contiguous Time Sets✗ Why would we need non-contiguous sets?✗ Set functions for periods can be applied to arrays of periods!

✗ Now we can solve (pretty easily) in SQL what would have taken LOTS of procedural code.

✗ Period Arrays vs Non-Contiguous SetsPeriod Array

NCS

Page 10: Temporal Data

Bitemporal DataBitemporal Data✗ “Temporal Database” vs temporal data

✗ Both Valid Time (VT) and Transaction Time (TT).

✗ Valid Time – A period for which a fact is true.✗ Employee X was in position Y from A to B.✗ Store not just the current fact but a historical record.

✗ Transaction Time – Records the time a row was inserted and superseded

✗ Provides time travel or temporal rollback.✗ Very much like the MVCC approach in PostgreSQL. ✗ Logging transaction ids with timestamp would let us

map xmin & xmax to times.

Page 11: Temporal Data

Current ImplementationsCurrent Implementations✗ Lots of theory out there, very few actual implementations.

✗ Oracle – Currently the best implementation in a general purpose database.

✗ Workspace Manager – version enable tables✗ Automatically renames table, adds versioning

metadata (VT), creates view w/ original table name, and defines INSTEAD OF triggers for DML.

✗ Handles temporal constraints.✗ Provides a period data type and a subset of the

functions provided in pgTemporal, Chronos.✗ Great implementation but solves only a single problem

domain and does not allow you to reuse them.

Page 12: Temporal Data

The Future for PostgreSQLThe Future for PostgreSQL✗ Version 8.5

✗ Temporal keys and constraints✗ Changes to the type system✗ Period data type either added to core or as a

contrib module!

✗ Version 8.6?✗ Full support for periods, NCS, table versioning and

time travel!✗ Hands down the most complete temporal

implementation of ANY general purpose database.

Page 13: Temporal Data

Additional ResourcesAdditional Resources✗ TSQL2 Specification -

http://www.cs.arizona.edu/~rts/tsql2.html

✗ Developing Time-Oriented Database Applications – Snodgrasshttp://www.cs.arizona.edu/~rts/tdbbook.pdf

✗ Temporal Data and the Relational Model – Date, Darwen, Lorentzoshttp://books.google.com/books?isbn=1558608559

✗ TimeDB – For Oracle and IBM Cloudscapehttp://www.timeconsult.com/Software/Software.html

✗ Oracle Workspace Manager - http://www.oracle.com/technology/products/database/workspace_manager/index.html