<?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>entwicklungsgedanken &#187; postgres</title>
	<atom:link href="http://www.entwicklungsgedanken.de/tag/postgres/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.entwicklungsgedanken.de</link>
	<description>Verschiedene Gedanken rund um die Softwareentwicklung</description>
	<lastBuildDate>Mon, 09 Jan 2012 14:47:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Solving NULL values are recognized as text-values within dynamic queries in PostgresSQL</title>
		<link>http://www.entwicklungsgedanken.de/2010/07/29/solving-null-values-are-recognized-as-text-values-within-dynamic-queries-in-postgressql/</link>
		<comments>http://www.entwicklungsgedanken.de/2010/07/29/solving-null-values-are-recognized-as-text-values-within-dynamic-queries-in-postgressql/#comments</comments>
		<pubDate>Thu, 29 Jul 2010 11:09:20 +0000</pubDate>
		<dc:creator>Eric Bartels</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[Webdevelopment]]></category>
		<category><![CDATA[dynamic query]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[NULL]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[postgres]]></category>

		<guid isPermaLink="false">http://www.entwicklungsgedanken.de/?p=435</guid>
		<description><![CDATA[I&#8217;ve written a dynamic query which is finally executed via EXECUTE my_dynamic_query; The query contained a select list in which two columns should always be NULL. -- ... stuff before dynamic_select_cols := 'tbl1.foo, tbl1.bar, null AS theAlias1, null AS theAlias2, tbl2.foo2'; -- ... stuff after combining the dynamic queries EXECUTE my_dynamic_query; In my case the [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve written a dynamic query which is finally executed via <code>EXECUTE my_dynamic_query;</code> The query contained a select list in which two columns should always be <code>NULL</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- ... stuff before</span>
dynamic_select_cols :<span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'tbl1.foo, tbl1.bar, null AS theAlias1, null AS theAlias2, tbl2.foo2'</span>;
<span style="color: #808080; font-style: italic;">-- ... stuff after combining the dynamic queries</span>
<span style="color: #993333; font-weight: bold;">EXECUTE</span> my_dynamic_query;</pre></div></div>

<p>In my case the result of the query will be inserted in a <a href="http://www.postgresql.org/docs/8.4/interactive/sql-createtable.html">temporary table</a> in memory. The destination column for <code>theAlias1</code> is of type <code>INTEGER</code>. When executing the query PostgreSQL gives me an error:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">column <span style="color: #ff0000;">&quot;theAlias1&quot;</span> is of <span style="color: #7a0874; font-weight: bold;">type</span> integer but expression is of <span style="color: #7a0874; font-weight: bold;">type</span> text</pre></div></div>

<p>So there is something wrong when the query is parsed, evaluated and executed.</p>
<p>Solving this was easy with the use of <a href="http://www.postgresql.org/docs/8.4/interactive/functions-conditional.html">NULLIF</a>. Zero equals zero so <code>NULL</code> is returned. Just as expected.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- ... stuff before</span>
dynamic_select_cols :<span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'tbl1.foo, tbl1.bar, NULLIF(0, 0) AS theAlias1, NULLIF(0, 0) AS theAlias2, tbl2.foo2'</span>;
<span style="color: #808080; font-style: italic;">-- ... stuff after combining the dynamic queries</span>
<span style="color: #993333; font-weight: bold;">EXECUTE</span> my_dynamic_query;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.entwicklungsgedanken.de/2010/07/29/solving-null-values-are-recognized-as-text-values-within-dynamic-queries-in-postgressql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to install postgres 8.4 on Debian 5 (lenny)</title>
		<link>http://www.entwicklungsgedanken.de/2010/03/06/how-to-install-postgres-8-4-on-debian-5-lenny/</link>
		<comments>http://www.entwicklungsgedanken.de/2010/03/06/how-to-install-postgres-8-4-on-debian-5-lenny/#comments</comments>
		<pubDate>Sat, 06 Mar 2010 13:28:23 +0000</pubDate>
		<dc:creator>Eric Bartels</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[Webdevelopment]]></category>
		<category><![CDATA[apt]]></category>
		<category><![CDATA[backport]]></category>
		<category><![CDATA[debian]]></category>
		<category><![CDATA[install]]></category>
		<category><![CDATA[postgres]]></category>

		<guid isPermaLink="false">http://www.entwicklungsgedanken.de/?p=385</guid>
		<description><![CDATA[Because I didn&#8217;t found this information in one place I&#8217;m writing this quick &#8220;guide&#8221; on how to install PostgreSQL 8.4.x on Debian 5.x &#8220;lenny&#8221;. Debian lenny stable packages are bundled with Postgres 8.3.x but thanks to Debian Backports the newer Postgres version is available for lenny. Simply do it Add the backports-sources (/etc/apt/sources.list) and update [...]]]></description>
			<content:encoded><![CDATA[<p>Because I didn&#8217;t found this information in one place I&#8217;m writing this quick &#8220;guide&#8221; on how to install <a href="http://www.postgresql.org/docs/8.4/interactive/index.html">PostgreSQL 8.4.x</a> on <a href="http://www.debian.org/">Debian 5.x &#8220;lenny&#8221;</a>.</p>
<p>Debian lenny stable packages are bundled with Postgres 8.3.x but thanks to Debian <a href="http://www.backports.org/dokuwiki/doku.php">Backports</a> the newer Postgres version is available for lenny.</p>
<h3>Simply do it</h3>
<p>Add the backports-sources (/etc/apt/sources.list) and update (apt-get update)</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">deb http:<span style="color: #000000; font-weight: bold;">//</span>www.backports.org<span style="color: #000000; font-weight: bold;">/</span>debian lenny-backports main contrib non-free</pre></div></div>

<p>Install postgres</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #660033;">-t</span> lenny-backports <span style="color: #c20cb9; font-weight: bold;">install</span> postgresql-<span style="color: #000000;">8.4</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.entwicklungsgedanken.de/2010/03/06/how-to-install-postgres-8-4-on-debian-5-lenny/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Value too long error in PostgreSQL inside a plpgsql-function</title>
		<link>http://www.entwicklungsgedanken.de/2009/11/03/value-too-long-error-in-postgresql-inside-a-plpgsql-function/</link>
		<comments>http://www.entwicklungsgedanken.de/2009/11/03/value-too-long-error-in-postgresql-inside-a-plpgsql-function/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 18:13:47 +0000</pubDate>
		<dc:creator>Eric Bartels</dc:creator>
				<category><![CDATA[Webdevelopment]]></category>
		<category><![CDATA[custom type]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[execute]]></category>
		<category><![CDATA[misleading]]></category>
		<category><![CDATA[postgres]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[value too long]]></category>

		<guid isPermaLink="false">http://www.entwicklungsgedanken.de/?p=353</guid>
		<description><![CDATA[In an application I am working on I recently found errors in the applications log file. Value too long for type character varying&#40;64&#41; Within a function (written with plpgsql) a dynamic query is built and executed via sql-EXECUTE. The error PostgreSQL gives me stated out that the error was in the line containing the EXECUTE-statement. [...]]]></description>
			<content:encoded><![CDATA[<p>In an application I am working on I recently found errors in the applications log file.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">Value too long <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #7a0874; font-weight: bold;">type</span> character varying<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">64</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>Within a function (written with <code>plpgsql</code>) a dynamic query is built and executed via sql-<code>EXECUTE</code>. The error PostgreSQL gives me stated out that the error was in the line containing the <code>EXECUTE</code>-statement. But the query was of type <code>TEXT</code> so there is no length limitation. </p>
<h3>Guide me</h3>
<p>In the end it showed that the error PostgreSQL gave me was misleading. The final dynamic query itself was fine. So the sql-<code>EXECUTE</code> cannot be the cause of the error.&#160; </p>
<p>The function returns a custom (composite) type (built via PostgreSQL <code>CREATE TYPE</code>). This custom type consists of a few “columns”. One of this column was indeed of type <code>character varying(64)</code> and was simply to short to hold the data collected inside the function. </p>
<p>Fixing the data type of that column in the custom composite type fixes the error.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.entwicklungsgedanken.de/2009/11/03/value-too-long-error-in-postgresql-inside-a-plpgsql-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Allowing PostgreSQL to use an index when the query uses the LIKE-operator</title>
		<link>http://www.entwicklungsgedanken.de/2009/08/05/allowing-postgresql-to-use-an-index-when-the-query-uses-the-like-operator/</link>
		<comments>http://www.entwicklungsgedanken.de/2009/08/05/allowing-postgresql-to-use-an-index-when-the-query-uses-the-like-operator/#comments</comments>
		<pubDate>Wed, 05 Aug 2009 18:31:14 +0000</pubDate>
		<dc:creator>Eric Bartels</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[Webdevelopment]]></category>
		<category><![CDATA[index]]></category>
		<category><![CDATA[like]]></category>
		<category><![CDATA[operator class]]></category>
		<category><![CDATA[postgres]]></category>
		<category><![CDATA[Search]]></category>
		<category><![CDATA[wildcard]]></category>

		<guid isPermaLink="false">http://www.entwicklungsgedanken.de/?p=312</guid>
		<description><![CDATA[When enabling search in database its very important to have the appropriate indexes defined to make the queries as fast as possible. If your query looks like this (and the appropriate index is defined) the index can be used by the database (the query planner) even with the LIKE-operator. So fast wildcard-searches are possible &#8230; [...]]]></description>
			<content:encoded><![CDATA[<p>When enabling search in database its very important to have the appropriate indexes defined to make the queries as fast as possible. If your query looks like this (and the appropriate index is defined) the index can be used by the database (the query planner) even with the <code>LIKE</code>-operator. So fast wildcard-searches are possible &#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> col1 <span style="color: #993333; font-weight: bold;">FROM</span> foobar <span style="color: #993333; font-weight: bold;">WHERE</span> col1 <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'1234%'</span>;</pre></div></div>

<h3>Yes?! Really?!</h3>
<p>In one of our production systems this kind of query (when debugged via <code>EXPLAIN</code> / <code>EXPLAIN ANALYZE</code>) return unexpected results. Every time our comparison operator turns from equality <code>=</code> into <code>LIKE</code> the index for the column is fully ignored and instead a full-table scan (sequential scan) is used by the planner. Things are really slow &#8230;</p>
<h3>I locale you!</h3>
<p>After asking Google its seems some more people having this issue, too.<br />Our database-cluster was initialized with the locale de_DE.utf8. A quick view into the excellent documentation for <code>CREATE INDEX</code> revealed there is an <a href="http://www.postgresql.org/docs/8.3/interactive/indexes-opclass.html">operator class</a> which controls the operators of the index. After adding an index with an operator class for that column the index (&#8220;FoobarWildcardIdx&#8221;) is now used even when the query contains <code>LIKE</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">INDEX</span> FoobarWildcardIdx <span style="color: #993333; font-weight: bold;">ON</span> foobar <span style="color: #66cc66;">&#40;</span>col1 varchar_pattern_ops<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.entwicklungsgedanken.de/2009/08/05/allowing-postgresql-to-use-an-index-when-the-query-uses-the-like-operator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Performance increase when using case-insensitive queries with Symfony, Propel and PostgreSQL</title>
		<link>http://www.entwicklungsgedanken.de/2008/01/24/performance-increase-when-using-case-insensitive-queries-with-symfony-propel-and-postgresql/</link>
		<comments>http://www.entwicklungsgedanken.de/2008/01/24/performance-increase-when-using-case-insensitive-queries-with-symfony-propel-and-postgresql/#comments</comments>
		<pubDate>Thu, 24 Jan 2008 10:39:25 +0000</pubDate>
		<dc:creator>Eric Bartels</dc:creator>
				<category><![CDATA[Symfony]]></category>
		<category><![CDATA[case-insensitive]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[postgres]]></category>
		<category><![CDATA[propel]]></category>

		<guid isPermaLink="false">http://www.entwicklungsgedanken.de/2008/01/24/performance-increase-when-using-case-insensitive-queries-with-symfony-propel-and-postgresql/</guid>
		<description><![CDATA[Symfony makes heavy use of Propel. Propel finds objects in the storage container using criteria. If you want to let Propel (okay the database) ignore the case you can set this in the criteria. $c = new Criteria &#40;&#41;; &#160; // Allow case sensitive searches $c-&#62;setIgnoreCase &#40;true&#41;; &#160; // Find all posts which name contains [...]]]></description>
			<content:encoded><![CDATA[<div class="lang-en"></div>
<p><a href="http://www.symfony-project.org/" class="external">Symfony</a> makes heavy use of <a href="http://propel.phpdb.org/trac/" class="external">Propel</a>. Propel finds objects in the storage container using criteria. If you want to let Propel (okay the database) ignore the case you can set this in the criteria.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Criteria <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Allow case sensitive searches</span>
<span style="color: #000088;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setIgnoreCase</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Find all posts which name contains propel</span>
<span style="color: #000088;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span> <span style="color: #009900;">&#40;</span>BlogPostPeer<span style="color: #339933;">::</span><span style="color: #004000;">NAME</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Propel'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$posts</span> <span style="color: #339933;">=</span> BlogPostPeer<span style="color: #339933;">::</span><span style="color: #004000;">doSelect</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Because PostgreSQL (unlike MySQL) does not support collations to easily manage how comparisons of strings are done Propel has to generate this sql-statement.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> blog_posts<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">,</span> blog_posts<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">DATE</span>
<span style="color: #993333; font-weight: bold;">FROM</span> blog_posts
<span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #993333; font-weight: bold;">UPPER</span><span style="color: #66cc66;">&#40;</span>blog_posts<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'PROPEL'</span>;</pre></div></div>

<p>Depending on your amount of data this can become <strong>very slow</strong> even if you have an index defined for the &quot;name&quot;-column.</p>
<p>
The trick is to create <em>another index</em> for the column &quot;name&quot;.
</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">INDEX</span> <span style="color: #ff0000;">&quot;UpperNameIndex&quot;</span> <span style="color: #993333; font-weight: bold;">ON</span> blog_posts <span style="color: #993333; font-weight: bold;">USING</span> BTREE <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">UPPER</span><span style="color: #66cc66;">&#40;</span>name<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Thats it. When now using <code>setIgnoreCase</code> its fast again.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.entwicklungsgedanken.de/2008/01/24/performance-increase-when-using-case-insensitive-queries-with-symfony-propel-and-postgresql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

