4 41.1.1. Advantages of Using PL/pgSQL
5 41.1.2. Supported Argument and Result Data Types
7 PL/pgSQL is a loadable procedural language for the PostgreSQL database
8 system. The design goals of PL/pgSQL were to create a loadable
9 procedural language that
10 * can be used to create functions, procedures, and triggers,
11 * adds control structures to the SQL language,
12 * can perform complex computations,
13 * inherits all user-defined types, functions, procedures, and
15 * can be defined to be trusted by the server,
18 Functions created with PL/pgSQL can be used anywhere that built-in
19 functions could be used. For example, it is possible to create complex
20 conditional computation functions and later use them to define
21 operators or use them in index expressions.
23 In PostgreSQL 9.0 and later, PL/pgSQL is installed by default. However
24 it is still a loadable module, so especially security-conscious
25 administrators could choose to remove it.
27 41.1.1. Advantages of Using PL/pgSQL #
29 SQL is the language PostgreSQL and most other relational databases use
30 as query language. It's portable and easy to learn. But every SQL
31 statement must be executed individually by the database server.
33 That means that your client application must send each query to the
34 database server, wait for it to be processed, receive and process the
35 results, do some computation, then send further queries to the server.
36 All this incurs interprocess communication and will also incur network
37 overhead if your client is on a different machine than the database
40 With PL/pgSQL you can group a block of computation and a series of
41 queries inside the database server, thus having the power of a
42 procedural language and the ease of use of SQL, but with considerable
43 savings of client/server communication overhead.
44 * Extra round trips between client and server are eliminated
45 * Intermediate results that the client does not need do not have to
46 be marshaled or transferred between server and client
47 * Multiple rounds of query parsing can be avoided
49 This can result in a considerable performance increase as compared to
50 an application that does not use stored functions.
52 Also, with PL/pgSQL you can use all the data types, operators and
55 41.1.2. Supported Argument and Result Data Types #
57 Functions written in PL/pgSQL can accept as arguments any scalar or
58 array data type supported by the server, and they can return a result
59 of any of these types. They can also accept or return any composite
60 type (row type) specified by name. It is also possible to declare a
61 PL/pgSQL function as accepting record, which means that any composite
62 type will do as input, or as returning record, which means that the
63 result is a row type whose columns are determined by specification in
64 the calling query, as discussed in Section 7.2.1.4.
66 PL/pgSQL functions can be declared to accept a variable number of
67 arguments by using the VARIADIC marker. This works exactly the same way
68 as for SQL functions, as discussed in Section 36.5.6.
70 PL/pgSQL functions can also be declared to accept and return the
71 polymorphic types described in Section 36.2.5, thus allowing the actual
72 data types handled by the function to vary from call to call. Examples
73 appear in Section 41.3.1.
75 PL/pgSQL functions can also be declared to return a “set” (or table) of
76 any data type that can be returned as a single instance. Such a
77 function generates its output by executing RETURN NEXT for each desired
78 element of the result set, or by using RETURN QUERY to output the
79 result of evaluating a query.
81 Finally, a PL/pgSQL function can be declared to return void if it has
82 no useful return value. (Alternatively, it could be written as a
83 procedure in that case.)
85 PL/pgSQL functions can also be declared with output parameters in place
86 of an explicit specification of the return type. This does not add any
87 fundamental capability to the language, but it is often convenient,
88 especially for returning multiple values. The RETURNS TABLE notation
89 can also be used in place of RETURNS SETOF.
91 Specific examples appear in Section 41.3.1 and Section 41.6.1.