X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id E3A46528BD for ; Mon, 20 Jun 2005 13:05:18 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 79384-06 for ; Mon, 20 Jun 2005 16:05:07 +0000 (GMT) Received: from wproxy.gmail.com (wproxy.gmail.com [64.233.184.202]) by svr1.postgresql.org (Postfix) with ESMTP id 6A7FC528D9 for ; Mon, 20 Jun 2005 13:05:06 -0300 (ADT) Received: by wproxy.gmail.com with SMTP id 68so822720wra for ; Mon, 20 Jun 2005 09:05:06 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:cc:in-reply-to:mime-version:content-type:references; b=XsO5N9FZEkuVQuVZ3upjA93+r1lXbZCS4Jh9vfS35mcfb+fC6E+1AbOsiYgn6gAQRyVqrn1pRA8T5wik6E+52Z0u701MQCRZAC1VCFxyBd4sSd7El14jALafmolLZEs5U0SeJL9YgC4qNkPeyQsAHY0VoqKZHi2MPBgfDel7wJU= Received: by 10.54.57.16 with SMTP id f16mr2588794wra; Mon, 20 Jun 2005 09:05:06 -0700 (PDT) Received: by 10.54.45.11 with HTTP; Mon, 20 Jun 2005 09:05:06 -0700 (PDT) Message-ID: Date: Mon, 20 Jun 2005 21:35:06 +0530 From: Akhil Srinivasan Reply-To: Akhil Srinivasan To: David Gagnon Subject: Re: Problems with temporary tables created in callable functions Cc: pgsql-jdbc@postgresql.org In-Reply-To: <42B6AA1D.3060809@siunik.com> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_10465_33474128.1119283506525" References: <42B6AA1D.3060809@siunik.com> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.166 tagged_above=0 required=5 tests=AWL, HTML_30_40, HTML_MESSAGE, RCVD_BY_IP X-Spam-Level: X-Archive-Number: 200506/105 X-Sequence-Number: 13067 ------=_Part_10465_33474128.1119283506525 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline I saw the faq, but its not a simple temporary table so formatting and=20 building the create and insert strings are non trivial tasks. I would rathe= r=20 not have to do it unless i really need to. And i still get "the table=20 already exists error" even with the execute. I have tried another approach after reading a mailing list posting. tempTableExists:=3D0; SELECT * INTO rectable FROM PG_TABLES WHERE SCHEMANAME LIKE \'pg_temp_%\'= =20 and tablename like \'%TEMQUERYPERSONALPPORTFOLIOLIST%\' ; IF rectable.tablename like \'%TEMQUERYPERSONALPPORTFOLIOLIST%\' THEN=20 tempTableExists:=3D1; END IF; if tempTableExists =3D 0 then CREATE TEMPORARY TABLE "TEMQUERYPERSONALPPORTFOLIOLIST" ( "PORTFOLIOID" INTEGER, "NAME" VARCHAR, "ACCOUNTID" INTEGER, "CONTESTID" INTEGER, "CASHAMOUNT" INTEGER, "CURRENTVALUE" INTEGER, "STATUS" VARCHAR )without oids on commit delete rows; end if ; with a commit being called at the end after retrieving the resultset. This= =20 seems to be working so far and i have done a couple of tests with multiple= =20 users and they have gone fine. Does anybody see anything that can cause a= =20 problem here. In this i check to see if the table is already created, if so i do a pretty= =20 much blanket delete. I believe that stored procedure call is atomic so=20 multiple calls to the same procedure should be serialized which works for m= e=20 here as there can be only one temporary table. I might try on commit drop, but i was curious as to how reliable it is.=20 Because that should remove any need for a temporary table check. Hopefully= =20 it would also become more reliable. Any ideas or improvements would be welcome. Thanks Akhil Srinivasan PS: I have another problem buts thats coming next. On 6/20/05, David Gagnon wrote: >=20 > Hi, >=20 > Temporary table a tricky in postgresql you must use: > EXECUTE '' > CREATE TEMP TABLE TMP_IC ( > ICNUM VARCHAR(20) primary key > ) ON COMMIT DROP''; >=20 >=20 > And for your sql : > statement :=3D '' > INSERT INTO TMP_IC > SELECT ICNUM FROM IC''; > EXECUTE STATEMENT; >=20 > The problem is that the SQL planner keeps the plan for deleted temp > table. Using the EXECUTE statement avoid caching in the planner. This > is the official way to use temp table in postgresql. Look in the FAQ (I > think,) everything is explained there. >=20 > Ciao > /David >=20 >=20 >=20 >=20 > Akhil Srinivasan wrote: >=20 > > Hi > > I have a an application that uses a lot of stored functions, some of > > them which create temporary tables and refcursors that point to the > > tables. Something that comes up occasionally are errors like > > > > This is when we have some code in the function to delete existing > > temporary tables. > > ERROR: relation with OID xxxxx does not exist. > > ERROR: relation "TEMPSTOCKVALUEALERT" already exists > > > > When we comment that code we get errors like > > ERROR: relation "TEMPSTOCKANALYSIS" already exists. This happens every > > second call to the function. > > > > In the jdbc call code we have setAutoCommit to false, And I have tried > > a couple of options when preparing the call like > > > > CallableStatement proc =3D con.prepareCall("{ ? =3D call > > \"queryStockAnalysisSingle\" ( ?,?,?,? ) }" > > ,ResultSet.CONCUR_READ_ONLY,ResultSet.CLOSE_CURSORS_AT_COMMIT); > > > > CallableStatement proc =3D con.prepareCall("{ ? =3D call > > \"queryStockAnalysisSingle\" ( ?,?,?,? ) }" > > ,ResultSet.CONCUR_READ_ONLY,ResultSet.HOLD_CURSORS_OVER_COMMIT); > > > > None of them seem to work. Has anybody faced this problem? And > > hopefully cone up with a usable fix. > > The application has a commons-dbcp maintaining a connection pool to > > the database server. The jdbc driver used is the jdbc3. THe database > > version is 7.4.x. It has been deployed on a Debian Linux system with > > kernel version 2.6.8. > > > > Thanks > > Akhil Srinivasan > > > > > > >=20 > ------=_Part_10465_33474128.1119283506525 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline I saw the faq, but its not a simple temporary table so formatting and building the create and insert strings are non trivial tasks. I would rather not have to do it unless i really need to. And i still get "the table already exists error" even with the execute.

I have tried another approach after reading a mailing list posting.

    tempTableExists:=3D0;

SELECT * INTO rectable FROM PG_TABLES WHERE SCHEMANAME LIKE \'pg_temp_%\' and tablename like \'%TEMQUERYPERSONALPPORTFOLIOLIST%\' ;
   
    IF rectable.tablename like \'%TEMQUERYPERSONALPPORTFOLIO= LIST%\' THEN
        tempTableExists:=3D1;
    END IF;

    if tempTableExists =3D 0 then
    CREATE TEMPORARY TABLE "TEMQUERYPERSONALPPORTFOLIOL= IST"
    (
        "PORTFOLIOID" INTEGER,
        "NAME"    VA= RCHAR,
        "ACCOUNTID" INTEGER,
        "CONTESTID" INTEGER,
        "CASHAMOUNT" INTEGER,
        "CURRENTVALUE" INTEGER,
        "STATUS" VARCHAR
    )without oids on commit delete rows;
    end if ;

with a commit being called at the end after retrieving the resultset. This seems to be working so far and i have done a couple of tests with multiple users and they have gone fine. Does anybody see anything that can cause a problem here.

In this i check to see if the table is already created, if so i do a pretty much blanket delete. I believe that stored procedure call is atomic so multiple calls to the same procedure should be serialized which works for me here as there can be only one temporary table.

I might try on commit drop, but i was curious as to how reliable it is. Because that should remove any need  for a temporary table check. Hopefully it would also become more reliable.

Any ideas or improvements would be welcome.

Thanks
Akhil Srinivasan

PS: I have another problem buts thats coming next.

On 6/20/05, David Gagnon <dgagnon@siun= ik.com> wrote:
Hi,

  Temporary table a tricky in postgresql you must use:=
  EXECUTE ''
    CREATE TEMP TABLE TMP= _IC (
        ICNUM VARCHAR(20) = primary key
    ) ON COMMIT DROP'';


And f= or your sql :
  statement :=3D ''
      INSERT INTO TMP_IC
  &= nbsp;   SELECT ICNUM FROM IC'';
EXECUTE STATEMENT;
The problem is that the SQL planner keeps the plan for deleted temp
tab= le.  Using the EXECUTE statement avoid caching in the planner.&nb= sp; This
is the official way to use temp table in postgresql.  Look in= the FAQ (I
think,)  everything is explained there.

Cia= o
/David




Akhil Srinivasan wrote:

> Hi
&g= t; I have a an application that uses a lot of stored functions, some of
> them which create temporary tables and refcursors that point to th= e
> tables. Something that comes up occasionally are errors like
&= gt;
> This is when we have some code in the function to delete existi= ng
> temporary tables.
> ERROR: relation with OID xxxxx does not = exist.
> ERROR: relation "TEMPSTOCKVALUEALERT" already exis= ts
>
> When we comment that code we get errors like
> ERR= OR: relation "TEMPSTOCKANALYSIS" already exists. This happens eve= ry
> second call to the function.
>
> In the jdbc call code= we have setAutoCommit to false, And I have tried
> a couple of optio= ns when preparing the call  like
>
>   = ;      CallableStatement proc =3D=20 con.prepareCall("{ ? =3D call
> \"queryStockAnalysisSingle\= " ( ?,?,?,? ) }"
>       = ;  ,ResultSet.CONCUR_READ_ONLY,ResultSet.CLOSE_CURSORS_AT_COMMIT);
= >
>         CallableStatem= ent proc =3D=20 con.prepareCall("{ ? =3D call
> \"queryStockAnalysisSingle\= " ( ?,?,?,? ) }"
>       = ;  ,ResultSet.CONCUR_READ_ONLY,ResultSet.HOLD_CURSORS_OVER_COMMIT);>
> None of them seem to work. Has anybody faced this problem? An= d
> hopefully cone up with a usable fix.
> The application has a= commons-dbcp maintaining a connection pool to
> the database server.= The jdbc driver used is the jdbc3. THe database
> version is 7.4.x . It has been deployed on a Debian Linux system with
> kernel version= 2.6.8.
>
> Thanks
> Akhil Srinivasan
>
>
= >


------=_Part_10465_33474128.1119283506525--