Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
postgres-lambda-diff
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jakob Huber
postgres-lambda-diff
Commits
2744abb7
Commit
2744abb7
authored
21 years ago
by
Tom Lane
Browse files
Options
Downloads
Patches
Plain Diff
Ooops, missed updating this part of the complex-datatype example.
parent
6fbb14a1
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
doc/src/sgml/xindex.sgml
+55
-25
55 additions, 25 deletions
doc/src/sgml/xindex.sgml
with
55 additions
and
25 deletions
doc/src/sgml/xindex.sgml
+
55
−
25
View file @
2744abb7
<!--
<!--
$Header: /cvsroot/pgsql/doc/src/sgml/xindex.sgml,v 1.3
2
2003/
08/31 17:32:21 petere
Exp $
$Header: /cvsroot/pgsql/doc/src/sgml/xindex.sgml,v 1.3
3
2003/
10/21 23:28:42 tgl
Exp $
-->
-->
<sect1 id="xindex">
<sect1 id="xindex">
...
@@ -408,7 +408,12 @@ $Header: /cvsroot/pgsql/doc/src/sgml/xindex.sgml,v 1.32 2003/08/31 17:32:21 pete
...
@@ -408,7 +408,12 @@ $Header: /cvsroot/pgsql/doc/src/sgml/xindex.sgml,v 1.32 2003/08/31 17:32:21 pete
<para>
<para>
Now that we have seen the ideas, here is the promised example of
Now that we have seen the ideas, here is the promised example of
creating a new operator class. The operator class encapsulates
creating a new operator class.
(You can find a working copy of this example in
<filename>src/tutorial/complex.c</filename> and
<filename>src/tutorial/complex.sql</filename> in the source
distribution.)
The operator class encapsulates
operators that sort complex numbers in absolute value order, so we
operators that sort complex numbers in absolute value order, so we
choose the name <literal>complex_abs_ops</literal>. First, we need
choose the name <literal>complex_abs_ops</literal>. First, we need
a set of operators. The procedure for defining operators was
a set of operators. The procedure for defining operators was
...
@@ -425,40 +430,65 @@ $Header: /cvsroot/pgsql/doc/src/sgml/xindex.sgml,v 1.32 2003/08/31 17:32:21 pete
...
@@ -425,40 +430,65 @@ $Header: /cvsroot/pgsql/doc/src/sgml/xindex.sgml,v 1.32 2003/08/31 17:32:21 pete
</para>
</para>
<para>
<para>
The C code for the equality operator look like this:
The least error-prone way to define a related set of comparison operators
is to write the btree comparison support function first, and then write the
other functions as one-line wrappers around the support function. This
reduces the odds of getting inconsistent results for corner cases.
Following this approach, we first write
<programlisting>
<programlisting>
#define Mag(c) ((c)->x*(c)->x + (c)->y*(c)->y)
#define Mag(c)
((c)->x*(c)->x + (c)->y*(c)->y)
bool
static int
complex_abs_
eq
(Complex *a, Complex *b)
complex_abs_
cmp_internal
(Complex *a, Complex *b)
{
{
double amag = Mag(a), bmag = Mag(b);
double amag = Mag(a),
return (amag == bmag);
bmag = Mag(b);
if (amag < bmag)
return -1;
if (amag > bmag)
return 1;
return 0;
}
}
</programlisting>
</programlisting>
The other four operators are very similar. You can find their code
in <filename>src/tutorial/complex.c</filename> and
Now the less-than function looks like
<filename>src/tutorial/complex.sql</filename> in the source
distribution.
<programlisting>
PG_FUNCTION_INFO_V1(complex_abs_lt);
Datum
complex_abs_lt(PG_FUNCTION_ARGS)
{
Complex *a = (Complex *) PG_GETARG_POINTER(0);
Complex *b = (Complex *) PG_GETARG_POINTER(1);
PG_RETURN_BOOL(complex_abs_cmp_internal(a, b) < 0);
}
</programlisting>
The other four functions differ only in how they compare the internal
function's result to zero.
</para>
</para>
<para>
<para>
Now declare the functions and the operators based on the functions:
Next we declare the functions and the operators based on the functions
to SQL:
<programlisting>
<programlisting>
CREATE FUNCTION complex_abs_eq(complex, complex) RETURNS boolean
CREATE FUNCTION complex_abs_lt(complex, complex) RETURNS bool
AS '<replaceable>filename</replaceable>', 'complex_abs_eq'
AS '<replaceable>filename</replaceable>', 'complex_abs_lt'
LANGUAGE C;
LANGUAGE C IMMUTABLE STRICT;
CREATE OPERATOR = (
CREATE OPERATOR < (
leftarg = complex,
leftarg = complex, rightarg = complex, procedure = complex_abs_lt,
rightarg = complex,
commutator = > , negator = >= ,
procedure = complex_abs_eq,
restrict = scalarltsel, join = scalarltjoinsel
restrict = eqsel,
join = eqjoinsel
);
);
</programlisting>
</programlisting>
It is important to specify the restriction and join selectivity
It is important to specify the correct commutator and negator operators,
as well as suitable restriction and join selectivity
functions, otherwise the optimizer will be unable to make effective
functions, otherwise the optimizer will be unable to make effective
use of the index. Note that the less-than, equal, and
use of the index. Note that the less-than, equal, and
greater-than cases should use different selectivity functions.
greater-than cases should use different selectivity functions.
...
@@ -518,7 +548,7 @@ CREATE OPERATOR = (
...
@@ -518,7 +548,7 @@ CREATE OPERATOR = (
CREATE FUNCTION complex_abs_cmp(complex, complex)
CREATE FUNCTION complex_abs_cmp(complex, complex)
RETURNS integer
RETURNS integer
AS '<replaceable>filename</replaceable>'
AS '<replaceable>filename</replaceable>'
LANGUAGE C;
LANGUAGE C
IMMUTABLE STRICT
;
</programlisting>
</programlisting>
</para>
</para>
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment