Server IP : 103.119.228.120 / Your IP : 13.59.129.141 Web Server : Apache System : Linux v8.techscape8.com 3.10.0-1160.119.1.el7.tuxcare.els2.x86_64 #1 SMP Mon Jul 15 12:09:18 UTC 2024 x86_64 User : nobody ( 99) PHP Version : 5.6.40 Disable Function : shell_exec,symlink,system,exec,proc_get_status,proc_nice,proc_terminate,define_syslog_variables,syslog,openlog,closelog,escapeshellcmd,passthru,ocinum cols,ini_alter,leak,listen,chgrp,apache_note,apache_setenv,debugger_on,debugger_off,ftp_exec,dl,dll,myshellexec,proc_open,socket_bind,proc_close,escapeshellarg,parse_ini_filepopen,fpassthru,exec,passthru,escapeshellarg,escapeshellcmd,proc_close,proc_open,ini_alter,popen,show_source,proc_nice,proc_terminate,proc_get_status,proc_close,pfsockopen,leak,apache_child_terminate,posix_kill,posix_mkfifo,posix_setpgid,posix_setsid,posix_setuid,dl,symlink,shell_exec,system,dl,passthru,escapeshellarg,escapeshellcmd,myshellexec,c99_buff_prepare,c99_sess_put,fpassthru,getdisfunc,fx29exec,fx29exec2,is_windows,disp_freespace,fx29sh_getupdate,fx29_buff_prepare,fx29_sess_put,fx29shexit,fx29fsearch,fx29ftpbrutecheck,fx29sh_tools,fx29sh_about,milw0rm,imagez,sh_name,myshellexec,checkproxyhost,dosyayicek,c99_buff_prepare,c99_sess_put,c99getsource,c99sh_getupdate,c99fsearch,c99shexit,view_perms,posix_getpwuid,posix_getgrgid,posix_kill,parse_perms,parsesort,view_perms_color,set_encoder_input,ls_setcheckboxall,ls_reverse_all,rsg_read,rsg_glob,selfURL,dispsecinfo,unix2DosTime,addFile,system,get_users,view_size,DirFiles,DirFilesWide,DirPrintHTMLHeaders,GetFilesTotal,GetTitles,GetTimeTotal,GetMatchesCount,GetFileMatchesCount,GetResultFiles,fs_copy_dir,fs_copy_obj,fs_move_dir,fs_move_obj,fs_rmdir,SearchText,getmicrotime MySQL : ON | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /usr/local/ssl/local/ssl/share/doc/postgresql-9.2.24/html/ |
Upload File : |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >Operator Optimization Information</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REV="MADE" HREF="mailto:pgsql-docs@postgresql.org"><LINK REL="HOME" TITLE="PostgreSQL 9.2.24 Documentation" HREF="index.html"><LINK REL="UP" TITLE="Extending SQL" HREF="extend.html"><LINK REL="PREVIOUS" TITLE="User-defined Operators" HREF="xoper.html"><LINK REL="NEXT" TITLE="Interfacing Extensions To Indexes" HREF="xindex.html"><LINK REL="STYLESHEET" TYPE="text/css" HREF="stylesheet.css"><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1"><META NAME="creation" CONTENT="2017-11-06T22:43:11"></HEAD ><BODY CLASS="SECT1" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="5" ALIGN="center" VALIGN="bottom" ><A HREF="index.html" >PostgreSQL 9.2.24 Documentation</A ></TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A TITLE="User-defined Operators" HREF="xoper.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="extend.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="60%" ALIGN="center" VALIGN="bottom" >Chapter 35. Extending <ACRONYM CLASS="ACRONYM" >SQL</ACRONYM ></TD ><TD WIDTH="20%" ALIGN="right" VALIGN="top" ><A TITLE="Interfacing Extensions To Indexes" HREF="xindex.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="SECT1" ><H1 CLASS="SECT1" ><A NAME="XOPER-OPTIMIZATION" >35.13. Operator Optimization Information</A ></H1 ><P > A <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > 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 slow queries, 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. </P ><P > Additional optimization clauses might be added in future versions of <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN >. The ones described here are all the ones that release 9.2.24 understands. </P ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN53816" >35.13.1. <TT CLASS="LITERAL" >COMMUTATOR</TT ></A ></H2 ><P > The <TT CLASS="LITERAL" >COMMUTATOR</TT > 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 <TT CLASS="LITERAL" ><</TT > and <TT CLASS="LITERAL" >></TT > for a particular data type are usually each others' commutators, and operator <TT CLASS="LITERAL" >+</TT > is usually commutative with itself. But operator <TT CLASS="LITERAL" >-</TT > is usually not commutative with anything. </P ><P > The left operand type of a commutable operator is the same as the right operand type of its commutator, and vice versa. So the name of the commutator operator is all that <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > needs to be given to look up the commutator, and that's all that needs to be provided in the <TT CLASS="LITERAL" >COMMUTATOR</TT > clause. </P ><P > It's critical to provide commutator information for operators that will be used in indexes and join clauses, because this allows the query optimizer to <SPAN CLASS="QUOTE" >"flip around"</SPAN > such a clause to the forms needed for different plan types. For example, consider a query with a WHERE clause like <TT CLASS="LITERAL" >tab1.x = tab2.y</TT >, where <TT CLASS="LITERAL" >tab1.x</TT > and <TT CLASS="LITERAL" >tab2.y</TT > are of a user-defined type, and suppose that <TT CLASS="LITERAL" >tab2.y</TT > is indexed. The optimizer cannot generate an index scan unless it can determine how to flip the clause around to <TT CLASS="LITERAL" >tab2.y = tab1.x</TT >, because the index-scan machinery expects to see the indexed column on the left of the operator it is given. <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > will <SPAN CLASS="emphasis" ><I CLASS="EMPHASIS" >not</I ></SPAN > simply assume that this is a valid transformation — the creator of the <TT CLASS="LITERAL" >=</TT > operator must specify that it is valid, by marking the operator with commutator information. </P ><P > 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: <P ></P ></P><UL ><LI ><P > One way is to omit the <TT CLASS="LITERAL" >COMMUTATOR</TT > clause in the first operator that you define, and then provide one in the second operator's definition. Since <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > knows that commutative operators come in pairs, when it sees the second definition it will automatically go back and fill in the missing <TT CLASS="LITERAL" >COMMUTATOR</TT > clause in the first definition. </P ></LI ><LI ><P > The other, more straightforward way is just to include <TT CLASS="LITERAL" >COMMUTATOR</TT > clauses in both definitions. When <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > processes the first definition and realizes that <TT CLASS="LITERAL" >COMMUTATOR</TT > refers to a nonexistent operator, the system will make a dummy entry for that operator in the system catalog. This dummy entry will have valid data only for the operator name, left and right operand types, and result type, since that's all that <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > 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. </P ></LI ></UL ><P> </P ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN53851" >35.13.2. <TT CLASS="LITERAL" >NEGATOR</TT ></A ></H2 ><P > The <TT CLASS="LITERAL" >NEGATOR</TT > 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, <TT CLASS="LITERAL" ><</TT > and <TT CLASS="LITERAL" >>=</TT > are a negator pair for most data types. An operator can never validly be its own negator. </P ><P > Unlike commutators, a pair of unary operators could validly be marked as each other's negators; that would mean (A x) equals NOT (B x) for all x, or the equivalent for right unary operators. </P ><P > An operator's negator must have the same left and/or right operand types as the operator to be defined, so just as with <TT CLASS="LITERAL" >COMMUTATOR</TT >, only the operator name need be given in the <TT CLASS="LITERAL" >NEGATOR</TT > clause. </P ><P > Providing a negator is very helpful to the query optimizer since it allows expressions like <TT CLASS="LITERAL" >NOT (x = y)</TT > to be simplified into <TT CLASS="LITERAL" >x <> y</TT >. This comes up more often than you might think, because <TT CLASS="LITERAL" >NOT</TT > operations can be inserted as a consequence of other rearrangements. </P ><P > Pairs of negator operators can be defined using the same methods explained above for commutator pairs. </P ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN53867" >35.13.3. <TT CLASS="LITERAL" >RESTRICT</TT ></A ></H2 ><P > The <TT CLASS="LITERAL" >RESTRICT</TT > clause, if provided, names a restriction selectivity estimation function for the operator. (Note that this is a function name, not an operator name.) <TT CLASS="LITERAL" >RESTRICT</TT > clauses only make sense for binary operators that return <TT CLASS="TYPE" >boolean</TT >. The idea behind a restriction selectivity estimator is to guess what fraction of the rows in a table will satisfy a <TT CLASS="LITERAL" >WHERE</TT >-clause condition of the form: </P><PRE CLASS="PROGRAMLISTING" >column OP constant</PRE ><P> 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 <TT CLASS="LITERAL" >WHERE</TT > clauses that have this form. (What happens if the constant is on the left, you might be wondering? Well, that's one of the things that <TT CLASS="LITERAL" >COMMUTATOR</TT > is for...) </P ><P > Writing new restriction selectivity estimation functions is far beyond the scope of this chapter, but fortunately you can usually just use one of the system's standard estimators for many of your own operators. These are the standard restriction estimators: <P ></P ><TABLE BORDER="0" ><TBODY ><TR ><TD ><CODE CLASS="FUNCTION" >eqsel</CODE > for <TT CLASS="LITERAL" >=</TT ></TD ></TR ><TR ><TD ><CODE CLASS="FUNCTION" >neqsel</CODE > for <TT CLASS="LITERAL" ><></TT ></TD ></TR ><TR ><TD ><CODE CLASS="FUNCTION" >scalarltsel</CODE > for <TT CLASS="LITERAL" ><</TT > or <TT CLASS="LITERAL" ><=</TT ></TD ></TR ><TR ><TD ><CODE CLASS="FUNCTION" >scalargtsel</CODE > for <TT CLASS="LITERAL" >></TT > or <TT CLASS="LITERAL" >>=</TT ></TD ></TR ></TBODY ></TABLE ><P ></P > It might seem a little odd that these are the categories, but they make sense if you think about it. <TT CLASS="LITERAL" >=</TT > will typically accept only a small fraction of the rows in a table; <TT CLASS="LITERAL" ><></TT > will typically reject only a small fraction. <TT CLASS="LITERAL" ><</TT > will accept a fraction that depends on where the given constant falls in the range of values for that table column (which, it just so happens, is information collected by <TT CLASS="COMMAND" >ANALYZE</TT > and made available to the selectivity estimator). <TT CLASS="LITERAL" ><=</TT > will accept a slightly larger fraction than <TT CLASS="LITERAL" ><</TT > for the same comparison constant, but they're close enough to not be worth distinguishing, especially since we're not likely to do better than a rough guess anyhow. Similar remarks apply to <TT CLASS="LITERAL" >></TT > and <TT CLASS="LITERAL" >>=</TT >. </P ><P > You can frequently get away with using either <CODE CLASS="FUNCTION" >eqsel</CODE > or <CODE CLASS="FUNCTION" >neqsel</CODE > for operators that have very high or very low selectivity, even if they aren't really equality or inequality. For example, the approximate-equality geometric operators use <CODE CLASS="FUNCTION" >eqsel</CODE > on the assumption that they'll usually only match a small fraction of the entries in a table. </P ><P > You can use <CODE CLASS="FUNCTION" >scalarltsel</CODE > and <CODE CLASS="FUNCTION" >scalargtsel</CODE > for comparisons on data types that have some sensible means of being converted into numeric scalars for range comparisons. If possible, add the data type to those understood by the function <CODE CLASS="FUNCTION" >convert_to_scalar()</CODE > in <TT CLASS="FILENAME" >src/backend/utils/adt/selfuncs.c</TT >. (Eventually, this function should be replaced by per-data-type functions identified through a column of the <CODE CLASS="CLASSNAME" >pg_type</CODE > system catalog; but that hasn't happened yet.) If you do not do this, things will still work, but the optimizer's estimates won't be as good as they could be. </P ><P > There are additional selectivity estimation functions designed for geometric operators in <TT CLASS="FILENAME" >src/backend/utils/adt/geo_selfuncs.c</TT >: <CODE CLASS="FUNCTION" >areasel</CODE >, <CODE CLASS="FUNCTION" >positionsel</CODE >, and <CODE CLASS="FUNCTION" >contsel</CODE >. At this writing these are just stubs, but you might want to use them (or even better, improve them) anyway. </P ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN53917" >35.13.4. <TT CLASS="LITERAL" >JOIN</TT ></A ></H2 ><P > The <TT CLASS="LITERAL" >JOIN</TT > clause, if provided, names a join selectivity estimation function for the operator. (Note that this is a function name, not an operator name.) <TT CLASS="LITERAL" >JOIN</TT > clauses only make sense for binary operators that return <TT CLASS="TYPE" >boolean</TT >. The idea behind a join selectivity estimator is to guess what fraction of the rows in a pair of tables will satisfy a <TT CLASS="LITERAL" >WHERE</TT >-clause condition of the form: </P><PRE CLASS="PROGRAMLISTING" >table1.column1 OP table2.column2</PRE ><P> for the current operator. As with the <TT CLASS="LITERAL" >RESTRICT</TT > clause, this helps the optimizer very substantially by letting it figure out which of several possible join sequences is likely to take the least work. </P ><P > As before, this chapter will make no attempt to explain how to write a join selectivity estimator function, but will just suggest that you use one of the standard estimators if one is applicable: <P ></P ><TABLE BORDER="0" ><TBODY ><TR ><TD ><CODE CLASS="FUNCTION" >eqjoinsel</CODE > for <TT CLASS="LITERAL" >=</TT ></TD ></TR ><TR ><TD ><CODE CLASS="FUNCTION" >neqjoinsel</CODE > for <TT CLASS="LITERAL" ><></TT ></TD ></TR ><TR ><TD ><CODE CLASS="FUNCTION" >scalarltjoinsel</CODE > for <TT CLASS="LITERAL" ><</TT > or <TT CLASS="LITERAL" ><=</TT ></TD ></TR ><TR ><TD ><CODE CLASS="FUNCTION" >scalargtjoinsel</CODE > for <TT CLASS="LITERAL" >></TT > or <TT CLASS="LITERAL" >>=</TT ></TD ></TR ><TR ><TD ><CODE CLASS="FUNCTION" >areajoinsel</CODE > for 2D area-based comparisons</TD ></TR ><TR ><TD ><CODE CLASS="FUNCTION" >positionjoinsel</CODE > for 2D position-based comparisons</TD ></TR ><TR ><TD ><CODE CLASS="FUNCTION" >contjoinsel</CODE > for 2D containment-based comparisons</TD ></TR ></TBODY ></TABLE ><P ></P > </P ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN53949" >35.13.5. <TT CLASS="LITERAL" >HASHES</TT ></A ></H2 ><P > The <TT CLASS="LITERAL" >HASHES</TT > clause, if present, tells the system that it is permissible to use the hash join method for a join based on this operator. <TT CLASS="LITERAL" >HASHES</TT > only makes sense for a binary operator that returns <TT CLASS="LITERAL" >boolean</TT >, and in practice the operator must represent equality for some data type or pair of data types. </P ><P > The assumption underlying hash join is that the join operator can only return true for pairs of left and right values that hash to the same hash code. If two values get put in different hash buckets, the join will never compare them at all, implicitly assuming that the result of the join operator must be false. So it never makes sense to specify <TT CLASS="LITERAL" >HASHES</TT > for operators that do not represent some form of equality. In most cases it is only practical to support hashing for operators that take the same data type on both sides. However, sometimes it is possible to design compatible hash functions for two or more data types; that is, functions that will generate the same hash codes for <SPAN CLASS="QUOTE" >"equal"</SPAN > values, even though the values have different representations. For example, it's fairly simple to arrange this property when hashing integers of different widths. </P ><P > To be marked <TT CLASS="LITERAL" >HASHES</TT >, the join operator must appear in a hash index operator family. This is not enforced when you create the operator, since of course the referencing operator family couldn't exist yet. But attempts to use the operator in hash joins will fail at run time if no such operator family exists. The system needs the operator family to find the data-type-specific hash function(s) for the operator's input data type(s). Of course, you must also create suitable hash functions before you can create the operator family. </P ><P > Care should be exercised when preparing a hash function, because there are machine-dependent ways in which it might fail to do the right thing. For example, if your data type is a structure in which there might be uninteresting pad bits, you cannot simply pass the whole structure to <CODE CLASS="FUNCTION" >hash_any</CODE >. (Unless you write your other operators and functions to ensure that the unused bits are always zero, which is the recommended strategy.) Another example is that on machines that meet the <ACRONYM CLASS="ACRONYM" >IEEE</ACRONYM > floating-point standard, negative zero and positive zero are different values (different bit patterns) but they are defined to compare equal. If a float value might contain negative zero then extra steps are needed to ensure it generates the same hash value as positive zero. </P ><P > A hash-joinable operator must have a commutator (itself if the two operand data types are the same, or a related equality operator if they are different) that appears in the same operator family. If this is not the case, planner errors might occur when the operator is used. Also, it is a good idea (but not strictly required) for a hash operator family that supports multiple data types to provide equality operators for every combination of the data types; this allows better optimization. </P ><DIV CLASS="NOTE" ><BLOCKQUOTE CLASS="NOTE" ><P ><B >Note: </B > The function underlying a hash-joinable operator must be marked immutable or stable. If it is volatile, the system will never attempt to use the operator for a hash join. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="NOTE" ><BLOCKQUOTE CLASS="NOTE" ><P ><B >Note: </B > If a hash-joinable operator has an underlying function that is marked strict, the function must also be complete: that is, it should return true or false, never null, for any two nonnull inputs. If this rule is not followed, hash-optimization of <TT CLASS="LITERAL" >IN</TT > operations might generate wrong results. (Specifically, <TT CLASS="LITERAL" >IN</TT > might return false where the correct answer according to the standard would be null; or it might yield an error complaining that it wasn't prepared for a null result.) </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN53971" >35.13.6. <TT CLASS="LITERAL" >MERGES</TT ></A ></H2 ><P > The <TT CLASS="LITERAL" >MERGES</TT > clause, if present, tells the system that it is permissible to use the merge-join method for a join based on this operator. <TT CLASS="LITERAL" >MERGES</TT > only makes sense for a binary operator that returns <TT CLASS="LITERAL" >boolean</TT >, and in practice the operator must represent equality for some data type or pair of data types. </P ><P > Merge join is based on the idea of sorting the left- and right-hand tables into order and then scanning them in parallel. So, both data types must be capable of being fully ordered, and the join operator must be one that can only succeed for pairs of values that fall at the <SPAN CLASS="QUOTE" >"same place"</SPAN > in the sort order. In practice this means that the join operator must behave like equality. But it is possible to merge-join two distinct data types so long as they are logically compatible. For example, the <TT CLASS="TYPE" >smallint</TT >-versus-<TT CLASS="TYPE" >integer</TT > equality operator is merge-joinable. We only need sorting operators that will bring both data types into a logically compatible sequence. </P ><P > To be marked <TT CLASS="LITERAL" >MERGES</TT >, the join operator must appear as an equality member of a <TT CLASS="LITERAL" >btree</TT > index operator family. This is not enforced when you create the operator, since of course the referencing operator family couldn't exist yet. But the operator will not actually be used for merge joins unless a matching operator family can be found. The <TT CLASS="LITERAL" >MERGES</TT > flag thus acts as a hint to the planner that it's worth looking for a matching operator family. </P ><P > A merge-joinable operator must have a commutator (itself if the two operand data types are the same, or a related equality operator if they are different) that appears in the same operator family. If this is not the case, planner errors might occur when the operator is used. Also, it is a good idea (but not strictly required) for a <TT CLASS="LITERAL" >btree</TT > operator family that supports multiple data types to provide equality operators for every combination of the data types; this allows better optimization. </P ><DIV CLASS="NOTE" ><BLOCKQUOTE CLASS="NOTE" ><P ><B >Note: </B > The function underlying a merge-joinable operator must be marked immutable or stable. If it is volatile, the system will never attempt to use the operator for a merge join. </P ></BLOCKQUOTE ></DIV ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="xoper.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="xindex.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >User-defined Operators</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="extend.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Interfacing Extensions To Indexes</TD ></TR ></TABLE ></DIV ></BODY ></HTML >