pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
General Performance Question
4+ messages / 3 participants
[nested] [flat]

* General Performance Question
@ 2021-11-18 14:15  DAVID ROTH <adaptron@comcast.net>
  0 siblings, 1 reply; 4+ messages in thread

From: DAVID ROTH @ 2021-11-18 14:15 UTC (permalink / raw)
  To: pgsql-general

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 :=            'SELECT name FROM employees';
v_sql := v_sql ||' WHERE employee_number = '||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 = 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?
Thanks
Dave

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: General Performance Question
@ 2021-11-18 14:27  Thomas Kellerer <shammat@gmx.net>
  parent: DAVID ROTH <adaptron@comcast.net>
  0 siblings, 1 reply; 4+ messages in thread

From: Thomas Kellerer @ 2021-11-18 14:27 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

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 :=            'SELECT name FROM employees';
> v_sql := v_sql ||' WHERE employee_number = '||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 = 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?

The query does not use any dynamic parts, so EXECUTE is not needed to begin with.
(not even in the Oracle implementation)

For functions that just encapsulate a SQL query, a "language sql" function might be better:

    CREATE OR REPLACE FUNCTION djr_foo_fnc (p_emp_no integer)
      RETURNS text
    AS
    $$
      SELECT name
      FROM employees
      WHERE employee_number = p_emp_no;
    $$
    language sql
    rows 1;


They are optimized together with the calling function which can be an advantage
but doesn't have to be in all cases.

If the function is called frequently avoiding the overhead of PL/pgSQL can make
a  difference though.






^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: General Performance Question
@ 2021-11-18 14:48  DAVID ROTH <adaptron@comcast.net>
  parent: Thomas Kellerer <shammat@gmx.net>
  0 siblings, 1 reply; 4+ messages in thread

From: DAVID ROTH @ 2021-11-18 14:48 UTC (permalink / raw)
  To: Thomas Kellerer <shammat@gmx.net>; pgsql-general@lists.postgresql.org

Agreed.
The code I am migrating uses the EXECUTE immediate form for almost everything.  The path of least resistance for me is to just copy the code to Postgres 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 optimizer works. Any links you could suggest for this would be appreciated.

> On 11/18/2021 9:27 AM Thomas Kellerer <shammat@gmx.net> wrote:
> 
>  
> 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 :=            'SELECT name FROM employees';
> > v_sql := v_sql ||' WHERE employee_number = '||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 = 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?
> 
> The query does not use any dynamic parts, so EXECUTE is not needed to begin with.
> (not even in the Oracle implementation)
> 
> For functions that just encapsulate a SQL query, a "language sql" function might be better:
> 
>     CREATE OR REPLACE FUNCTION djr_foo_fnc (p_emp_no integer)
>       RETURNS text
>     AS
>     $$
>       SELECT name
>       FROM employees
>       WHERE employee_number = p_emp_no;
>     $$
>     language sql
>     rows 1;
> 
> 
> They are optimized together with the calling function which can be an advantage
> but doesn't have to be in all cases.
> 
> If the function is called frequently avoiding the overhead of PL/pgSQL can make
> a  difference though.





^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: General Performance Question
@ 2021-11-18 15:36  Tom Lane <tgl@sss.pgh.pa.us>
  parent: DAVID ROTH <adaptron@comcast.net>
  0 siblings, 0 replies; 4+ messages in thread

From: Tom Lane @ 2021-11-18 15:36 UTC (permalink / raw)
  To: DAVID ROTH <adaptron@comcast.net>; +Cc: Thomas Kellerer <shammat@gmx.net>; pgsql-general@lists.postgresql.org

DAVID ROTH <adaptron@comcast.net> writes:
> The code I am migrating uses the EXECUTE immediate form for almost everything.  The path of least resistance for me is to just copy the code to Postgres 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.

Yes, absolutely.  (I would've imagined that that habit is pretty awful
for performance on Oracle, too.)

Having said that, the golden rule in such things is to get it to work
first, and make it fast later.  I wouldn't worry about cleaning up
unnecessary EXECUTE usage till you have a working port.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2021-11-18 15:36 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-11-18 14:15 General Performance Question DAVID ROTH <adaptron@comcast.net>
2021-11-18 14:27 ` Thomas Kellerer <shammat@gmx.net>
2021-11-18 14:48   ` DAVID ROTH <adaptron@comcast.net>
2021-11-18 15:36     ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox