Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PL/pgSQL perform vs execute

What are the difference between perform and execute on PL/pgSQL?

From the manual:

Sometimes it is useful to evaluate an expression or SELECT query but discard the result, for example when calling a function that has side-effects but no useful result value. To do this in PL/pgSQL, use the PERFORM statement.

But, when I'm trying something like:

perform 'create table foo as (select 1)'; 

Nothing happens. Although this query should have side effects (creating table), and the result can be discarded.

I think I get 1 thing right: in order to run functions I can use perform:

perform pg_temp.addInheritance(foo); 
like image 993
Guy s Avatar asked Mar 21 '17 07:03

Guy s


People also ask

Why we use perform in PostgreSQL?

PERFORM is plpgsql command used for calls of void functions. PLpgSQL is careful about useless SELECT statements - the SELECT without INTO clause is not allowed. But sometimes you need to call a function and you don't need to store result (or functions has no result). The function in SQL is called with SELECT statement.

What is execute in PostgreSQL?

The PostgreSQL EXECUTE command prepares and runs commands dynamically. The EXECUTE command can also run DDL statements and retrieve data using SQL commands. Similar to SQL Server, you can use the PostgreSQL EXECUTE command with bind variables.

What does := mean in PostgreSQL?

:= is the assignment operator in PL/pgSQL.

Is PLpgSQL fast?

Plpgsql is faster, as you don't have to fetch the data, process them and then submit a new query. All the process is done internally and it is also precompiled which also boosts performance. Moreover when the database is on a remote server and not locally, you will have the network roundtrip delay.


1 Answers

PERFORM is plpgsql command used for calls of void functions. PLpgSQL is careful about useless SELECT statements - the SELECT without INTO clause is not allowed. But sometimes you need to call a function and you don't need to store result (or functions has no result). The function in SQL is called with SELECT statement. But it is not possible in PLpgSQL - so the command PERFORM was introduced.

CREATE OR REPLACE FUNCTION foo() RETURNS void AS $$ BEGIN   RAISE NOTICE 'Hello from void function'; END; $$ LANGUAGE plpgsql;  -- direct call from SQL SELECT foo();  -- in PLpgSQL DO $$ BEGIN   SELECT foo(); -- is not allowed   PERFORM foo(); -- is ok END; $$; 

The PERFORM statements execute a parameter and forgot result.

Your example perform 'create table foo as (select 1)';

is same like SELECT 'create table foo as (select 1)'. It returns a string "create table foo as (select 1)" and this string is discarded.

The EXECUTE statement evaluate a expression to get string. In next step this string is executed.

So EXECUTE 'create table ' || some_var || '(a int)'; has two steps

  1. evaluate expression 'create table ' || some_var || '(a int)'
  2. if some_var is mytab for example, then execute a command create table mytab(a int)

The PERFORM statement is used for function calls, when functions are not used in assignment statement. The EXECUTE is used for evaluation of dynamic SQL - when a form of SQL command is known in runtime.

like image 51
Pavel Stehule Avatar answered Sep 22 '22 18:09

Pavel Stehule