<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Experimental Thoughts &#187; Temporal</title>
	<atom:link href="http://thoughts.j-davis.com/tag/temporal/feed/" rel="self" type="application/rss+xml" />
	<link>http://thoughts.j-davis.com</link>
	<description>Ideas on Databases, Logic, and Language by Jeff Davis</description>
	<lastBuildDate>Thu, 06 May 2010 19:29:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Temporal PostgreSQL Roadmap</title>
		<link>http://thoughts.j-davis.com/2010/03/09/temporal-postgresql-roadmap/</link>
		<comments>http://thoughts.j-davis.com/2010/03/09/temporal-postgresql-roadmap/#comments</comments>
		<pubDate>Wed, 10 Mar 2010 04:49:06 +0000</pubDate>
		<dc:creator>Jeff Davis</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Language]]></category>
		<category><![CDATA[Logic]]></category>
		<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Temporal]]></category>

		<guid isPermaLink="false">http://thoughts.j-davis.com/?p=254</guid>
		<description><![CDATA[Why are temporal extensions in PostgreSQL important? Quite simply, managing time data is one of the most common requirements, and current general-purpose database systems don&#8217;t provide us with the basic tools to do it. Every general-purpose DBMS falls short both in terms of usability and performance when trying to manage temporal data. What is already [...]]]></description>
			<content:encoded><![CDATA[<p>Why are temporal extensions in PostgreSQL important? Quite simply, managing time data is one of the most common requirements, and current general-purpose database systems don&#8217;t provide us with the basic tools to do it. Every general-purpose DBMS falls short both in terms of usability and performance when trying to manage temporal data.</p>
<p>What is already done?</p>
<p><span id="more-254"></span></p>
<ul>
<li><a href="http://pgfoundry.org/projects/temporal">PERIOD data type</a>, which can represent anchored intervals of time; that is, a chunk of time with a definite beginning and a definite end (in contrast to a SQL INTERVAL, which is not anchored to any specific beginning or end time).
<ul>
<li>Critical for usability because it acts as a <em>set</em> of time, so you can easily test for containment and other operations without using awkward constructs like BETWEEN or lots of comparisons (and keeping track of inclusivity/exclusivity of boundary points).</li>
<li>Critical for performance because you can index the values for efficient &#8220;contains&#8221; and &#8220;overlaps&#8221; queries (among others).</li>
</ul>
</li>
</ul>
<ul>
<li>Temporal Keys (called Exclusion Constraints, and will be available in the next release of PostgreSQL, 9.0), which can enforce the constraint that no two periods of time (usually for a given resource, like a person) overlap. See the <a href="http://developer.postgresql.org/pgdocs/postgres/sql-createtable.html">documentation</a> (look for the word &#8220;EXCLUDE&#8221;), and see my previous articles (<a href="../2009/11/01/temporal-keys-part-1/">part 1</a> and <a href="../2009/11/08/temporal-keys-part-2/">part 2</a>) on the subject.
<ul>
<li>Critical for usability to avoid procedural, error-prone hacks to enforce the constraint with triggers or by splitting time into big chunks.</li>
<li>Critical for performance because it performs comparably to a UNIQUE index, unlike the other procedural hacks which are generally too slow to use for most real systems.</li>
</ul>
</li>
</ul>
<p>What needs to be done?</p>
<ul>
<li>Range Types &#8212; Aside from PERIOD, which is based on TIMESTAMPTZ, it would also be useful to have very similar types based on, for example, DATE. It doesn&#8217;t stop there, so the natural conclusion is to generalize PERIOD into &#8220;range types&#8221; which could be based on almost any subtype.</li>
<li>Range Keys, Foreign Range Keys &#8212; If Range Types are known to the Postgres engine, that means that we can have syntactic sugar for range keys (like temporal keys, except for any range type), etc., that would internally use Exclusion Constraints.</li>
<li>Range Join &#8212; If Range Types are known to the Postgres engine, there could be syntactic sugar for a &#8220;range join,&#8221; that is, a join based on &#8220;overlaps&#8221; rather than &#8220;equals&#8221;. More importantly, there could be a new join type, a Range Merge Join, that could perform this join efficiently (without a Range Merge Join, a range join would always be a nested loop join).</li>
<li>Simple table logs &#8212; The ability to easily create an effective &#8220;audit log&#8221; or similar trigger-based table log, that can record changes and be efficiently queried for historical state or state changes.</li>
</ul>
<p>I&#8217;ll be speaking on this subject (specifically, the new Exclusion Constraints feature) in the upcoming <a href="http://postgresqlconference.org">PostgreSQL Conference EAST 2010</a> (my <a href="http://postgresqlconference.org/2010/east/talks/not_just_unique_exclusion_constraints">talk description</a>) in Philadelphia later this month and <a href="http://pgcon.org">PGCon 2010</a> (my <a href="http://www.pgcon.org/2010/schedule/events/201.en.html">talk description</a>) in Ottawa this May. In the past, these conferences and others have been a great place to get ideas and help me move the temporal features forward.</p>
<p>The existing features have been picking up a little steam lately. The <a href="http://lists.pgfoundry.org/pipermail/temporal-general/">temporal-general mailing list</a> has some traffic now &#8212; fairly low, but enough that others contribute to the discussions, which is a great start. I&#8217;ve also received some great feedback from a number of people, including the folks at <a href="http://pgexperts.com">PGX</a>. There&#8217;s still a ways to go before we have all the features we want, but progress is being made.</p>
]]></content:encoded>
			<wfw:commentRss>http://thoughts.j-davis.com/2010/03/09/temporal-postgresql-roadmap/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Temporal Keys, Part 2</title>
		<link>http://thoughts.j-davis.com/2009/11/08/temporal-keys-part-2/</link>
		<comments>http://thoughts.j-davis.com/2009/11/08/temporal-keys-part-2/#comments</comments>
		<pubDate>Mon, 09 Nov 2009 05:48:37 +0000</pubDate>
		<dc:creator>Jeff Davis</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Language]]></category>
		<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Temporal]]></category>

		<guid isPermaLink="false">http://thoughts.j-davis.com/?p=180</guid>
		<description><![CDATA[In the last article, I argued that: A schedule conflict is a typical business problem. The later you try to resolve a schedule conflict, the more costly it is to resolve. In particular, there is a big jump in the cost the moment after conflicting data is recorded. Therefore, it&#8217;s best for the DBMS itself [...]]]></description>
			<content:encoded><![CDATA[<p>In the <a title="Temporal Keys, Part 1" href="http://thoughts.j-davis.com/2009/11/01/temporal-keys-part-1/">last article</a>, I argued that:</p>
<ul>
<li>A schedule conflict is a typical business problem.</li>
<li>The later you try to resolve a schedule conflict, the more costly it is to resolve.</li>
<li>In particular, there is a big jump in the cost the moment after conflicting data is recorded.</li>
<li>Therefore, it&#8217;s best for the DBMS itself to enforce the constraint, because only the DBMS can avoid the conflict effectively before the conflict is recorded.</li>
</ul>
<p>Then, I opened up a discussion to see how people are dealing with these schedule conflicts. In the comments I received at the end of the article, as well as other anecdotes from conferences, user groups, mailing lists, and my own experience, the solutions fall into a few categories:</p>
<p><span id="more-180"></span></p>
<ul>
<li>The rate of conflicts is so low that the costs are not important. For instance, you may make 0.1% of your customers unhappy, and need to refund them, but perhaps that&#8217;s a cost you&#8217;re willing to pay.</li>
<li>The application receives so few requests that performance is not an object, and serialization of all requests is a viable option. The serialization is done using big locks and a read-check-write cycle. Even if performance is not an object, these applications sometimes run into maintenance problems or unexpected outages because of the big locks required.</li>
<li>You can break the time slices into manageable chunks, e.g. one day chunks aligned at midnight. This kind of solution is highly specific to the business, reduces the flexibility of the business, and often requires a substantial amount of custom, error-prone procedural code.</li>
<li>Complex procedural code: usually a mix of application code, functions in the DBMS, row-level locking, static data in tables that only exists for the purposes of row-level locks, etc. This kind of solution is generally very specific to the application and the business, requires lots of very error-prone custom procedural code, is difficult to adequately test, and it&#8217;s hard to understand what&#8217;s going on in the system at any given time. Hunting down sporadic performance problems would be a nightmare.</li>
</ul>
<p>Those solutions just aren&#8217;t good enough. We use relational database systems because they are smart, declarative, generally useful for many problems, and maintainable (Note: these principles contrast with <a href="http://en.wikipedia.org/wiki/NoSQL">NoSQL</a>, which is moving in the opposite direction &#8212; more on that in another article).</p>
<p><em>[UPDATE: The following project has been committed for the next release of PostgreSQL; the feature is now called "Exclusion Constraints"; and the new version of PostgreSQL will be called 9.0 (not 8.5). See the <a href="http://developer.postgresql.org/pgdocs/postgres/sql-createtable.html">documentation</a> under the heading "EXCLUDE".]</em></p>
<p>The project that I&#8217;ve been working on for PostgreSQL 8.5 is called &#8220;<a href="https://commitfest.postgresql.org/action/patch_view?id=199">Operator Exclusion Constraints</a>&#8220;. These are a new type of constraint that most closely resembles the <code>UNIQUE</code> constraint, because one tuple can preclude the existence of other tuples. With a <code>UNIQUE</code> constraint on attribute <code>A</code> of a table with attributes <code>(A, B, C)</code>, the existence of the tuple <code>(5, 6, 7)</code> precludes the existence of any tuple <code>(5, _, _)</code> in that table at the same time. This is different from a foreign key, which <em>requires</em> the existence of a tuple in another table; and different from a <code>CHECK</code> constraint which rejects tuples independently from any other tuple in any table (and the same goes for NOT NULL).</p>
<p>The same semantics as a <code>UNIQUE</code> constraint can be easily specified as an Operator Exclusion Constraint, with a minor performance penalty at insert time (one additional index search, usually only touching pages that are already in cache). Exclusion constraints are more general than <code>UNIQUE</code>, however. For instance, with a complex type such as <a href="http://www.postgresql.org/docs/8.4/static/datatype-geometric.html#AEN6211">CIRCLE</a>, you can specify that no two circles in a table overlap &#8212; which is a constraint that is impossible to specify otherwise (without resorting to the poor solutions mentioned above<a title="Temporal Keys, Part 1" href="../2009/11/01/temporal-keys-part-1/"></a>).</p>
<p>This applies to temporal keys very nicely. First, get the <a href="http://pgfoundry.org/projects/temporal">PERIOD data type</a>, which allows you a better way to work with periods of time (sets of time, really), rather than points in time. Second, you need to install the <a href="http://www.postgresql.org/docs/8.4/static/btree-gist.html">btree_gist</a> contrib module. Then, use an exclusion constraint like so:</p>
<p><em>[UPDATE 2010-03-09: Syntax updated to reflect the version of this project committed for PostgreSQL 9.0. ]</em></p>
<pre>CREATE TABLE room_reservation
(
  name   TEXT,
  room   TEXT,
  during PERIOD,
  EXCLUDE USING gist (room WITH =, during WITH &amp;&amp;)
);</pre>
<p>That will prevent two reservations on the same room from overlapping. There are a few pieces to this that require explanation:</p>
<ul>
<li><code>&amp;&amp;</code> is the &#8220;overlaps&#8221; operator for the <code>PERIOD</code> data type.</li>
<li><code>USING gist</code> tells PostgreSQL what kind of index to create to enforce this constraint. The operators must map to search strategies for this index method, and searching for overlapping periods requires a GiST index.</li>
<li>Because we are using GiST, we need GiST support for equality searches for the <code>TEXT</code> data type, which is the reason we need the btree_gist contrib module.</li>
<li>Conflicts will only occur if two tuples have equal room numbers, <em>and</em> overlapping periods of time for the reservation.</li>
</ul>
<p>This solution:</p>
<ul>
<li>Performs well under light and heavy contention. Not quite as well as a UNIQUE constraint, but much better than the alternatives, and without the surprises you might get from using big locks. Note that the constraint <em>will </em>be enforced at some point, so ignoring the problem is not a high-performance alternative (interpersonal communication has higher latency than a computer).</li>
<li>Is declarative. The implementation shows through a little bit &#8212; the user will know that an index is being used, for instance &#8212; but it&#8217;s a relatively simple declaration. As a consequence, it&#8217;s not very error-prone from the schema designer&#8217;s standpoint.</li>
<li>Is not specific to the business. You don&#8217;t have to decide on an appropriate time slice (e.g. one hour, one day, etc.); you don&#8217;t have to try to partition locks in creative ways; you don&#8217;t have to write procedural code (in the database system or application); and you don&#8217;t have to come up with interesting ways to detect a conflict or notify the user.</li>
</ul>
<p>Temporal keys are just one part of the support required for effective temporal data management inside the DBMS. However, it&#8217;s one of the most important pieces that requires support from the core engine, and cannot be implemented as a module.</p>
]]></content:encoded>
			<wfw:commentRss>http://thoughts.j-davis.com/2009/11/08/temporal-keys-part-2/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Temporal Keys, Part 1</title>
		<link>http://thoughts.j-davis.com/2009/11/01/temporal-keys-part-1/</link>
		<comments>http://thoughts.j-davis.com/2009/11/01/temporal-keys-part-1/#comments</comments>
		<pubDate>Mon, 02 Nov 2009 00:55:34 +0000</pubDate>
		<dc:creator>Jeff Davis</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Logic]]></category>
		<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Temporal]]></category>

		<guid isPermaLink="false">http://thoughts.j-davis.com/?p=171</guid>
		<description><![CDATA[&#8220;Schedule conflict&#8221; &#8212; it&#8217;s one of the simplest and most common constraints for business or any other organization. One person cannot be in two places at the same time; and in many cases a only a single person can use a given resource at any time. Does your database system know anything about a schedule [...]]]></description>
			<content:encoded><![CDATA[<p>&#8220;Schedule conflict&#8221; &#8212; it&#8217;s one of the simplest and most common constraints for business or any other organization. One person cannot be in two places at the same time; and in many cases a only a single person can use a given resource at any time.</p>
<p>Does your database system know anything about a schedule conflict? Should it?</p>
<p>Constraints are always enforced at some point, it&#8217;s just a matter of when, how, and the cost of correcting the situation.</p>
<p><span id="more-171"></span>Consider two professors who try to reserve the same lecture hall at overlapping times (let&#8217;s say 1-2pm, and 1:30-2:30pm). Now imagine two possible resolutions:</p>
<ol>
<li>They are blissfully unaware of the schedule conflict until they have already promoted their lecture, attracted an audience, and arrived at the building. The professor scheduled for 1:30-2:30pm will be disappointed to find it already in use, and the students will be confused. The second lecture may have to be canceled due to the confusion, or lose a substantial number of attendees.</li>
<li>At the time of scheduling, one professor is given an error message to the effect of &#8220;this room is already reserved, please choose a different time&#8221;.</li>
</ol>
<p>Observe that neither one really solves the problem: the second resolution still forces one professor to choose a less-desirable time. However, it is a <em>much</em> cheaper resolution. As with many problems, early detection is simplest and cheapest to resolve.</p>
<p>Of course, there are many resolutions that fall between the first and the second. For instance, you might run a program every hour that checks for conflicts and alerts the parties involved. That may work, but there are still problems:</p>
<ul>
<li>You&#8217;ve now introduced uncertainty into the system: do I have a reservation or not? If there is a conflict later, will I be kicked out or will they?</li>
<li>You have to come up with rules for resolution: does the first one win? How do you define &#8220;first&#8221; if transactions are running for a while? What if someone makes a reservation &#8220;first&#8221; but then makes all kinds of changes later; were they really first or did they just game the system?</li>
<li>If someone&#8217;s reservation gets bumped, you have to now have a new strange state for a reservation, in which it is disabled, the organizer has (hopefully) been notified, and it needs to change.</li>
<li>Notice that everything is very procedural and specific to the business. You have to have a notification mechanism, and rules for how to resolve it.</li>
<li>Let&#8217;s go back to the definition of &#8220;first&#8221;: say you have made a reservation, and you get bumped by the conflict detector. In between the time you made the reservation and the time you were notified of a conflict, someone else reserved your second choice. Are you now first in line for that time slot? If so, that has a cascading effect such that it&#8217;s almost impossible for the person that took the second-choice slot to know that they are at risk of being bumped.</li>
</ul>
<p>These thought experiments might seem like edge cases, but reservation systems have two common traits that make these problems very real:</p>
<ol>
<li>They tend to start allowing reservations of a specific resource at a specific time, published in advance.</li>
<li>There tend to be some resources and time slots that have a much higher value than the others.</li>
</ol>
<p>These two traits lead to heavy contention.</p>
<p>Now, how would you go about enforcing such a constraint (non-overlapping time periods) in the database? While considering possible solutions, think about:</p>
<ul>
<li>Does the solution work for a wide variety of use cases, or only in special cases?</li>
<li>How well would it perform, under low contention and high contention, and under varied workloads?</li>
<li>Can it be implemented in a general purpose RDBMS, like PostgreSQL?</li>
<li>Is it procedural in nature, or declarative?</li>
</ul>
<p>I think it&#8217;s worthwhile to consider the problem, so I will end this article now, and provide some approaches, as well as my real answer, in the next article. In the meantime, feel free to post ideas as comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://thoughts.j-davis.com/2009/11/01/temporal-keys-part-1/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>PostgreSQL WEST and Temporal Databases</title>
		<link>http://thoughts.j-davis.com/2009/10/12/postgresql-west-and-temporal-databases/</link>
		<comments>http://thoughts.j-davis.com/2009/10/12/postgresql-west-and-temporal-databases/#comments</comments>
		<pubDate>Tue, 13 Oct 2009 05:09:57 +0000</pubDate>
		<dc:creator>Jeff Davis</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Language]]></category>
		<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Temporal]]></category>

		<guid isPermaLink="false">http://thoughts.j-davis.com/?p=152</guid>
		<description><![CDATA[I&#8217;ve been interested in temporal data and relational databases for quite some time. There are going to be at least two people talking about temporal data at PostgreSQL WEST in Seattle: Scott Bailey and me. See the talk descriptions. In the past, I&#8217;ve worked on a temporal extension to PostgreSQL that implements the PERIOD data [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been interested in temporal data and relational databases for quite some time. There are going to be at least two people talking about temporal data at <a title="PG WEST" href="http://postgresqlconference.org">PostgreSQL WEST</a> in Seattle: <a href="http://scottrbailey.wordpress.com/">Scott Bailey</a> and me. See the <a href="http://postgresqlconference.org/2009/west/talks">talk descriptions</a>.</p>
<p>In the past, I&#8217;ve worked on a <a title="Temporal PostgreSQL" href="http://pgfoundry.org/projects/temporal">temporal extension</a> to PostgreSQL that implements the <code>PERIOD</code> data type. This is a data type that offers both a definite beginning and a definite end time, which is important for describing things that happen over a period of time, rather than instantaneously. Trying to use separate attributes for &#8220;start&#8221; and &#8220;end&#8221; is bad for a number of reasons, and will certainly be addressed in a subsequent blog entry. For now, I&#8217;ll just say that I believe the <code>PERIOD</code> data type is fundamentally important for handling all kinds of time data, which I believe is a common problem.</p>
<p>At WEST, I&#8217;ll be presenting my progress on <em>temporal keys</em>. Temporal keys are used to prevent overlapping periods of time &#8212; a schedule conflict &#8212; by using an index and following the same concurrent behavior as <code>UNIQUE</code> with minimal performance cost (one extra index search, to be precise).</p>
<p>Temporal keys cannot be expressed in PostgreSQL 8.4, unless you resort to triggers and a full table lock (ouch!). So, additional backend support is required. This is accomplished in my patch for <a title="Operator Exclusion Constraints GIT repo" href="http://git.postgresql.org/gitweb?p=users/jdavis/postgres.git;a=shortlog;h=refs/heads/operator-exclusion-constraints">operator exclusion constraints</a>, which are a more general way of using arbitrary operators and index searches to enforce a constraint. I plan to do what&#8217;s required for the patch to be accepted in PostgreSQL 8.5.</p>
<p>Temporal modeling is a common problem. It seems like almost every PostgreSQL conference has had at least one talk on the matter, so we know there is some demand for improvement. If you&#8217;re interested, I hope you come to WEST and chat with Scott or I, and let&#8217;s see if we can come up with some real solutions.</p>
]]></content:encoded>
			<wfw:commentRss>http://thoughts.j-davis.com/2009/10/12/postgresql-west-and-temporal-databases/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
