Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Am very new in Database development so I have some doubts regarding my following example:

Function f1() - language sql

 create or replace function f1(istr  varchar) returns text as $$ 
 select 'hello! '::varchar || istr;
 $$ language sql;

Function f2() - language plpgsql

 create  or replace function f2(istr  varchar)
 returns text as $$ 
 begin select 'hello! '::varchar || istr; end;
 $$ language plpgsql;
  • Both functions can be called like select f1('world') or select f2('world').

  • If I call select f1('world') the output will be:

    `hello! world`
    
  • And output for select f2('world'):

    ERROR: query has no destination for result data HINT: If you want to discard the results of a SELECT, use PERFORM instead. CONTEXT: PL/pgSQL function f11(character varying) line 2 at SQL statement ****** Error ******

  • I wish to know the difference and in which situations I should use language sql or language plpgsql.

Any useful link or answers regarding functions will much appreciated.

share|improve this question
3  
any usefull link or answers regarding functions will much appreciated Who would ever think about the manual? postgresql.org/docs/current/static/server-programming.html –  Clodoaldo Neto Jul 15 '14 at 10:20
    
@ClodoaldoNeto someone may lazy to find out or not able to find out the correct solutions...!! –  user3814846 Jul 15 '14 at 10:23
1  
@keet You may want to read this chapter of the manual: postgresql.org/docs/current/static/plpgsql.html It contains full description of PL/pgSQL language with examples. –  Igor Romanchenko Jul 15 '14 at 10:29

2 Answers 2

up vote 4 down vote accepted

SQL functions

are the better choice:

  • For simple scalar queries. Not much to plan, better save any overhead.

  • For single calls per session. Nothing to gain from plan caching and prepared statements that PL/pgSQL has to offer. See below.

  • If they are typically called in the context of bigger queries and are simple enough to be inlined.

  • For lack of experience with any procedural language like PL/pgSQL. Many know SQL well and that's about all you need for SQL functions. Few can say the same about PL/pgSQL.

  • A bit shorter code. No block overhead.

PL/pgSQL functions

are the better choice:

share|improve this answer
    
Well said; I forgot to mention dynamic SQL, too. –  Craig Ringer Jul 16 '14 at 4:00

PL/PgSQL is a PostgreSQL-specific procedural language based on SQL. It has loops, variables, error/exception handling, etc. Not all SQL is valid PL/PgSQL - as you discovered, for example, you can't use SELECT without INTO or RETURN QUERY. PL/PgSQL may also be used in DO blocks for one-shot procedures.

sql functions can only use pure SQL, but they're often more efficient and they're simpler to write because you don't need a BEGIN ... END; block, etc. SQL functions may be inlined, which is not true for PL/PgSQL.

People often use PL/PgSQL where plain SQL would be sufficient, because they're used to thinking procedurally. In most cases when you think you need PL/PgSQL you probably actually don't. Recursive CTEs, lateral queries, etc generally meet most needs.

For more info ... see the manual.

share|improve this answer
    
well explained this is what i expected –  user3814846 Jul 15 '14 at 10:45
1  
True, set based solutions are typically superior. But that's no final verdict in whether to use an SQL or a PL/pgSQL function. Newcomers are sometimes using plpgsql for the wrong reasons. –  Erwin Brandstetter Jul 16 '14 at 3:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.