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 1mniiY-00084M-Ah for pgsql-general@arkaria.postgresql.org; Thu, 18 Nov 2021 14:48:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mniiX-00036c-9L for pgsql-general@arkaria.postgresql.org; Thu, 18 Nov 2021 14:48:49 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mniiW-00036S-Uz for pgsql-general@lists.postgresql.org; Thu, 18 Nov 2021 14:48:48 +0000 Received: from resqmta-a1p-077438.sys.comcast.net ([2001:558:fd01:2bb4::4]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mniiT-0004J4-O6 for pgsql-general@lists.postgresql.org; Thu, 18 Nov 2021 14:48:48 +0000 Received: from resomta-ch2-06v.sys.comcast.net ([69.252.207.102]) by resqmta-a1p-077438.sys.comcast.net with ESMTP id nhZQmyepQQ8E6niiPmYOk7; Thu, 18 Nov 2021 14:48:41 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=comcast.net; s=20190202a; t=1637246921; bh=B+oUdP0+QT3aPVdTrTPBnZaHa33HALDfNKdpZu/YQ6k=; h=Received:Received:Date:From:To:Message-ID:Subject:MIME-Version: Content-Type; b=xOKTiFJTY9lSvD8x24xqr09SXKknZeDXE6F79Tih7QI4aNnkrKGMaHkJ9dlDcGV+z iXxGTav+xLM/us3K+hiLyk+ngoAlYeyRgOSFHVqaUQRfRMWHfwaNAny7I6wxTsDPQN 5WPhU1Q6uKD5El7neF8bGINYb50bcnfgeegQ74pTAO4EiDuc9OVYNuZPcvhOUzvGhE TzAYqrSrKX5EmxVGQtwBXFlHXtH1IhLemEya1u+iG4XJSIrz2cMvXPDmjn6/FOUAU8 Ffiun+vQU7T3wc1kHU4zh2ibjGkapa/XnXmLU1Gope4iXv8ahUqsBB8DTaAAhz5M+4 pwQgOLe+gjRPg== Received: from oxapp-ch2f-13o.email.comcast.net ([96.117.82.148]) by resomta-ch2-06v.sys.comcast.net with ESMTPS id niiOmS3iKydrhniiPmGM0s; Thu, 18 Nov 2021 14:48:41 +0000 X-Xfinity-VAAS: gggruggvucftvghtrhhoucdtuddrgedvuddrfeeigdeigecutefuodetggdotefrodftvfcurfhrohhfihhlvgemucevohhmtggrshhtqdftvghsihdpqfgfvfdppffquffrtefokffrnecuuegrihhlohhuthemuceftddunecunecujfgurhepfffhvffkjghfufggtgfgrfgkofhisehtqhgstdertdejnecuhfhrohhmpeffteggkfffucftqffvjfcuoegruggrphhtrhhonhestghomhgtrghsthdrnhgvtheqnecuggftrfgrthhtvghrnhepuefhgfejtedvteegjeehhfelheeviedvfedvhfdtieefuedugeeludehteefffeinecukfhppeeliedruddujedrkedvrddugeekpddviedtudemudeludemkeegtddtmeguvdgrtdemrgdumeekjeegjeemuggstgemugdvugegnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehhvghlohepohigrghpphdqtghhvdhfqddufehordgvmhgrihhlrdgtohhmtggrshhtrdhnvghtpdhinhgvthepleeirdduudejrdekvddrudegkedpmhgrihhlfhhrohhmpegruggrphhtrhhonhestghomhgtrghsthdrnhgvthdprhgtphhtthhopehshhgrmhhmrghtsehgmhigrdhnvghtpdhrtghpthhtohepphhgshhqlhdqghgvnhgvrhgrlheslhhishhtshdrphhoshhtghhrvghsqhhlrdhorhhg X-Xfinity-VMeta: sc=0.00;st=legit Date: Thu, 18 Nov 2021 09:48:40 -0500 (EST) From: DAVID ROTH To: Thomas Kellerer , pgsql-general@lists.postgresql.org Message-ID: <966668681.565028.1637246920878@connect.xfinity.com> In-Reply-To: References: <1653563957.563818.1637244913291@connect.xfinity.com> Subject: Re: General Performance Question MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Priority: 3 Importance: Normal X-Mailer: Open-Xchange Mailer v7.10.5-Rev21 X-Originating-IP: 2601:191:8400:d2a0:a1:8747:dbc:d2d4 X-Originating-Client: open-xchange-appsuite List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Agreed. The code I am migrating uses the EXECUTE immediate form for almost everythi= ng. The path of least resistance for me is to just copy the code to Postgr= es and change "EXECUTE IMMEDIATE" to "EXECUTE". I am asking about performance differences to see if it is worth converting = code that does not have any dynamic elements. I am also hoping to get a better understanding of the way the Postgres opti= mizer works. Any links you could suggest for this would be appreciated. > On 11/18/2021 9:27 AM Thomas Kellerer wrote: >=20 > =20 > DAVID ROTH schrieb am 18.11.2021 um 15:15: > > I am working on a large Oracle to Postgres migration. > > The existing code frequently constructs a string and then uses Oracle's= "EXECUTE IMMEDIATE" to run it. > > "EXECUTE" has the same functionality in Postgres. > > > > For example: > > CREATE or REPLACE FUNCTION djr_foo_fnc (p_emp_no IN number) > > RETURN VARCHAR2 > > AS > > v_sql VARCHAR2(1000); > > v_name VARCHAR2(30); > > BEGIN > > v_sql :=3D=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 'SELECT name FROM e= mployees'; > > v_sql :=3D v_sql ||' WHERE employee_number =3D '||p_emp_no; > > EXECUTE IMMEDIATE v_sql INTO v_name; > > RETURN v_name; > > END; > > / > > > > CREATE or REPLACE FUNCTION djr_foo_fnc (p_emp_no IN number) > > RETURN VARCHAR2 > > AS > > v_name VARCHAR2(30); > > BEGIN > > SELECT name INTO v_name FROM employees > > WHERE employee_number =3D p_emp_no; > > RETURN v_name; > > END; > > / > > > > These are oversimplified samples of some very complex queries I need to= migrate. > > > > How does the Postgres optimizer handle these 2 formats? > > Which format is likely to perform better? >=20 > The query does not use any dynamic parts, so EXECUTE is not needed to beg= in with. > (not even in the Oracle implementation) >=20 > For functions that just encapsulate a SQL query, a "language sql" functio= n might be better: >=20 > CREATE OR REPLACE FUNCTION djr_foo_fnc (p_emp_no integer) > RETURNS text > AS > $$ > SELECT name > FROM employees > WHERE employee_number =3D p_emp_no; > $$ > language sql > rows 1; >=20 >=20 > They are optimized together with the calling function which can be an adv= antage > but doesn't have to be in all cases. >=20 > If the function is called frequently avoiding the overhead of PL/pgSQL ca= n make > a difference though.