PostgreSQL

Скачать в pdf «PostgreSQL»
19.3    Register the New Functions


Now that you have created a dynamically linkable object file, you must register its functions with PostgreSQL. The create function command registers a new function by storing information in the database. Figure
19.2 shows the create FUNCTION command for ctaf. The function ctaf takes


Nome operating systems may use gmake rather than make. Also, some operating systems will use regress.o rather than regress.so.


test=> CREATE FUNCTION ctof(float) test-> RETURNS float test-> AS ‘/users/pgman/sample/ctof.so’ test-> LANGUAGE ‘C’;


CREATE


Figure 19.2: Create function ctaf


test=> SELECT ctof(20); ctof


68


(1 row)


Figure 19.3: Calling function ctaf


a float argument and returns a float. The SQL data type float is the same as the C type double used in ctaf(). The dynamically linkable object file is specified as /users/pgman/sample/ctof.so and is written in the C language.


A single object file can contain many functions. You must use create function to register each function you want to access from PostgreSQL. Create function also allows nonobject files to be used as functions (see Chapter
18).


Once the functions are registered, they can be called just like PostgreSQL internal functions. Figure 19.3 shows the ctaf() function used in a SELECT statement. See CREATE_FUNCTION for more information.

19.4 Create Operators, Types, and Aggregates


Optionally, you can build operators, types, and aggregates using functions. CREATE OPERATOR, CREATE TYPE, and CREATE AGGREGATE register that a set of functions should behave as an operator, type, or aggregate. They name the new operator, type, or aggregate, and then call the supplied function whenever its name is accessed. See CREATE_OPERATOR, CREATE_TYPE, and CREATE_-AGGREGATE for more information.

Скачать в pdf «PostgreSQL»