Commit 8daeb5dd authored by Tom Lane's avatar Tom Lane

Add SP-GiST (space-partitioned GiST) index access method.

SP-GiST is comparable to GiST in flexibility, but supports non-balanced
partitioned search structures rather than balanced trees.  As described at
PGCon 2011, this new indexing structure can beat GiST in both index build
time and query speed for search problems that it is well matched to.

There are a number of areas that could still use improvement, but at this
point the code seems committable.

Teodor Sigaev and Oleg Bartunov, with considerable revisions by Tom Lane
parent 19fc0fe3
...@@ -569,6 +569,15 @@ ...@@ -569,6 +569,15 @@
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry>
<term><acronym>SP-GiST</acronym></term>
<listitem>
<para>
<link linkend="SPGiST">Space-Partitioned Generalized Search Tree</link>
</para>
</listitem>
</varlistentry>
<varlistentry> <varlistentry>
<term><acronym>SQL</acronym></term> <term><acronym>SQL</acronym></term>
<listitem> <listitem>
......
...@@ -82,6 +82,7 @@ ...@@ -82,6 +82,7 @@
<!ENTITY catalogs SYSTEM "catalogs.sgml"> <!ENTITY catalogs SYSTEM "catalogs.sgml">
<!ENTITY geqo SYSTEM "geqo.sgml"> <!ENTITY geqo SYSTEM "geqo.sgml">
<!ENTITY gist SYSTEM "gist.sgml"> <!ENTITY gist SYSTEM "gist.sgml">
<!ENTITY spgist SYSTEM "spgist.sgml">
<!ENTITY gin SYSTEM "gin.sgml"> <!ENTITY gin SYSTEM "gin.sgml">
<!ENTITY planstats SYSTEM "planstats.sgml"> <!ENTITY planstats SYSTEM "planstats.sgml">
<!ENTITY indexam SYSTEM "indexam.sgml"> <!ENTITY indexam SYSTEM "indexam.sgml">
......
...@@ -116,7 +116,7 @@ CREATE INDEX test1_id_index ON test1 (id); ...@@ -116,7 +116,7 @@ CREATE INDEX test1_id_index ON test1 (id);
<para> <para>
<productname>PostgreSQL</productname> provides several index types: <productname>PostgreSQL</productname> provides several index types:
B-tree, Hash, GiST and GIN. Each index type uses a different B-tree, Hash, GiST, SP-GiST and GIN. Each index type uses a different
algorithm that is best suited to different types of queries. algorithm that is best suited to different types of queries.
By default, the <command>CREATE INDEX</command> command creates By default, the <command>CREATE INDEX</command> command creates
B-tree indexes, which fit the most common situations. B-tree indexes, which fit the most common situations.
...@@ -253,6 +253,37 @@ SELECT * FROM places ORDER BY location <-> point '(101,456)' LIMIT 10; ...@@ -253,6 +253,37 @@ SELECT * FROM places ORDER BY location <-> point '(101,456)' LIMIT 10;
to do this is again dependent on the particular operator class being used. to do this is again dependent on the particular operator class being used.
</para> </para>
<para>
<indexterm>
<primary>index</primary>
<secondary>SP-GiST</secondary>
</indexterm>
<indexterm>
<primary>SP-GiST</primary>
<see>index</see>
</indexterm>
SP-GiST indexes, like GiST indexes, offer an infrastructure that supports
various kinds of searches. SP-GiST permits implementation of a wide range
of different non-balanced disk-based data structures, such as quadtrees,
k-d trees, and suffix trees (tries). As an example, the standard distribution of
<productname>PostgreSQL</productname> includes SP-GiST operator classes
for two-dimensional points, which support indexed
queries using these operators:
<simplelist>
<member><literal>&lt;&lt;</literal></member>
<member><literal>&gt;&gt;</literal></member>
<member><literal>~=</literal></member>
<member><literal>&lt;@</literal></member>
<member><literal>&lt;^</literal></member>
<member><literal>&gt;^</literal></member>
</simplelist>
(See <xref linkend="functions-geometry"> for the meaning of
these operators.)
For more information see <xref linkend="SPGiST">.
</para>
<para> <para>
<indexterm> <indexterm>
<primary>index</primary> <primary>index</primary>
...@@ -263,7 +294,7 @@ SELECT * FROM places ORDER BY location <-> point '(101,456)' LIMIT 10; ...@@ -263,7 +294,7 @@ SELECT * FROM places ORDER BY location <-> point '(101,456)' LIMIT 10;
<see>index</see> <see>index</see>
</indexterm> </indexterm>
GIN indexes are inverted indexes which can handle values that contain more GIN indexes are inverted indexes which can handle values that contain more
than one key, arrays for example. Like GiST, GIN can support than one key, arrays for example. Like GiST and SP-GiST, GIN can support
many different user-defined indexing strategies and the particular many different user-defined indexing strategies and the particular
operators with which a GIN index can be used vary depending on the operators with which a GIN index can be used vary depending on the
indexing strategy. indexing strategy.
......
...@@ -1460,7 +1460,7 @@ SELECT pg_advisory_lock(q.id) FROM ...@@ -1460,7 +1460,7 @@ SELECT pg_advisory_lock(q.id) FROM
<variablelist> <variablelist>
<varlistentry> <varlistentry>
<term> <term>
B-tree and <acronym>GiST</acronym> indexes B-tree, <acronym>GiST</acronym> and <acronym>SP-GiST</acronym> indexes
</term> </term>
<listitem> <listitem>
<para> <para>
...@@ -1510,8 +1510,8 @@ SELECT pg_advisory_lock(q.id) FROM ...@@ -1510,8 +1510,8 @@ SELECT pg_advisory_lock(q.id) FROM
applications; since they also have more features than hash applications; since they also have more features than hash
indexes, they are the recommended index type for concurrent indexes, they are the recommended index type for concurrent
applications that need to index scalar data. When dealing with applications that need to index scalar data. When dealing with
non-scalar data, B-trees are not useful, and GiST or GIN indexes should non-scalar data, B-trees are not useful, and GiST, SP-GiST or GIN
be used instead. indexes should be used instead.
</para> </para>
</sect1> </sect1>
</chapter> </chapter>
...@@ -242,6 +242,7 @@ ...@@ -242,6 +242,7 @@
&geqo; &geqo;
&indexam; &indexam;
&gist; &gist;
&spgist;
&gin; &gin;
&storage; &storage;
&bki; &bki;
......
...@@ -144,8 +144,8 @@ ALTER OPERATOR FAMILY <replaceable>name</replaceable> USING <replaceable class=" ...@@ -144,8 +144,8 @@ ALTER OPERATOR FAMILY <replaceable>name</replaceable> USING <replaceable class="
and hash functions it is not necessary to specify <replaceable and hash functions it is not necessary to specify <replaceable
class="parameter">op_type</replaceable> since the function's input class="parameter">op_type</replaceable> since the function's input
data type(s) are always the correct ones to use. For B-tree sort data type(s) are always the correct ones to use. For B-tree sort
support functions and all functions in GiST and GIN operator classes, support functions and all functions in GiST, SP-GiST and GIN operator
it is necessary to specify the operand data type(s) the function classes, it is necessary to specify the operand data type(s) the function
is to be used with. is to be used with.
</para> </para>
...@@ -245,8 +245,8 @@ ALTER OPERATOR FAMILY <replaceable>name</replaceable> USING <replaceable class=" ...@@ -245,8 +245,8 @@ ALTER OPERATOR FAMILY <replaceable>name</replaceable> USING <replaceable class="
type(s). The name of the operator or function occupying the slot is not type(s). The name of the operator or function occupying the slot is not
mentioned. Also, for <literal>DROP FUNCTION</> the type(s) to specify mentioned. Also, for <literal>DROP FUNCTION</> the type(s) to specify
are the input data type(s) the function is intended to support; for are the input data type(s) the function is intended to support; for
GIN and GiST indexes this might have nothing to do with the actual input GiST, SP-GiST and GIN indexes this might have nothing to do with the actual
argument types of the function. input argument types of the function.
</para> </para>
<para> <para>
......
...@@ -57,7 +57,7 @@ CREATE [ UNIQUE ] INDEX [ CONCURRENTLY ] [ <replaceable class="parameter">name</ ...@@ -57,7 +57,7 @@ CREATE [ UNIQUE ] INDEX [ CONCURRENTLY ] [ <replaceable class="parameter">name</
<para> <para>
<productname>PostgreSQL</productname> provides the index methods <productname>PostgreSQL</productname> provides the index methods
B-tree, hash, GiST, and GIN. Users can also define their own index B-tree, hash, GiST, SP-GiST, and GIN. Users can also define their own index
methods, but that is fairly complicated. methods, but that is fairly complicated.
</para> </para>
...@@ -154,8 +154,8 @@ CREATE [ UNIQUE ] INDEX [ CONCURRENTLY ] [ <replaceable class="parameter">name</ ...@@ -154,8 +154,8 @@ CREATE [ UNIQUE ] INDEX [ CONCURRENTLY ] [ <replaceable class="parameter">name</
<para> <para>
The name of the index method to be used. Choices are The name of the index method to be used. Choices are
<literal>btree</literal>, <literal>hash</literal>, <literal>btree</literal>, <literal>hash</literal>,
<literal>gist</literal>, and <literal>gin</>. The <literal>gist</literal>, <literal>spgist</> and <literal>gin</>.
default method is <literal>btree</literal>. The default method is <literal>btree</literal>.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
...@@ -281,12 +281,11 @@ CREATE [ UNIQUE ] INDEX [ CONCURRENTLY ] [ <replaceable class="parameter">name</ ...@@ -281,12 +281,11 @@ CREATE [ UNIQUE ] INDEX [ CONCURRENTLY ] [ <replaceable class="parameter">name</
<para> <para>
The optional <literal>WITH</> clause specifies <firstterm>storage The optional <literal>WITH</> clause specifies <firstterm>storage
parameters</> for the index. Each index method has its own set of allowed parameters</> for the index. Each index method has its own set of allowed
storage parameters. The B-tree, hash and GiST index methods all accept a storage parameters. The B-tree, hash, GiST and SP-GiST index methods all
single parameter: accept this parameter:
</para> </para>
<variablelist> <variablelist>
<varlistentry> <varlistentry>
<term><literal>FILLFACTOR</></term> <term><literal>FILLFACTOR</></term>
<listitem> <listitem>
...@@ -307,7 +306,25 @@ CREATE [ UNIQUE ] INDEX [ CONCURRENTLY ] [ <replaceable class="parameter">name</ ...@@ -307,7 +306,25 @@ CREATE [ UNIQUE ] INDEX [ CONCURRENTLY ] [ <replaceable class="parameter">name</
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
</variablelist>
<para>
GiST indexes additionally accept this parameter:
</para>
<variablelist>
<varlistentry>
<term><literal>BUFFERING</></term>
<listitem>
<para>
Determines whether the buffering build technique described in
<xref linkend="gist-buffering-build"> is used to build the index. With
<literal>OFF</> it is disabled, with <literal>ON</> it is enabled, and
with <literal>AUTO</> it is initially disabled, but turned on
on-the-fly once the index size reaches <xref linkend="guc-effective-cache-size">. The default is <literal>AUTO</>.
</para>
</listitem>
</varlistentry>
</variablelist> </variablelist>
<para> <para>
...@@ -315,7 +332,6 @@ CREATE [ UNIQUE ] INDEX [ CONCURRENTLY ] [ <replaceable class="parameter">name</ ...@@ -315,7 +332,6 @@ CREATE [ UNIQUE ] INDEX [ CONCURRENTLY ] [ <replaceable class="parameter">name</
</para> </para>
<variablelist> <variablelist>
<varlistentry> <varlistentry>
<term><literal>FASTUPDATE</></term> <term><literal>FASTUPDATE</></term>
<listitem> <listitem>
...@@ -339,27 +355,6 @@ CREATE [ UNIQUE ] INDEX [ CONCURRENTLY ] [ <replaceable class="parameter">name</ ...@@ -339,27 +355,6 @@ CREATE [ UNIQUE ] INDEX [ CONCURRENTLY ] [ <replaceable class="parameter">name</
</note> </note>
</listitem> </listitem>
</varlistentry> </varlistentry>
</variablelist>
<para>
GiST indexes additionally accept parameter:
</para>
<variablelist>
<varlistentry>
<term><literal>BUFFERING</></term>
<listitem>
<para>
Determines whether the buffering build technique described in
<xref linkend="gist-buffering-build"> is used to build the index. With
<literal>OFF</> it is disabled, with <literal>ON</> it is enabled, and
with <literal>AUTO</> it is initially disabled, but turned on
on-the-fly once the index size reaches <xref linkend="guc-effective-cache-size">. The default is <literal>AUTO</>.
</para>
</listitem>
</varlistentry>
</variablelist> </variablelist>
</refsect2> </refsect2>
......
...@@ -172,7 +172,7 @@ CREATE OPERATOR CLASS <replaceable class="parameter">name</replaceable> [ DEFAUL ...@@ -172,7 +172,7 @@ CREATE OPERATOR CLASS <replaceable class="parameter">name</replaceable> [ DEFAUL
the input data type(s) of the function (for B-tree comparison functions the input data type(s) of the function (for B-tree comparison functions
and hash functions) and hash functions)
or the class's data type (for B-tree sort support functions and all or the class's data type (for B-tree sort support functions and all
functions in GiST and GIN operator classes). These defaults functions in GiST, SP-GiST and GIN operator classes). These defaults
are correct, and so <replaceable are correct, and so <replaceable
class="parameter">op_type</replaceable> need not be specified in class="parameter">op_type</replaceable> need not be specified in
<literal>FUNCTION</> clauses, except for the case of a B-tree sort <literal>FUNCTION</> clauses, except for the case of a B-tree sort
...@@ -232,7 +232,7 @@ CREATE OPERATOR CLASS <replaceable class="parameter">name</replaceable> [ DEFAUL ...@@ -232,7 +232,7 @@ CREATE OPERATOR CLASS <replaceable class="parameter">name</replaceable> [ DEFAUL
<para> <para>
The data type actually stored in the index. Normally this is The data type actually stored in the index. Normally this is
the same as the column data type, but some index methods the same as the column data type, but some index methods
(currently GIN and GiST) allow it to be different. The (currently GiST and GIN) allow it to be different. The
<literal>STORAGE</> clause must be omitted unless the index <literal>STORAGE</> clause must be omitted unless the index
method allows a different type to be used. method allows a different type to be used.
</para> </para>
......
...@@ -540,7 +540,8 @@ CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXI ...@@ -540,7 +540,8 @@ CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXI
cannot be used. Although it's allowed, there is little point in using cannot be used. Although it's allowed, there is little point in using
B-tree or hash indexes with an exclusion constraint, because this B-tree or hash indexes with an exclusion constraint, because this
does nothing that an ordinary unique constraint doesn't do better. does nothing that an ordinary unique constraint doesn't do better.
So in practice the access method will always be <acronym>GiST</>. So in practice the access method will always be <acronym>GiST</> or
<acronym>SP-GiST</>.
</para> </para>
<para> <para>
......
This diff is collapsed.
...@@ -237,12 +237,59 @@ ...@@ -237,12 +237,59 @@
</table> </table>
<para> <para>
GIN indexes are similar to GiST indexes in flexibility: they don't have a SP-GiST indexes are similar to GiST indexes in flexibility: they don't have
fixed set of strategies. Instead the support routines of each operator a fixed set of strategies. Instead the support routines of each operator
class interpret the strategy numbers according to the operator class's class interpret the strategy numbers according to the operator class's
definition. As an example, the strategy numbers used by the built-in definition. As an example, the strategy numbers used by the built-in
operator classes for arrays are operator classes for points are shown in <xref
shown in <xref linkend="xindex-gin-array-strat-table">. linkend="xindex-spgist-point-strat-table">.
</para>
<table tocentry="1" id="xindex-spgist-point-strat-table">
<title>SP-GiST Point Strategies</title>
<tgroup cols="2">
<thead>
<row>
<entry>Operation</entry>
<entry>Strategy Number</entry>
</row>
</thead>
<tbody>
<row>
<entry>strictly left of</entry>
<entry>1</entry>
</row>
<row>
<entry>strictly right of</entry>
<entry>5</entry>
</row>
<row>
<entry>same</entry>
<entry>6</entry>
</row>
<row>
<entry>contained by</entry>
<entry>8</entry>
</row>
<row>
<entry>strictly below</entry>
<entry>10</entry>
</row>
<row>
<entry>strictly above</entry>
<entry>11</entry>
</row>
</tbody>
</tgroup>
</table>
<para>
GIN indexes are similar to GiST and SP-GiST indexes, in that they don't
have a fixed set of strategies either. Instead the support routines of
each operator class interpret the strategy numbers according to the
operator class's definition. As an example, the strategy numbers used by
the built-in operator classes for arrays are shown in
<xref linkend="xindex-gin-array-strat-table">.
</para> </para>
<table tocentry="1" id="xindex-gin-array-strat-table"> <table tocentry="1" id="xindex-gin-array-strat-table">
...@@ -434,6 +481,54 @@ ...@@ -434,6 +481,54 @@
</tgroup> </tgroup>
</table> </table>
<para>
SP-GiST indexes require five support functions, as
shown in <xref linkend="xindex-spgist-support-table">.
(For more information see <xref linkend="SPGiST">.)
</para>
<table tocentry="1" id="xindex-spgist-support-table">
<title>SP-GiST Support Functions</title>
<tgroup cols="3">
<thead>
<row>
<entry>Function</entry>
<entry>Description</entry>
<entry>Support Number</entry>
</row>
</thead>
<tbody>
<row>
<entry><function>config</></entry>
<entry>provide basic information about the operator class</entry>
<entry>1</entry>
</row>
<row>
<entry><function>choose</></entry>
<entry>determine how to insert a new value into an inner tuple</entry>
<entry>2</entry>
</row>
<row>
<entry><function>picksplit</></entry>
<entry>determine how to partition a set of values</entry>
<entry>3</entry>
</row>
<row>
<entry><function>inner_consistent</></entry>
<entry>determine which sub-partitions need to be searched for a
query</entry>
<entry>4</entry>
</row>
<row>
<entry><function>leaf_consistent</></entry>
<entry>determine whether key satisfies the
query qualifier</entry>
<entry>5</entry>
</row>
</tbody>
</tgroup>
</table>
<para> <para>
GIN indexes require four support functions, with an optional fifth, as GIN indexes require four support functions, with an optional fifth, as
shown in <xref linkend="xindex-gin-support-table">. shown in <xref linkend="xindex-gin-support-table">.
...@@ -495,9 +590,9 @@ ...@@ -495,9 +590,9 @@
of the comparison function for B-trees, a signed integer. The number of the comparison function for B-trees, a signed integer. The number
and types of the arguments to each support function are likewise and types of the arguments to each support function are likewise
dependent on the index method. For B-tree and hash the comparison and dependent on the index method. For B-tree and hash the comparison and
hashing support functions hashing support functions take the same input data types as do the
take the same input data types as do the operators included in the operator operators included in the operator class, but this is not the case for
class, but this is not the case for most GIN and GiST support functions. most GiST, SP-GiST, and GIN support functions.
</para> </para>
</sect2> </sect2>
...@@ -876,9 +971,10 @@ ALTER OPERATOR FAMILY integer_ops USING btree ADD ...@@ -876,9 +971,10 @@ ALTER OPERATOR FAMILY integer_ops USING btree ADD
</para> </para>
<para> <para>
GIN and GiST indexes do not have any explicit notion of cross-data-type GiST, SP-GiST, and GIN indexes do not have any explicit notion of
operations. The set of operators supported is just whatever the primary cross-data-type operations. The set of operators supported is just
support functions for a given operator class can handle. whatever the primary support functions for a given operator class can
handle.
</para> </para>
<note> <note>
...@@ -1045,7 +1141,7 @@ SELECT * FROM table WHERE integer_column &lt; 4; ...@@ -1045,7 +1141,7 @@ SELECT * FROM table WHERE integer_column &lt; 4;
the index is guaranteed to return all the required rows, plus perhaps the index is guaranteed to return all the required rows, plus perhaps
some additional rows, which can be eliminated by performing the original some additional rows, which can be eliminated by performing the original
operator invocation. The index methods that support lossy searches operator invocation. The index methods that support lossy searches
(currently, GiST and GIN) allow the support functions of individual (currently, GiST, SP-GiST and GIN) allow the support functions of individual
operator classes to set the recheck flag, and so this is essentially an operator classes to set the recheck flag, and so this is essentially an
operator-class feature. operator-class feature.
</para> </para>
......
...@@ -8,6 +8,6 @@ subdir = src/backend/access ...@@ -8,6 +8,6 @@ subdir = src/backend/access
top_builddir = ../../.. top_builddir = ../../..
include $(top_builddir)/src/Makefile.global include $(top_builddir)/src/Makefile.global
SUBDIRS = common gist hash heap index nbtree transam gin SUBDIRS = common gist hash heap index nbtree transam gin spgist
include $(top_srcdir)/src/backend/common.mk include $(top_srcdir)/src/backend/common.mk
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "access/hash.h" #include "access/hash.h"
#include "access/nbtree.h" #include "access/nbtree.h"
#include "access/reloptions.h" #include "access/reloptions.h"
#include "access/spgist.h"
#include "catalog/pg_type.h" #include "catalog/pg_type.h"
#include "commands/defrem.h" #include "commands/defrem.h"
#include "commands/tablespace.h" #include "commands/tablespace.h"
...@@ -104,6 +105,14 @@ static relopt_int intRelOpts[] = ...@@ -104,6 +105,14 @@ static relopt_int intRelOpts[] =
}, },
GIST_DEFAULT_FILLFACTOR, GIST_MIN_FILLFACTOR, 100 GIST_DEFAULT_FILLFACTOR, GIST_MIN_FILLFACTOR, 100
}, },
{
{
"fillfactor",
"Packs spgist index pages only to this percentage",
RELOPT_KIND_SPGIST
},
SPGIST_DEFAULT_FILLFACTOR, SPGIST_MIN_FILLFACTOR, 100
},
{ {
{ {
"autovacuum_vacuum_threshold", "autovacuum_vacuum_threshold",
......
#-------------------------------------------------------------------------
#
# Makefile--
# Makefile for access/spgist
#
# IDENTIFICATION
# src/backend/access/spgist/Makefile
#
#-------------------------------------------------------------------------
subdir = src/backend/access/spgist
top_builddir = ../../../..
include $(top_builddir)/src/Makefile.global
OBJS = spgutils.o spginsert.o spgscan.o spgvacuum.o \
spgdoinsert.o spgxlog.o \
spgtextproc.o spgquadtreeproc.o spgkdtreeproc.o
include $(top_srcdir)/src/backend/common.mk
This diff is collapsed.
This diff is collapsed.
/*-------------------------------------------------------------------------
*
* spginsert.c
* Externally visible index creation/insertion routines
*
* All the actual insertion logic is in spgdoinsert.c.
*
* Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* src/backend/access/spgist/spginsert.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "access/genam.h"
#include "access/spgist_private.h"
#include "catalog/index.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
#include "storage/smgr.h"
#include "utils/memutils.h"
typedef struct
{
SpGistState spgstate; /* SPGiST's working state */
MemoryContext tmpCtx; /* per-tuple temporary context */
} SpGistBuildState;
/* Callback to process one heap tuple during IndexBuildHeapScan */
static void
spgistBuildCallback(Relation index, HeapTuple htup, Datum *values,
bool *isnull, bool tupleIsAlive, void *state)
{
SpGistBuildState *buildstate = (SpGistBuildState *) state;
/* SPGiST doesn't index nulls */
if (*isnull == false)
{
/* Work in temp context, and reset it after each tuple */
MemoryContext oldCtx = MemoryContextSwitchTo(buildstate->tmpCtx);
spgdoinsert(index, &buildstate->spgstate, &htup->t_self, *values);
MemoryContextSwitchTo(oldCtx);
MemoryContextReset(buildstate->tmpCtx);
}
}
/*
* Build an SP-GiST index.
*/
Datum
spgbuild(PG_FUNCTION_ARGS)
{
Relation heap = (Relation) PG_GETARG_POINTER(0);
Relation index = (Relation) PG_GETARG_POINTER(1);
IndexInfo *indexInfo = (IndexInfo *) PG_GETARG_POINTER(2);
IndexBuildResult *result;
double reltuples;
SpGistBuildState buildstate;
Buffer metabuffer,
rootbuffer;
if (RelationGetNumberOfBlocks(index) != 0)
elog(ERROR, "index \"%s\" already contains data",
RelationGetRelationName(index));
/*
* Initialize the meta page and root page
*/
metabuffer = SpGistNewBuffer(index);
rootbuffer = SpGistNewBuffer(index);
Assert(BufferGetBlockNumber(metabuffer) == SPGIST_METAPAGE_BLKNO);
Assert(BufferGetBlockNumber(rootbuffer) == SPGIST_HEAD_BLKNO);
START_CRIT_SECTION();
SpGistInitMetapage(BufferGetPage(metabuffer));
MarkBufferDirty(metabuffer);
SpGistInitBuffer(rootbuffer, SPGIST_LEAF);
MarkBufferDirty(rootbuffer);
if (RelationNeedsWAL(index))
{
XLogRecPtr recptr;
XLogRecData rdata;
/* WAL data is just the relfilenode */
rdata.data = (char *) &(index->rd_node);
rdata.len = sizeof(RelFileNode);
rdata.buffer = InvalidBuffer;
rdata.next = NULL;
recptr = XLogInsert(RM_SPGIST_ID, XLOG_SPGIST_CREATE_INDEX, &rdata);
PageSetLSN(BufferGetPage(metabuffer), recptr);
PageSetTLI(BufferGetPage(metabuffer), ThisTimeLineID);
PageSetLSN(BufferGetPage(rootbuffer), recptr);
PageSetTLI(BufferGetPage(rootbuffer), ThisTimeLineID);
}
END_CRIT_SECTION();
UnlockReleaseBuffer(metabuffer);
UnlockReleaseBuffer(rootbuffer);
/*
* Now insert all the heap data into the index
*/
initSpGistState(&buildstate.spgstate, index);
buildstate.spgstate.isBuild = true;
buildstate.tmpCtx = AllocSetContextCreate(CurrentMemoryContext,
"SP-GiST build temporary context",
ALLOCSET_DEFAULT_MINSIZE,
ALLOCSET_DEFAULT_INITSIZE,
ALLOCSET_DEFAULT_MAXSIZE);
reltuples = IndexBuildHeapScan(heap, index, indexInfo, true,
spgistBuildCallback, (void *) &buildstate);
MemoryContextDelete(buildstate.tmpCtx);
SpGistUpdateMetaPage(index);
result = (IndexBuildResult *) palloc0(sizeof(IndexBuildResult));
result->heap_tuples = result->index_tuples = reltuples;
PG_RETURN_POINTER(result);
}
/*
* Build an empty SPGiST index in the initialization fork
*/
Datum
spgbuildempty(PG_FUNCTION_ARGS)
{
Relation index = (Relation) PG_GETARG_POINTER(0);
Page page;
/* Construct metapage. */
page = (Page) palloc(BLCKSZ);
SpGistInitMetapage(page);
/* Write the page. If archiving/streaming, XLOG it. */
smgrwrite(index->rd_smgr, INIT_FORKNUM, SPGIST_METAPAGE_BLKNO,
(char *) page, true);
if (XLogIsNeeded())
log_newpage(&index->rd_smgr->smgr_rnode.node, INIT_FORKNUM,
SPGIST_METAPAGE_BLKNO, page);
/* Likewise for the root page. */
SpGistInitPage(page, SPGIST_LEAF);
smgrwrite(index->rd_smgr, INIT_FORKNUM, SPGIST_HEAD_BLKNO,
(char *) page, true);
if (XLogIsNeeded())
log_newpage(&index->rd_smgr->smgr_rnode.node, INIT_FORKNUM,
SPGIST_HEAD_BLKNO, page);
/*
* An immediate sync is required even if we xlog'd the pages, because the
* writes did not go through shared buffers and therefore a concurrent
* checkpoint may have moved the redo pointer past our xlog record.
*/
smgrimmedsync(index->rd_smgr, INIT_FORKNUM);
PG_RETURN_VOID();
}
/*
* Insert one new tuple into an SPGiST index.
*/
Datum
spginsert(PG_FUNCTION_ARGS)
{
Relation index = (Relation) PG_GETARG_POINTER(0);
Datum *values = (Datum *) PG_GETARG_POINTER(1);
bool *isnull = (bool *) PG_GETARG_POINTER(2);
ItemPointer ht_ctid = (ItemPointer) PG_GETARG_POINTER(3);
#ifdef NOT_USED
Relation heapRel = (Relation) PG_GETARG_POINTER(4);
IndexUniqueCheck checkUnique = (IndexUniqueCheck) PG_GETARG_INT32(5);
#endif
SpGistState spgstate;
MemoryContext oldCtx;
MemoryContext insertCtx;
/* SPGiST doesn't index nulls */
if (*isnull)
PG_RETURN_BOOL(false);
insertCtx = AllocSetContextCreate(CurrentMemoryContext,
"SP-GiST insert temporary context",
ALLOCSET_DEFAULT_MINSIZE,
ALLOCSET_DEFAULT_INITSIZE,
ALLOCSET_DEFAULT_MAXSIZE);
oldCtx = MemoryContextSwitchTo(insertCtx);
initSpGistState(&spgstate, index);
spgdoinsert(index, &spgstate, ht_ctid, *values);
SpGistUpdateMetaPage(index);
MemoryContextSwitchTo(oldCtx);
MemoryContextDelete(insertCtx);
/* return false since we've not done any unique check */
PG_RETURN_BOOL(false);
}
/*-------------------------------------------------------------------------
*
* spgkdtreeproc.c
* implementation of k-d tree over points for SP-GiST
*
*
* Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* src/backend/access/spgist/spgkdtreeproc.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "access/gist.h" /* for RTree strategy numbers */
#include "access/spgist.h"
#include "catalog/pg_type.h"
#include "utils/builtins.h"
#include "utils/geo_decls.h"
Datum
spg_kd_config(PG_FUNCTION_ARGS)
{
/* spgConfigIn *cfgin = (spgConfigIn *) PG_GETARG_POINTER(0); */
spgConfigOut *cfg = (spgConfigOut *) PG_GETARG_POINTER(1);
cfg->prefixType = FLOAT8OID;
cfg->labelType = VOIDOID; /* we don't need node labels */
cfg->longValuesOK = false;
PG_RETURN_VOID();
}
static int
getSide(double coord, bool isX, Point *tst)
{
double tstcoord = (isX) ? tst->x : tst->y;
if (coord == tstcoord)
return 0;
else if (coord > tstcoord)
return 1;
else
return -1;
}
Datum
spg_kd_choose(PG_FUNCTION_ARGS)
{
spgChooseIn *in = (spgChooseIn *) PG_GETARG_POINTER(0);
spgChooseOut *out = (spgChooseOut *) PG_GETARG_POINTER(1);
Point *inPoint = DatumGetPointP(in->datum);
double coord;
if (in->allTheSame)
elog(ERROR, "allTheSame should not occur for k-d trees");
Assert(in->hasPrefix);
coord = DatumGetFloat8(in->prefixDatum);
Assert(in->nNodes == 2);
out->resultType = spgMatchNode;
out->result.matchNode.nodeN =
(getSide(coord, in->level % 2, inPoint) > 0) ? 0 : 1;
out->result.matchNode.levelAdd = 1;
out->result.matchNode.restDatum = PointPGetDatum(inPoint);
PG_RETURN_VOID();
}
typedef struct SortedPoint
{
Point *p;
int i;
} SortedPoint;
static int
x_cmp(const void *a, const void *b)
{
SortedPoint *pa = (SortedPoint *) a;
SortedPoint *pb = (SortedPoint *) b;
if (pa->p->x == pb->p->x)
return 0;
return (pa->p->x > pb->p->x) ? 1 : -1;
}
static int
y_cmp(const void *a, const void *b)
{
SortedPoint *pa = (SortedPoint *) a;
SortedPoint *pb = (SortedPoint *) b;
if (pa->p->y == pb->p->y)
return 0;
return (pa->p->y > pb->p->y) ? 1 : -1;
}
Datum
spg_kd_picksplit(PG_FUNCTION_ARGS)
{
spgPickSplitIn *in = (spgPickSplitIn *) PG_GETARG_POINTER(0);
spgPickSplitOut *out = (spgPickSplitOut *) PG_GETARG_POINTER(1);
int i;
int middle;
SortedPoint *sorted;
double coord;
sorted = palloc(sizeof(*sorted) * in->nTuples);
for (i = 0; i < in->nTuples; i++)
{
sorted[i].p = DatumGetPointP(in->datums[i]);
sorted[i].i = i;
}
qsort(sorted, in->nTuples, sizeof(*sorted),
(in->level % 2) ? x_cmp : y_cmp);
middle = in->nTuples >> 1;
coord = (in->level % 2) ? sorted[middle].p->x : sorted[middle].p->y;
out->hasPrefix = true;
out->prefixDatum = Float8GetDatum(coord);
out->nNodes = 2;
out->nodeLabels = NULL; /* we don't need node labels */
out->mapTuplesToNodes = palloc(sizeof(int) * in->nTuples);
out->leafTupleDatums = palloc(sizeof(Datum) * in->nTuples);
/*
* Note: points that have coordinates exactly equal to coord may get
* classified into either node, depending on where they happen to fall
* in the sorted list. This is okay as long as the inner_consistent
* function descends into both sides for such cases. This is better
* than the alternative of trying to have an exact boundary, because
* it keeps the tree balanced even when we have many instances of the
* same point value. So we should never trigger the allTheSame logic.
*/
for (i = 0; i < in->nTuples; i++)
{
Point *p = sorted[i].p;
int n = sorted[i].i;
out->mapTuplesToNodes[n] = (i < middle) ? 0 : 1;
out->leafTupleDatums[n] = PointPGetDatum(p);
}
PG_RETURN_VOID();
}
Datum
spg_kd_inner_consistent(PG_FUNCTION_ARGS)
{
spgInnerConsistentIn *in = (spgInnerConsistentIn *) PG_GETARG_POINTER(0);
spgInnerConsistentOut *out = (spgInnerConsistentOut *) PG_GETARG_POINTER(1);
Point *query;
BOX *boxQuery;
double coord;
query = DatumGetPointP(in->query);
Assert(in->hasPrefix);
coord = DatumGetFloat8(in->prefixDatum);
if (in->allTheSame)
elog(ERROR, "allTheSame should not occur for k-d trees");
Assert(in->nNodes == 2);
out->nodeNumbers = (int *) palloc(sizeof(int) * 2);
out->levelAdds = (int *) palloc(sizeof(int) * 2);
out->levelAdds[0] = 1;
out->levelAdds[1] = 1;
out->nNodes = 0;
switch (in->strategy)
{
case RTLeftStrategyNumber:
out->nNodes = 1;
out->nodeNumbers[0] = 0;
if ((in->level % 2) == 0 || FPge(query->x, coord))
{
out->nodeNumbers[1] = 1;
out->nNodes++;
}
break;
case RTRightStrategyNumber:
out->nNodes = 1;
out->nodeNumbers[0] = 1;
if ((in->level % 2) == 0 || FPle(query->x, coord))
{
out->nodeNumbers[1] = 0;
out->nNodes++;
}
break;
case RTSameStrategyNumber:
if (in->level % 2)
{
if (FPle(query->x, coord))
{
out->nodeNumbers[out->nNodes] = 0;
out->nNodes++;
}
if (FPge(query->x, coord))
{
out->nodeNumbers[out->nNodes] = 1;
out->nNodes++;
}
}
else
{
if (FPle(query->y, coord))
{
out->nodeNumbers[out->nNodes] = 0;
out->nNodes++;
}
if (FPge(query->y, coord))
{
out->nodeNumbers[out->nNodes] = 1;
out->nNodes++;
}
}
break;
case RTBelowStrategyNumber:
out->nNodes = 1;
out->nodeNumbers[0] = 0;
if ((in->level % 2) == 1 || FPge(query->y, coord))
{
out->nodeNumbers[1] = 1;
out->nNodes++;
}
break;
case RTAboveStrategyNumber:
out->nNodes = 1;
out->nodeNumbers[0] = 1;
if ((in->level % 2) == 1 || FPle(query->y, coord))
{
out->nodeNumbers[1] = 0;
out->nNodes++;
}
break;
case RTContainedByStrategyNumber:
/*
* For this operator, the query is a box not a point. We cheat to
* the extent of assuming that DatumGetPointP won't do anything
* that would be bad for a pointer-to-box.
*/
boxQuery = DatumGetBoxP(in->query);
out->nNodes = 1;
if (in->level % 2)
{
if (FPlt(boxQuery->high.x, coord))
out->nodeNumbers[0] = 0;
else if (FPgt(boxQuery->low.x, coord))
out->nodeNumbers[0] = 1;
else
{
out->nodeNumbers[0] = 0;
out->nodeNumbers[1] = 1;
out->nNodes = 2;
}
}
else
{
if (FPlt(boxQuery->high.y, coord))
out->nodeNumbers[0] = 0;
else if (FPgt(boxQuery->low.y, coord))
out->nodeNumbers[0] = 1;
else
{
out->nodeNumbers[0] = 0;
out->nodeNumbers[1] = 1;
out->nNodes = 2;
}
}
break;
default:
elog(ERROR, "unrecognized strategy number: %d", in->strategy);
break;
}
PG_RETURN_VOID();
}
/*
* spg_kd_leaf_consistent() is the same as spg_quad_leaf_consistent(),
* since we support the same operators and the same leaf data type.
* So we just borrow that function.
*/
/*-------------------------------------------------------------------------
*
* spgquadtreeproc.c
* implementation of quad tree over points for SP-GiST
*
*
* Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* src/backend/access/spgist/spgquadtreeproc.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "access/gist.h" /* for RTree strategy numbers */
#include "access/spgist.h"
#include "catalog/pg_type.h"
#include "utils/builtins.h"
#include "utils/geo_decls.h"
Datum
spg_quad_config(PG_FUNCTION_ARGS)
{
/* spgConfigIn *cfgin = (spgConfigIn *) PG_GETARG_POINTER(0); */
spgConfigOut *cfg = (spgConfigOut *) PG_GETARG_POINTER(1);
cfg->prefixType = POINTOID;
cfg->labelType = VOIDOID; /* we don't need node labels */
cfg->longValuesOK = false;
PG_RETURN_VOID();
}
#define SPTEST(f, x, y) \
DatumGetBool(DirectFunctionCall2(f, PointPGetDatum(x), PointPGetDatum(y)))
/*
* Determine which quadrant a point falls into, relative to the centroid.
*
* Quadrants are identified like this:
*
* 4 | 1
* ----+-----
* 3 | 2
*
* Points on one of the axes are taken to lie in the lowest-numbered
* adjacent quadrant.
*/
static int2
getQuadrant(Point *centroid, Point *tst)
{
if ((SPTEST(point_above, tst, centroid) ||
SPTEST(point_horiz, tst, centroid)) &&
(SPTEST(point_right, tst, centroid) ||
SPTEST(point_vert, tst, centroid)))
return 1;
if (SPTEST(point_below, tst, centroid) &&
(SPTEST(point_right, tst, centroid) ||
SPTEST(point_vert, tst, centroid)))
return 2;
if ((SPTEST(point_below, tst, centroid) ||
SPTEST(point_horiz, tst, centroid)) &&
SPTEST(point_left, tst, centroid))
return 3;
if (SPTEST(point_above, tst, centroid) &&
SPTEST(point_left, tst, centroid))
return 4;
elog(ERROR, "getQuadrant: impossible case");
return 0;
}
Datum
spg_quad_choose(PG_FUNCTION_ARGS)
{
spgChooseIn *in = (spgChooseIn *) PG_GETARG_POINTER(0);
spgChooseOut *out = (spgChooseOut *) PG_GETARG_POINTER(1);
Point *inPoint = DatumGetPointP(in->datum),
*centroid;
if (in->allTheSame)
{
out->resultType = spgMatchNode;
/* nodeN will be set by core */
out->result.matchNode.levelAdd = 0;
out->result.matchNode.restDatum = PointPGetDatum(inPoint);
PG_RETURN_VOID();
}
Assert(in->hasPrefix);
centroid = DatumGetPointP(in->prefixDatum);
Assert(in->nNodes == 4);
out->resultType = spgMatchNode;
out->result.matchNode.nodeN = getQuadrant(centroid, inPoint) - 1;
out->result.matchNode.levelAdd = 0;
out->result.matchNode.restDatum = PointPGetDatum(inPoint);
PG_RETURN_VOID();
}
#ifdef USE_MEDIAN
static int
x_cmp(const void *a, const void *b, void *arg)
{
Point *pa = *(Point **) a;
Point *pb = *(Point **) b;
if (pa->x == pb->x)
return 0;
return (pa->x > pb->x) ? 1 : -1;
}
static int
y_cmp(const void *a, const void *b, void *arg)
{
Point *pa = *(Point **) a;
Point *pb = *(Point **) b;
if (pa->y == pb->y)
return 0;
return (pa->y > pb->y) ? 1 : -1;
}
#endif
Datum
spg_quad_picksplit(PG_FUNCTION_ARGS)
{
spgPickSplitIn *in = (spgPickSplitIn *) PG_GETARG_POINTER(0);
spgPickSplitOut *out = (spgPickSplitOut *) PG_GETARG_POINTER(1);
int i;
Point *centroid;
#ifdef USE_MEDIAN
/* Use the median values of x and y as the centroid point */
Point **sorted;
sorted = palloc(sizeof(*sorted) * in->nTuples);
for (i = 0; i < in->nTuples; i++)
sorted[i] = DatumGetPointP(in->datums[i]);
centroid = palloc(sizeof(*centroid));
qsort(sorted, in->nTuples, sizeof(*sorted), x_cmp);
centroid->x = sorted[in->nTuples >> 1]->x;
qsort(sorted, in->nTuples, sizeof(*sorted), y_cmp);
centroid->y = sorted[in->nTuples >> 1]->y;
#else
/* Use the average values of x and y as the centroid point */
centroid = palloc0(sizeof(*centroid));
for (i = 0; i < in->nTuples; i++)
{
centroid->x += DatumGetPointP(in->datums[i])->x;
centroid->y += DatumGetPointP(in->datums[i])->y;
}
centroid->x /= in->nTuples;
centroid->y /= in->nTuples;
#endif
out->hasPrefix = true;
out->prefixDatum = PointPGetDatum(centroid);
out->nNodes = 4;
out->nodeLabels = NULL; /* we don't need node labels */
out->mapTuplesToNodes = palloc(sizeof(int) * in->nTuples);
out->leafTupleDatums = palloc(sizeof(Datum) * in->nTuples);
for (i = 0; i < in->nTuples; i++)
{
Point *p = DatumGetPointP(in->datums[i]);
int quadrant = getQuadrant(centroid, p) - 1;
out->leafTupleDatums[i] = PointPGetDatum(p);
out->mapTuplesToNodes[i] = quadrant;
}
PG_RETURN_VOID();
}
/* Subroutine to fill out->nodeNumbers[] for spg_quad_inner_consistent */
static void
setNodes(spgInnerConsistentOut *out, bool isAll, int first, int second)
{
if (isAll)
{
out->nNodes = 4;
out->nodeNumbers[0] = 0;
out->nodeNumbers[1] = 1;
out->nodeNumbers[2] = 2;
out->nodeNumbers[3] = 3;
}
else
{
out->nNodes = 2;
out->nodeNumbers[0] = first - 1;
out->nodeNumbers[1] = second - 1;
}
}
Datum
spg_quad_inner_consistent(PG_FUNCTION_ARGS)
{
spgInnerConsistentIn *in = (spgInnerConsistentIn *) PG_GETARG_POINTER(0);
spgInnerConsistentOut *out = (spgInnerConsistentOut *) PG_GETARG_POINTER(1);
Point *query,
*centroid;
BOX *boxQuery;
query = DatumGetPointP(in->query);
Assert(in->hasPrefix);
centroid = DatumGetPointP(in->prefixDatum);
if (in->allTheSame)
{
/* Report that all nodes should be visited */
int i;
out->nNodes = in->nNodes;
out->nodeNumbers = (int *) palloc(sizeof(int) * in->nNodes);
for (i = 0; i < in->nNodes; i++)
out->nodeNumbers[i] = i;
PG_RETURN_VOID();
}
Assert(in->nNodes == 4);
out->nodeNumbers = (int *) palloc(sizeof(int) * 4);
switch (in->strategy)
{
case RTLeftStrategyNumber:
setNodes(out, SPTEST(point_left, centroid, query), 3, 4);
break;
case RTRightStrategyNumber:
setNodes(out, SPTEST(point_right, centroid, query), 1, 2);
break;
case RTSameStrategyNumber:
out->nNodes = 1;
out->nodeNumbers[0] = getQuadrant(centroid, query) - 1;
break;
case RTBelowStrategyNumber:
setNodes(out, SPTEST(point_below, centroid, query), 2, 3);
break;
case RTAboveStrategyNumber:
setNodes(out, SPTEST(point_above, centroid, query), 1, 4);
break;
case RTContainedByStrategyNumber:
/*
* For this operator, the query is a box not a point. We cheat to
* the extent of assuming that DatumGetPointP won't do anything
* that would be bad for a pointer-to-box.
*/
boxQuery = DatumGetBoxP(in->query);
if (DatumGetBool(DirectFunctionCall2(box_contain_pt,
PointerGetDatum(boxQuery),
PointerGetDatum(centroid))))
{
/* centroid is in box, so descend to all quadrants */
setNodes(out, true, 0, 0);
}
else
{
/* identify quadrant(s) containing all corners of box */
Point p;
int i,
r = 0;
p = boxQuery->low;
r |= 1 << (getQuadrant(centroid, &p) - 1);
p.y = boxQuery->high.y;
r |= 1 << (getQuadrant(centroid, &p) - 1);
p = boxQuery->high;
r |= 1 << (getQuadrant(centroid, &p) - 1);
p.x = boxQuery->low.x;
r |= 1 << (getQuadrant(centroid, &p) - 1);
/* we must descend into those quadrant(s) */
out->nNodes = 0;
for (i = 0; i < 4; i++)
{
if (r & (1 << i))
{
out->nodeNumbers[out->nNodes] = i;
out->nNodes++;
}
}
}
break;
default:
elog(ERROR, "unrecognized strategy number: %d", in->strategy);
break;
}
PG_RETURN_VOID();
}
Datum
spg_quad_leaf_consistent(PG_FUNCTION_ARGS)
{
spgLeafConsistentIn *in = (spgLeafConsistentIn *) PG_GETARG_POINTER(0);
spgLeafConsistentOut *out = (spgLeafConsistentOut *) PG_GETARG_POINTER(1);
Point *query = DatumGetPointP(in->query);
Point *datum = DatumGetPointP(in->leafDatum);
bool res;
/* all tests are exact */
out->recheck = false;
switch (in->strategy)
{
case RTLeftStrategyNumber:
res = SPTEST(point_left, datum, query);
break;
case RTRightStrategyNumber:
res = SPTEST(point_right, datum, query);
break;
case RTSameStrategyNumber:
res = SPTEST(point_eq, datum, query);
break;
case RTBelowStrategyNumber:
res = SPTEST(point_below, datum, query);
break;
case RTAboveStrategyNumber:
res = SPTEST(point_above, datum, query);
break;
case RTContainedByStrategyNumber:
/*
* For this operator, the query is a box not a point. We cheat to
* the extent of assuming that DatumGetPointP won't do anything
* that would be bad for a pointer-to-box.
*/
res = SPTEST(box_contain_pt, query, datum);
break;
default:
elog(ERROR, "unrecognized strategy number: %d", in->strategy);
res = false;
break;
}
PG_RETURN_BOOL(res);
}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include "access/heapam.h" #include "access/heapam.h"
#include "access/multixact.h" #include "access/multixact.h"
#include "access/nbtree.h" #include "access/nbtree.h"
#include "access/spgist.h"
#include "access/xact.h" #include "access/xact.h"
#include "access/xlog_internal.h" #include "access/xlog_internal.h"
#include "catalog/storage.h" #include "catalog/storage.h"
...@@ -40,5 +41,6 @@ const RmgrData RmgrTable[RM_MAX_ID + 1] = { ...@@ -40,5 +41,6 @@ const RmgrData RmgrTable[RM_MAX_ID + 1] = {
{"Hash", hash_redo, hash_desc, NULL, NULL, NULL}, {"Hash", hash_redo, hash_desc, NULL, NULL, NULL},
{"Gin", gin_redo, gin_desc, gin_xlog_startup, gin_xlog_cleanup, gin_safe_restartpoint}, {"Gin", gin_redo, gin_desc, gin_xlog_startup, gin_xlog_cleanup, gin_safe_restartpoint},
{"Gist", gist_redo, gist_desc, gist_xlog_startup, gist_xlog_cleanup, NULL}, {"Gist", gist_redo, gist_desc, gist_xlog_startup, gist_xlog_cleanup, NULL},
{"Sequence", seq_redo, seq_desc, NULL, NULL, NULL} {"Sequence", seq_redo, seq_desc, NULL, NULL, NULL},
{"SPGist", spg_redo, spg_desc, spg_xlog_startup, spg_xlog_cleanup, NULL}
}; };
...@@ -6555,6 +6555,26 @@ gistcostestimate(PG_FUNCTION_ARGS) ...@@ -6555,6 +6555,26 @@ gistcostestimate(PG_FUNCTION_ARGS)
PG_RETURN_VOID(); PG_RETURN_VOID();
} }
Datum
spgcostestimate(PG_FUNCTION_ARGS)
{
PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
IndexOptInfo *index = (IndexOptInfo *) PG_GETARG_POINTER(1);
List *indexQuals = (List *) PG_GETARG_POINTER(2);
List *indexOrderBys = (List *) PG_GETARG_POINTER(3);
RelOptInfo *outer_rel = (RelOptInfo *) PG_GETARG_POINTER(4);
Cost *indexStartupCost = (Cost *) PG_GETARG_POINTER(5);
Cost *indexTotalCost = (Cost *) PG_GETARG_POINTER(6);
Selectivity *indexSelectivity = (Selectivity *) PG_GETARG_POINTER(7);
double *indexCorrelation = (double *) PG_GETARG_POINTER(8);
genericcostestimate(root, index, indexQuals, indexOrderBys, outer_rel, 0.0,
indexStartupCost, indexTotalCost,
indexSelectivity, indexCorrelation);
PG_RETURN_VOID();
}
/* Find the index column matching "op"; return its index, or -1 if no match */ /* Find the index column matching "op"; return its index, or -1 if no match */
static int static int
find_index_column(Node *op, IndexOptInfo *index) find_index_column(Node *op, IndexOptInfo *index)
......
...@@ -24,6 +24,10 @@ ...@@ -24,6 +24,10 @@
* Note: GIN does not include a page ID word as do the other index types. * Note: GIN does not include a page ID word as do the other index types.
* This is OK because the opaque data is only 8 bytes and so can be reliably * This is OK because the opaque data is only 8 bytes and so can be reliably
* distinguished by size. Revisit this if the size ever increases. * distinguished by size. Revisit this if the size ever increases.
* Further note: as of 9.2, SP-GiST also uses 8-byte special space. This is
* still OK, as long as GIN isn't using all of the high-order bits in its
* flags word, because that way the flags word cannot match the page ID used
* by SP-GiST.
*/ */
typedef struct GinPageOpaqueData typedef struct GinPageOpaqueData
{ {
......
...@@ -42,8 +42,9 @@ typedef enum relopt_kind ...@@ -42,8 +42,9 @@ typedef enum relopt_kind
RELOPT_KIND_GIST = (1 << 5), RELOPT_KIND_GIST = (1 << 5),
RELOPT_KIND_ATTRIBUTE = (1 << 6), RELOPT_KIND_ATTRIBUTE = (1 << 6),
RELOPT_KIND_TABLESPACE = (1 << 7), RELOPT_KIND_TABLESPACE = (1 << 7),
RELOPT_KIND_SPGIST = (1 << 8),
/* if you add a new kind, make sure you update "last_default" too */ /* if you add a new kind, make sure you update "last_default" too */
RELOPT_KIND_LAST_DEFAULT = RELOPT_KIND_TABLESPACE, RELOPT_KIND_LAST_DEFAULT = RELOPT_KIND_SPGIST,
/* some compilers treat enums as signed ints, so we can't use 1 << 31 */ /* some compilers treat enums as signed ints, so we can't use 1 << 31 */
RELOPT_KIND_MAX = (1 << 30) RELOPT_KIND_MAX = (1 << 30)
} relopt_kind; } relopt_kind;
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment