📄 xoper.sgml
字号:
<Chapter Id="xoper"> <Title>Extending <Acronym>SQL</Acronym>: Operators</Title> <Para> <ProductName>Postgres</ProductName> 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 arguments. If there is an ambiguous situation and the system cannot determine the correct operator to use, it will return an error. You may have to typecast the left and/or right operands to help it understand which operator you meant to use. </Para> <Para> Every operator is "syntactic 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 is <emphasis>not</emphasis> merely syntactic sugar, because it carries additional information that helps the query planner optimize queries that use the operator. Much of this chapter will be devoted to explaining that additional information. </Para> <Para> Here is an example of creating an operator for adding two complex numbers. We assume we've already created the definition of type complex. First we need a function that does the work; then we can define the operator: <ProgramListing>CREATE FUNCTION complex_add(complex, complex) RETURNS complex AS '$PWD/obj/complex.so' LANGUAGE 'c';CREATE OPERATOR + ( leftarg = complex, rightarg = complex, procedure = complex_add, commutator = +); </ProgramListing> </Para> <Para> Now we can do: <ProgramListing>SELECT (a + b) AS c FROM test_complex;+----------------+|c |+----------------+|(5.2,6.05) |+----------------+|(133.42,144.95) |+----------------+ </ProgramListing> </Para> <Para> We've shown how to create a binary operator here. To create unary operators, just omit one of leftarg (for left unary) or rightarg (for right unary). The procedure clause and the argument clauses are the only required items in CREATE OPERATOR. The COMMUTATOR clause shown in the example is an optional hint to the query optimizer. Further details about COMMUTATOR and other optimizer hints appear below. </Para> <sect1> <title>Operator Optimization Information</title> <note> <title>Author</title> <para> Written by Tom Lane. </para> </note> <para> A <ProductName>Postgres</ProductName> operator definition can include several optional clauses that tell the system useful things about how the operator behaves. These clauses should be provided whenever appropriate, because they can make for considerable speedups in execution of queries that use the operator. But if you provide them, you must be sure that they are right! Incorrect use of an optimization clause can result in backend crashes, subtly wrong output, or other Bad Things. You can always leave out an optimization clause if you are not sure about it; the only consequence is that queries might run slower than they need to. </para> <para> Additional optimization clauses might be added in future versions of <ProductName>Postgres</ProductName>. The ones described here are all the ones that release 6.5 understands. </para> <sect2> <title>COMMUTATOR</title> <para> The COMMUTATOR clause, if provided, names an operator that is the commutator of the operator being defined. We say that operator A is the commutator of operator B if (x A y) equals (y B x) for all possible input values x,y. Notice that B is also the commutator of A. For example, operators '<' and '>' for a particular datatype are usually each others' commutators, and operator '+' is usually commutative with itself. But operator '-' is usually not commutative with anything. </para> <para> The left argument type of a commuted operator is the same as the right argument type of its commutator, and vice versa. So the name of the commutator operator is all that <ProductName>Postgres</ProductName> needs to be given to look up the commutator, and that's all that need be provided in the COMMUTATOR clause. </para> <para> When you are defining a self-commutative operator, you just do it. When you are defining a pair of commutative operators, things are a little trickier: how can the first one to be defined refer to the other one, which you haven't defined yet? There are two solutions to this problem: <itemizedlist> <listitem> <para> One way is to omit the COMMUTATOR clause in the first operator that you define, and then provide one in the second operator's definition. Since <ProductName>Postgres</ProductName> knows that commutative operators come in pairs, when it sees the second definition it will automatically go back and fill in the missing COMMUTATOR clause in the first definition. </para> </listitem> <listitem> <para> The other, more straightforward way is just to include COMMUTATOR clauses in both definitions. When <ProductName>Postgres</ProductName> processes the first definition and realizes that COMMUTATOR refers to a non-existent operator, the system will make a dummy entry for that operator in the system's pg_operator table. This dummy entry will have valid data only for the operator name, left and right argument types, and result type, since that's all that <ProductName>Postgres</ProductName> can deduce at this point. The first operator's catalog entry will link to this dummy entry. Later, when you define the second operator, the system updates the dummy entry with the additional information from the second definition. If you try to use the dummy operator before it's been filled in, you'll just get an error message. (Note: this procedure did not work reliably in <ProductName>Postgres</ProductName> versions before 6.5, but it is now the recommended way to do things.) </para> </listitem> </itemizedlist> </para> </sect2> <sect2> <title>NEGATOR</title> <para> The NEGATOR clause, if provided, names an operator that is the negator of the operator being defined. We say that operator A is the negator of operator B if both return boolean results and (x A y) equals NOT (x B y) for all possible inputs x,y. Notice that B is also the negator of A. For example, '<' and '>=' are a negator pair for most datatypes. An operator can never be validly be its own negator. </para> <para> Unlike COMMUTATOR, a pair of unary operators could validly be marked as each others' negators; that would mean (A x) equals NOT (B x) for all x, or the equivalent for right-unary operators. </para> <para> An operator's negator must have the same left and/or right argument types as the operator itself, so just as with COMMUTATOR, only the operator name need be given in the NEGATOR clause. </para> <para> Providing NEGATOR is very helpful to the query optimizer since it allows expressions like NOT (x = y) to be simplified into x <> y. This comes up more often than you might think, because NOTs can be inserted as a consequence of other rearrangements. </para> <para> Pairs of negator operators can be defined using the same methods explained above for commutator pairs. </para> </sect2> <sect2> <title>RESTRICT</title> <para> The RESTRICT clause, if provided, names a restriction selectivity estimation function for the operator (note that this is a function name, not an operator name). RESTRICT clauses only make sense for binary operators that return boolean. The idea behind a restriction selectivity estimator is to guess what fraction of the rows in a table will satisfy a WHERE-clause condition of the form <ProgramListing> field OP constant </ProgramListing> for the current operator and a particular constant value. This assists the optimizer by giving it some idea of how many rows will be eliminated by WHERE clauses that have this form. (What happens if the constant is on the left, you may be wondering? Well, that's one of the things that
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -