<?xml version="1.0" encoding="utf-8" ?>

<rss version="2.0" 
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:admin="http://webns.net/mvcb/"
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
   xmlns:wfw="http://wellformedweb.org/CommentAPI/"
   xmlns:content="http://purl.org/rss/1.0/modules/content/"
   >
<channel>
    <title>Postgres OnLine Journal (Entries tagged as ansi sql)</title>
    <link>http://www.postgresonline.com/journal/</link>
    <description>an In depth look at the PostgreSQL open source database</description>
    <dc:language>en</dc:language>
    <generator>Serendipity 1.4.1 - http://www.s9y.org/</generator>
    <pubDate>Fri, 15 Feb 2013 04:37:09 GMT</pubDate>

    <image>
        <url>http://www.postgresonline.com/journal/templates/default/img/s9y_banner_small.png</url>
        <title>RSS: Postgres OnLine Journal - an In depth look at the PostgreSQL open source database</title>
        <link>http://www.postgresonline.com/journal/</link>
        <width>100</width>
        <height>21</height>
    </image>

<item>
    <title>Saying Happy Valentine in PostGIS</title>
    <link>http://www.postgresonline.com/journal/archives/293-Saying-Happy-Valentine-in-PostGIS.html</link>
            <category>advanced</category>
            <category>application development</category>
            <category>cte</category>
            <category>postgis</category>
            <category>window functions</category>
    
    <comments>http://www.postgresonline.com/journal/archives/293-Saying-Happy-Valentine-in-PostGIS.html#comments</comments>
    <wfw:comment>http://www.postgresonline.com/journal/wfwcomment.php?cid=293</wfw:comment>

    <slash:comments>3</slash:comments>
    <wfw:commentRss>http://www.postgresonline.com/journal/rss.php?version=2.0&amp;type=comments&amp;cid=293</wfw:commentRss>
    

    <author>nospam@example.com (Leo Hsu and Regina Obe)</author>
    <content:encoded>
    &lt;p&gt;A while back I mentioned to Bborie (aka dustymugs) and Sandro (aka strk): &lt;em&gt;We&#039;re missing ability to label our images with text. 
Picture this: What if someone has got a road or a parcel of land and they want to stamp
the parcel id or the road name on it and they don&#039;t want to have to depend on some piece of mapping software.&lt;/em&gt;  Many report writers and databases can talk to each other directly 
such as we demonstrated in &lt;a href=&quot;http://www.postgresonline.com/journal/archives/244-Rendering-PostGIS-Raster-graphics-with-LibreOffice-Base-Reports.html&quot; target=&quot;_blank&quot;&gt;Rendering PostGIS raster graphics with LibreOffice&lt;/a&gt;
and we&#039;ve got raster functionality in the database already. Can we cut out the middleman?
Some ideas came to mind. One we could embed a font lib into PostGIS thus having the ultimate bloatware minus the built-in coffee maker.  
&lt;em&gt;strk screeched&lt;/em&gt;. He&#039;s still recovering from my constant demands of having him upgrade his PostgreSQL version.  Okay fair enough. &lt;/p&gt;
&lt;p&gt;Bborie suggested &lt;em&gt;why don&#039;t you import your letters as rasters and then vectorize them&lt;/em&gt;.  So that&#039;s exactly what we are going to demonstrate in this article
and in doing so we will combine some of the new features coming in PostGIS 2.1 we&#039;ve been talking about in &lt;a href=&quot;http://www.bostongis.com/blog/index.php?/categories/21-waiting_postgis_21&quot; target=&quot;_blank&quot;&gt;Waiting for PostGIS 2.1 series&lt;/a&gt;.&lt;/p&gt;
&lt;div style=&#039;background-color:yellow&#039;&gt;Warning this article will have a hint of &lt;a href=&quot;http://www.rubegoldberg.com/&quot; target=&quot;_blank&quot;&gt;Rube Goldbergishness&lt;/a&gt; in it.
If you are easily offended by seeing stupid tricks done with SQL, stop reading now.
We are going to take a natural vector product and rasterize it just so 
we can vectorize it again so we can then rasterize it again.  Don&#039;t think about it too much.  It may trigger activity in parts of your brain you didn&#039;t know were there
thus resulting in stabbing pains similar to what you experience by quickly guplping down a handful of Wasabi peas.&lt;/div&gt; 
So here are the steps for creating your own font set you can 
overlay on your geometries and rasters.&lt;/p&gt;
&lt;p&gt;See if you can spot the use of window functions and CTEs in these examples.&lt;/p&gt; &lt;br /&gt;&lt;a href=&quot;http://www.postgresonline.com/journal/archives/293-Saying-Happy-Valentine-in-PostGIS.html#extended&quot;&gt;Continue reading &quot;Saying Happy Valentine in PostGIS&quot;&lt;/a&gt;
    </content:encoded>

    <pubDate>Thu, 14 Feb 2013 02:44:00 -0500</pubDate>
    <guid isPermaLink="false">http://www.postgresonline.com/journal/archives/293-guid.html</guid>
    <category>ansi sql</category>
<category>common table expressions</category>
<category>row_number</category>
<category>window functions</category>

</item>
<item>
    <title>greatest and least - oldies but goodies</title>
    <link>http://www.postgresonline.com/journal/archives/183-greatest-and-least-oldies-but-goodies.html</link>
            <category>8.2</category>
            <category>beginner</category>
            <category>mysql</category>
            <category>oracle</category>
            <category>other dbms</category>
            <category>q&amp;a</category>
            <category>sql server</category>
    
    <comments>http://www.postgresonline.com/journal/archives/183-greatest-and-least-oldies-but-goodies.html#comments</comments>
    <wfw:comment>http://www.postgresonline.com/journal/wfwcomment.php?cid=183</wfw:comment>

    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://www.postgresonline.com/journal/rss.php?version=2.0&amp;type=comments&amp;cid=183</wfw:commentRss>
    

    <author>nospam@example.com (Leo Hsu and Regina Obe)</author>
    <content:encoded>
    &lt;h4&gt;Problem: You have a set of numbers, or characters or whatever and you are trying to find the max or min of this set?&lt;/h4&gt;
&lt;p&gt;If the values are separate records in a table or query, the answer is well known and respected across all relational databases -- use the aggregate MAX and MIN functions.&lt;/p&gt;
&lt;p&gt;But what if you have a set of free wheeling numbers or text not in separate records, and you want the max or min of each.  Here is where the 
&lt;strong&gt;least&lt;/strong&gt; and &lt;strong&gt;greatest&lt;/strong&gt; functions come in handy.  &lt;/p&gt;
&lt;p&gt;PostgreSQL has had these functions for as far back as I can remember and  is not the only database to sport these marvelous functions.  Our beloved MySQL and Oracle database have these functions as well. Sadly our more beloved SQL Server even in the SQL Server 2008
variant - lacks these functions.&lt;/p&gt;


&lt;p&gt;Okay how to use these functions -- you use it like this: 
&lt;code&gt;
&lt;pre&gt;
   &lt;span class=&quot;syntax0&quot;&gt; &lt;span class=&quot;syntax-KEYWORD1&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;syntax-FUNCTION&quot;&gt;least&lt;/span&gt;&lt;span class=&quot;syntax-OPERATOR&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-DIGIT&quot;&gt;1&lt;/span&gt;,&lt;span class=&quot;syntax-OPERATOR&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;syntax-DIGIT&quot;&gt;2&lt;/span&gt;,&lt;span class=&quot;syntax-DIGIT&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;syntax-OPERATOR&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-KEYWORD1&quot;&gt;As&lt;/span&gt; num_least, &lt;span class=&quot;syntax-FUNCTION&quot;&gt;greatest&lt;/span&gt;&lt;span class=&quot;syntax-OPERATOR&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-LITERAL1&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax-LITERAL1&quot;&gt;Bobby&lt;/span&gt;&lt;span class=&quot;syntax-LITERAL1&quot;&gt;&#039;&lt;/span&gt;, &lt;span class=&quot;syntax-LITERAL1&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax-LITERAL1&quot;&gt;Catty&lt;/span&gt;&lt;span class=&quot;syntax-LITERAL1&quot;&gt;&#039;&lt;/span&gt;, &lt;span class=&quot;syntax-LITERAL1&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax-LITERAL1&quot;&gt;Kitty&lt;/span&gt;&lt;span class=&quot;syntax-LITERAL1&quot;&gt;&#039;&lt;/span&gt;&lt;span class=&quot;syntax-OPERATOR&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-KEYWORD1&quot;&gt;As&lt;/span&gt; greatest_cat;&lt;/span&gt;
    
    Yields:
 num_least| greatest_cat
----------+----------
       -2 | Kitty
&lt;/pre&gt;
&lt;/code&gt;

&lt;p&gt;We would classify these functions along the lines of COALESCE.  They are like COALESCE because they take an arbitrary number of arguments and the datatype that is returned
is highest datatype that all arguments in the function can be autocast to.  If there is no autocast then well you get an error. To demonstrate, guess what happens when you do this:
&lt;/p&gt;

&lt;code&gt;SELECT least(-1, &#039;Kitty&#039;);&lt;/code&gt;

&lt;p&gt;Well do this in PostgreSQL at least in 8.3+, you get a nice slap if you haven&#039;t installed any deprecated autocasts: &lt;/p&gt;
&lt;span style=&quot;color:red&quot;&gt;ERROR:  invalid input syntax for integer: &quot;Kitty&quot;
LINE 1: SELECT least(&#039;Kitty&#039;, -1)&lt;/span&gt;

&lt;p&gt;Do this in MySQL - so friendly and forgiving, and great reader of minds and you get: &lt;/p&gt;
&lt;code&gt;-1&lt;/code&gt;
&lt;br /&gt;
&lt;strong&gt;OF COURSE&lt;/strong&gt;

&lt;p&gt;I apologize for the ambiguous sarcasm, its just sometimes I want my mind read and sometimes I don&#039;t and I just can&#039;t figure out whether today is one of those days or the other day.&lt;/p&gt;
 
    </content:encoded>

    <pubDate>Fri, 05 Nov 2010 01:50:00 -0400</pubDate>
    <guid isPermaLink="false">http://www.postgresonline.com/journal/archives/183-guid.html</guid>
    <category>ansi sql</category>
<category>mysql</category>
<category>oracle</category>
<category>sql server</category>

</item>
<item>
    <title>Using LTree to Represent and Query Hierarchy and Tree Structures</title>
    <link>http://www.postgresonline.com/journal/archives/173-Using-LTree-to-Represent-and-Query-Hierarchy-and-Tree-Structures.html</link>
            <category>8.2</category>
            <category>8.3</category>
            <category>8.4</category>
            <category>9.0</category>
            <category>contrib spotlight</category>
            <category>db2</category>
            <category>firebird</category>
            <category>intermediate</category>
            <category>ltree</category>
            <category>oracle</category>
            <category>postgresql versions</category>
            <category>sql server</category>
    
    <comments>http://www.postgresonline.com/journal/archives/173-Using-LTree-to-Represent-and-Query-Hierarchy-and-Tree-Structures.html#comments</comments>
    <wfw:comment>http://www.postgresonline.com/journal/wfwcomment.php?cid=173</wfw:comment>

    <slash:comments>5</slash:comments>
    <wfw:commentRss>http://www.postgresonline.com/journal/rss.php?version=2.0&amp;type=comments&amp;cid=173</wfw:commentRss>
    

    <author>nospam@example.com (Leo Hsu and Regina Obe)</author>
    <content:encoded>
    &lt;p&gt;PostgreSQL offers several options for displaying and querying tree like structures.  
In &lt;a href=&quot;http://www.postgresonline.com/journal/archives/131-Using-Recursive-Common-table-expressions-to-represent-Tree-structures.html&quot; target=&quot;_blank&quot;&gt;Using Recursive Common Table Expressions (CTE) to represent tree structures&lt;/a&gt;
we demonstrated how to use common table expressions to display a tree like structure.  Common Table Expressions required PostgreSQL 8.4 and above but  was fairly ANSI standards compliant. In addition to that 
approach you have the option of using recursive functions.  There is yet another common approach for this which is specific to PostgreSQL.  This is using the &lt;a href=&quot;http://www.postgresql.org/docs/current/static/ltree.html&quot; target=&quot;_blank&quot;&gt;ltree contrib datatype&lt;/a&gt;
that has been supported for sometime in PostgreSQL.  For one of our recent projects, we chose ltree over the other approaches because the performance is much better when you need to do ad-hoc queries over the tree since it can take advantage of btree and gist indexes
and also has built-in tree query expressions that make ad-hoc queries simpler to do; similar in concept to the tsearch query syntax for querying text. &lt;/p&gt;

&lt;p&gt;In this article we&#039;ll demonstrate how to use ltree and along the way also show the PostgreSQL 9.0 new features &lt;b&gt;conditional triggers&lt;/b&gt; and &lt;b&gt;ordered aggregates&lt;/b&gt;.&lt;/p&gt;
 &lt;br /&gt;&lt;a href=&quot;http://www.postgresonline.com/journal/archives/173-Using-LTree-to-Represent-and-Query-Hierarchy-and-Tree-Structures.html#extended&quot;&gt;Continue reading &quot;Using LTree to Represent and Query Hierarchy and Tree Structures&quot;&lt;/a&gt;
    </content:encoded>

    <pubDate>Sun, 22 Aug 2010 01:15:00 -0400</pubDate>
    <guid isPermaLink="false">http://www.postgresonline.com/journal/archives/173-guid.html</guid>
    <category>ansi sql</category>
<category>common table expressions</category>
<category>firebird</category>
<category>ibm db2</category>
<category>oracle</category>
<category>postgresql 9.0</category>
<category>sql server</category>
<category>triggers</category>

</item>
<item>
    <title>NOT IN NULL Uniqueness trickery</title>
    <link>http://www.postgresonline.com/journal/archives/166-NOT-IN-NULL-Uniqueness-trickery.html</link>
            <category>basics</category>
            <category>beginner</category>
            <category>postgresql versions</category>
            <category>sql server</category>
    
    <comments>http://www.postgresonline.com/journal/archives/166-NOT-IN-NULL-Uniqueness-trickery.html#comments</comments>
    <wfw:comment>http://www.postgresonline.com/journal/wfwcomment.php?cid=166</wfw:comment>

    <slash:comments>2</slash:comments>
    <wfw:commentRss>http://www.postgresonline.com/journal/rss.php?version=2.0&amp;type=comments&amp;cid=166</wfw:commentRss>
    

    <author>nospam@example.com (Leo Hsu and Regina Obe)</author>
    <content:encoded>
    &lt;p&gt;I know a lot has been said about this beautiful value we affectionately call &lt;a href=&quot;http://en.wikipedia.org/wiki/Null_%28SQL%29&quot; target=&quot;_blank&quot;&gt;NULL&lt;/a&gt;, which is neither here nor there and that manages to catch many of us 
off guard with its casual neither here nor thereness.  Database analysts who are really just back seat mathematicians in disguise like to &lt;a href=&quot;http://sqlblog.com/blogs/hugo_kornelis/archive/2007/09/30/what-if-null-if-null-is-null-null-null-is-null.aspx&quot; target=&quot;_blank&quot;&gt;philosophize&lt;/a&gt;
about the unknown and pat themselves on the back when they feel they have mastered the unknown better than any one else.  Of course database spatial analysts, the worst kind of back seat mathematicians, 
like to talk not only about NULL but about &lt;a href=&quot;http://trac.osgeo.org/postgis/wiki/DevWikiEmptyGeometry&quot; target=&quot;_blank&quot;&gt;EMPTY and compare notes with their brethren&lt;/a&gt; and &lt;a href=&quot;http://blog.cleverelephant.ca/2010/03/nothing-nada-zip-bupkus.html&quot; target=&quot;_blank&quot;&gt;write dissertations&lt;/a&gt;
about what to do about something that is neither here nor there
but is more known than the unknown, but not quite as known as the empty string.&lt;/p&gt;

&lt;p&gt;Okay getting to the point, one of our clients asked us about a peculiar problem they had with a query, and the strange results they were getting.  We admit this still manages to catch us off guard every once in a while.&lt;/p&gt;
 &lt;br /&gt;&lt;a href=&quot;http://www.postgresonline.com/journal/archives/166-NOT-IN-NULL-Uniqueness-trickery.html#extended&quot;&gt;Continue reading &quot;NOT IN NULL Uniqueness trickery&quot;&lt;/a&gt;
    </content:encoded>

    <pubDate>Tue, 22 Jun 2010 04:19:00 -0400</pubDate>
    <guid isPermaLink="false">http://www.postgresonline.com/journal/archives/166-guid.html</guid>
    <category>ansi sql</category>
<category>null</category>

</item>
<item>
    <title>Forcing the planner's hand with set enable_seqscan off WTF</title>
    <link>http://www.postgresonline.com/journal/archives/149-Forcing-the-planners-hand-with-set-enable_seqscan-off-WTF.html</link>
            <category>8.3</category>
            <category>8.4</category>
            <category>intermediate</category>
            <category>postgresql versions</category>
            <category>q&amp;a</category>
    
    <comments>http://www.postgresonline.com/journal/archives/149-Forcing-the-planners-hand-with-set-enable_seqscan-off-WTF.html#comments</comments>
    <wfw:comment>http://www.postgresonline.com/journal/wfwcomment.php?cid=149</wfw:comment>

    <slash:comments>8</slash:comments>
    <wfw:commentRss>http://www.postgresonline.com/journal/rss.php?version=2.0&amp;type=comments&amp;cid=149</wfw:commentRss>
    

    <author>nospam@example.com (Leo Hsu and Regina Obe)</author>
    <content:encoded>
    &lt;p&gt;&lt;span style=&quot;color:green&quot;&gt;UPDATE: &lt;/span&gt;Thanks all for the suggestions.  For now we ended up increasing the
seq_page_cost from 1 to 2  in the database.  That has gotten us back to our old much much faster speeds without change in code and seems to have
improved the speeds of other queries as well, without reducing speed of any.

&lt;code&gt;ALTER DATABASE mydb SET seq_page_cost=2;&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;As Jeff suggested, we&#039;ll try to come up with a standalone example that exhibits the behavior.  The below example was more to demonstrate the construct.  Table names and fields were changed to protect the innocent so that is why we didn&#039;t bother showing explain plans.  The behavior also seems to do 
with the distribution of data and gets worse when stats are updated (via vacuum analyze).  Didn&#039;t see this in PostgreSQL 8.3 and this was a system recently upgraded from 8.3 to 8.4 &lt;/p&gt;
&lt;b&gt;---ORIGINAL ARTICLE HERE --&lt;/b&gt;&lt;br /&gt;
&lt;p&gt;This is a very odd thing and I think has happened to us perhaps once before.
Its a bit puzzling, and we aren&#039;t particularly happy with our work around because its
something that looks to a casual observer as a bit bizarre. The hack is setting the enable_seqscan setting
off for a particular query to force the planner to use indexes available to it.&lt;/p&gt;

&lt;p&gt;What is particularly troubling about this problem, is that it wasn&#039;t always this way.
This is a piece of query code we&#039;ve had in an application for a while, and its worked shall
I say &lt;em&gt;really fast&lt;/em&gt;.  Response times in 300 ms - 1 sec, for what is not a trivial query against a not
so trivially sized hierarchy of tables.  
Anyrate, one day -- this query that we were very happy with, suddenly started
hanging taking 5 minutes to run.  Sure data had been added and so forth, but that didn&#039;t 
completely explain this sudden change of behavior.  The plan it had taken had changed drastically.
It just suddenly decided to stop using a critical index it had always used.  Well it was still using it but just on 
the root table, not the children.  Though querying a child directly proved that it still refused to use it, 
so it didn&#039;t seem to be the hierarchy at fault here.&lt;/p&gt; &lt;br /&gt;&lt;a href=&quot;http://www.postgresonline.com/journal/archives/149-Forcing-the-planners-hand-with-set-enable_seqscan-off-WTF.html#extended&quot;&gt;Continue reading &quot;Forcing the planner&#039;s hand with set enable_seqscan off WTF&quot;&lt;/a&gt;
    </content:encoded>

    <pubDate>Sat, 09 Jan 2010 01:12:00 -0500</pubDate>
    <guid isPermaLink="false">http://www.postgresonline.com/journal/archives/149-guid.html</guid>
    <category>ansi sql</category>
<category>performance optimization</category>

</item>
<item>
    <title>DML to generate DDL and DCL- Making structural and Permission changes to multiple tables</title>
    <link>http://www.postgresonline.com/journal/archives/30-DML-to-generate-DDL-and-DCL-Making-structural-and-Permission-changes-to-multiple-tables.html</link>
            <category>intermediate</category>
            <category>q&amp;a</category>
    
    <comments>http://www.postgresonline.com/journal/archives/30-DML-to-generate-DDL-and-DCL-Making-structural-and-Permission-changes-to-multiple-tables.html#comments</comments>
    <wfw:comment>http://www.postgresonline.com/journal/wfwcomment.php?cid=30</wfw:comment>

    <slash:comments>2</slash:comments>
    <wfw:commentRss>http://www.postgresonline.com/journal/rss.php?version=2.0&amp;type=comments&amp;cid=30</wfw:commentRss>
    

    <author>nospam@example.com (Leo Hsu and Regina Obe)</author>
    <content:encoded>
    &lt;P&gt;Every once in a while you are tasked with an issue such as having to create logging fields
in each of your tables or having to put like constraints on each of your tables or you need to Grant an X group or X user rights to
a certain set of tables.&lt;/P&gt;

&lt;P&gt;The nice thing about having an information_schema is that it provides an easy way to generate scripts to do just that with plain SELECT statements. 
In PostgreSQL its even easier with the array_to_string functions and ARRAY functions, you can get the script in a single field result.
In the following sections we&#039;ll demonstrate some examples of this.
&lt;/P&gt; &lt;br /&gt;&lt;a href=&quot;http://www.postgresonline.com/journal/archives/30-DML-to-generate-DDL-and-DCL-Making-structural-and-Permission-changes-to-multiple-tables.html#extended&quot;&gt;Continue reading &quot;DML to generate DDL and DCL- Making structural and Permission changes to multiple tables&quot;&lt;/a&gt;
    </content:encoded>

    <pubDate>Wed, 13 Feb 2008 05:35:00 -0500</pubDate>
    <guid isPermaLink="false">http://www.postgresonline.com/journal/archives/30-guid.html</guid>
    <category>ansi sql</category>

</item>

</channel>
</rss>