Re: Altering a column if it exists

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Thara Vadakkeveedu <tharagv(at)yahoo(dot)com>
Cc: "pgsql-novice(at)postgresql(dot)org" <pgsql-novice(at)postgresql(dot)org>
Subject: Re: Altering a column if it exists
Date: 2014-01-08 21:34:12
Message-ID: 17369.1389216852@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Thara Vadakkeveedu <tharagv(at)yahoo(dot)com> writes:
> The following piece of code throws a plsql exception when run through jdbc...
> DO $$
>
> BEGIN
>
> IF EXISTS (SELECT COLUMN_NAME FROM information_schema.columns WHERE table_name
> = 'position' and column_name='org_role_id')
>
> THEN
>
> ALTER TABLE POSITION RENAME COLUMN org_role_id TO job_id;
>
> END IF;
> END$$;

If you dig down through all the Java noise, the problem reported by the
database server is:

> org.postgresql.util.PSQLException: ERROR: unterminated dollar-quoted string

So apparently something on the client side is splitting this into more
than one command to be sent to the server; it looks like that something
thinks the first semicolon terminates the command, even though it's inside
a quoted string. Most likely, that code doesn't understand dollar-quoting
at all.

This might be the fault of the JDBC driver, if you're using an old one;
I'm not sure when that code got taught about dollar-quoted strings.
It could be something further up the stack, though, too.

If you can't identify and fix the culprit, you could switch to using
a plain string literal for the DO (and then doubling all the quote
marks inside the literal...)

regards, tom lane

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message David Johnston 2014-01-08 22:31:59 Re: Altering a column if it exists
Previous Message Thara Vadakkeveedu 2014-01-08 21:24:58 Altering a column if it exists