Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bSopm-0000EA-Vz for pgsql-bugs@arkaria.postgresql.org; Thu, 28 Jul 2016 17:10:59 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1bSopm-0005YU-5y for pgsql-bugs@arkaria.postgresql.org; Thu, 28 Jul 2016 17:10:58 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bSoh3-0004KO-6P for pgsql-bugs@postgresql.org; Thu, 28 Jul 2016 17:01:57 +0000 Received: from smtp5.hushmail.com ([65.39.178.142]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bSogu-00084H-9r for pgsql-bugs@postgresql.org; Thu, 28 Jul 2016 17:01:56 +0000 Received: from smtp5.hushmail.com (localhost [127.0.0.1]) by smtp5.hushmail.com (Postfix) with SMTP id 7149320168 for ; Thu, 28 Jul 2016 17:01:44 +0000 (UTC) X-hush-tls-connected: 1 Received: from smtp.hushmail.com (w9.hushmail.com [65.39.178.29]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp5.hushmail.com (Postfix) with ESMTPS; Thu, 28 Jul 2016 17:01:43 +0000 (UTC) Received: by smtp.hushmail.com (Postfix, from userid 99) id 0D9BB4010C; Thu, 28 Jul 2016 17:01:43 +0000 (UTC) MIME-Version: 1.0 Date: Thu, 28 Jul 2016 13:01:42 -0400 To: "Vitaly Burovoy" Cc: pgsql-bugs@postgresql.org Subject: Re: bug error message constraint "con_item_id_costelement" for table "costs" does not exist SQL state: 42704 From: joel.traf@magwerks.com In-Reply-To: References: <20160728151154.AABE64016B@smtp.hushmail.com> Content-Type: multipart/alternative; boundary="=_097a1c25631b212e370a85df5b040e3f" Message-Id: <20160728170143.0D9BB4010C@smtp.hushmail.com> X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --=_097a1c25631b212e370a85df5b040e3f Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="UTF-8" Hi Vitaly In the code below i changed things around shorten names and created code to create the error. I failed to rename the constraint to match all the other changes before submitting the bug. When i ran into this bug it took a bit of digging because several plpgsql functions are being called and this is just one table that a temp table is created to run "what if's" based on user input. Once the transaction ends it discards all the temp tables which are clones of real data. Also ran into another potential bug that i have not written code to create the error Run the below command inside a PLpgsql function it fails stating the table can not be altered as its in use in another transaction. its a temp table created by the function ;-/ create temp table costs ( like xmag.costs including all ); ALTER TABLE public.itemcost ADD CONSTRAINT con_item_id_itemcostelement UNIQUE(itemcost_item_id, itemcost_costelem_id); The solution was to move the Temp Table creation to the client side before all the plpgsql functions are called. I went to using Upsert command because it simplified code and to speed up a function that uses it. To get the Upsert to work i had to add this constraint, to a table that has 5 other constraints that are not included in the below code. Thanks On 7/28/2016 at 11:43 AM, "Vitaly Burovoy" wrote:On 7/28/16, joel.traf@magwerks.com wrote: > Hello PostgreSQL Developers > > Ran into a bug or issue with the documentation where creating a TEMP > table fails to create all the constraints and Upsert fails on check > constraint > > The documentation states "create temp table mytable (including all) " > is suppose to create all the constraints > https://www.postgresql.org/docs/9.5/static/sql-createtable.html > > below is the the code to duplicate this issue. > > I was able to work around the issue by adding the needed constraint > below the create temp command. > > select version() result: "PostgreSQL 9.5.0, compiled by Visual C++ > build 1800, 64-bit" > > ---code to create error. > > CREATE TABLE xmag.costs > ( > cost_id serial primary key, > cost_item_id integer NOT NULL, > cost_costelem_id integer NOT NULL, > cost_lowlevel boolean NOT NULL DEFAULT false, > cost_stdcost numeric(24,8) NOT NULL DEFAULT 0, > cost_posted date, > cost_actcost numeric(24,8) NOT NULL DEFAULT 0, > cost_updated date, > CONSTRAINT con_item_id_costelement UNIQUE (cost_item_id, > cost_costelem_id) > ); > > insert into xmag.costs values > (12083,12210,3,FALSE,16.95000000,'2014-03-25',16.95000000,'2014-03-25'), > (12084,12211,3,FALSE,6.27000000,'2014-03-25',6.27000000,'2014-03-25'), > (12085,12212,3,FALSE,35.36000000,'2014-03-25',35.36000000,'2014-03-25'), > (12088,12215,3,FALSE,31.50000000,'2013-10-25',31.50000000,'2014-07-31'), > (12089,12216,3,FALSE,0.64000000,'2013-06-27',0.64000000,'2014-07-31'), > (12090,12217,3,FALSE,0.00000000,'2013-06-27',0.00000000,'2014-07-31'); > create temp table costs ( like xmag.costs including all ); > --commented out to show bug > --ALTER TABLE public.itemcost > --ADD CONSTRAINT con_item_id_itemcostelement UNIQUE(itemcost_item_id, > itemcost_costelem_id); You have there columns which are present neither in xmag.costs nor in costs. CREATE TABLE ... (LIKE ...) creates indexes and constraints with names as if they weren't given (i.e. by default), so your constraint "con_item_id_costelement" becomes "costs_cost_item_id_cost_costelem_id_key" > insert into costs (select * from costs); The command above does nothing since "costs" has just been creates and has nothing. > Insert into costs > values > > (default,12210,3,FALSE,16.95000000,'2014-03-25',16.95000000,'2014-03-25') > on conflict on constraint con_item_id_costelement Here you can use columns of index, e.g: ON CONFLICT (cost_item_id, cost_costelem_id) DO UPDATE > do update set cost_actcost = matcost, cost_updated = now()::Date, > cost_posted = now()::date, > cost_stdcost = 10 > where cost_item_id= 12210 and costs.cost_costelem_id = 3; > -- Best regards, Vitaly Burovoy --=_097a1c25631b212e370a85df5b040e3f Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset="UTF-8" Hi Vitaly

In th= e code below i changed things around shorten names and created code to crea= te the error.  I failed to rename the constraint to match all the othe= r changes before submitting the bug.

When i ran into this bug it to= ok a bit of digging because several plpgsql functions are being called and = this is just one table that a temp table is created to run "what if's" base= d on user input. 

Once the transaction ends it discards all th= e temp tables which are clones of real data.  

Also ran into a= nother potential bug that i have not written code to create the error  = ; Run the below  command inside a PLpgsql function it fails stating th= e table can not be altered as its in use in another transaction.  its = a temp table created by the function ;-/  

create temp table = costs ( like xmag.costs including all );
ALTER TABLE public.itemcostADD CONSTRAINT con_item_id_itemcostelement UNIQUE(itemcost_item_id, itemco= st_costelem_id);


The solution was to move the  Temp Table c= reation to the client side before all the plpgsql functions are called.
=

I went to using Upsert command because it  simplified code and= to speed  up a function that uses it.

To get the Upsert to wor= k i had to add this constraint, to a table that has 5 other constraints tha= t are not included in the below code. 

Thanks


On 7/28/2016 at 11:43 AM, "Vitaly Burovoy" <vitaly.burovoy@gmail= .com> wrote:
On 7/28/16, joel.traf@magwerks.com <joel.traf@m= agwerks.com> wrote:
> Hello PostgreSQL Developers
>
>= Ran into a bug or issue with the documentation where creating a TEMP
&= gt; table fails to create all the constraints and Upsert fails on check
= > constraint
>
> The documentation states "create temp table= mytable (including all) "
> is suppose to create all the constraints=
> https://www.postgresql.org/docs/9.5/static/sql-createtable.html
>
> below is the the code to duplicate this issue.
><= br>> I was able to work around the issue by adding the needed constraint=
> below the create temp command.
>
> select version() re= sult: "PostgreSQL 9.5.0, compiled by Visual C++
> build 1800, 64-bit"=
>
> ---code to create error.
>
> CREATE TABLE xmag= .costs
> (
> cost_id serial primary key,
> cost_item_= id integer NOT NULL,
> cost_costelem_id integer NOT NULL,
> = cost_lowlevel boolean NOT NULL DEFAULT false,
> cost_stdcost numer= ic(24,8) NOT NULL DEFAULT 0,
> cost_posted date,
> cost_act= cost numeric(24,8) NOT NULL DEFAULT 0,
> cost_updated date,
>= CONSTRAINT con_item_id_costelement UNIQUE (cost_item_id,
> cost_co= stelem_id)
> );
>
> insert into xmag.costs values
>= (12083,12210,3,FALSE,16.95000000,'2014-03-25',16.95000000,'2014-03-25'),> (12084,12211,3,FALSE,6.27000000,'2014-03-25',6.27000000,'2014-03-25'= ),
> (12085,12212,3,FALSE,35.36000000,'2014-03-25',35.36000000,'2014-= 03-25'),
> (12088,12215,3,FALSE,31.50000000,'2013-10-25',31.50000000,= '2014-07-31'),
> (12089,12216,3,FALSE,0.64000000,'2013-06-27',0.64000= 000,'2014-07-31'),
> (12090,12217,3,FALSE,0.00000000,'2013-06-27',0.0= 0000000,'2014-07-31');
> create temp table costs ( like xmag.costs in= cluding all );
> --commented out to show bug
> --ALTER TABLE p= ublic.itemcost
> --ADD CONSTRAINT con_item_id_itemcostelement UNIQUE(= itemcost_item_id,
> itemcost_costelem_id);

You have there colu= mns which are present neither in xmag.costs nor in costs.
CREATE TABLE .= .. (LIKE ...)
creates indexes and constraints with names as if they were= n't given
(i.e. by default), so your
constraint "con_item_id_costelem= ent" becomes
"costs_cost_item_id_cost_costelem_id_key"

> inser= t into costs (select * from costs);

The command above does nothing s= ince "costs" has just been creates and
has nothing.

> Insert i= nto costs
> values
>
> (default,12210,3,FALSE,16.9500= 0000,'2014-03-25',16.95000000,'2014-03-25')
> on conflict on cons= traint con_item_id_costelement

Here you can use columns of index, e.= g:
ON CONFLICT (cost_item_id, cost_costelem_id) DO UPDATE

> = do update set cost_actcost =3D matcost, cost_updated =3D now()::Date,
= > cost_posted =3D now()::date,
> cost_stdcost =3D= 10
> where cost_item_id=3D 12210 and costs.cost_costelem_id = =3D 3;
>

--
Best regards,
Vitaly Burovoy
--=_097a1c25631b212e370a85df5b040e3f--