Referencia · §8.22

PostgreSQL's text search

modules/pg-compat tsearch, §M14.21 T8

Ez az oldal a nucleus saját dokumentációjából készül, ezért angol nyelvű.

pg-compat brings PostgreSQL’s tsvector and tsquery — its text forms both ways ('cat':2 'mat':6A, 'cat' & !( 'dog' | 'mice' ), <N>, :*, weights), regconfig a name for text — the makers to_tsvector, to_tsquery, plainto_tsquery, phraseto_tsquery, websearch_to_tsquery ([cfg,] text), and @@ between a tsvector and a tsquery (either way round; text @@ tsquery by simple). A configuration is nucleus’s analyzer of the name (english, hungarian, …, pg_catalog. dropped; none named: simple): a tsvector is its terms at their positions plus one. Queries are built as PostgreSQL builds them — each operand analyzed, its terms a phrase by their positions, stop words cleaned out with the phrase distances across them kept (cat <-> the <-> mat is 'cat' <2> 'mat'; <N> up to 16,384, and the distance after the clean-up added unchecked, as PostgreSQL’s: cat <16384> the <-> dog is <16385>) — and evaluated as its TS_execute: a phrase’s parts’ positions merged per operator, & under a phrase where both match, a NOT under one where its operand does not, weights filtering positions, a term without positions a “maybe” (no phrase matches it); the empty query matches nothing, !x the empty vector.

The rewrite (ScalarFunction::rewrite, §8.12): to_tsvector(cfg, e) @@ maker(cfg, 'literal') — what a PostgreSQL client writes for a GIN index on to_tsvector(cfg, e) — binds as nucleus’s e @@ q by the analyzer cfg (§8.19), which a text index on e analyzing so serves, when q can say the query exactly: each operand a quoted phrase of its words, kept only where the analyzer finds the same terms in it; a prefix as its stem and *; &, |, ! as AND, OR, NOT; a phrase of words a step apart as one quoted phrase, kept only where the analyzer gives them joined the positions PostgreSQL’s chain has. Otherwise — weights, other distances, a phrase over operators, a query of stop words only, a parameter, different configurations — @@ is computed on the values. Departures: nucleus’s analyzers, not PostgreSQL’s parser and dictionaries; positions not capped at 16,383 nor 256 a term.

Ranking and headlines (tsrank, tsheadline, §M14.21 T8b): ts_rank and ts_rank_cd ([weights,] vector, query [, normalization]; weights {D, C, B, A}, default {0.1, 0.2, 0.4, 1.0}; normalization flags 1, 2, 4, 8, 16, 32) computed as tsrank.c computes them — ts_rank in real with C’s promotions, ts_rank_cd in double precision — and the query’s operands sorted by PostgreSQL’s own qsort (unstable past six items: which of a and a:* counts follows it), so the values are PostgreSQL’s to the bit. ts_headline([cfg,] document, query [, options]) as the default parser’s prsd_headline and generateHeadline: the text a list of tokens — simple’s words, the text between two words one token, a number never a headline’s end — each word the operands its terms match (a word matching two repeated); the query’s covers (hlCover, by TS_execute_locations, each checked on its words); the best stretched to MinWords–MaxWords words (mark_hl_words), or the best MaxFragments fragments (mark_hl_fragments) joined by FragmentDelimiter; the matches between StartSel and StopSel; HighlightAll the whole text. Options are deserialize_deflist’s key = value list (quoted values; errors as PostgreSQL’s: MinWords must be less than MaxWords, unrecognized headline parameter, …). Departures: a query whose ORs combine more than 4,096 sets of matches (or 2²⁰ positions) — TS_execute_locations makes one of each pair of an OR‘s sides’, so (a & b) | (c & d) | … doubles with each | — is 54000, not the memory and minutes it would take (a fuzzing find; PostgreSQL has no bound); UAX #29’s words (PostgreSQL’s parser splits don't and hyphenated words), HTML tags kept as text.

On the wire (§M14.21 T8c): PostgreSQL’s front end sends them as PostgreSQL’s types — tsvector oid 3614, tsquery 3615, in pg_type with their arrays (3643, 3645), named by format_type and to_regtype, so a column’s type is theirs in pg_attribute and \d —: in text format their text forms, in binary format as tsvectorsend and tsquerysend give them (the term count, each term’s bytes and a zero, its position count, each position with its weight in the top two bits; the item count, the items in prefix order — an operator before its right operand, then its left —, an operand’s weights as bits, whether a prefix, its bytes and a zero, an operator’s code and a phrase’s distance); and it takes them as parameters either way, a binary one checked as tsvectorrecv and tsqueryrecv check it (22P03; a query nested deeper than 10,000 refused too, its tree read without recursion). tsvectorsend(v) and tsquerysend(q) give the binary forms in SQL, as in PostgreSQL. Positions past 16,383, or past 256 of a term, which nucleus’s values keep, are sent as PostgreSQL would have kept them (capped, each once with the greater weight, the first 256). A tsquery is what its text form reads back as: PostgreSQL’s prints a & (b & c) as 'a' & 'b' & 'c', which reads back as (a & b) & c, and a nucleus value — whose text form must read back as it (§M14.19’s laws) — of either is that one: an & of &s, an | of |s, grouped from the left, which matches, ranks and headlines alike; its binary form is 'a' & 'b' & 'c'’s, and the two are equal (PostgreSQL’s are not). nucleus’s own protocol, whose sessions do not have the module, sends a value as any registered type’s (ncp tag 11, §10: the name and the binary form, here its text form’s UTF-8): the drivers give a value of a type they do not know (Rust Value::Other, Python and JavaScript Ext, Java Ext, C NC_EXT), and a text parameter is taken where a column holds one.

Tests: the module’s tests/tsearch.rs — 5,407 cases PostgreSQL 18 answered (tests/tsearch_pg.tsv, by bench/text/tsearch_pg.py: random vectors and queries, phrases at distances 0–3 over every operator, prefixes, weights, terms without positions, and the makers’ queries, text forms included, the longest distances; ts_rank and ts_rank_cd six ways a case, to the bit; headlines of random English with 11 sets of options, to the byte; 305 values’ binary forms, to the byte, the edges among them) alike; 400 random queries of the four makers by the rewrite and over stored tsvectors alike, without the index and through it, on a row table, a column one and one partitioned into partitions of each storage; the plans served and not. tests/wire.rs — the types through rust-postgres (their oids, PostgreSQL 18’s binary forms as parameters and results, a & (b & c) read as (a & b) & c, a malformed one refused) and through nucleus’s protocol (the Rust driver’s Value::Other, a text parameter). tests/slt/03_text_search.slt (PostgreSQL 18’s results); unit tests of the printer, the clean-up, the evaluation, the rewrite’s text queries, and the binary forms (round trips, the capped positions, what tsvectorrecv and tsqueryrecv refuse, the depth).