Skip to content

Implement AMPROP_DISTANCE_ORDERABLE in amroutine->amproperty #103

@SteveLauC

Description

@SteveLauC

Problem Statement

pg_textsearch supports ranking results via the <@> operator in ORDER BY clauses. However, because the amproperty hook is not implemented to handle AMPROP_DISTANCE_ORDERABLE, Postgres core metadata logic returns NULL (Unknown) when inspecting this index property:

amroutine->amproperty = NULL; /* No property function */

create table document (content text);
create index bm25_index on document using bm25 (content) with (text_config = 'english');

select pg_index_column_has_property('bm25_index'::regclass, 1, 'distance_orderable');
 pg_index_column_has_property 
------------------------------
 
(1 row)

Proposed Solution

Implement amproperty():

bool
tp_property(Oid index_oid, int attno,
            IndexAMProperty prop, const char *propname,
            bool *res, bool *isnull)
{
    /* Only handle column-level properties */
    if (OidIsValid(index_oid) && attno > 0)
    {
        switch (prop)
        {
            case AMPROP_DISTANCE_ORDERABLE:
                *res = true;
                *isnull = false;
                return true;

            default: /* Do not override other properties' impl */
        }
    }

    /* use Postgres's default logic */
    return false;
}

Alternatives Considered

None

Additional Context

It is recommended (see the amproperty() section) to implement this:

Access methods that support ordering operators should implement AMPROP_DISTANCE_ORDERABLE property testing, as the core code does not know how to do that and will return NULL.

But it is actually ok to not do this as omitting it won't prevent queries from running.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions