Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jzLFH-0005A5-E4 for pgsql-general@arkaria.postgresql.org; Sat, 25 Jul 2020 14:33:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jzLFF-0006wT-FQ for pgsql-general@arkaria.postgresql.org; Sat, 25 Jul 2020 14:33:49 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jzKY0-0008IQ-Qt for pgsql-general@lists.postgresql.org; Sat, 25 Jul 2020 13:49:08 +0000 Received: from relay.yourmailgateway.de ([188.68.63.101]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jzKXw-0004AJ-R5 for pgsql-general@postgresql.org; Sat, 25 Jul 2020 13:49:07 +0000 Received: from mors-relay-2502.netcup.net (localhost [127.0.0.1]) by mors-relay-2502.netcup.net (Postfix) with ESMTPS id 4BDS8s5k9vz55xr for ; Sat, 25 Jul 2020 15:49:01 +0200 (CEST) Authentication-Results: mors-relay-2502.netcup.net; dkim=permerror (bad message/signature format) Received: from policy02-mors.netcup.net (unknown [46.38.225.53]) by mors-relay-2502.netcup.net (Postfix) with ESMTPS id 4BDS8s5KwSz54x7 for ; Sat, 25 Jul 2020 15:49:01 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at policy02-mors.netcup.net X-Spam-Flag: NO X-Spam-Score: -2.899 X-Spam-Level: X-Spam-Status: No, score=-2.899 required=6.31 tests=[ALL_TRUSTED=-1, BAYES_00=-1.9, HTML_MESSAGE=0.001] autolearn=ham autolearn_force=no Received: from mxe217.netcup.net (unknown [10.243.12.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by policy02-mors.netcup.net (Postfix) with ESMTPS id 4BDS8s1Q8tz8tHn for ; Sat, 25 Jul 2020 15:49:00 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by mxe217.netcup.net (Postfix) with ESMTP id A72F6AD448 for ; Sat, 25 Jul 2020 15:49:00 +0200 (CEST) Received: from mxe217.netcup.net ([127.0.0.1]) by localhost (mxe217.netcup.net [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id U3U7uuhWDCJg for ; Sat, 25 Jul 2020 15:48:59 +0200 (CEST) Received: from [192.168.0.110] (unknown [46.21.1.226]) by mxe217.netcup.net (Postfix) with ESMTPSA id D07A7A8A6D for ; Sat, 25 Jul 2020 15:48:59 +0200 (CEST) Date: Sat, 25 Jul 2020 15:48:54 +0200 From: Nils Bergmann To: pgsql-general@postgresql.org Message-ID: <03e76a96-bf36-4773-8272-cc29bd786386@Spark> References: Subject: Format generation_expression X-Readdle-Message-ID: 03e76a96-bf36-4773-8272-cc29bd786386@Spark MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="5f1c384b_2eb141f2_1c2" X-NC-CID: s6PJ4ROcSX2gDTSIdlCyOxfvFtbQmSFhWOEcjWihSadL7YWx X-NC-CID: 8AizwNHxLalUeKM3auCMzimnlcaQNIPMZ3ARcifeZVAumExu List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --5f1c384b_2eb141f2_1c2 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Hi, Currently, I have a problem. I am working on a project with generated col= umns. I try to implement the generated column feature in the typeorm proj= ect. The thing is, I don't want to drop and add a generated column every = time the application starts. Therefore I need a way to check whether a ge= nerated column expression has changed. With=C2=A0select=C2=A0generation=5Fexpression=C2=A0from=C2=A0information=5F= schema.=22columns=22 c=C2=A0where=C2=A0c.generation=5Fexpression=C2=A0is=C2= =A0not=C2=A0null;=C2=A0I can retrieve the saved generation expression. Ho= wever, It seems like Postgres optimizes or changes it. Therefore, it is d= ifferent. Example: I add a column like this: alter=C2=A0table=C2=A0company=C2=A0add=C2=A0tsv tsvector=C2=A0generated=C2= =A0always=C2=A0as=C2=A0(setweight(to=5Ftsvector('german',=C2=A0coalesce(n= ame, '')), 'A') =7C=7C=C2=A0setweight(jsonb=5Fto=5Ftsvector('german', key= words::jsonb, '=5B=22string=22=5D'),'A'))=C2=A0stored; Now if I try to retrieve the expression with=C2=A0select=C2=A0c.generatio= n=5Fexpression=C2=A0from=C2=A0information=5Fschema.=22columns=22 c=C2=A0w= here=C2=A0c.column=5Fname =3D 'tsv'=C2=A0and=C2=A0c.table=5Fname =3D 'com= pany';=C2=A0it is different. Input expression: setweight(to=5Ftsvector('german',=C2=A0coalesce(title, '')), 'A') =7C=7C=C2= =A0setweight(to=5Ftsvector('german',=C2=A0coalesce(description, '')), 'B'= ); What I got from postgres: (setweight(to=5Ftsvector('german'::regconfig, (COALESCE(name, ''::charact= er varying))::text), 'A'::=22char=22) =7C=7C setweight(jsonb=5Fto=5Ftsvec= tor('german'::regconfig, keywords, '=5B=22string=22=5D'::jsonb), 'A'::=22= char=22)) Postgres formats the query in some way. Is there any way to do this manua= lly=3F Like: magic=5Ffunction=5Fto=5Fformat=5Fsql(=60setweight(to=5Ftsvector('german',= =C2=A0coalesce(title, '')), 'A') =7C=7C=C2=A0setweight(to=5Ftsvector('ger= man',=C2=A0coalesce(description, '')), 'B')=60)=C2=A0Returns:=C2=A0(setwe= ight(to=5Ftsvector('german'::regconfig, (COALESCE(name, ''::character var= ying))::text), 'A'::=22char=22) =7C=7C setweight(jsonb=5Fto=5Ftsvector('g= erman'::regconfig, keywords, '=5B=22string=22=5D'::jsonb), 'A'::=22char=22= )) Then I could check if the saved expression is equal to the formatted one.= I tried to find information in the documentation. But I had no luck. Best regards, Nils Bergmann --5f1c384b_2eb141f2_1c2 Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline
Hi,

Currently, I have a problem. I am working on a project with generated col= umns. I try to implement the generated column feature in the typeorm proj= ect. The thing is, I don't want to drop and add a generated column every = time the application starts. Therefore I need a way to check whether a ge= nerated column expression has changed.&=23160;

With&=23160;select&=23160;generation=5Fexpression&=23160;from&=23160;= information=5Fschema.=22columns=22 c&=23160;where&=23160;c.generation=5Fe= xpression&=23160;is&=23160;not&=23160;null;&=23160;I can retrieve th= e saved generation expression. However, It seems like Postgres optimizes = or changes it. Therefore, it is different.&=23160;&=23160;

Example:

I add a column like this:

alter&=23160;table&=23160;company&=23160;add&=23160;tsv tsvector&=231= 60;generated&=23160;always&=23160;as&=23160;(setweight(to=5Ftsvector('ger= man',&=23160;coalesce(name, '')), 'A') =7C=7C&=23160;setweight(jsonb=5Fto= =5Ftsvector('german', keywords::jsonb, '=5B=22string=22=5D'),'A'))&=23160= ;stored;

Now if I try to retrieve the expression with&=23160;select&=23160;c.g= eneration=5Fexpression&=23160;from&=23160;information=5Fschema.=22columns= =22 c&=23160;where&=23160;c.column=5Fname =3D 'tsv'&=23160;and&=23160;c.t= able=5Fname =3D 'company';&=23160;it is different.&=23160;

Input expression:
setweight(to=5Ftsvector('german',&=23160;coalesce(title, '')), 'A') =7C= =7C&=23160;setweight(to=5Ftsvector('german',&=23160;coalesce(description,= '')), 'B');
What I got from postgres:
(setweight(to=5Ftsvector('german'::regconfig, (COALESCE(name, ''::cha= racter varying))::text), 'A'::=22char=22) =7C=7C setweight(jsonb=5Fto=5Ft= svector('german'::regconfig, keywords, '=5B=22string=22=5D'::jsonb), 'A':= :=22char=22))

Postgres formats the query in some way. Is there any way to do this manua= lly=3F&=23160;
Like:
magic=5Ffunction=5Fto=5Fformat=5Fsql(=60setweight(to=5Ftsvector('germ= an',&=23160;coalesce(title, '')), 'A') =7C=7C&=23160;setweight(to=5Ftsvec= tor('german',&=23160;coalesce(description, '')), 'B')=60)&=23160;Ret= urns:&=23160;(setweight(to=5Ftsvector('german'::regconfig, (COALESCE(= name, ''::character varying))::text), 'A'::=22char=22) =7C=7C setweight(j= sonb=5Fto=5Ftsvector('german'::regconfig, keywords, '=5B=22string=22=5D':= :jsonb), 'A'::=22char=22))

Then I could check if the saved expression is equal to the formatted one.= I tried to find information in the documentation. But I had no luck.&=23= 160;

Best regards,

Nils Bergmann
--5f1c384b_2eb141f2_1c2--