Movatterモバイル変換


[0]ホーム

URL:



Facebook
Postgres Pro
Facebook
Downloads
41.1. Overview
Prev UpChapter 41. PL/pgSQLSQL Procedural LanguageHome Next

41.1. Overview#

PL/pgSQL is a loadable procedural language for thePostgreSQL database system. The design goals ofPL/pgSQL were to create a loadable procedural language that

Functions created withPL/pgSQL can be used anywhere that built-in functions could be used. For example, it is possible to create complex conditional computation functions and later use them to define operators or use them in index expressions.

InPostgreSQL 9.0 and later,PL/pgSQL is installed by default. However it is still a loadable module, so especially security-conscious administrators could choose to remove it.

SQL is the languagePostgreSQL and most other relational databases use as query language. It's portable and easy to learn. But everySQL statement must be executed individually by the database server.

That means that your client application must send each query to the database server, wait for it to be processed, receive and process the results, do some computation, then send further queries to the server. All this incurs interprocess communication and will also incur network overhead if your client is on a different machine than the database server.

WithPL/pgSQL you can group a block of computation and a series of queriesinside the database server, thus having the power of a procedural language and the ease of use of SQL, but with considerable savings of client/server communication overhead.

This can result in a considerable performance increase as compared to an application that does not use stored functions.

Also, withPL/pgSQL you can use all the data types, operators and functions of SQL.

Functions written inPL/pgSQL can accept as arguments any scalar or array data type supported by the server, and they can return a result of any of these types. They can also accept or return any composite type (row type) specified by name. It is also possible to declare aPL/pgSQL function as acceptingrecord, which means that any composite type will do as input, or as returningrecord, which means that the result is a row type whose columns are determined by specification in the calling query, as discussed inSection 7.2.1.4.

PL/pgSQL functions can be declared to accept a variable number of arguments by using theVARIADIC marker. This works exactly the same way as for SQL functions, as discussed inSection 36.5.6.

PL/pgSQL functions can also be declared to accept and return the polymorphic types described inSection 36.2.5, thus allowing the actual data types handled by the function to vary from call to call. Examples appear inSection 41.3.1.

PL/pgSQL functions can also be declared to return aset (or table) of any data type that can be returned as a single instance. Such a function generates its output by executingRETURN NEXT for each desired element of the result set, or by usingRETURN QUERY to output the result of evaluating a query.

Finally, aPL/pgSQL function can be declared to returnvoid if it has no useful return value. (Alternatively, it could be written as a procedure in that case.)

PL/pgSQL functions can also be declared with output parameters in place of an explicit specification of the return type. This does not add any fundamental capability to the language, but it is often convenient, especially for returning multiple values. TheRETURNS TABLE notation can also be used in place ofRETURNS SETOF.

Specific examples appear inSection 41.3.1 andSection 41.6.1.


Prev Up Next
Chapter 41. PL/pgSQLSQL Procedural Language Home 41.2. Structure ofPL/pgSQL
pdfepub
Go to PostgreSQL 17
By continuing to browse this website, you agree to the use of cookies. Go toPrivacy Policy.

[8]ページ先頭

©2009-2025 Movatter.jp