Brian Ray's Blog : postgreSQL/postgres8.html

Painting is just another way of keeping a diary. --Picasso

Mon, 24 Jan 2005

PostgreSQL double dollars

I have been hanging out at #postgresql on freenode for the past couple of months, watching the release candidates for PostgreSQL 8 wizz by, rc1, rc2, rc3, rc4, rc5 .... launch. On last Wednesday, 01/19/05 eight was finally launched!

The big news on the eight for much of the world was it's move to Native Windows. For example read this article or this one . If you know me, you would know I do not care to work on Windows... so this is not the biggest issue to me. The biggest new feature for me is called double dollars coded by Andrew Dunstan and documented by David Fetter.

What is the bid deal with double quotes, you ask. Quoting in postgres, in particular, plpgsql, is much like Oracle in that when you write a stored procedure you best use double-single-quotes:

CREATE FUNCTION blog (BLOG, integer) RETURNS boolean AS '
    DECLARE
        v_blog ALIAS FOR $1;
        v_blognum ALIAS FOR $2;
    BEGIN
        IF v_blog.text ISNULL THEN
            RETURN ''f'';
        END IF;
        RETURN v_blog.id > v_blognum;
    END;
' LANGUAGE 'plpgsql';

The problem here is this gets tricker and tricker the more escaping you need to do:

a_output := a_output || '' if v_'' || 
    referrer_keys.kind || '' like '''''''''' 
    || referrer_keys.key_string || '''''''''' 
    then return ''''''  || referrer_keys.referrer_type 
    || ''''''; end if;''; 

The double dollar alleviates some of this pain by allowing another quoting mechanism:

a_output := a_output || $$ if v_$$ || referrer_keys.kind || $$ like '$$
    || referrer_keys.key_string || $$'
    then return '$$  || referrer_keys.referrer_type 
    || $$'; end if;$$; 

ahh, much better.