Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1aHFI1-0007nY-IJ for pgsql-bugs@arkaria.postgresql.org; Thu, 07 Jan 2016 18:28:01 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1aHFI1-0008Mw-5E for pgsql-bugs@arkaria.postgresql.org; Thu, 07 Jan 2016 18:28:01 +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) (envelope-from ) id 1aGyDX-0005ac-Pu for pgsql-bugs@postgresql.org; Thu, 07 Jan 2016 00:14:16 +0000 Received: from mail-oi0-x22c.google.com ([2607:f8b0:4003:c06::22c]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1aGyDU-0000LS-6b for pgsql-bugs@postgresql.org; Thu, 07 Jan 2016 00:14:14 +0000 Received: by mail-oi0-x22c.google.com with SMTP id l9so276001490oia.2 for ; Wed, 06 Jan 2016 16:14:11 -0800 (PST) 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:content-type; bh=6Pt3mSXPNZE7HdVy334kpRHwdtZuyFxT8IW4OOlQWXs=; b=gP/ayUpzcEY9dYO4SD64MxbfuPLh3+j7auSFMmBndGu1AWN+KgMN/8Qgs95l4M15IQ FdFAhGZ88hwOKlXpOcu0zwJcrV6W2eYyiV/25JADKMwnEFFTHArFPMuXBnhlxSGvpIb4 r4Yoy1LiD1pym916zI9xUNId/uhkUQ8v8VexQ= 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:content-type; bh=6Pt3mSXPNZE7HdVy334kpRHwdtZuyFxT8IW4OOlQWXs=; b=GvH+6lITJNUbh+XlakMP9M7njVSXRwQoQz5UgmKP4YNIgQGoNBO6gVgU95ZVYcHKVs tXKSuiArnkc3BTEA3ISo6GRCXMrv/Y0CduUt9hoybWuhGgXPr60vrRRScg8+zfRJO5RX giwnrIVM5Wqg9Rpwcd9bLUPpajxXHEt9u0QD7Bo64OduB5AmZZJDgaTyxnG8BvUesnzQ TXL9FocWrhuerpGbpm14rLcIfhTRg+5B7rr1zuAifxzYmXutmr1/tct0FWlVGT/HG48H BHCfZRJ2s4nOFCzmooEVXYocVM9Iu4V7rf0/QiwdWC5s5Lb9OCqntlgtMNtWRZIBGqcx gLhA== X-Gm-Message-State: ALoCoQkmh1F0sZUJ3h+mh3SLYZpmmt/VH2fmuJX2hmvwR3NGdsZyfRVCwkau1ACx06SVYvh7OgHkslLZc8zJm9HKNzbtVj7b8Q== X-Received: by 10.202.200.79 with SMTP id y76mr64939210oif.111.1452125650920; Wed, 06 Jan 2016 16:14:10 -0800 (PST) Received: from ?IPv6:::ffff:192.168.1.7? (71-218-214-23.hlrn.qwest.net. [71.218.214.23]) by smtp.gmail.com with ESMTPSA id o63sm40294945oif.21.2016.01.06.16.14.09 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 06 Jan 2016 16:14:09 -0800 (PST) Message-ID: <568dadd1.42beca0a.8c605.fffff6eb@mx.google.com> MIME-Version: 1.0 To: Andres Freund Cc: Peter Geoghegan , pgsql-bugs From: Paul Subject: Re: BUG #13846: INSERT ON CONFLICT consumessequencersonconflicts Date: Wed, 6 Jan 2016 17:14:07 -0700 Importance: normal X-Priority: 3 In-Reply-To: <20160106221130.GG7650@awork2.anarazel.de> 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> Content-Type: multipart/alternative; boundary="_BA4AD882-9B87-47F9-9C39-17A63BA5422B_" 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 --_BA4AD882-9B87-47F9-9C39-17A63BA5422B_ Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Hi Andres, appreciate your replies. I hope you will still read this when yo= u can, as I believe I can make the financial case, and that the solution is= relatively simple. I also believe there=E2=80=99s still some misunderstand= ing to the problem. Maybe you could point me to the right place in the code= . >> Row headers, padding & alignment makes that absolutely different in >> reality. It's like a sub 20% difference. Regarding the space increase going from 10 INTs to 10 BIGINTS per row, usin= g my example of 500 million fact records, and referring to http://www.postg= resql.org/docs/9.5/static/storage-page-layout.html Using 10 INT columns per record, with row-index, row-header, and 64 bit ali= gnment overhead, requires 4B (row Idx) + 24B (row hdr) + 40B (row data) =3D= 68B per row. With a page size of 8192B, minus 24B for the page header, gi= ves 8168 / 68 =3D ~120 rows per page. Using 20 BIGINT columns per record, with row-index, row-header, and 64 bit = alignment overhead, requires 4B (row Idx) + 24B (row hdr) + 80B (row data) = =3D 108B per row. With a page size of 8192B, minus 24B for the page header= , gives 8168 / 108 =3D ~75 rows per page. 1 =E2=80=93 75 / 120 =3D ~38% So its much nearer 40%, and not sub 20%. When dealing in 10s to 100=E2=80= =99s of GBs (there are terabyte warehouses), 38% is significant, and can ha= ve a real financial impact with respect to memory, network, primary and bac= kup storage costs.=20 >> And in pretty much all the cases with sufficient insertion rates you're >> going to want bigints anyway. If there's few rows it doesn't matter >> anyway. This is true for the surrogate key (generated by a sequencer default) of a = Fact table (which comes from operational business transactions, like taking= orders, or web-page visits). As I stated, there are warehouses with billio= ns of fact records, that are just about always sequentially inserted and ha= ve a timestamp holding when they occurred. I have never come across a pract= ical need to do UPSERTs on Fact tables (i.e. transaction tables, event tabl= es, data generated by things occurring in the real world etc.). But the table where the UPSERT is being applied is not the Fact table! It= =E2=80=99s a related Dimension table, and in most cases, the INSERT path wi= ll not be taken!! A Fact table will have columns representing Members along= Dimensions. For example, an Order Fact table holding records for individua= l Order Line Items, would most likely have a Dimension column to represent = the Status of the Line Item; i.e. Open, Shipped, Invoiced, Closed. There wo= uld be a separate Status dimension table, with a SMALLINT column, that woul= d be used in the Fact table to relate an Order Line Item to a particular St= atus. It is the Status Dimension table upon which an UPSERT would be perfor= med for every Order Line Item ETL=E2=80=99d from the OLTP system into the w= arehouse. So, I=E2=80=99m not clear how your above statement applies exactly. >> The fragility comes from the fact that it'd only be used in a subset of >> cases. E.g. if a BEFORE trigger is present the to-be-inserted rows needs >> to be "materialized" to be presented to the trigger. (I=E2=80=99m assuming you mean the BEFORE INSERT trigger, and I=E2=80=99m n= oting you state =E2=80=98to-be-inserted=E2=80=99 records.) I would then argue that the BEFORE trigger is being applied inappropriately= in the case of an INSERT with an ON CONFLICT clause. First, I think the pu= rpose of the INSERT ON CONFLICT is to implement UPSERTs; would you agree? W= e were always able to implement UPSERT logic before, it just was a pain in = the butt because of concurrency. Before, I would have had a separate INSERT= statement and a separate UPDATE statement. If I had a BEFORE INSERT trigge= r, it would only execute for records that were actually going to be inserte= d. However, you=E2=80=99re now stating that the INSERT ON CONFLICT will exe= cute a BEFORE INSERT trigger even for records that are 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? >> Wrong. Rows with sequences can very well conflict, there's nothing >> forcing sequences to always be used. My oversight, you are correct here, should an explicit value be provided fo= r a column normally defaulted from a sequencer. But that doesn=E2=80=99t in= validate the basic premise of what I=E2=80=99ve been trying to get across. >> Also note that sequence default values aren't in any way different from = other >> default values, and that relevant pieces of code currently don't know >> whether a default value is a nextval or not. The relevant pieces of code wouldn=E2=80=99t need to know anything about ho= w the default value is computed for any column. The relevant code merely ne= eds to only evaluate default values for only the columns used to resolve co= nflicts, and not all of the columns. Then, for the records that don=E2=80= =99t conflict, evaluate any remaining default expressions, and then call th= e BEFORE INSERT trigger with only the records that are actually going to be= inserted!! I greatly welcome your thoughts, and please point to the right place in the= code. Regards, paul From: Andres Freund Sent: Wednesday, January 6, 2016 3:11 PM To: Paul Cc: Peter Geoghegan; pgsql-bugs Subject: Re: [BUGS] BUG #13846: INSERT ON CONFLICT consumessequencersonconf= licts Hi, On 2016-01-06 15:00:17 -0700, Paul wrote: > I=E2=80=99m looking at math more like a single Fact table having 500 mil= lion > records, with 10 dimension columns. If INTs were used for the > dimension columns, that=E2=80=99s 20GB. > If I had to change those dimension columns to BIGINTs, that=E2=80=99s > 40GB. This can impact how much you can fit into server memory, where > olaps like to live, and just slow down moving stuff around between > memory and disk and over the network and backups, etc. Row headers, padding & alignment makes that absolutely different in reality. It's like a sub 20% difference. And in pretty much all the cases with sufficient insertion rates you're going to want bigints anyway. If there's few rows it doesn't matter anyway. > On a technical note, why is the following flow considered =E2=80=98fragil= e=E2=80=99? The fragility comes from the fact that it'd only be used in a subset of cases. E.g. if a BEFORE trigger is present the to-be-inserted rows needs to be "materialized" to be presented to the trigger. > 1) Evaluate only columns used in conflict_target > a. Conflict-resolving Columns with default nextval() increment the corres= ponding sequencer > i. And in this case, there were never be conflicts by definition, so > ON CONFLICT can always be ignored Wrong. Rows with sequences can very well conflict, there's nothing forcing sequences to always be used. Also note that sequence default values aren't in any way different from oth= er default values, and that relevant pieces of code currently don't know whether a default value is a nextval or not. > 2) If conflict, DO UPDATE > a. If nextval()-defaulted column used in conflict_target, we never get he= re > b. Defaults never evaluated > 3) Else Evaluate remaining columns not used in conflict_target and INSERT > a. Columns with nextval() increment their corresponding sequencer Perhaps read the code, and prototype it? I can tell you that it'd be a significant amount of work, and that I'm personally absolutely not interested in investing significant amounts time into it. But that doesn't have to stop *you*. Anyway, EOD for me. --_BA4AD882-9B87-47F9-9C39-17A63BA5422B_ Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset="utf-8"

Hi Andres, appreciate your replies. = I hope you will still read this when you can, as I believe I can make the f= inancial case, and that the solution is relatively simple. I also believe t= here=E2=80=99s still some misunderstanding to the problem. Maybe you could = point me to the right place in the code.

 = ;

 

<= o:p> 

 

>> Row headers, padding & alignment makes that absolutely= different in

>> reality. It's lik= e a sub 20% difference.

 

Regarding the space increase going from 10 INTs t= o 10 BIGINTS per row, using my example of 500 million fact records, and ref= erring to http://www.postgresql.org/docs/9.5/static/storage-page-layout= .html

 

Using 10 INT columns per record, with row-index, row-header, and 64 bit al= ignment overhead, requires 4B (row Idx) + 24B (row hdr) + 40B (row data) = =3D 68B per row.=C2=A0 With a page size of 8192B, minus 24B for the page he= ader, gives 8168 / 68 =3D ~120 rows per page.

=  

Using 20 BIGINT columns per record, wi= th row-index, row-header, and 64 bit alignment overhead, requires 4B (row I= dx) + 24B (row hdr) + 80B (row data) =3D 108B per row.=C2=A0 With a page si= ze of 8192B, minus 24B for the page header, gives 8168 / 108 =3D ~75 rows p= er page.

 

= 1 =E2=80=93 75 / 120 =3D ~38%

&n= bsp;

So its much nearer 40%, and not sub 20%.= When dealing in 10s to 100=E2=80=99s of GBs (there are terabyte warehouses= ), 38% is significant, and can have a real financial impact with respect to= memory, network, primary and backup storage costs.

 

 

 

 =

>> And in pretty much all the cases wi= th sufficient insertion rates you're

>= ;> going to want bigints anyway. If there's few rows it doesn't matter

>> anyway.

 

This is true for the= surrogate key (generated by a sequencer default) of a Fact table (which co= mes from operational business transactions, like taking orders, or web-page= visits). As I stated, there are warehouses with billions of fact records, = that are just about always sequentially inserted and have a timestamp holdi= ng when they occurred. I have never come across a practical need to do UPSE= RTs on Fact tables (i.e. transaction tables, event tables, data generated b= y things occurring in the real world etc.).

&n= bsp;

But the table where the UPSERT is being = applied is not the Fact table! It=E2=80=99s a related Dimension table, and = in most cases, the INSERT path will not be taken!! A Fact table will have c= olumns representing Members along Dimensions. For example, an Order Fact ta= ble holding records for individual Order Line Items, would most likely have= a Dimension column to represent the Status of the Line Item; i.e. Open, Sh= ipped, Invoiced, Closed. There would be a separate Status dimension table, = with a SMALLINT column, that would be used in the Fact table to relate an O= rder Line Item to a particular Status. It is the Status Dimension table upo= n which an UPSERT would be performed for every Order Line Item ETL=E2=80=99= d from the OLTP system into the warehouse.

&nb= sp;

So, I=E2=80=99m not clear how your above = statement applies exactly.

 

 

 

>> The fragility comes from the fact that = it'd only be used in a subset of

>>= ; cases. E.g. if a BEFORE trigger is present the to-be-inserted rows needs<= o:p>

>> to be "materialized" = to be presented to the trigger.

(I=E2=80=99m assumi= ng you mean the BEFORE INSERT trigger, and I=E2=80=99m noting you state =E2= =80=98to-be-inserted=E2=80=99 records.)

 =

I would then argue that the BEFORE trigger i= s being applied inappropriately in the case of an INSERT with an ON CONFLIC= T clause. First, I think the purpose of the INSERT ON CONFLICT is to implem= ent UPSERTs; would you agree? We were always able to implement UPSERT logic= before, it just was a pain in the butt because of concurrency. Before, I w= ould have had a separate INSERT statement and a separate UPDATE statement. = 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?

 

=  

 

>> Wrong. Rows with sequences can very well conflict, there's not= hing

>> forcing sequences to alway= s be used.

 

My oversight, you are correct here, should an explicit value be provided = for a column normally defaulted from a sequencer. But that doesn=E2=80=99t = invalidate the basic premise of what I=E2=80=99ve been trying to get across= .

 

&n= bsp;

 

>> Also note that sequence default values aren't in any way differe= nt from other

>> default values, a= nd that relevant pieces of code currently don't know

>> whether a default value is a nextval or not.

 

The re= levant pieces of code wouldn=E2=80=99t need to know anything about how the = default value is computed for any column. The relevant code merely needs to= only evaluate default values for only the columns used to resolve conflict= s, and not all of the columns. Then, for the records that don=E2=80=99t con= flict, evaluate any remaining default expressions, and then call the BEFORE= INSERT trigger with only the records that are actually going to be inserte= d!!

 

=  

I greatly welcome your thoughts, and p= lease point to the right place in the code.

&n= bsp;

Regards,

paul

 


From: Andres Freund
Sent: Wednesday, January 6, 20= 16 3:11 PM
To: PaulCc: Peter Geoghegan; pgsql-bugs
Subject: Re:= [BUGS] BUG #13846: INSERT ON CONFLICT consumessequencersonconflicts

 

Hi,

<= p class=3DMsoNormal> 

On 2016-01-06= 15:00:17 -0700, Paul wrote:

> I=E2=80=99m looki= ng at math more like a single Fact table having=C2=A0 500 million

> records, with 10 dimension columns. If INTs were used f= or the

> dimension columns, that=E2=80=99s 20GB.=

 

> If = I had to change those dimension columns to BIGINTs, that=E2=80=99s

> 40GB. This can impact how much you can fit into server= memory, where

> olaps like to live, and just sl= ow down moving stuff around between

> memory and= disk and over the network and backups, etc.

&= nbsp;

Row headers, padding & alignment ma= kes that absolutely different in

reality. It's like= a sub 20% difference.

 

And in pretty much all the cases with sufficient insertion ra= tes you're

going to want bigints anyway. If there's= few rows it doesn't matter

anyway.

 

 

> On a technical note, why is the following flow consi= dered =E2=80=98fragile=E2=80=99?

 <= /p>

The fragility comes from the fact that it'd only be= used in a subset of

cases. E.g. if a BEFORE trigge= r is present the to-be-inserted rows needs

to be &q= uot;materialized" to be presented to the trigger.

 

 

> 1) Evaluate only columns used in conflict_target

> a. Conflict-resolving Columns with default nextval() i= ncrement the corresponding sequencer

> i. And in= this case, there were never be conflicts by definition, so

> ON CONFLICT can always be ignored

 

Wrong. Rows with sequences can very = well conflict, there's nothing

forcing sequences to= always be used.

 

Also note that sequence default values aren't in any way different = from other

default values, and that relevant pieces= of code currently don't know

whether a default val= ue is a nextval or not.

 

> 2) If conflict, DO UPDATE

> = a. If nextval()-defaulted column used in conflict_target, we never get here=

> b. Defaults never evaluated

> 3) Else Evaluate remaining columns not used in conflict_target = and INSERT

> a. Columns with nextval() increment= their corresponding sequencer

 

Perhaps read the code, and prototype it? I can tell y= ou that it'd be a

significant amount of work, and t= hat I'm personally absolutely not

interested in inv= esting significant amounts time into it. But that

d= oesn't have to stop *you*.

 

 

Anyway, EOD for= me.

 

= --_BA4AD882-9B87-47F9-9C39-17A63BA5422B_--