Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ayo4E-0006dV-2z for pgsql-bugs@arkaria.postgresql.org; Fri, 06 May 2016 22:17:50 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1ayo4D-0002Gs-Lb for pgsql-bugs@arkaria.postgresql.org; Fri, 06 May 2016 22:17:49 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1aynSw-0007UB-RR for pgsql-bugs@postgresql.org; Fri, 06 May 2016 21:39:19 +0000 Received: from mail-io0-x232.google.com ([2607:f8b0:4001:c06::232]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1aynSt-0004Ck-0Z for pgsql-bugs@postgresql.org; Fri, 06 May 2016 21:39:17 +0000 Received: by mail-io0-x232.google.com with SMTP id u185so145889825iod.3 for ; Fri, 06 May 2016 14:39:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=salesintel.com; s=google; h=message-id:mime-version:to:cc:from:subject:date:importance :in-reply-to:references; bh=2HoZUJhEsg7biFjJjc51R7qDY9XTND8Byn9Wk8Ukda4=; b=KIwL7Y1uFVa72ovN0fnmSlvxOTvTuuK6ug/KUWWWnGQtGNTR5bhG7vVf0ngDGn7LPk eduDxwgClJgYMrK9N0DN4pbfSRXqLhMuogCXm5S0OIO9dC2kiOBISuw+KwHb+xHrxcBr aJEuxAgmceq4mGAevaJ1e3PowAiv2paLiDW/k= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:message-id:mime-version:to:cc:from:subject:date :importance:in-reply-to:references; bh=2HoZUJhEsg7biFjJjc51R7qDY9XTND8Byn9Wk8Ukda4=; b=bLCkCFdCH7VmM775l5t/oYz2NGOQMgh4b6VDjkMQZu0y8t7TKy0G47sL4eptxlJOdt E5URPk2A2vo37kI3Mr6TJwrq7x8z+jkSJqOr0sEE8NSRMAGvikGd5NhD5OLgsIHbspty HTvNhuLFwGWCU6K6peEYK/OnmR5TkOMi5niacgDLFese4Uj2EOH70y/ngGsOm3gCm3P0 h/QyW9wi+iFXBxxf/3j4oBcGx8C2TipFvk57VBjo4YfLt9L0PqycBDw4L6gDKxOpmjtf vOBDuaTmMPpvqjMsaxEPAR81lBPGxkKwtf+kqBxsGLf2AaO8ZaadJLphTRdFJpSJmvp6 QBzQ== X-Gm-Message-State: AOPr4FWuz/560K4kK6KPGJX6TxRrFHsqqMm7lT0yEgfQ4VkN4xSB5xlUF3TUEMPG0qtobg== X-Received: by 10.107.6.28 with SMTP id 28mr27273318iog.18.1462570753827; Fri, 06 May 2016 14:39:13 -0700 (PDT) Received: from ?IPv6:::ffff:192.168.1.5? (75-171-210-80.hlrn.qwest.net. [75.171.210.80]) by smtp.gmail.com with ESMTPSA id u3sm5138433igq.15.2016.05.06.14.39.12 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 06 May 2016 14:39:12 -0700 (PDT) Message-ID: <572d0f00.e33a320a.57866.ffffdf07@mx.google.com> MIME-Version: 1.0 To: "David G. Johnston" Cc: Andres Freund , Peter Geoghegan , pgsql-bugs From: Paul Subject: Re: BUG #13846: INSERT ONCONFLICTconsumessequencersonconflicts Date: Fri, 6 May 2016 15:39:00 -0600 Importance: normal X-Priority: 3 In-Reply-To: <572cf946.489b320a.429be.ffffd369@mx.google.com> References: <20160105150227.1117.51692@wrigleys.postgresql.org> <20160105161326.GD7650@awork2.anarazel.de> <20160106181946.GE7650@awork2.anarazel.de> <568d8060.291cb60a.e8a06.ffff9d00@mx.google.com> <568d8e74.d1a9ca0a.d9558.ffff9026@mx.google.com> <20160106221130.GG7650@awork2.anarazel.de> <568dadd1.42beca0a.8c605.fffff6eb@mx.google.com> <572cf946.489b320a.429be.ffffd369@mx.google.com> Content-Type: multipart/alternative; boundary="_2914D964-3185-4E86-890F-684BA6ACCD92_" X-Pg-Spam-Score: -2.7 (--) 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 --_2914D964-3185-4E86-890F-684BA6ACCD92_ Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Just for clarity on the real financial problem of usability, In our situation, we have =E2=80=98event=E2=80=99 tables, and related =E2= =80=98category=E2=80=99 tables; I=E2=80=99m simplifying and generalizing a = bit. The event tables capture streams of activity. Each record has several = columns that categorize each event. Sometimes new categories come along fro= m the raw event data source, but several categories would never have more t= han a couple hundred discreet values, if that. We also need to filter, grou= p, and aggregate the events along some of those categories. So structure is something like this (although with more category columns) CREATE TABLE event ( id BIGSERAL NOT NULL, cat_a_id SMALLINT NOT NULL, /* would have to be BIGINT if using IOC */ cat_b_id SMALLINT NOT NULL, /* would have to be BIGINT if using IOC */ measure FLOAT, CONSTRAINT pk_event PRIMARY KEY (id) ); CREATE INDEX ix_event1 ON event USING BTREE(cat_a_id); CREATE INDEX ix_event2 ON event USING BTREE(cat_b_id, cat_a_id); CREATE TABLE cat_a ( id SMALLSERIAL NOT NULL, /* would have to be BIGSERIAL if using IOC */ name TEXT NOT NULL, CONSTRAINT pk_cat_a PRIMARY KEY (id) ); CREATE UNIQUE INDEX ux_cat_a ON EVENT USING BTREE(name); CREATE TABLE cat_b ( id SMALLSERIAL NOT NULL, /* would have to be BIGSERIAL if using IOC */ name TEXT NOT NULL, CONSTRAINT pk_cat_b PRIMARY KEY (id) ); CREATE UNIQUE INDEX ux_cat_b ON EVENT USING BTREE(name); Some =E2=80=98raw=E2=80=99 input source records might look like this =E2=80=9Cso-reg=E2=80=9D, =E2=80=9Copen=E2=80=9D, 10 =E2=80=9Cso-reg=E2=80=9D, =E2=80=9Cstalled=E2=80=9D, 1 =E2=80=9Cno-reg=E2=80=9D, =E2=80=9Cstalled=E2=80=9D, 1 =E2=80=9Cunknown=E2=80=99, =E2=80=9Congoing=E2=80=9D, 1 As part of ingesting the raw event source into the event table, we do an up= sert on the category tables, get the category=E2=80=99s id, then store the = id in the respective category column in the event table. Just in a few months, one of our event tables, having 9 category type colum= ns, is already up over 200M records, and may hit a 1B in a few more months,= and maybe in a year or so over 4B. If we had continued to use INSERT ON CONFLICT [IOC], the cat_a_id and cat_b= _id columns in the event table would have to be changed from SMALLINT to BI= GINT, just because the related id sequences from the cat_a and cat_b tables= are getting consumed on every insert into the event table, even though 99.= 9% of the time nothing is actually being inserted into the cat_a and cat_b = tables. We=E2=80=99re running our stuff in a cloud host, so we=E2=80=99re paying fo= r memory, cpu, SSD storage, HDD storage, and network bandwidth (backups) on= a monthly basis. So if using IOC, our one event table with 9 category columns at 200M record= s goes from 9 * 2 =3D 18 bytes-per-record to 9 * 8 =3D 72 bytes-per-record= . Or, from 3.6GB to 14.4GB! At a 1B events 18GB to 72GB!! And that doesn=E2= =80=99t include the indexes on the event table for certain category columns= and combinations, and that=E2=80=99s just one table in one replica. 56GB extra means we can fit way less of the event rows into memory, it take= s up way more expensive SSD space (and HDD for backup), uses way more bandw= idth and just takes longer for backups and replication, and the CPU when do= ing sorts and grouping etc has to compare lots more bits. This can add up t= o thousands of dollars or more over time. Also, if we had kept using IOC and made the category columns just INT, woul= d we have had to convert 4B records from INT to BIGINT sometime next year. = Ouch!! So, are problem with using IOC isn=E2=80=99t at all technical, it=E2=80=99s= financial! Very expensive just to save a few lines of SQL=E2=80=A6 I=E2=80=99m kinda curious what the initial use cases for IOC were? If it ac= tually was for UPSERTs, under what conditions? A huge amount of data is jus= t events and categories; i.e. tweets, emails, sensor readings, purchase ord= ers, etc., and region, person, device, product-brand, etc. Ingesting this k= ind of data is a prime use case for UPSERTs.. Just curious what problem IOC= is solving? Anyways, none of this is meant to be at all a knock on anyone contributing = to PG.. It=E2=80=99s an absolutely amazing amount of high quality value for= free.. Thanks to all of you. From: Paul Sent: Friday, May 6, 2016 2:06 PM To: David G. Johnston Cc: Andres Freund; Peter Geoghegan; pgsql-bugs Subject: RE: [BUGS] BUG #13846: INSERT ONCONFLICTconsumessequencersonconfli= cts Hi Dave,=20 Thanks for the informative reply. We absolutely agree it=E2=80=99s not a bu= g in implementation, but maybe a bug in conceptualization with regard to us= ability. Not knowing the initial motive for the feature, we assumed it was = meant for an intuitively simpler form of the rather common upsert pattern, = but it seems INSERT ON CONFLICT was meant for some other purpose that we do= n=E2=80=99t quite understand. We stopped using and went back to a manual ap= proach, which also works great because PostgreSQL is just kinda great =F0= =9F=98=8A. If any related enhancements where to be done, our =E2=80=98holy grail=E2=80= =99 would be an implementation of the MERGE statement, like this, or this, = or this=E2=80=A6 We can at least ask for it right?? =F0=9F=98=89 Thanks for all your guys=E2=80=99 hard work -p From: David G. Johnston Sent: Friday, May 6, 2016 1:02 PM To: Paul Cc: Andres Freund; Peter Geoghegan; pgsql-bugs Subject: Re: [BUGS] BUG #13846: INSERT ON CONFLICTconsumessequencersonconfl= icts On Wed, Jan 6, 2016 at 5:14 PM, Paul wrote: If I had a BEFORE INSERT trigger, it would only execute for records that we= re actually going to be inserted. However, you=E2=80=99re now stating that = the INSERT ON CONFLICT will execute a BEFORE INSERT trigger even for record= s that are not inserted?? That doesn=E2=80=99t seem quite logical, and cont= rary to =E2=80=98old fashioned=E2=80=99 upsert logic. Does this also mean t= hat the BEFORE UPDATE trigger is always called as well, or is it never call= ed? =E2=80=8BThis seems to boil down to the two possible ways of manually imple= menting UPSERT: UPDATE, if not present, INSERT INSERT, if failing, UPDATE In the later the before insert trigger fires and influences whether the ins= ert=E2=80=8B =C2=A0 =E2=80=8Bfails.=C2=A0 In the former you are already pretty certain the inse= rt will =E2=80=8Bsucceed because the UPDATE found no records. We've implemented INSERT, if failing UPDATE.=C2=A0 The insert has to be att= empted and right now there is no concept of targeted partial deferrability = when constructing the record to be inserted. To solve this situation it is likely that some form of "UPDATE ON MISSING I= NSERT" would need to be designed.=C2=A0 The insert portion would specify "D= EFAULT" for sequence columns and would execute nextval() only if the ON MIS= SING portion is executed. Fundamentally, the difference is that ON MISSING is considerably less compl= icated than ON CONFLICT.=C2=A0 What is wanted here is an ON MISSING interpr= etation but what we've implemented is ON CONFLICT.=C2=A0 It seems that the = hackers are in agreement that our implementation of ON CONFLICT is consiste= nt with its definition.=C2=A0 That it doesn't efficiently solve problems be= tter handled by ON MISSING - while unfortunate - doesn't constitute a bug: = only an opportunity for future enhancement. David J. =E2=80=8B --_2914D964-3185-4E86-890F-684BA6ACCD92_ Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset="utf-8"

Just for clarity on the real financi= al problem of usability,

 

In our situation, we have =E2=80=98event=E2=80=99 tables, a= nd related =E2=80=98category=E2=80=99 tables; I=E2=80=99m simplifying and g= eneralizing a bit. The event tables capture streams of activity. Each recor= d has several columns that categorize each event. Sometimes new categories = come along from the raw event data source, but several categories would nev= er have more than a couple hundred discreet values, if that. We also need t= o filter, group, and aggregate the events along some of those categories.

 

So structu= re is something like this (although with more category columns)

 

CREATE TABLE event (=

=C2=A0 id BIGSERAL NOT NULL,

=C2=A0 cat_a_id SMALLINT NOT NULL,=C2=A0 /* would have to be BIGINT if u= sing IOC */

=C2=A0 cat_b_id SMALLINT NOT NULL, =C2= =A0/* would have to be BIGINT if using IOC */

=C2= =A0 measure FLOAT,

=C2=A0 CONSTRAINT pk_event PRIMA= RY KEY (id)

);

CREATE INDEX= ix_event1 ON event USING BTREE(cat_a_id);

CREATE I= NDEX ix_event2 ON event USING BTREE(cat_b_id, cat_a_id);

 

CREATE TABLE cat_a (

=C2=A0 id SMALLSERIAL NOT NULL, =C2=A0/* would have to be= BIGSERIAL if using IOC */

=C2=A0 name TEXT NOT NUL= L,

=C2=A0 CONSTRAINT pk_cat_a PRIMARY KEY (id)

<= p class=3DMsoNormal>);

CREATE UNIQUE INDEX ux_cat_a= ON EVENT USING BTREE(name);

 =

CREATE TABLE cat_b (

=C2=A0 id SMALLSERIAL NOT NULL,=C2=A0 /* would have to be BIGS= ERIAL if using IOC */

=C2=A0 name TEXT N= OT NULL,

=C2=A0 CONSTRAINT pk_cat_b PRIM= ARY KEY (id)

);

CREATE UNIQUE INDEX ux_cat_b ON EVENT USING BTREE(name);<= /o:p>

 

Som= e =E2=80=98raw=E2=80=99 input source records might look like this

=E2=80=9Cso-reg=E2=80=9D, =E2=80=9Copen=E2=80=9D,= 10

=E2=80=9Cso-reg=E2=80=9D, =E2=80=9Cs= talled=E2=80=9D, 1

=E2=80=9Cno-reg=E2=80= =9D, =E2=80=9Cstalled=E2=80=9D, 1

=E2=80= =9Cunknown=E2=80=99, =E2=80=9Congoing=E2=80=9D, 1

 

As part of ingesting th= e raw event source into the event table, we do an upsert on the category ta= bles, get the category=E2=80=99s id, then store the id in the respective ca= tegory column in the event table.

 =

Just in a few months, one of our event tables, hav= ing 9 category type columns, is already up over 200M records, and may hit a= 1B in a few more months, and maybe in a year or so over 4B.

=

 

If we had co= ntinued to use INSERT ON CONFLICT [IOC], the cat_a_id and cat_b_id columns = in the event table would have to be changed from SMALLINT to BIGINT, just b= ecause the related id sequences from the cat_a and cat_b tables are getting= consumed on every insert into the event table, even though 99.9% of the ti= me nothing is actually being inserted into the cat_a and cat_b tables.=

 

We= =E2=80=99re running our stuff in a cloud host, so we=E2=80=99re paying for = memory, cpu, SSD storage, HDD storage, and network bandwidth (backups) on a= monthly basis.

 

So if using IOC, our one event table with 9 category columns at 200M= records goes from 9 * 2 =3D 18 bytes-per-record to =C2=A09 * 8 =3D 72 byte= s-per-record. Or, from 3.6GB to 14.4GB! At a 1B events 18GB to 72GB!! And t= hat doesn=E2=80=99t include the indexes on the event table for certain cate= gory columns and combinations, and that=E2=80=99s just one table in one rep= lica.

 

56G= B extra means we can fit way less of the event rows into memory, it takes u= p way more expensive SSD space (and HDD for backup), uses way more bandwidt= h and just takes longer for backups and replication, and the CPU when doing= sorts and grouping etc has to compare lots more bits. This can add up to t= housands of dollars or more over time.

 <= /o:p>

Also, if we had kept using IOC and made the c= ategory columns just INT, would we have had to convert 4B records from INT = to BIGINT sometime next year. Ouch!!

 

So, are problem with using IOC isn=E2=80=99t at= all technical, it=E2=80=99s financial! Very expensive just to save a few l= ines of SQL=E2=80=A6

 

I=E2=80=99m kinda curious what the initial use cases for IOC w= ere? If it actually was for UPSERTs, under what conditions? A huge amount o= f data is just events and categories; i.e. tweets, emails, sensor readings,= purchase orders, etc., and region, person, device, product-brand, etc. Ing= esting this kind of data is a prime use case for UPSERTs.. Just curious wha= t problem IOC is solving?

 

Anyways, none of this is meant to be at all a knock on any= one contributing to PG.. It=E2=80=99s an absolutely amazing amount of high = quality value for free.. Thanks to all of you.

 = ;

From: Paul
Sent: Friday, May 6, 2016 2:06 PM
To: = David G. Johnston
= Cc: Andres Freund; Peter Geoghegan; pgsql-bugs
Subject: RE: [BUGS] BUG #13846: I= NSERT ONCONFLICTconsumessequencersonconflicts

=  

Hi Dave,

Thanks for the informative reply. We absolutely agree it=E2=80=99s not a= bug in implementation, but maybe a bug in conceptualization with regard to= usability. Not knowing the initial motive for the feature, we assumed it w= as meant for an intuitively simpler form of the rather common upsert patter= n, but it seems INSERT ON CONFLICT was meant for some other purpose that we= don=E2=80=99t quite understand. We stopped using and went back to a manual= approach, which also works great because PostgreSQL is just kinda great 😊.

=

 

If any relat= ed enhancements where to be done, our =E2=80=98holy grail=E2=80=99 would be= an implementation of the MERGE statement, like this, or this, or this=E2=80=A6 W= e can at least ask for it right?? 😉

 

Thanks for all your guys=E2=80=99 hard work

 

-p

 

From: = David G. Johnston
S= ent: Friday, May 6, 2016 1:02 PM
To: Paul
Cc: Andres Freund; Peter Geoghegan; <= a href=3D"mailto:pgsql-bugs@postgresql.org">pgsql-bugs
Subject: <= /b>Re: [BUGS] BUG #13846: INSERT ON CONFLICTconsumessequencersonconflicts

 

On Wed, Jan 6, 2016 at 5:14 PM, Paul <paul@salesintel.com> wrote:=

If I had= a BEFORE INSERT trigger, it would only execute for records that were actua= lly going to be inserted. However, you=E2=80=99re now stating that the INSE= RT ON CONFLICT will execute a BEFORE INSERT trigger even for records that a= re not inserted?? That doesn=E2=80=99t seem quite logical, and contrary to = =E2=80=98old fashioned=E2=80=99 upsert logic. Does this also mean that the = BEFORE UPDATE trigger is always called as well, or is it never called?=

 

=E2=80=8BThis seems to boil down to the two pos= sible ways of manually implementing UPSERT:

 

UPDATE, if not present, INSERT

INSERT, if failing, UPDATE

 

In the later the before insert trigger fires and influences whether t= he insert=E2=80=8B

 <= span style=3D'font-size:12.0pt;font-family:"Times New Roman",serif'>

=E2=80=8Bfails.  In the former you are a= lready pretty certain the insert will =E2=80=8Bsucceed because the UPDATE f= ound no records.

=  

We've implemented IN= SERT, if failing UPDATE.  The insert has to be attempted and right now= there is no concept of targeted partial deferrability when constructing th= e record to be inserted.

 

To solve this situ= ation it is likely that some form of "UPDATE ON MISSING INSERT" w= ould need to be designed.  The insert portion would specify "DEFA= ULT" for sequence columns and would execute nextval() only if the ON M= ISSING portion is executed.

=  

Fundamentally, the difference i= s that ON MISSING is considerably less complicated than ON CONFLICT.  = What is wanted here is an ON MISSING interpretation but what we've implemen= ted is ON CONFLICT.  It seems that the hackers are in agreement that o= ur implementation of ON CONFLICT is consistent with its definition.  T= hat it doesn't efficiently solve problems better handled by ON MISSING - wh= ile unfortunate - doesn't constitute a bug: only an opportunity for future = enhancement.

 

David J.

 

 

=E2=80=8B

 

<= o:p> 

 

 

= --_2914D964-3185-4E86-890F-684BA6ACCD92_--