Re: Unable to CREATE SCHEMA and INSERT data in table in that schema in same EXECUTE

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: vibhuti nataraj <vvnataraj(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Unable to CREATE SCHEMA and INSERT data in table in that schema in same EXECUTE
Date: 2013-08-30 17:47:02
Message-ID: 11691.1377884822@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

vibhuti nataraj <vvnataraj(at)gmail(dot)com> writes:
> EXECUTE 'CREATE SCHEMA test; CREATE TABLE test.t (id integer ); INSERT INTO
> test.t (id) VALUES (0);';

That's not going to work because the whole string is parsed before any of
it is executed. In particular, parse analysis of the INSERT is going to
spit up because neither the table nor even the schema exist yet.

> EXECUTE 'CREATE SCHEMA test; CREATE TABLE test.t (id integer );';
> EXECUTE 'INSERT INTO test.t (id) VALUES (0);';

The reason this happens to work is that CREATE TABLE is a utility
statement, which doesn't do any parse analysis to speak of, in
particular it doesn't notice at parse time whether the mentioned
schema exists. However, that's an implementation detail that
could change from release to release. By and large, trying to
EXECUTE multiple statements in one query string is something best
avoided, especially if any of them are DDL.

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Kevin Grittner 2013-08-30 20:25:21 Re: What is the relationship between checkpoint and wal
Previous Message Michael Paquier 2013-08-30 13:07:26 Re: My Experiment of PG crash when dealing with huge amount of data