Movatterモバイル変換


[0]ホーム

URL:



Facebook
Postgres Pro
Facebook
Downloads
38.13. User-defined Operators
Prev UpChapter 38. ExtendingSQLHome Next

38.13. User-defined Operators

Every operator issyntactic sugar for a call to an underlying function that does the real work; so you must first create the underlying function before you can create the operator. However, an operator isnot merely syntactic sugar, because it carries additional information that helps the query planner optimize queries that use the operator. The next section will be devoted to explaining that additional information.

PostgreSQL supports left unary, right unary, and binary operators. Operators can be overloaded; that is, the same operator name can be used for different operators that have different numbers and types of operands. When a query is executed, the system determines the operator to call from the number and types of the provided operands.

Here is an example of creating an operator for adding two complex numbers. We assume we've already created the definition of typecomplex (seeSection 38.12). First we need a function that does the work, then we can define the operator:

CREATE FUNCTION complex_add(complex, complex)    RETURNS complex    AS 'filename', 'complex_add'    LANGUAGE C IMMUTABLE STRICT;CREATE OPERATOR + (    leftarg = complex,    rightarg = complex,    function = complex_add,    commutator = +);

Now we could execute a query like this:

SELECT (a + b) AS c FROM test_complex;        c----------------- (5.2,6.05) (133.42,144.95)

We've shown how to create a binary operator here. To create unary operators, just omit one ofleftarg (for left unary) orrightarg (for right unary). Thefunction clause and the argument clauses are the only required items inCREATE OPERATOR. Thecommutator clause shown in the example is an optional hint to the query optimizer. Further details aboutcommutator and other optimizer hints appear in the next section.


Prev Up Next
38.12. User-defined Types Home 38.14. Operator Optimization Information
epubpdf
Go to PostgreSQL 11
By continuing to browse this website, you agree to the use of cookies. Go toPrivacy Policy.

[8]ページ先頭

©2009-2025 Movatter.jp