1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-11 20:28:21 +03:00

Add support for building GiST index by sorting.

This adds a new optional support function to the GiST access method:
sortsupport. If it is defined, the GiST index is built by sorting all data
to the order defined by the sortsupport's comparator function, and packing
the tuples in that order to GiST pages. This is similar to how B-tree
index build works, and is much faster than inserting the tuples one by
one. The resulting index is smaller too, because the pages are packed more
tightly, upto 'fillfactor'. The normal build method works by splitting
pages, which tends to lead to more wasted space.

The quality of the resulting index depends on how good the opclass-defined
sort order is. A good order preserves locality of the input data.

As the first user of this facility, add 'sortsupport' function to the
point_ops opclass. It sorts the points in Z-order (aka Morton Code), by
interleaving the bits of the X and Y coordinates.

Author: Andrey Borodin
Reviewed-by: Pavel Borisov, Thomas Munro
Discussion: https://www.postgresql.org/message-id/1A36620E-CAD8-4267-9067-FB31385E7C0D%40yandex-team.ru
This commit is contained in:
Heikki Linnakangas
2020-09-17 11:33:40 +03:00
parent 089da3c477
commit 16fa9b2b30
17 changed files with 939 additions and 110 deletions

View File

@ -259,6 +259,8 @@ CREATE INDEX ON my_table USING GIST (my_inet_column inet_ops);
<function>compress</function> method is omitted. The optional tenth method
<function>options</function> is needed if the operator class provides
the user-specified parameters.
The <function>sortsupport</function> method is also optional and is used to
speed up building a <acronym>GiST</acronym> index.
</para>
<variablelist>
@ -1065,6 +1067,74 @@ my_compress(PG_FUNCTION_ARGS)
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><function>sortsupport</function></term>
<listitem>
<para>
Returns a comparator function to sort data in a way that preserves
locality. It is used by <command>CREATE INDEX</command> and
<command>REINDEX</command> commands. The quality of the created index
depends on how well the sort order determined by the comparator function
preserves locality of the inputs.
</para>
<para>
The <function>sortsupport</function> method is optional. If it is not
provided, <command>CREATE INDEX</command> builds the index by inserting
each tuple to the tree using the <function>penalty</function> and
<function>picksplit</function> functions, which is much slower.
</para>
<para>
The <acronym>SQL</acronym> declaration of the function must look like
this:
<programlisting>
CREATE OR REPLACE FUNCTION my_sortsupport(internal)
RETURNS void
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT;
</programlisting>
The argument is a pointer to a <structname>SortSupport</structname>
struct. At a minimum, the function must fill in its comparator field.
The comparator takes three arguments: two Datums to compare, and
a pointer to the <structname>SortSupport</structname> struct. The
Datums are the two indexed values in the format that they are stored
in the index; that is, in the format returned by the
<function>compress</function> method. The full API is defined in
<filename>src/include/utils/sortsupport.h</filename>.
</para>
<para>
The matching code in the C module could then follow this skeleton:
<programlisting>
PG_FUNCTION_INFO_V1(my_sortsupport);
static int
my_fastcmp(Datum x, Datum y, SortSupport ssup)
{
/* establish order between x and y by computing some sorting value z */
int z1 = ComputeSpatialCode(x);
int z2 = ComputeSpatialCode(y);
return z1 == z2 ? 0 : z1 > z2 ? 1 : -1;
}
Datum
my_sortsupport(PG_FUNCTION_ARGS)
{
SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
ssup->comparator = my_fastcmp;
PG_RETURN_VOID();
}
</programlisting>
</para>
</listitem>
</varlistentry>
</variablelist>
<para>