Home Explore Blog CI



postgresql

7th chunk of `doc/src/sgml/spgist.sgml`
8b1782d065ecf3658494ea68e781e6234b136e217e35706d0000000100000fa0
 <term><function>choose</function></term>
     <listitem>
      <para>
        Chooses a method for inserting a new value into an inner tuple.
      </para>

     <para>
      The <acronym>SQL</acronym> declaration of the function must look like this:
<programlisting>
CREATE FUNCTION my_choose(internal, internal) RETURNS void ...
</programlisting>
      The first argument is a pointer to a <structname>spgChooseIn</structname>
      C struct, containing input data for the function.
      The second argument is a pointer to a <structname>spgChooseOut</structname>
      C struct, which the function must fill with result data.
<programlisting>
typedef struct spgChooseIn
{
    Datum       datum;          /* original datum to be indexed */
    Datum       leafDatum;      /* current datum to be stored at leaf */
    int         level;          /* current level (counting from zero) */

    /* Data from current inner tuple */
    bool        allTheSame;     /* tuple is marked all-the-same? */
    bool        hasPrefix;      /* tuple has a prefix? */
    Datum       prefixDatum;    /* if so, the prefix value */
    int         nNodes;         /* number of nodes in the inner tuple */
    Datum      *nodeLabels;     /* node label values (NULL if none) */
} spgChooseIn;

typedef enum spgChooseResultType
{
    spgMatchNode = 1,           /* descend into existing node */
    spgAddNode,                 /* add a node to the inner tuple */
    spgSplitTuple               /* split inner tuple (change its prefix) */
} spgChooseResultType;

typedef struct spgChooseOut
{
    spgChooseResultType resultType;     /* action code, see above */
    union
    {
        struct                  /* results for spgMatchNode */
        {
            int         nodeN;      /* descend to this node (index from 0) */
            int         levelAdd;   /* increment level by this much */
            Datum       restDatum;  /* new leaf datum */
        }           matchNode;
        struct                  /* results for spgAddNode */
        {
            Datum       nodeLabel;  /* new node's label */
            int         nodeN;      /* where to insert it (index from 0) */
        }           addNode;
        struct                  /* results for spgSplitTuple */
        {
            /* Info to form new upper-level inner tuple with one child tuple */
            bool        prefixHasPrefix;    /* tuple should have a prefix? */
            Datum       prefixPrefixDatum;  /* if so, its value */
            int         prefixNNodes;       /* number of nodes */
            Datum      *prefixNodeLabels;   /* their labels (or NULL for
                                             * no labels) */
            int         childNodeN;         /* which node gets child tuple */

            /* Info to form new lower-level inner tuple with all old nodes */
            bool        postfixHasPrefix;   /* tuple should have a prefix? */
            Datum       postfixPrefixDatum; /* if so, its value */
        }           splitTuple;
    }           result;
} spgChooseOut;
</programlisting>

       <structfield>datum</structfield> is the original datum of
       <structname>spgConfigIn</structname>.<structfield>attType</structfield>
       type that was to be inserted into the index.
       <structfield>leafDatum</structfield> is a value of
       <structname>spgConfigOut</structname>.<structfield>leafType</structfield>
       type, which is initially a result of method
       <function>compress</function> applied to <structfield>datum</structfield>
       when method <function>compress</function> is provided, or the same value as
       <structfield>datum</structfield> otherwise.
       <structfield>leafDatum</structfield> can change at lower levels of the tree
       if the <function>choose</function> or <function>picksplit</function>
       methods change it.  When the insertion search reaches a leaf page,
       the current value of <structfield>leafDatum</structfield> is

Title: SP-GiST Choose Method for Inserting Values into Inner Tuples
Summary
The choose method is used to select a method for inserting a new value into an inner tuple in an SP-GiST index, taking into account the original datum, the current leaf datum, and other information from the current inner tuple, and returns a result type indicating whether to match an existing node, add a new node, or split the inner tuple, along with additional data depending on the chosen action.