Temporal Data


Published on

Scott Bailey

Few 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.

1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Temporal Data

  1. 1. Temporal Data in PostgreSQL Scott Bailey The Evergreen State College PostgreSQL Conference West 2009 http://scottrbailey.wordpress.com
  2. 2. Agenda ✗ Provided data types ✗ Extended data types ✗ Temporal Databases ✗ What is in store for PostgreSQL ✗ Tips, tricks and questions
  3. 3. Thinking 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!
  4. 4. Basic 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
  5. 5. 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)
  6. 6. Period 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.
  7. 7. Period 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()
  8. 8. Period 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.
  9. 9. Non-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 Sets Period Array NCS
  10. 10. Bitemporal 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.
  11. 11. Current 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.
  12. 12. The 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.
  13. 13. Additional Resources ✗ TSQL2 Specification - http://www.cs.arizona.edu/~rts/tsql2.html ✗ Developing Time-Oriented Database Applications – Snodgrass http://www.cs.arizona.edu/~rts/tdbbook.pdf ✗ Temporal Data and the Relational Model – Date, Darwen, Lorentzos http://books.google.com/books?isbn=1558608559 ✗ TimeDB – For Oracle and IBM Cloudscape http://www.timeconsult.com/Software/Software.html ✗ Oracle Workspace Manager - http://www.oracle.com/technology/products/database/workspace_manager/index.html