LCOV - code coverage report
Current view: top level - src/backend/utils/adt - varlena.c (source / functions) Hit Total Coverage
Test: PostgreSQL 19devel Lines: 1712 1897 90.2 %
Date: 2026-02-11 11:20:14 Functions: 132 143 92.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * varlena.c
       4             :  *    Functions for the variable-length built-in types.
       5             :  *
       6             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  *
      10             :  * IDENTIFICATION
      11             :  *    src/backend/utils/adt/varlena.c
      12             :  *
      13             :  *-------------------------------------------------------------------------
      14             :  */
      15             : #include "postgres.h"
      16             : 
      17             : #include <ctype.h>
      18             : #include <limits.h>
      19             : 
      20             : #include "access/detoast.h"
      21             : #include "access/toast_compression.h"
      22             : #include "catalog/pg_collation.h"
      23             : #include "catalog/pg_type.h"
      24             : #include "common/hashfn.h"
      25             : #include "common/int.h"
      26             : #include "common/unicode_category.h"
      27             : #include "common/unicode_norm.h"
      28             : #include "common/unicode_version.h"
      29             : #include "funcapi.h"
      30             : #include "lib/hyperloglog.h"
      31             : #include "libpq/pqformat.h"
      32             : #include "miscadmin.h"
      33             : #include "nodes/execnodes.h"
      34             : #include "parser/scansup.h"
      35             : #include "port/pg_bswap.h"
      36             : #include "regex/regex.h"
      37             : #include "utils/builtins.h"
      38             : #include "utils/guc.h"
      39             : #include "utils/lsyscache.h"
      40             : #include "utils/memutils.h"
      41             : #include "utils/pg_locale.h"
      42             : #include "utils/sortsupport.h"
      43             : #include "utils/varlena.h"
      44             : 
      45             : typedef varlena VarString;
      46             : 
      47             : /*
      48             :  * State for text_position_* functions.
      49             :  */
      50             : typedef struct
      51             : {
      52             :     pg_locale_t locale;         /* collation used for substring matching */
      53             :     bool        is_multibyte_char_in_char;  /* need to check char boundaries? */
      54             :     bool        greedy;         /* find longest possible substring? */
      55             : 
      56             :     char       *str1;           /* haystack string */
      57             :     char       *str2;           /* needle string */
      58             :     int         len1;           /* string lengths in bytes */
      59             :     int         len2;
      60             : 
      61             :     /* Skip table for Boyer-Moore-Horspool search algorithm: */
      62             :     int         skiptablemask;  /* mask for ANDing with skiptable subscripts */
      63             :     int         skiptable[256]; /* skip distance for given mismatched char */
      64             : 
      65             :     /*
      66             :      * Note that with nondeterministic collations, the length of the last
      67             :      * match is not necessarily equal to the length of the "needle" passed in.
      68             :      */
      69             :     char       *last_match;     /* pointer to last match in 'str1' */
      70             :     int         last_match_len; /* length of last match */
      71             :     int         last_match_len_tmp; /* same but for internal use */
      72             : 
      73             :     /*
      74             :      * Sometimes we need to convert the byte position of a match to a
      75             :      * character position.  These store the last position that was converted,
      76             :      * so that on the next call, we can continue from that point, rather than
      77             :      * count characters from the very beginning.
      78             :      */
      79             :     char       *refpoint;       /* pointer within original haystack string */
      80             :     int         refpos;         /* 0-based character offset of the same point */
      81             : } TextPositionState;
      82             : 
      83             : typedef struct
      84             : {
      85             :     char       *buf1;           /* 1st string, or abbreviation original string
      86             :                                  * buf */
      87             :     char       *buf2;           /* 2nd string, or abbreviation strxfrm() buf */
      88             :     int         buflen1;        /* Allocated length of buf1 */
      89             :     int         buflen2;        /* Allocated length of buf2 */
      90             :     int         last_len1;      /* Length of last buf1 string/strxfrm() input */
      91             :     int         last_len2;      /* Length of last buf2 string/strxfrm() blob */
      92             :     int         last_returned;  /* Last comparison result (cache) */
      93             :     bool        cache_blob;     /* Does buf2 contain strxfrm() blob, etc? */
      94             :     bool        collate_c;
      95             :     Oid         typid;          /* Actual datatype (text/bpchar/name) */
      96             :     hyperLogLogState abbr_card; /* Abbreviated key cardinality state */
      97             :     hyperLogLogState full_card; /* Full key cardinality state */
      98             :     double      prop_card;      /* Required cardinality proportion */
      99             :     pg_locale_t locale;
     100             : } VarStringSortSupport;
     101             : 
     102             : /*
     103             :  * Output data for split_text(): we output either to an array or a table.
     104             :  * tupstore and tupdesc must be set up in advance to output to a table.
     105             :  */
     106             : typedef struct
     107             : {
     108             :     ArrayBuildState *astate;
     109             :     Tuplestorestate *tupstore;
     110             :     TupleDesc   tupdesc;
     111             : } SplitTextOutputData;
     112             : 
     113             : /*
     114             :  * This should be large enough that most strings will fit, but small enough
     115             :  * that we feel comfortable putting it on the stack
     116             :  */
     117             : #define TEXTBUFLEN      1024
     118             : 
     119             : #define DatumGetVarStringP(X)       ((VarString *) PG_DETOAST_DATUM(X))
     120             : #define DatumGetVarStringPP(X)      ((VarString *) PG_DETOAST_DATUM_PACKED(X))
     121             : 
     122             : static int  varstrfastcmp_c(Datum x, Datum y, SortSupport ssup);
     123             : static int  bpcharfastcmp_c(Datum x, Datum y, SortSupport ssup);
     124             : static int  namefastcmp_c(Datum x, Datum y, SortSupport ssup);
     125             : static int  varlenafastcmp_locale(Datum x, Datum y, SortSupport ssup);
     126             : static int  namefastcmp_locale(Datum x, Datum y, SortSupport ssup);
     127             : static int  varstrfastcmp_locale(char *a1p, int len1, char *a2p, int len2, SortSupport ssup);
     128             : static Datum varstr_abbrev_convert(Datum original, SortSupport ssup);
     129             : static bool varstr_abbrev_abort(int memtupcount, SortSupport ssup);
     130             : static int32 text_length(Datum str);
     131             : static text *text_catenate(text *t1, text *t2);
     132             : static text *text_substring(Datum str,
     133             :                             int32 start,
     134             :                             int32 length,
     135             :                             bool length_not_specified);
     136             : static text *text_overlay(text *t1, text *t2, int sp, int sl);
     137             : static int  text_position(text *t1, text *t2, Oid collid);
     138             : static void text_position_setup(text *t1, text *t2, Oid collid, TextPositionState *state);
     139             : static bool text_position_next(TextPositionState *state);
     140             : static char *text_position_next_internal(char *start_ptr, TextPositionState *state);
     141             : static char *text_position_get_match_ptr(TextPositionState *state);
     142             : static int  text_position_get_match_pos(TextPositionState *state);
     143             : static void text_position_cleanup(TextPositionState *state);
     144             : static void check_collation_set(Oid collid);
     145             : static int  text_cmp(text *arg1, text *arg2, Oid collid);
     146             : static void appendStringInfoText(StringInfo str, const text *t);
     147             : static bool split_text(FunctionCallInfo fcinfo, SplitTextOutputData *tstate);
     148             : static void split_text_accum_result(SplitTextOutputData *tstate,
     149             :                                     text *field_value,
     150             :                                     text *null_string,
     151             :                                     Oid collation);
     152             : static text *array_to_text_internal(FunctionCallInfo fcinfo, ArrayType *v,
     153             :                                     const char *fldsep, const char *null_string);
     154             : static StringInfo makeStringAggState(FunctionCallInfo fcinfo);
     155             : static bool text_format_parse_digits(const char **ptr, const char *end_ptr,
     156             :                                      int *value);
     157             : static const char *text_format_parse_format(const char *start_ptr,
     158             :                                             const char *end_ptr,
     159             :                                             int *argpos, int *widthpos,
     160             :                                             int *flags, int *width);
     161             : static void text_format_string_conversion(StringInfo buf, char conversion,
     162             :                                           FmgrInfo *typOutputInfo,
     163             :                                           Datum value, bool isNull,
     164             :                                           int flags, int width);
     165             : static void text_format_append_string(StringInfo buf, const char *str,
     166             :                                       int flags, int width);
     167             : 
     168             : 
     169             : /*****************************************************************************
     170             :  *   CONVERSION ROUTINES EXPORTED FOR USE BY C CODE                          *
     171             :  *****************************************************************************/
     172             : 
     173             : /*
     174             :  * cstring_to_text
     175             :  *
     176             :  * Create a text value from a null-terminated C string.
     177             :  *
     178             :  * The new text value is freshly palloc'd with a full-size VARHDR.
     179             :  */
     180             : text *
     181    25725478 : cstring_to_text(const char *s)
     182             : {
     183    25725478 :     return cstring_to_text_with_len(s, strlen(s));
     184             : }
     185             : 
     186             : /*
     187             :  * cstring_to_text_with_len
     188             :  *
     189             :  * Same as cstring_to_text except the caller specifies the string length;
     190             :  * the string need not be null_terminated.
     191             :  */
     192             : text *
     193    28457162 : cstring_to_text_with_len(const char *s, int len)
     194             : {
     195    28457162 :     text       *result = (text *) palloc(len + VARHDRSZ);
     196             : 
     197    28457162 :     SET_VARSIZE(result, len + VARHDRSZ);
     198    28457162 :     memcpy(VARDATA(result), s, len);
     199             : 
     200    28457162 :     return result;
     201             : }
     202             : 
     203             : /*
     204             :  * text_to_cstring
     205             :  *
     206             :  * Create a palloc'd, null-terminated C string from a text value.
     207             :  *
     208             :  * We support being passed a compressed or toasted text value.
     209             :  * This is a bit bogus since such values shouldn't really be referred to as
     210             :  * "text *", but it seems useful for robustness.  If we didn't handle that
     211             :  * case here, we'd need another routine that did, anyway.
     212             :  */
     213             : char *
     214    18884524 : text_to_cstring(const text *t)
     215             : {
     216             :     /* must cast away the const, unfortunately */
     217    18884524 :     text       *tunpacked = pg_detoast_datum_packed(unconstify(text *, t));
     218    18884524 :     int         len = VARSIZE_ANY_EXHDR(tunpacked);
     219             :     char       *result;
     220             : 
     221    18884524 :     result = (char *) palloc(len + 1);
     222    18884524 :     memcpy(result, VARDATA_ANY(tunpacked), len);
     223    18884524 :     result[len] = '\0';
     224             : 
     225    18884524 :     if (tunpacked != t)
     226       46136 :         pfree(tunpacked);
     227             : 
     228    18884524 :     return result;
     229             : }
     230             : 
     231             : /*
     232             :  * text_to_cstring_buffer
     233             :  *
     234             :  * Copy a text value into a caller-supplied buffer of size dst_len.
     235             :  *
     236             :  * The text string is truncated if necessary to fit.  The result is
     237             :  * guaranteed null-terminated (unless dst_len == 0).
     238             :  *
     239             :  * We support being passed a compressed or toasted text value.
     240             :  * This is a bit bogus since such values shouldn't really be referred to as
     241             :  * "text *", but it seems useful for robustness.  If we didn't handle that
     242             :  * case here, we'd need another routine that did, anyway.
     243             :  */
     244             : void
     245        1006 : text_to_cstring_buffer(const text *src, char *dst, size_t dst_len)
     246             : {
     247             :     /* must cast away the const, unfortunately */
     248        1006 :     text       *srcunpacked = pg_detoast_datum_packed(unconstify(text *, src));
     249        1006 :     size_t      src_len = VARSIZE_ANY_EXHDR(srcunpacked);
     250             : 
     251        1006 :     if (dst_len > 0)
     252             :     {
     253        1006 :         dst_len--;
     254        1006 :         if (dst_len >= src_len)
     255        1006 :             dst_len = src_len;
     256             :         else                    /* ensure truncation is encoding-safe */
     257           0 :             dst_len = pg_mbcliplen(VARDATA_ANY(srcunpacked), src_len, dst_len);
     258        1006 :         memcpy(dst, VARDATA_ANY(srcunpacked), dst_len);
     259        1006 :         dst[dst_len] = '\0';
     260             :     }
     261             : 
     262        1006 :     if (srcunpacked != src)
     263           0 :         pfree(srcunpacked);
     264        1006 : }
     265             : 
     266             : 
     267             : /*****************************************************************************
     268             :  *   USER I/O ROUTINES                                                       *
     269             :  *****************************************************************************/
     270             : 
     271             : /*
     272             :  *      textin          - converts cstring to internal representation
     273             :  */
     274             : Datum
     275    22381938 : textin(PG_FUNCTION_ARGS)
     276             : {
     277    22381938 :     char       *inputText = PG_GETARG_CSTRING(0);
     278             : 
     279    22381938 :     PG_RETURN_TEXT_P(cstring_to_text(inputText));
     280             : }
     281             : 
     282             : /*
     283             :  *      textout         - converts internal representation to cstring
     284             :  */
     285             : Datum
     286     8276556 : textout(PG_FUNCTION_ARGS)
     287             : {
     288     8276556 :     Datum       txt = PG_GETARG_DATUM(0);
     289             : 
     290     8276556 :     PG_RETURN_CSTRING(TextDatumGetCString(txt));
     291             : }
     292             : 
     293             : /*
     294             :  *      textrecv            - converts external binary format to text
     295             :  */
     296             : Datum
     297          48 : textrecv(PG_FUNCTION_ARGS)
     298             : {
     299          48 :     StringInfo  buf = (StringInfo) PG_GETARG_POINTER(0);
     300             :     text       *result;
     301             :     char       *str;
     302             :     int         nbytes;
     303             : 
     304          48 :     str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
     305             : 
     306          48 :     result = cstring_to_text_with_len(str, nbytes);
     307          48 :     pfree(str);
     308          48 :     PG_RETURN_TEXT_P(result);
     309             : }
     310             : 
     311             : /*
     312             :  *      textsend            - converts text to binary format
     313             :  */
     314             : Datum
     315        4756 : textsend(PG_FUNCTION_ARGS)
     316             : {
     317        4756 :     text       *t = PG_GETARG_TEXT_PP(0);
     318             :     StringInfoData buf;
     319             : 
     320        4756 :     pq_begintypsend(&buf);
     321        4756 :     pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t));
     322        4756 :     PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
     323             : }
     324             : 
     325             : 
     326             : /*
     327             :  *      unknownin           - converts cstring to internal representation
     328             :  */
     329             : Datum
     330           0 : unknownin(PG_FUNCTION_ARGS)
     331             : {
     332           0 :     char       *str = PG_GETARG_CSTRING(0);
     333             : 
     334             :     /* representation is same as cstring */
     335           0 :     PG_RETURN_CSTRING(pstrdup(str));
     336             : }
     337             : 
     338             : /*
     339             :  *      unknownout          - converts internal representation to cstring
     340             :  */
     341             : Datum
     342         940 : unknownout(PG_FUNCTION_ARGS)
     343             : {
     344             :     /* representation is same as cstring */
     345         940 :     char       *str = PG_GETARG_CSTRING(0);
     346             : 
     347         940 :     PG_RETURN_CSTRING(pstrdup(str));
     348             : }
     349             : 
     350             : /*
     351             :  *      unknownrecv         - converts external binary format to unknown
     352             :  */
     353             : Datum
     354           0 : unknownrecv(PG_FUNCTION_ARGS)
     355             : {
     356           0 :     StringInfo  buf = (StringInfo) PG_GETARG_POINTER(0);
     357             :     char       *str;
     358             :     int         nbytes;
     359             : 
     360           0 :     str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
     361             :     /* representation is same as cstring */
     362           0 :     PG_RETURN_CSTRING(str);
     363             : }
     364             : 
     365             : /*
     366             :  *      unknownsend         - converts unknown to binary format
     367             :  */
     368             : Datum
     369           0 : unknownsend(PG_FUNCTION_ARGS)
     370             : {
     371             :     /* representation is same as cstring */
     372           0 :     char       *str = PG_GETARG_CSTRING(0);
     373             :     StringInfoData buf;
     374             : 
     375           0 :     pq_begintypsend(&buf);
     376           0 :     pq_sendtext(&buf, str, strlen(str));
     377           0 :     PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
     378             : }
     379             : 
     380             : 
     381             : /* ========== PUBLIC ROUTINES ========== */
     382             : 
     383             : /*
     384             :  * textlen -
     385             :  *    returns the logical length of a text*
     386             :  *     (which is less than the VARSIZE of the text*)
     387             :  */
     388             : Datum
     389      430896 : textlen(PG_FUNCTION_ARGS)
     390             : {
     391      430896 :     Datum       str = PG_GETARG_DATUM(0);
     392             : 
     393             :     /* try to avoid decompressing argument */
     394      430896 :     PG_RETURN_INT32(text_length(str));
     395             : }
     396             : 
     397             : /*
     398             :  * text_length -
     399             :  *  Does the real work for textlen()
     400             :  *
     401             :  *  This is broken out so it can be called directly by other string processing
     402             :  *  functions.  Note that the argument is passed as a Datum, to indicate that
     403             :  *  it may still be in compressed form.  We can avoid decompressing it at all
     404             :  *  in some cases.
     405             :  */
     406             : static int32
     407      430908 : text_length(Datum str)
     408             : {
     409             :     /* fastpath when max encoding length is one */
     410      430908 :     if (pg_database_encoding_max_length() == 1)
     411          20 :         return (toast_raw_datum_size(str) - VARHDRSZ);
     412             :     else
     413             :     {
     414      430888 :         text       *t = DatumGetTextPP(str);
     415             : 
     416      430888 :         return (pg_mbstrlen_with_len(VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)));
     417             :     }
     418             : }
     419             : 
     420             : /*
     421             :  * textoctetlen -
     422             :  *    returns the physical length of a text*
     423             :  *     (which is less than the VARSIZE of the text*)
     424             :  */
     425             : Datum
     426          70 : textoctetlen(PG_FUNCTION_ARGS)
     427             : {
     428          70 :     Datum       str = PG_GETARG_DATUM(0);
     429             : 
     430             :     /* We need not detoast the input at all */
     431          70 :     PG_RETURN_INT32(toast_raw_datum_size(str) - VARHDRSZ);
     432             : }
     433             : 
     434             : /*
     435             :  * textcat -
     436             :  *    takes two text* and returns a text* that is the concatenation of
     437             :  *    the two.
     438             :  *
     439             :  * Rewritten by Sapa, sapa@hq.icb.chel.su. 8-Jul-96.
     440             :  * Updated by Thomas, Thomas.Lockhart@jpl.nasa.gov 1997-07-10.
     441             :  * Allocate space for output in all cases.
     442             :  * XXX - thomas 1997-07-10
     443             :  */
     444             : Datum
     445     1963182 : textcat(PG_FUNCTION_ARGS)
     446             : {
     447     1963182 :     text       *t1 = PG_GETARG_TEXT_PP(0);
     448     1963182 :     text       *t2 = PG_GETARG_TEXT_PP(1);
     449             : 
     450     1963182 :     PG_RETURN_TEXT_P(text_catenate(t1, t2));
     451             : }
     452             : 
     453             : /*
     454             :  * text_catenate
     455             :  *  Guts of textcat(), broken out so it can be used by other functions
     456             :  *
     457             :  * Arguments can be in short-header form, but not compressed or out-of-line
     458             :  */
     459             : static text *
     460     1963262 : text_catenate(text *t1, text *t2)
     461             : {
     462             :     text       *result;
     463             :     int         len1,
     464             :                 len2,
     465             :                 len;
     466             :     char       *ptr;
     467             : 
     468     1963262 :     len1 = VARSIZE_ANY_EXHDR(t1);
     469     1963262 :     len2 = VARSIZE_ANY_EXHDR(t2);
     470             : 
     471             :     /* paranoia ... probably should throw error instead? */
     472     1963262 :     if (len1 < 0)
     473           0 :         len1 = 0;
     474     1963262 :     if (len2 < 0)
     475           0 :         len2 = 0;
     476             : 
     477     1963262 :     len = len1 + len2 + VARHDRSZ;
     478     1963262 :     result = (text *) palloc(len);
     479             : 
     480             :     /* Set size of result string... */
     481     1963262 :     SET_VARSIZE(result, len);
     482             : 
     483             :     /* Fill data field of result string... */
     484     1963262 :     ptr = VARDATA(result);
     485     1963262 :     if (len1 > 0)
     486     1962438 :         memcpy(ptr, VARDATA_ANY(t1), len1);
     487     1963262 :     if (len2 > 0)
     488     1963052 :         memcpy(ptr + len1, VARDATA_ANY(t2), len2);
     489             : 
     490     1963262 :     return result;
     491             : }
     492             : 
     493             : /*
     494             :  * charlen_to_bytelen()
     495             :  *  Compute the number of bytes occupied by n characters starting at *p
     496             :  *
     497             :  * The caller shall ensure there are n complete characters.  Callers achieve
     498             :  * this by deriving "n" from regmatch_t findings from searching a wchar array.
     499             :  * pg_mb2wchar_with_len() skips any trailing incomplete character, so regex
     500             :  * matches will end no later than the last complete character.  (The string
     501             :  * need not be null-terminated.)
     502             :  */
     503             : static int
     504       17332 : charlen_to_bytelen(const char *p, int n)
     505             : {
     506       17332 :     if (pg_database_encoding_max_length() == 1)
     507             :     {
     508             :         /* Optimization for single-byte encodings */
     509         180 :         return n;
     510             :     }
     511             :     else
     512             :     {
     513             :         const char *s;
     514             : 
     515     6064610 :         for (s = p; n > 0; n--)
     516     6047458 :             s += pg_mblen_unbounded(s); /* caller verified encoding */
     517             : 
     518       17152 :         return s - p;
     519             :     }
     520             : }
     521             : 
     522             : /*
     523             :  * text_substr()
     524             :  * Return a substring starting at the specified position.
     525             :  * - thomas 1997-12-31
     526             :  *
     527             :  * Input:
     528             :  *  - string
     529             :  *  - starting position (is one-based)
     530             :  *  - string length
     531             :  *
     532             :  * If the starting position is zero or less, then return from the start of the string
     533             :  *  adjusting the length to be consistent with the "negative start" per SQL.
     534             :  * If the length is less than zero, return the remaining string.
     535             :  *
     536             :  * Added multibyte support.
     537             :  * - Tatsuo Ishii 1998-4-21
     538             :  * Changed behavior if starting position is less than one to conform to SQL behavior.
     539             :  * Formerly returned the entire string; now returns a portion.
     540             :  * - Thomas Lockhart 1998-12-10
     541             :  * Now uses faster TOAST-slicing interface
     542             :  * - John Gray 2002-02-22
     543             :  * Remove "#ifdef MULTIBYTE" and test for encoding_max_length instead. Change
     544             :  * behaviors conflicting with SQL to meet SQL (if E = S + L < S throw
     545             :  * error; if E < 1, return '', not entire string). Fixed MB related bug when
     546             :  * S > LC and < LC + 4 sometimes garbage characters are returned.
     547             :  * - Joe Conway 2002-08-10
     548             :  */
     549             : Datum
     550      661960 : text_substr(PG_FUNCTION_ARGS)
     551             : {
     552      661960 :     PG_RETURN_TEXT_P(text_substring(PG_GETARG_DATUM(0),
     553             :                                     PG_GETARG_INT32(1),
     554             :                                     PG_GETARG_INT32(2),
     555             :                                     false));
     556             : }
     557             : 
     558             : /*
     559             :  * text_substr_no_len -
     560             :  *    Wrapper to avoid opr_sanity failure due to
     561             :  *    one function accepting a different number of args.
     562             :  */
     563             : Datum
     564          36 : text_substr_no_len(PG_FUNCTION_ARGS)
     565             : {
     566          36 :     PG_RETURN_TEXT_P(text_substring(PG_GETARG_DATUM(0),
     567             :                                     PG_GETARG_INT32(1),
     568             :                                     -1, true));
     569             : }
     570             : 
     571             : /*
     572             :  * text_substring -
     573             :  *  Does the real work for text_substr() and text_substr_no_len()
     574             :  *
     575             :  *  This is broken out so it can be called directly by other string processing
     576             :  *  functions.  Note that the argument is passed as a Datum, to indicate that
     577             :  *  it may still be in compressed/toasted form.  We can avoid detoasting all
     578             :  *  of it in some cases.
     579             :  *
     580             :  *  The result is always a freshly palloc'd datum.
     581             :  */
     582             : static text *
     583      702108 : text_substring(Datum str, int32 start, int32 length, bool length_not_specified)
     584             : {
     585      702108 :     int32       eml = pg_database_encoding_max_length();
     586      702108 :     int32       S = start;      /* start position */
     587             :     int32       S1;             /* adjusted start position */
     588             :     int32       L1;             /* adjusted substring length */
     589             :     int32       E;              /* end position */
     590             : 
     591             :     /*
     592             :      * SQL99 says S can be zero or negative (which we don't document), but we
     593             :      * still must fetch from the start of the string.
     594             :      * https://www.postgresql.org/message-id/170905442373.643.11536838320909376197%40wrigleys.postgresql.org
     595             :      */
     596      702108 :     S1 = Max(S, 1);
     597             : 
     598             :     /* life is easy if the encoding max length is 1 */
     599      702108 :     if (eml == 1)
     600             :     {
     601          22 :         if (length_not_specified)   /* special case - get length to end of
     602             :                                      * string */
     603           0 :             L1 = -1;
     604          22 :         else if (length < 0)
     605             :         {
     606             :             /* SQL99 says to throw an error for E < S, i.e., negative length */
     607           0 :             ereport(ERROR,
     608             :                     (errcode(ERRCODE_SUBSTRING_ERROR),
     609             :                      errmsg("negative substring length not allowed")));
     610             :             L1 = -1;            /* silence stupider compilers */
     611             :         }
     612          22 :         else if (pg_add_s32_overflow(S, length, &E))
     613             :         {
     614             :             /*
     615             :              * L could be large enough for S + L to overflow, in which case
     616             :              * the substring must run to end of string.
     617             :              */
     618           0 :             L1 = -1;
     619             :         }
     620             :         else
     621             :         {
     622             :             /*
     623             :              * A zero or negative value for the end position can happen if the
     624             :              * start was negative or one. SQL99 says to return a zero-length
     625             :              * string.
     626             :              */
     627          22 :             if (E < 1)
     628           0 :                 return cstring_to_text("");
     629             : 
     630          22 :             L1 = E - S1;
     631             :         }
     632             : 
     633             :         /*
     634             :          * If the start position is past the end of the string, SQL99 says to
     635             :          * return a zero-length string -- DatumGetTextPSlice() will do that
     636             :          * for us.  We need only convert S1 to zero-based starting position.
     637             :          */
     638          22 :         return DatumGetTextPSlice(str, S1 - 1, L1);
     639             :     }
     640      702086 :     else if (eml > 1)
     641             :     {
     642             :         /*
     643             :          * When encoding max length is > 1, we can't get LC without
     644             :          * detoasting, so we'll grab a conservatively large slice now and go
     645             :          * back later to do the right thing
     646             :          */
     647             :         int32       slice_start;
     648             :         int32       slice_size;
     649             :         int32       slice_strlen;
     650             :         int32       slice_len;
     651             :         text       *slice;
     652             :         int32       E1;
     653             :         int32       i;
     654             :         char       *p;
     655             :         char       *s;
     656             :         text       *ret;
     657             : 
     658             :         /*
     659             :          * We need to start at position zero because there is no way to know
     660             :          * in advance which byte offset corresponds to the supplied start
     661             :          * position.
     662             :          */
     663      702086 :         slice_start = 0;
     664             : 
     665      702086 :         if (length_not_specified)   /* special case - get length to end of
     666             :                                      * string */
     667          76 :             slice_size = L1 = -1;
     668      702010 :         else if (length < 0)
     669             :         {
     670             :             /* SQL99 says to throw an error for E < S, i.e., negative length */
     671          12 :             ereport(ERROR,
     672             :                     (errcode(ERRCODE_SUBSTRING_ERROR),
     673             :                      errmsg("negative substring length not allowed")));
     674             :             slice_size = L1 = -1;   /* silence stupider compilers */
     675             :         }
     676      701998 :         else if (pg_add_s32_overflow(S, length, &E))
     677             :         {
     678             :             /*
     679             :              * L could be large enough for S + L to overflow, in which case
     680             :              * the substring must run to end of string.
     681             :              */
     682           6 :             slice_size = L1 = -1;
     683             :         }
     684             :         else
     685             :         {
     686             :             /*
     687             :              * A zero or negative value for the end position can happen if the
     688             :              * start was negative or one. SQL99 says to return a zero-length
     689             :              * string.
     690             :              */
     691      701992 :             if (E < 1)
     692           0 :                 return cstring_to_text("");
     693             : 
     694             :             /*
     695             :              * if E is past the end of the string, the tuple toaster will
     696             :              * truncate the length for us
     697             :              */
     698      701992 :             L1 = E - S1;
     699             : 
     700             :             /*
     701             :              * Total slice size in bytes can't be any longer than the start
     702             :              * position plus substring length times the encoding max length.
     703             :              * If that overflows, we can just use -1.
     704             :              */
     705      701992 :             if (pg_mul_s32_overflow(E, eml, &slice_size))
     706           6 :                 slice_size = -1;
     707             :         }
     708             : 
     709             :         /*
     710             :          * If we're working with an untoasted source, no need to do an extra
     711             :          * copying step.
     712             :          */
     713     1404082 :         if (VARATT_IS_COMPRESSED(DatumGetPointer(str)) ||
     714      702008 :             VARATT_IS_EXTERNAL(DatumGetPointer(str)))
     715         372 :             slice = DatumGetTextPSlice(str, slice_start, slice_size);
     716             :         else
     717      701702 :             slice = (text *) DatumGetPointer(str);
     718             : 
     719             :         /* see if we got back an empty string */
     720      702074 :         slice_len = VARSIZE_ANY_EXHDR(slice);
     721      702074 :         if (slice_len == 0)
     722             :         {
     723           0 :             if (slice != (text *) DatumGetPointer(str))
     724           0 :                 pfree(slice);
     725           0 :             return cstring_to_text("");
     726             :         }
     727             : 
     728             :         /* Now we can get the actual length of the slice in MB characters */
     729      702074 :         slice_strlen = pg_mbstrlen_with_len(VARDATA_ANY(slice),
     730             :                                             slice_len);
     731             : 
     732             :         /*
     733             :          * Check that the start position wasn't > slice_strlen. If so, SQL99
     734             :          * says to return a zero-length string.
     735             :          */
     736      702068 :         if (S1 > slice_strlen)
     737             :         {
     738          34 :             if (slice != (text *) DatumGetPointer(str))
     739           0 :                 pfree(slice);
     740          34 :             return cstring_to_text("");
     741             :         }
     742             : 
     743             :         /*
     744             :          * Adjust L1 and E1 now that we know the slice string length. Again
     745             :          * remember that S1 is one based, and slice_start is zero based.
     746             :          */
     747      702034 :         if (L1 > -1)
     748      701974 :             E1 = Min(S1 + L1, slice_start + 1 + slice_strlen);
     749             :         else
     750          60 :             E1 = slice_start + 1 + slice_strlen;
     751             : 
     752             :         /*
     753             :          * Find the start position in the slice; remember S1 is not zero based
     754             :          */
     755      702034 :         p = VARDATA_ANY(slice);
     756     6716658 :         for (i = 0; i < S1 - 1; i++)
     757     6014624 :             p += pg_mblen_unbounded(p);
     758             : 
     759             :         /* hang onto a pointer to our start position */
     760      702034 :         s = p;
     761             : 
     762             :         /*
     763             :          * Count the actual bytes used by the substring of the requested
     764             :          * length.
     765             :          */
     766     9963646 :         for (i = S1; i < E1; i++)
     767     9261612 :             p += pg_mblen_unbounded(p);
     768             : 
     769      702034 :         ret = (text *) palloc(VARHDRSZ + (p - s));
     770      702034 :         SET_VARSIZE(ret, VARHDRSZ + (p - s));
     771      702034 :         memcpy(VARDATA(ret), s, (p - s));
     772             : 
     773      702034 :         if (slice != (text *) DatumGetPointer(str))
     774         372 :             pfree(slice);
     775             : 
     776      702034 :         return ret;
     777             :     }
     778             :     else
     779           0 :         elog(ERROR, "invalid backend encoding: encoding max length < 1");
     780             : 
     781             :     /* not reached: suppress compiler warning */
     782             :     return NULL;
     783             : }
     784             : 
     785             : /*
     786             :  * textoverlay
     787             :  *  Replace specified substring of first string with second
     788             :  *
     789             :  * The SQL standard defines OVERLAY() in terms of substring and concatenation.
     790             :  * This code is a direct implementation of what the standard says.
     791             :  */
     792             : Datum
     793          28 : textoverlay(PG_FUNCTION_ARGS)
     794             : {
     795          28 :     text       *t1 = PG_GETARG_TEXT_PP(0);
     796          28 :     text       *t2 = PG_GETARG_TEXT_PP(1);
     797          28 :     int         sp = PG_GETARG_INT32(2);    /* substring start position */
     798          28 :     int         sl = PG_GETARG_INT32(3);    /* substring length */
     799             : 
     800          28 :     PG_RETURN_TEXT_P(text_overlay(t1, t2, sp, sl));
     801             : }
     802             : 
     803             : Datum
     804          12 : textoverlay_no_len(PG_FUNCTION_ARGS)
     805             : {
     806          12 :     text       *t1 = PG_GETARG_TEXT_PP(0);
     807          12 :     text       *t2 = PG_GETARG_TEXT_PP(1);
     808          12 :     int         sp = PG_GETARG_INT32(2);    /* substring start position */
     809             :     int         sl;
     810             : 
     811          12 :     sl = text_length(PointerGetDatum(t2));  /* defaults to length(t2) */
     812          12 :     PG_RETURN_TEXT_P(text_overlay(t1, t2, sp, sl));
     813             : }
     814             : 
     815             : static text *
     816          40 : text_overlay(text *t1, text *t2, int sp, int sl)
     817             : {
     818             :     text       *result;
     819             :     text       *s1;
     820             :     text       *s2;
     821             :     int         sp_pl_sl;
     822             : 
     823             :     /*
     824             :      * Check for possible integer-overflow cases.  For negative sp, throw a
     825             :      * "substring length" error because that's what should be expected
     826             :      * according to the spec's definition of OVERLAY().
     827             :      */
     828          40 :     if (sp <= 0)
     829           0 :         ereport(ERROR,
     830             :                 (errcode(ERRCODE_SUBSTRING_ERROR),
     831             :                  errmsg("negative substring length not allowed")));
     832          40 :     if (pg_add_s32_overflow(sp, sl, &sp_pl_sl))
     833           0 :         ereport(ERROR,
     834             :                 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
     835             :                  errmsg("integer out of range")));
     836             : 
     837          40 :     s1 = text_substring(PointerGetDatum(t1), 1, sp - 1, false);
     838          40 :     s2 = text_substring(PointerGetDatum(t1), sp_pl_sl, -1, true);
     839          40 :     result = text_catenate(s1, t2);
     840          40 :     result = text_catenate(result, s2);
     841             : 
     842          40 :     return result;
     843             : }
     844             : 
     845             : /*
     846             :  * textpos -
     847             :  *    Return the position of the specified substring.
     848             :  *    Implements the SQL POSITION() function.
     849             :  *    Ref: A Guide To The SQL Standard, Date & Darwen, 1997
     850             :  * - thomas 1997-07-27
     851             :  */
     852             : Datum
     853         136 : textpos(PG_FUNCTION_ARGS)
     854             : {
     855         136 :     text       *str = PG_GETARG_TEXT_PP(0);
     856         136 :     text       *search_str = PG_GETARG_TEXT_PP(1);
     857             : 
     858         136 :     PG_RETURN_INT32((int32) text_position(str, search_str, PG_GET_COLLATION()));
     859             : }
     860             : 
     861             : /*
     862             :  * text_position -
     863             :  *  Does the real work for textpos()
     864             :  *
     865             :  * Inputs:
     866             :  *      t1 - string to be searched
     867             :  *      t2 - pattern to match within t1
     868             :  * Result:
     869             :  *      Character index of the first matched char, starting from 1,
     870             :  *      or 0 if no match.
     871             :  *
     872             :  *  This is broken out so it can be called directly by other string processing
     873             :  *  functions.
     874             :  */
     875             : static int
     876         136 : text_position(text *t1, text *t2, Oid collid)
     877             : {
     878             :     TextPositionState state;
     879             :     int         result;
     880             : 
     881         136 :     check_collation_set(collid);
     882             : 
     883             :     /* Empty needle always matches at position 1 */
     884         136 :     if (VARSIZE_ANY_EXHDR(t2) < 1)
     885          12 :         return 1;
     886             : 
     887             :     /* Otherwise, can't match if haystack is shorter than needle */
     888         124 :     if (VARSIZE_ANY_EXHDR(t1) < VARSIZE_ANY_EXHDR(t2) &&
     889          22 :         pg_newlocale_from_collation(collid)->deterministic)
     890          22 :         return 0;
     891             : 
     892         102 :     text_position_setup(t1, t2, collid, &state);
     893             :     /* don't need greedy mode here */
     894         102 :     state.greedy = false;
     895             : 
     896         102 :     if (!text_position_next(&state))
     897          24 :         result = 0;
     898             :     else
     899          78 :         result = text_position_get_match_pos(&state);
     900         102 :     text_position_cleanup(&state);
     901         102 :     return result;
     902             : }
     903             : 
     904             : 
     905             : /*
     906             :  * text_position_setup, text_position_next, text_position_cleanup -
     907             :  *  Component steps of text_position()
     908             :  *
     909             :  * These are broken out so that a string can be efficiently searched for
     910             :  * multiple occurrences of the same pattern.  text_position_next may be
     911             :  * called multiple times, and it advances to the next match on each call.
     912             :  * text_position_get_match_ptr() and text_position_get_match_pos() return
     913             :  * a pointer or 1-based character position of the last match, respectively.
     914             :  *
     915             :  * The "state" variable is normally just a local variable in the caller.
     916             :  *
     917             :  * NOTE: text_position_next skips over the matched portion.  For example,
     918             :  * searching for "xx" in "xxx" returns only one match, not two.
     919             :  */
     920             : 
     921             : static void
     922        1930 : text_position_setup(text *t1, text *t2, Oid collid, TextPositionState *state)
     923             : {
     924        1930 :     int         len1 = VARSIZE_ANY_EXHDR(t1);
     925        1930 :     int         len2 = VARSIZE_ANY_EXHDR(t2);
     926             : 
     927        1930 :     check_collation_set(collid);
     928             : 
     929        1930 :     state->locale = pg_newlocale_from_collation(collid);
     930             : 
     931             :     /*
     932             :      * Most callers need greedy mode, but some might want to unset this to
     933             :      * optimize.
     934             :      */
     935        1930 :     state->greedy = true;
     936             : 
     937             :     Assert(len2 > 0);
     938             : 
     939             :     /*
     940             :      * Even with a multi-byte encoding, we perform the search using the raw
     941             :      * byte sequence, ignoring multibyte issues.  For UTF-8, that works fine,
     942             :      * because in UTF-8 the byte sequence of one character cannot contain
     943             :      * another character.  For other multi-byte encodings, we do the search
     944             :      * initially as a simple byte search, ignoring multibyte issues, but
     945             :      * verify afterwards that the match we found is at a character boundary,
     946             :      * and continue the search if it was a false match.
     947             :      */
     948        1930 :     if (pg_database_encoding_max_length() == 1)
     949         108 :         state->is_multibyte_char_in_char = false;
     950        1822 :     else if (GetDatabaseEncoding() == PG_UTF8)
     951        1822 :         state->is_multibyte_char_in_char = false;
     952             :     else
     953           0 :         state->is_multibyte_char_in_char = true;
     954             : 
     955        1930 :     state->str1 = VARDATA_ANY(t1);
     956        1930 :     state->str2 = VARDATA_ANY(t2);
     957        1930 :     state->len1 = len1;
     958        1930 :     state->len2 = len2;
     959        1930 :     state->last_match = NULL;
     960        1930 :     state->refpoint = state->str1;
     961        1930 :     state->refpos = 0;
     962             : 
     963             :     /*
     964             :      * Prepare the skip table for Boyer-Moore-Horspool searching.  In these
     965             :      * notes we use the terminology that the "haystack" is the string to be
     966             :      * searched (t1) and the "needle" is the pattern being sought (t2).
     967             :      *
     968             :      * If the needle is empty or bigger than the haystack then there is no
     969             :      * point in wasting cycles initializing the table.  We also choose not to
     970             :      * use B-M-H for needles of length 1, since the skip table can't possibly
     971             :      * save anything in that case.
     972             :      *
     973             :      * (With nondeterministic collations, the search is already
     974             :      * multibyte-aware, so we don't need this.)
     975             :      */
     976        1930 :     if (len1 >= len2 && len2 > 1 && state->locale->deterministic)
     977             :     {
     978        1596 :         int         searchlength = len1 - len2;
     979             :         int         skiptablemask;
     980             :         int         last;
     981             :         int         i;
     982        1596 :         const char *str2 = state->str2;
     983             : 
     984             :         /*
     985             :          * First we must determine how much of the skip table to use.  The
     986             :          * declaration of TextPositionState allows up to 256 elements, but for
     987             :          * short search problems we don't really want to have to initialize so
     988             :          * many elements --- it would take too long in comparison to the
     989             :          * actual search time.  So we choose a useful skip table size based on
     990             :          * the haystack length minus the needle length.  The closer the needle
     991             :          * length is to the haystack length the less useful skipping becomes.
     992             :          *
     993             :          * Note: since we use bit-masking to select table elements, the skip
     994             :          * table size MUST be a power of 2, and so the mask must be 2^N-1.
     995             :          */
     996        1596 :         if (searchlength < 16)
     997         114 :             skiptablemask = 3;
     998        1482 :         else if (searchlength < 64)
     999          34 :             skiptablemask = 7;
    1000        1448 :         else if (searchlength < 128)
    1001          26 :             skiptablemask = 15;
    1002        1422 :         else if (searchlength < 512)
    1003         332 :             skiptablemask = 31;
    1004        1090 :         else if (searchlength < 2048)
    1005         808 :             skiptablemask = 63;
    1006         282 :         else if (searchlength < 4096)
    1007         198 :             skiptablemask = 127;
    1008             :         else
    1009          84 :             skiptablemask = 255;
    1010        1596 :         state->skiptablemask = skiptablemask;
    1011             : 
    1012             :         /*
    1013             :          * Initialize the skip table.  We set all elements to the needle
    1014             :          * length, since this is the correct skip distance for any character
    1015             :          * not found in the needle.
    1016             :          */
    1017      111924 :         for (i = 0; i <= skiptablemask; i++)
    1018      110328 :             state->skiptable[i] = len2;
    1019             : 
    1020             :         /*
    1021             :          * Now examine the needle.  For each character except the last one,
    1022             :          * set the corresponding table element to the appropriate skip
    1023             :          * distance.  Note that when two characters share the same skip table
    1024             :          * entry, the one later in the needle must determine the skip
    1025             :          * distance.
    1026             :          */
    1027        1596 :         last = len2 - 1;
    1028             : 
    1029       20362 :         for (i = 0; i < last; i++)
    1030       18766 :             state->skiptable[(unsigned char) str2[i] & skiptablemask] = last - i;
    1031             :     }
    1032        1930 : }
    1033             : 
    1034             : /*
    1035             :  * Advance to the next match, starting from the end of the previous match
    1036             :  * (or the beginning of the string, on first call).  Returns true if a match
    1037             :  * is found.
    1038             :  *
    1039             :  * Note that this refuses to match an empty-string needle.  Most callers
    1040             :  * will have handled that case specially and we'll never see it here.
    1041             :  */
    1042             : static bool
    1043        9772 : text_position_next(TextPositionState *state)
    1044             : {
    1045        9772 :     int         needle_len = state->len2;
    1046             :     char       *start_ptr;
    1047             :     char       *matchptr;
    1048             : 
    1049        9772 :     if (needle_len <= 0)
    1050           0 :         return false;           /* result for empty pattern */
    1051             : 
    1052             :     /* Start from the point right after the previous match. */
    1053        9772 :     if (state->last_match)
    1054        7830 :         start_ptr = state->last_match + state->last_match_len;
    1055             :     else
    1056        1942 :         start_ptr = state->str1;
    1057             : 
    1058        9772 : retry:
    1059        9772 :     matchptr = text_position_next_internal(start_ptr, state);
    1060             : 
    1061        9772 :     if (!matchptr)
    1062        1840 :         return false;
    1063             : 
    1064             :     /*
    1065             :      * Found a match for the byte sequence.  If this is a multibyte encoding,
    1066             :      * where one character's byte sequence can appear inside a longer
    1067             :      * multi-byte character, we need to verify that the match was at a
    1068             :      * character boundary, not in the middle of a multi-byte character.
    1069             :      */
    1070        7932 :     if (state->is_multibyte_char_in_char && state->locale->deterministic)
    1071             :     {
    1072           0 :         const char *haystack_end = state->str1 + state->len1;
    1073             : 
    1074             :         /* Walk one character at a time, until we reach the match. */
    1075             : 
    1076             :         /* the search should never move backwards. */
    1077             :         Assert(state->refpoint <= matchptr);
    1078             : 
    1079           0 :         while (state->refpoint < matchptr)
    1080             :         {
    1081             :             /* step to next character. */
    1082           0 :             state->refpoint += pg_mblen_range(state->refpoint, haystack_end);
    1083           0 :             state->refpos++;
    1084             : 
    1085             :             /*
    1086             :              * If we stepped over the match's start position, then it was a
    1087             :              * false positive, where the byte sequence appeared in the middle
    1088             :              * of a multi-byte character.  Skip it, and continue the search at
    1089             :              * the next character boundary.
    1090             :              */
    1091           0 :             if (state->refpoint > matchptr)
    1092             :             {
    1093           0 :                 start_ptr = state->refpoint;
    1094           0 :                 goto retry;
    1095             :             }
    1096             :         }
    1097             :     }
    1098             : 
    1099        7932 :     state->last_match = matchptr;
    1100        7932 :     state->last_match_len = state->last_match_len_tmp;
    1101        7932 :     return true;
    1102             : }
    1103             : 
    1104             : /*
    1105             :  * Subroutine of text_position_next().  This searches for the raw byte
    1106             :  * sequence, ignoring any multi-byte encoding issues.  Returns the first
    1107             :  * match starting at 'start_ptr', or NULL if no match is found.
    1108             :  */
    1109             : static char *
    1110        9772 : text_position_next_internal(char *start_ptr, TextPositionState *state)
    1111             : {
    1112        9772 :     int         haystack_len = state->len1;
    1113        9772 :     int         needle_len = state->len2;
    1114        9772 :     int         skiptablemask = state->skiptablemask;
    1115        9772 :     const char *haystack = state->str1;
    1116        9772 :     const char *needle = state->str2;
    1117        9772 :     const char *haystack_end = &haystack[haystack_len];
    1118             :     const char *hptr;
    1119             : 
    1120             :     Assert(start_ptr >= haystack && start_ptr <= haystack_end);
    1121             :     Assert(needle_len > 0);
    1122             : 
    1123        9772 :     state->last_match_len_tmp = needle_len;
    1124             : 
    1125        9772 :     if (!state->locale->deterministic)
    1126             :     {
    1127             :         /*
    1128             :          * With a nondeterministic collation, we have to use an unoptimized
    1129             :          * route.  We walk through the haystack and see if at each position
    1130             :          * there is a substring of the remaining string that is equal to the
    1131             :          * needle under the given collation.
    1132             :          *
    1133             :          * Note, the found substring could have a different length than the
    1134             :          * needle.  Callers that want to skip over the found string need to
    1135             :          * read the length of the found substring from last_match_len rather
    1136             :          * than just using the length of their needle.
    1137             :          *
    1138             :          * Most callers will require "greedy" semantics, meaning that we need
    1139             :          * to find the longest such substring, not the shortest.  For callers
    1140             :          * that don't need greedy semantics, we can finish on the first match.
    1141             :          *
    1142             :          * This loop depends on the assumption that the needle is nonempty and
    1143             :          * any matching substring must also be nonempty.  (Even if the
    1144             :          * collation would accept an empty match, returning one would send
    1145             :          * callers that search for successive matches into an infinite loop.)
    1146             :          */
    1147         252 :         const char *result_hptr = NULL;
    1148             : 
    1149         252 :         hptr = start_ptr;
    1150         678 :         while (hptr < haystack_end)
    1151             :         {
    1152             :             const char *test_end;
    1153             : 
    1154             :             /*
    1155             :              * First check the common case that there is a match in the
    1156             :              * haystack of exactly the length of the needle.
    1157             :              */
    1158         564 :             if (!state->greedy &&
    1159         108 :                 haystack_end - hptr >= needle_len &&
    1160          54 :                 pg_strncoll(hptr, needle_len, needle, needle_len, state->locale) == 0)
    1161          12 :                 return (char *) hptr;
    1162             : 
    1163             :             /*
    1164             :              * Else check if any of the non-empty substrings starting at hptr
    1165             :              * compare equal to the needle.
    1166             :              */
    1167         552 :             test_end = hptr;
    1168             :             do
    1169             :             {
    1170        2154 :                 test_end += pg_mblen_range(test_end, haystack_end);
    1171        2154 :                 if (pg_strncoll(hptr, (test_end - hptr), needle, needle_len, state->locale) == 0)
    1172             :                 {
    1173         138 :                     state->last_match_len_tmp = (test_end - hptr);
    1174         138 :                     result_hptr = hptr;
    1175         138 :                     if (!state->greedy)
    1176           0 :                         break;
    1177             :                 }
    1178        2154 :             } while (test_end < haystack_end);
    1179             : 
    1180         552 :             if (result_hptr)
    1181         126 :                 break;
    1182             : 
    1183         426 :             hptr += pg_mblen_range(hptr, haystack_end);
    1184             :         }
    1185             : 
    1186         240 :         return (char *) result_hptr;
    1187             :     }
    1188        9520 :     else if (needle_len == 1)
    1189             :     {
    1190             :         /* No point in using B-M-H for a one-character needle */
    1191         760 :         char        nchar = *needle;
    1192             : 
    1193         760 :         hptr = start_ptr;
    1194        5878 :         while (hptr < haystack_end)
    1195             :         {
    1196        5712 :             if (*hptr == nchar)
    1197         594 :                 return (char *) hptr;
    1198        5118 :             hptr++;
    1199             :         }
    1200             :     }
    1201             :     else
    1202             :     {
    1203        8760 :         const char *needle_last = &needle[needle_len - 1];
    1204             : 
    1205             :         /* Start at startpos plus the length of the needle */
    1206        8760 :         hptr = start_ptr + needle_len - 1;
    1207      216720 :         while (hptr < haystack_end)
    1208             :         {
    1209             :             /* Match the needle scanning *backward* */
    1210             :             const char *nptr;
    1211             :             const char *p;
    1212             : 
    1213      215160 :             nptr = needle_last;
    1214      215160 :             p = hptr;
    1215      323146 :             while (*nptr == *p)
    1216             :             {
    1217             :                 /* Matched it all?  If so, return 1-based position */
    1218      115186 :                 if (nptr == needle)
    1219        7200 :                     return (char *) p;
    1220      107986 :                 nptr--, p--;
    1221             :             }
    1222             : 
    1223             :             /*
    1224             :              * No match, so use the haystack char at hptr to decide how far to
    1225             :              * advance.  If the needle had any occurrence of that character
    1226             :              * (or more precisely, one sharing the same skiptable entry)
    1227             :              * before its last character, then we advance far enough to align
    1228             :              * the last such needle character with that haystack position.
    1229             :              * Otherwise we can advance by the whole needle length.
    1230             :              */
    1231      207960 :             hptr += state->skiptable[(unsigned char) *hptr & skiptablemask];
    1232             :         }
    1233             :     }
    1234             : 
    1235        1726 :     return 0;                   /* not found */
    1236             : }
    1237             : 
    1238             : /*
    1239             :  * Return a pointer to the current match.
    1240             :  *
    1241             :  * The returned pointer points into the original haystack string.
    1242             :  */
    1243             : static char *
    1244        7824 : text_position_get_match_ptr(TextPositionState *state)
    1245             : {
    1246        7824 :     return state->last_match;
    1247             : }
    1248             : 
    1249             : /*
    1250             :  * Return the offset of the current match.
    1251             :  *
    1252             :  * The offset is in characters, 1-based.
    1253             :  */
    1254             : static int
    1255          78 : text_position_get_match_pos(TextPositionState *state)
    1256             : {
    1257             :     /* Convert the byte position to char position. */
    1258         156 :     state->refpos += pg_mbstrlen_with_len(state->refpoint,
    1259          78 :                                           state->last_match - state->refpoint);
    1260          78 :     state->refpoint = state->last_match;
    1261          78 :     return state->refpos + 1;
    1262             : }
    1263             : 
    1264             : /*
    1265             :  * Reset search state to the initial state installed by text_position_setup.
    1266             :  *
    1267             :  * The next call to text_position_next will search from the beginning
    1268             :  * of the string.
    1269             :  */
    1270             : static void
    1271          12 : text_position_reset(TextPositionState *state)
    1272             : {
    1273          12 :     state->last_match = NULL;
    1274          12 :     state->refpoint = state->str1;
    1275          12 :     state->refpos = 0;
    1276          12 : }
    1277             : 
    1278             : static void
    1279        1930 : text_position_cleanup(TextPositionState *state)
    1280             : {
    1281             :     /* no cleanup needed */
    1282        1930 : }
    1283             : 
    1284             : 
    1285             : static void
    1286    17555854 : check_collation_set(Oid collid)
    1287             : {
    1288    17555854 :     if (!OidIsValid(collid))
    1289             :     {
    1290             :         /*
    1291             :          * This typically means that the parser could not resolve a conflict
    1292             :          * of implicit collations, so report it that way.
    1293             :          */
    1294          30 :         ereport(ERROR,
    1295             :                 (errcode(ERRCODE_INDETERMINATE_COLLATION),
    1296             :                  errmsg("could not determine which collation to use for string comparison"),
    1297             :                  errhint("Use the COLLATE clause to set the collation explicitly.")));
    1298             :     }
    1299    17555824 : }
    1300             : 
    1301             : /*
    1302             :  * varstr_cmp()
    1303             :  *
    1304             :  * Comparison function for text strings with given lengths, using the
    1305             :  * appropriate locale. Returns an integer less than, equal to, or greater than
    1306             :  * zero, indicating whether arg1 is less than, equal to, or greater than arg2.
    1307             :  *
    1308             :  * Note: many functions that depend on this are marked leakproof; therefore,
    1309             :  * avoid reporting the actual contents of the input when throwing errors.
    1310             :  * All errors herein should be things that can't happen except on corrupt
    1311             :  * data, anyway; otherwise we will have trouble with indexing strings that
    1312             :  * would cause them.
    1313             :  */
    1314             : int
    1315    10004098 : varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid)
    1316             : {
    1317             :     int         result;
    1318             :     pg_locale_t mylocale;
    1319             : 
    1320    10004098 :     check_collation_set(collid);
    1321             : 
    1322    10004080 :     mylocale = pg_newlocale_from_collation(collid);
    1323             : 
    1324    10004080 :     if (mylocale->collate_is_c)
    1325             :     {
    1326     3929722 :         result = memcmp(arg1, arg2, Min(len1, len2));
    1327     3929722 :         if ((result == 0) && (len1 != len2))
    1328      136886 :             result = (len1 < len2) ? -1 : 1;
    1329             :     }
    1330             :     else
    1331             :     {
    1332             :         /*
    1333             :          * memcmp() can't tell us which of two unequal strings sorts first,
    1334             :          * but it's a cheap way to tell if they're equal.  Testing shows that
    1335             :          * memcmp() followed by strcoll() is only trivially slower than
    1336             :          * strcoll() by itself, so we don't lose much if this doesn't work out
    1337             :          * very often, and if it does - for example, because there are many
    1338             :          * equal strings in the input - then we win big by avoiding expensive
    1339             :          * collation-aware comparisons.
    1340             :          */
    1341     6074358 :         if (len1 == len2 && memcmp(arg1, arg2, len1) == 0)
    1342     1566672 :             return 0;
    1343             : 
    1344     4507686 :         result = pg_strncoll(arg1, len1, arg2, len2, mylocale);
    1345             : 
    1346             :         /* Break tie if necessary. */
    1347     4507686 :         if (result == 0 && mylocale->deterministic)
    1348             :         {
    1349           0 :             result = memcmp(arg1, arg2, Min(len1, len2));
    1350           0 :             if ((result == 0) && (len1 != len2))
    1351           0 :                 result = (len1 < len2) ? -1 : 1;
    1352             :         }
    1353             :     }
    1354             : 
    1355     8437408 :     return result;
    1356             : }
    1357             : 
    1358             : /* text_cmp()
    1359             :  * Internal comparison function for text strings.
    1360             :  * Returns -1, 0 or 1
    1361             :  */
    1362             : static int
    1363     7923582 : text_cmp(text *arg1, text *arg2, Oid collid)
    1364             : {
    1365             :     char       *a1p,
    1366             :                *a2p;
    1367             :     int         len1,
    1368             :                 len2;
    1369             : 
    1370     7923582 :     a1p = VARDATA_ANY(arg1);
    1371     7923582 :     a2p = VARDATA_ANY(arg2);
    1372             : 
    1373     7923582 :     len1 = VARSIZE_ANY_EXHDR(arg1);
    1374     7923582 :     len2 = VARSIZE_ANY_EXHDR(arg2);
    1375             : 
    1376     7923582 :     return varstr_cmp(a1p, len1, a2p, len2, collid);
    1377             : }
    1378             : 
    1379             : /*
    1380             :  * Comparison functions for text strings.
    1381             :  *
    1382             :  * Note: btree indexes need these routines not to leak memory; therefore,
    1383             :  * be careful to free working copies of toasted datums.  Most places don't
    1384             :  * need to be so careful.
    1385             :  */
    1386             : 
    1387             : Datum
    1388     6699894 : texteq(PG_FUNCTION_ARGS)
    1389             : {
    1390     6699894 :     Oid         collid = PG_GET_COLLATION();
    1391     6699894 :     pg_locale_t mylocale = 0;
    1392             :     bool        result;
    1393             : 
    1394     6699894 :     check_collation_set(collid);
    1395             : 
    1396     6699894 :     mylocale = pg_newlocale_from_collation(collid);
    1397             : 
    1398     6699894 :     if (mylocale->deterministic)
    1399             :     {
    1400     6695534 :         Datum       arg1 = PG_GETARG_DATUM(0);
    1401     6695534 :         Datum       arg2 = PG_GETARG_DATUM(1);
    1402             :         Size        len1,
    1403             :                     len2;
    1404             : 
    1405             :         /*
    1406             :          * Since we only care about equality or not-equality, we can avoid all
    1407             :          * the expense of strcoll() here, and just do bitwise comparison.  In
    1408             :          * fact, we don't even have to do a bitwise comparison if we can show
    1409             :          * the lengths of the strings are unequal; which might save us from
    1410             :          * having to detoast one or both values.
    1411             :          */
    1412     6695534 :         len1 = toast_raw_datum_size(arg1);
    1413     6695534 :         len2 = toast_raw_datum_size(arg2);
    1414     6695534 :         if (len1 != len2)
    1415     3186534 :             result = false;
    1416             :         else
    1417             :         {
    1418     3509000 :             text       *targ1 = DatumGetTextPP(arg1);
    1419     3509000 :             text       *targ2 = DatumGetTextPP(arg2);
    1420             : 
    1421     3509000 :             result = (memcmp(VARDATA_ANY(targ1), VARDATA_ANY(targ2),
    1422             :                              len1 - VARHDRSZ) == 0);
    1423             : 
    1424     3509000 :             PG_FREE_IF_COPY(targ1, 0);
    1425     3509000 :             PG_FREE_IF_COPY(targ2, 1);
    1426             :         }
    1427             :     }
    1428             :     else
    1429             :     {
    1430        4360 :         text       *arg1 = PG_GETARG_TEXT_PP(0);
    1431        4360 :         text       *arg2 = PG_GETARG_TEXT_PP(1);
    1432             : 
    1433        4360 :         result = (text_cmp(arg1, arg2, collid) == 0);
    1434             : 
    1435        4360 :         PG_FREE_IF_COPY(arg1, 0);
    1436        4360 :         PG_FREE_IF_COPY(arg2, 1);
    1437             :     }
    1438             : 
    1439     6699894 :     PG_RETURN_BOOL(result);
    1440             : }
    1441             : 
    1442             : Datum
    1443      408598 : textne(PG_FUNCTION_ARGS)
    1444             : {
    1445      408598 :     Oid         collid = PG_GET_COLLATION();
    1446             :     pg_locale_t mylocale;
    1447             :     bool        result;
    1448             : 
    1449      408598 :     check_collation_set(collid);
    1450             : 
    1451      408598 :     mylocale = pg_newlocale_from_collation(collid);
    1452             : 
    1453      408598 :     if (mylocale->deterministic)
    1454             :     {
    1455      408574 :         Datum       arg1 = PG_GETARG_DATUM(0);
    1456      408574 :         Datum       arg2 = PG_GETARG_DATUM(1);
    1457             :         Size        len1,
    1458             :                     len2;
    1459             : 
    1460             :         /* See comment in texteq() */
    1461      408574 :         len1 = toast_raw_datum_size(arg1);
    1462      408574 :         len2 = toast_raw_datum_size(arg2);
    1463      408574 :         if (len1 != len2)
    1464       22366 :             result = true;
    1465             :         else
    1466             :         {
    1467      386208 :             text       *targ1 = DatumGetTextPP(arg1);
    1468      386208 :             text       *targ2 = DatumGetTextPP(arg2);
    1469             : 
    1470      386208 :             result = (memcmp(VARDATA_ANY(targ1), VARDATA_ANY(targ2),
    1471             :                              len1 - VARHDRSZ) != 0);
    1472             : 
    1473      386208 :             PG_FREE_IF_COPY(targ1, 0);
    1474      386208 :             PG_FREE_IF_COPY(targ2, 1);
    1475             :         }
    1476             :     }
    1477             :     else
    1478             :     {
    1479          24 :         text       *arg1 = PG_GETARG_TEXT_PP(0);
    1480          24 :         text       *arg2 = PG_GETARG_TEXT_PP(1);
    1481             : 
    1482          24 :         result = (text_cmp(arg1, arg2, collid) != 0);
    1483             : 
    1484          24 :         PG_FREE_IF_COPY(arg1, 0);
    1485          24 :         PG_FREE_IF_COPY(arg2, 1);
    1486             :     }
    1487             : 
    1488      408598 :     PG_RETURN_BOOL(result);
    1489             : }
    1490             : 
    1491             : Datum
    1492      209294 : text_lt(PG_FUNCTION_ARGS)
    1493             : {
    1494      209294 :     text       *arg1 = PG_GETARG_TEXT_PP(0);
    1495      209294 :     text       *arg2 = PG_GETARG_TEXT_PP(1);
    1496             :     bool        result;
    1497             : 
    1498      209294 :     result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) < 0);
    1499             : 
    1500      209276 :     PG_FREE_IF_COPY(arg1, 0);
    1501      209276 :     PG_FREE_IF_COPY(arg2, 1);
    1502             : 
    1503      209276 :     PG_RETURN_BOOL(result);
    1504             : }
    1505             : 
    1506             : Datum
    1507      317726 : text_le(PG_FUNCTION_ARGS)
    1508             : {
    1509      317726 :     text       *arg1 = PG_GETARG_TEXT_PP(0);
    1510      317726 :     text       *arg2 = PG_GETARG_TEXT_PP(1);
    1511             :     bool        result;
    1512             : 
    1513      317726 :     result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) <= 0);
    1514             : 
    1515      317726 :     PG_FREE_IF_COPY(arg1, 0);
    1516      317726 :     PG_FREE_IF_COPY(arg2, 1);
    1517             : 
    1518      317726 :     PG_RETURN_BOOL(result);
    1519             : }
    1520             : 
    1521             : Datum
    1522      196076 : text_gt(PG_FUNCTION_ARGS)
    1523             : {
    1524      196076 :     text       *arg1 = PG_GETARG_TEXT_PP(0);
    1525      196076 :     text       *arg2 = PG_GETARG_TEXT_PP(1);
    1526             :     bool        result;
    1527             : 
    1528      196076 :     result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) > 0);
    1529             : 
    1530      196076 :     PG_FREE_IF_COPY(arg1, 0);
    1531      196076 :     PG_FREE_IF_COPY(arg2, 1);
    1532             : 
    1533      196076 :     PG_RETURN_BOOL(result);
    1534             : }
    1535             : 
    1536             : Datum
    1537      175720 : text_ge(PG_FUNCTION_ARGS)
    1538             : {
    1539      175720 :     text       *arg1 = PG_GETARG_TEXT_PP(0);
    1540      175720 :     text       *arg2 = PG_GETARG_TEXT_PP(1);
    1541             :     bool        result;
    1542             : 
    1543      175720 :     result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) >= 0);
    1544             : 
    1545      175720 :     PG_FREE_IF_COPY(arg1, 0);
    1546      175720 :     PG_FREE_IF_COPY(arg2, 1);
    1547             : 
    1548      175720 :     PG_RETURN_BOOL(result);
    1549             : }
    1550             : 
    1551             : Datum
    1552       37914 : text_starts_with(PG_FUNCTION_ARGS)
    1553             : {
    1554       37914 :     Datum       arg1 = PG_GETARG_DATUM(0);
    1555       37914 :     Datum       arg2 = PG_GETARG_DATUM(1);
    1556       37914 :     Oid         collid = PG_GET_COLLATION();
    1557             :     pg_locale_t mylocale;
    1558             :     bool        result;
    1559             :     Size        len1,
    1560             :                 len2;
    1561             : 
    1562       37914 :     check_collation_set(collid);
    1563             : 
    1564       37914 :     mylocale = pg_newlocale_from_collation(collid);
    1565             : 
    1566       37914 :     if (!mylocale->deterministic)
    1567           0 :         ereport(ERROR,
    1568             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    1569             :                  errmsg("nondeterministic collations are not supported for substring searches")));
    1570             : 
    1571       37914 :     len1 = toast_raw_datum_size(arg1);
    1572       37914 :     len2 = toast_raw_datum_size(arg2);
    1573       37914 :     if (len2 > len1)
    1574           0 :         result = false;
    1575             :     else
    1576             :     {
    1577       37914 :         text       *targ1 = text_substring(arg1, 1, len2, false);
    1578       37914 :         text       *targ2 = DatumGetTextPP(arg2);
    1579             : 
    1580       37914 :         result = (memcmp(VARDATA_ANY(targ1), VARDATA_ANY(targ2),
    1581             :                          VARSIZE_ANY_EXHDR(targ2)) == 0);
    1582             : 
    1583       37914 :         PG_FREE_IF_COPY(targ1, 0);
    1584       37914 :         PG_FREE_IF_COPY(targ2, 1);
    1585             :     }
    1586             : 
    1587       37914 :     PG_RETURN_BOOL(result);
    1588             : }
    1589             : 
    1590             : Datum
    1591     6704746 : bttextcmp(PG_FUNCTION_ARGS)
    1592             : {
    1593     6704746 :     text       *arg1 = PG_GETARG_TEXT_PP(0);
    1594     6704746 :     text       *arg2 = PG_GETARG_TEXT_PP(1);
    1595             :     int32       result;
    1596             : 
    1597     6704746 :     result = text_cmp(arg1, arg2, PG_GET_COLLATION());
    1598             : 
    1599     6704746 :     PG_FREE_IF_COPY(arg1, 0);
    1600     6704746 :     PG_FREE_IF_COPY(arg2, 1);
    1601             : 
    1602     6704746 :     PG_RETURN_INT32(result);
    1603             : }
    1604             : 
    1605             : Datum
    1606       89058 : bttextsortsupport(PG_FUNCTION_ARGS)
    1607             : {
    1608       89058 :     SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
    1609       89058 :     Oid         collid = ssup->ssup_collation;
    1610             :     MemoryContext oldcontext;
    1611             : 
    1612       89058 :     oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
    1613             : 
    1614             :     /* Use generic string SortSupport */
    1615       89058 :     varstr_sortsupport(ssup, TEXTOID, collid);
    1616             : 
    1617       89046 :     MemoryContextSwitchTo(oldcontext);
    1618             : 
    1619       89046 :     PG_RETURN_VOID();
    1620             : }
    1621             : 
    1622             : /*
    1623             :  * Generic sortsupport interface for character type's operator classes.
    1624             :  * Includes locale support, and support for BpChar semantics (i.e. removing
    1625             :  * trailing spaces before comparison).
    1626             :  *
    1627             :  * Relies on the assumption that text, VarChar, and BpChar all have the
    1628             :  * same representation.
    1629             :  */
    1630             : void
    1631      140046 : varstr_sortsupport(SortSupport ssup, Oid typid, Oid collid)
    1632             : {
    1633      140046 :     bool        abbreviate = ssup->abbreviate;
    1634      140046 :     bool        collate_c = false;
    1635             :     VarStringSortSupport *sss;
    1636             :     pg_locale_t locale;
    1637             : 
    1638      140046 :     check_collation_set(collid);
    1639             : 
    1640      140034 :     locale = pg_newlocale_from_collation(collid);
    1641             : 
    1642             :     /*
    1643             :      * If possible, set ssup->comparator to a function which can be used to
    1644             :      * directly compare two datums.  If we can do this, we'll avoid the
    1645             :      * overhead of a trip through the fmgr layer for every comparison, which
    1646             :      * can be substantial.
    1647             :      *
    1648             :      * Most typically, we'll set the comparator to varlenafastcmp_locale,
    1649             :      * which uses strcoll() to perform comparisons.  We use that for the
    1650             :      * BpChar case too, but type NAME uses namefastcmp_locale. However, if
    1651             :      * LC_COLLATE = C, we can make things quite a bit faster with
    1652             :      * varstrfastcmp_c, bpcharfastcmp_c, or namefastcmp_c, all of which use
    1653             :      * memcmp() rather than strcoll().
    1654             :      */
    1655      140034 :     if (locale->collate_is_c)
    1656             :     {
    1657       93350 :         if (typid == BPCHAROID)
    1658         308 :             ssup->comparator = bpcharfastcmp_c;
    1659       93042 :         else if (typid == NAMEOID)
    1660             :         {
    1661       49938 :             ssup->comparator = namefastcmp_c;
    1662             :             /* Not supporting abbreviation with type NAME, for now */
    1663       49938 :             abbreviate = false;
    1664             :         }
    1665             :         else
    1666       43104 :             ssup->comparator = varstrfastcmp_c;
    1667             : 
    1668       93350 :         collate_c = true;
    1669             :     }
    1670             :     else
    1671             :     {
    1672             :         /*
    1673             :          * We use varlenafastcmp_locale except for type NAME.
    1674             :          */
    1675       46684 :         if (typid == NAMEOID)
    1676             :         {
    1677           0 :             ssup->comparator = namefastcmp_locale;
    1678             :             /* Not supporting abbreviation with type NAME, for now */
    1679           0 :             abbreviate = false;
    1680             :         }
    1681             :         else
    1682       46684 :             ssup->comparator = varlenafastcmp_locale;
    1683             : 
    1684             :         /*
    1685             :          * Unfortunately, it seems that abbreviation for non-C collations is
    1686             :          * broken on many common platforms; see pg_strxfrm_enabled().
    1687             :          *
    1688             :          * Even apart from the risk of broken locales, it's possible that
    1689             :          * there are platforms where the use of abbreviated keys should be
    1690             :          * disabled at compile time.  For example, macOS's strxfrm()
    1691             :          * implementation is known to not effectively concentrate a
    1692             :          * significant amount of entropy from the original string in earlier
    1693             :          * transformed blobs.  It's possible that other supported platforms
    1694             :          * are similarly encumbered.  So, if we ever get past disabling this
    1695             :          * categorically, we may still want or need to disable it for
    1696             :          * particular platforms.
    1697             :          */
    1698       46684 :         if (!pg_strxfrm_enabled(locale))
    1699       45888 :             abbreviate = false;
    1700             :     }
    1701             : 
    1702             :     /*
    1703             :      * If we're using abbreviated keys, or if we're using a locale-aware
    1704             :      * comparison, we need to initialize a VarStringSortSupport object. Both
    1705             :      * cases will make use of the temporary buffers we initialize here for
    1706             :      * scratch space (and to detect requirement for BpChar semantics from
    1707             :      * caller), and the abbreviation case requires additional state.
    1708             :      */
    1709      140034 :     if (abbreviate || !collate_c)
    1710             :     {
    1711       70750 :         sss = palloc_object(VarStringSortSupport);
    1712       70750 :         sss->buf1 = palloc(TEXTBUFLEN);
    1713       70750 :         sss->buflen1 = TEXTBUFLEN;
    1714       70750 :         sss->buf2 = palloc(TEXTBUFLEN);
    1715       70750 :         sss->buflen2 = TEXTBUFLEN;
    1716             :         /* Start with invalid values */
    1717       70750 :         sss->last_len1 = -1;
    1718       70750 :         sss->last_len2 = -1;
    1719             :         /* Initialize */
    1720       70750 :         sss->last_returned = 0;
    1721       70750 :         if (collate_c)
    1722       24066 :             sss->locale = NULL;
    1723             :         else
    1724       46684 :             sss->locale = locale;
    1725             : 
    1726             :         /*
    1727             :          * To avoid somehow confusing a strxfrm() blob and an original string,
    1728             :          * constantly keep track of the variety of data that buf1 and buf2
    1729             :          * currently contain.
    1730             :          *
    1731             :          * Comparisons may be interleaved with conversion calls.  Frequently,
    1732             :          * conversions and comparisons are batched into two distinct phases,
    1733             :          * but the correctness of caching cannot hinge upon this.  For
    1734             :          * comparison caching, buffer state is only trusted if cache_blob is
    1735             :          * found set to false, whereas strxfrm() caching only trusts the state
    1736             :          * when cache_blob is found set to true.
    1737             :          *
    1738             :          * Arbitrarily initialize cache_blob to true.
    1739             :          */
    1740       70750 :         sss->cache_blob = true;
    1741       70750 :         sss->collate_c = collate_c;
    1742       70750 :         sss->typid = typid;
    1743       70750 :         ssup->ssup_extra = sss;
    1744             : 
    1745             :         /*
    1746             :          * If possible, plan to use the abbreviated keys optimization.  The
    1747             :          * core code may switch back to authoritative comparator should
    1748             :          * abbreviation be aborted.
    1749             :          */
    1750       70750 :         if (abbreviate)
    1751             :         {
    1752       24664 :             sss->prop_card = 0.20;
    1753       24664 :             initHyperLogLog(&sss->abbr_card, 10);
    1754       24664 :             initHyperLogLog(&sss->full_card, 10);
    1755       24664 :             ssup->abbrev_full_comparator = ssup->comparator;
    1756       24664 :             ssup->comparator = ssup_datum_unsigned_cmp;
    1757       24664 :             ssup->abbrev_converter = varstr_abbrev_convert;
    1758       24664 :             ssup->abbrev_abort = varstr_abbrev_abort;
    1759             :         }
    1760             :     }
    1761      140034 : }
    1762             : 
    1763             : /*
    1764             :  * sortsupport comparison func (for C locale case)
    1765             :  */
    1766             : static int
    1767    46252562 : varstrfastcmp_c(Datum x, Datum y, SortSupport ssup)
    1768             : {
    1769    46252562 :     VarString  *arg1 = DatumGetVarStringPP(x);
    1770    46252562 :     VarString  *arg2 = DatumGetVarStringPP(y);
    1771             :     char       *a1p,
    1772             :                *a2p;
    1773             :     int         len1,
    1774             :                 len2,
    1775             :                 result;
    1776             : 
    1777    46252562 :     a1p = VARDATA_ANY(arg1);
    1778    46252562 :     a2p = VARDATA_ANY(arg2);
    1779             : 
    1780    46252562 :     len1 = VARSIZE_ANY_EXHDR(arg1);
    1781    46252562 :     len2 = VARSIZE_ANY_EXHDR(arg2);
    1782             : 
    1783    46252562 :     result = memcmp(a1p, a2p, Min(len1, len2));
    1784    46252562 :     if ((result == 0) && (len1 != len2))
    1785     1211942 :         result = (len1 < len2) ? -1 : 1;
    1786             : 
    1787             :     /* We can't afford to leak memory here. */
    1788    46252562 :     if (PointerGetDatum(arg1) != x)
    1789           0 :         pfree(arg1);
    1790    46252562 :     if (PointerGetDatum(arg2) != y)
    1791           0 :         pfree(arg2);
    1792             : 
    1793    46252562 :     return result;
    1794             : }
    1795             : 
    1796             : /*
    1797             :  * sortsupport comparison func (for BpChar C locale case)
    1798             :  *
    1799             :  * BpChar outsources its sortsupport to this module.  Specialization for the
    1800             :  * varstr_sortsupport BpChar case, modeled on
    1801             :  * internal_bpchar_pattern_compare().
    1802             :  */
    1803             : static int
    1804       62420 : bpcharfastcmp_c(Datum x, Datum y, SortSupport ssup)
    1805             : {
    1806       62420 :     BpChar     *arg1 = DatumGetBpCharPP(x);
    1807       62420 :     BpChar     *arg2 = DatumGetBpCharPP(y);
    1808             :     char       *a1p,
    1809             :                *a2p;
    1810             :     int         len1,
    1811             :                 len2,
    1812             :                 result;
    1813             : 
    1814       62420 :     a1p = VARDATA_ANY(arg1);
    1815       62420 :     a2p = VARDATA_ANY(arg2);
    1816             : 
    1817       62420 :     len1 = bpchartruelen(a1p, VARSIZE_ANY_EXHDR(arg1));
    1818       62420 :     len2 = bpchartruelen(a2p, VARSIZE_ANY_EXHDR(arg2));
    1819             : 
    1820       62420 :     result = memcmp(a1p, a2p, Min(len1, len2));
    1821       62420 :     if ((result == 0) && (len1 != len2))
    1822           4 :         result = (len1 < len2) ? -1 : 1;
    1823             : 
    1824             :     /* We can't afford to leak memory here. */
    1825       62420 :     if (PointerGetDatum(arg1) != x)
    1826           0 :         pfree(arg1);
    1827       62420 :     if (PointerGetDatum(arg2) != y)
    1828           0 :         pfree(arg2);
    1829             : 
    1830       62420 :     return result;
    1831             : }
    1832             : 
    1833             : /*
    1834             :  * sortsupport comparison func (for NAME C locale case)
    1835             :  */
    1836             : static int
    1837    45508918 : namefastcmp_c(Datum x, Datum y, SortSupport ssup)
    1838             : {
    1839    45508918 :     Name        arg1 = DatumGetName(x);
    1840    45508918 :     Name        arg2 = DatumGetName(y);
    1841             : 
    1842    45508918 :     return strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN);
    1843             : }
    1844             : 
    1845             : /*
    1846             :  * sortsupport comparison func (for locale case with all varlena types)
    1847             :  */
    1848             : static int
    1849    37756682 : varlenafastcmp_locale(Datum x, Datum y, SortSupport ssup)
    1850             : {
    1851    37756682 :     VarString  *arg1 = DatumGetVarStringPP(x);
    1852    37756682 :     VarString  *arg2 = DatumGetVarStringPP(y);
    1853             :     char       *a1p,
    1854             :                *a2p;
    1855             :     int         len1,
    1856             :                 len2,
    1857             :                 result;
    1858             : 
    1859    37756682 :     a1p = VARDATA_ANY(arg1);
    1860    37756682 :     a2p = VARDATA_ANY(arg2);
    1861             : 
    1862    37756682 :     len1 = VARSIZE_ANY_EXHDR(arg1);
    1863    37756682 :     len2 = VARSIZE_ANY_EXHDR(arg2);
    1864             : 
    1865    37756682 :     result = varstrfastcmp_locale(a1p, len1, a2p, len2, ssup);
    1866             : 
    1867             :     /* We can't afford to leak memory here. */
    1868    37756682 :     if (PointerGetDatum(arg1) != x)
    1869           0 :         pfree(arg1);
    1870    37756682 :     if (PointerGetDatum(arg2) != y)
    1871           0 :         pfree(arg2);
    1872             : 
    1873    37756682 :     return result;
    1874             : }
    1875             : 
    1876             : /*
    1877             :  * sortsupport comparison func (for locale case with NAME type)
    1878             :  */
    1879             : static int
    1880           0 : namefastcmp_locale(Datum x, Datum y, SortSupport ssup)
    1881             : {
    1882           0 :     Name        arg1 = DatumGetName(x);
    1883           0 :     Name        arg2 = DatumGetName(y);
    1884             : 
    1885           0 :     return varstrfastcmp_locale(NameStr(*arg1), strlen(NameStr(*arg1)),
    1886           0 :                                 NameStr(*arg2), strlen(NameStr(*arg2)),
    1887             :                                 ssup);
    1888             : }
    1889             : 
    1890             : /*
    1891             :  * sortsupport comparison func for locale cases
    1892             :  */
    1893             : static int
    1894    37756682 : varstrfastcmp_locale(char *a1p, int len1, char *a2p, int len2, SortSupport ssup)
    1895             : {
    1896    37756682 :     VarStringSortSupport *sss = (VarStringSortSupport *) ssup->ssup_extra;
    1897             :     int         result;
    1898             :     bool        arg1_match;
    1899             : 
    1900             :     /* Fast pre-check for equality, as discussed in varstr_cmp() */
    1901    37756682 :     if (len1 == len2 && memcmp(a1p, a2p, len1) == 0)
    1902             :     {
    1903             :         /*
    1904             :          * No change in buf1 or buf2 contents, so avoid changing last_len1 or
    1905             :          * last_len2.  Existing contents of buffers might still be used by
    1906             :          * next call.
    1907             :          *
    1908             :          * It's fine to allow the comparison of BpChar padding bytes here,
    1909             :          * even though that implies that the memcmp() will usually be
    1910             :          * performed for BpChar callers (though multibyte characters could
    1911             :          * still prevent that from occurring).  The memcmp() is still very
    1912             :          * cheap, and BpChar's funny semantics have us remove trailing spaces
    1913             :          * (not limited to padding), so we need make no distinction between
    1914             :          * padding space characters and "real" space characters.
    1915             :          */
    1916     9374936 :         return 0;
    1917             :     }
    1918             : 
    1919    28381746 :     if (sss->typid == BPCHAROID)
    1920             :     {
    1921             :         /* Get true number of bytes, ignoring trailing spaces */
    1922       39062 :         len1 = bpchartruelen(a1p, len1);
    1923       39062 :         len2 = bpchartruelen(a2p, len2);
    1924             :     }
    1925             : 
    1926    28381746 :     if (len1 >= sss->buflen1)
    1927             :     {
    1928          10 :         sss->buflen1 = Max(len1 + 1, Min(sss->buflen1 * 2, MaxAllocSize));
    1929          10 :         sss->buf1 = repalloc(sss->buf1, sss->buflen1);
    1930             :     }
    1931    28381746 :     if (len2 >= sss->buflen2)
    1932             :     {
    1933           6 :         sss->buflen2 = Max(len2 + 1, Min(sss->buflen2 * 2, MaxAllocSize));
    1934           6 :         sss->buf2 = repalloc(sss->buf2, sss->buflen2);
    1935             :     }
    1936             : 
    1937             :     /*
    1938             :      * We're likely to be asked to compare the same strings repeatedly, and
    1939             :      * memcmp() is so much cheaper than strcoll() that it pays to try to cache
    1940             :      * comparisons, even though in general there is no reason to think that
    1941             :      * that will work out (every string datum may be unique).  Caching does
    1942             :      * not slow things down measurably when it doesn't work out, and can speed
    1943             :      * things up by rather a lot when it does.  In part, this is because the
    1944             :      * memcmp() compares data from cachelines that are needed in L1 cache even
    1945             :      * when the last comparison's result cannot be reused.
    1946             :      */
    1947    28381746 :     arg1_match = true;
    1948    28381746 :     if (len1 != sss->last_len1 || memcmp(sss->buf1, a1p, len1) != 0)
    1949             :     {
    1950    26304842 :         arg1_match = false;
    1951    26304842 :         memcpy(sss->buf1, a1p, len1);
    1952    26304842 :         sss->buf1[len1] = '\0';
    1953    26304842 :         sss->last_len1 = len1;
    1954             :     }
    1955             : 
    1956             :     /*
    1957             :      * If we're comparing the same two strings as last time, we can return the
    1958             :      * same answer without calling strcoll() again.  This is more likely than
    1959             :      * it seems (at least with moderate to low cardinality sets), because
    1960             :      * quicksort compares the same pivot against many values.
    1961             :      */
    1962    28381746 :     if (len2 != sss->last_len2 || memcmp(sss->buf2, a2p, len2) != 0)
    1963             :     {
    1964     4347362 :         memcpy(sss->buf2, a2p, len2);
    1965     4347362 :         sss->buf2[len2] = '\0';
    1966     4347362 :         sss->last_len2 = len2;
    1967             :     }
    1968    24034384 :     else if (arg1_match && !sss->cache_blob)
    1969             :     {
    1970             :         /* Use result cached following last actual strcoll() call */
    1971     1621758 :         return sss->last_returned;
    1972             :     }
    1973             : 
    1974    26759988 :     result = pg_strcoll(sss->buf1, sss->buf2, sss->locale);
    1975             : 
    1976             :     /* Break tie if necessary. */
    1977    26759988 :     if (result == 0 && sss->locale->deterministic)
    1978           0 :         result = strcmp(sss->buf1, sss->buf2);
    1979             : 
    1980             :     /* Cache result, perhaps saving an expensive strcoll() call next time */
    1981    26759988 :     sss->cache_blob = false;
    1982    26759988 :     sss->last_returned = result;
    1983    26759988 :     return result;
    1984             : }
    1985             : 
    1986             : /*
    1987             :  * Conversion routine for sortsupport.  Converts original to abbreviated key
    1988             :  * representation.  Our encoding strategy is simple -- pack the first 8 bytes
    1989             :  * of a strxfrm() blob into a Datum (on little-endian machines, the 8 bytes are
    1990             :  * stored in reverse order), and treat it as an unsigned integer.  When the "C"
    1991             :  * locale is used just memcpy() from original instead.
    1992             :  */
    1993             : static Datum
    1994      844464 : varstr_abbrev_convert(Datum original, SortSupport ssup)
    1995             : {
    1996      844464 :     const size_t max_prefix_bytes = sizeof(Datum);
    1997      844464 :     VarStringSortSupport *sss = (VarStringSortSupport *) ssup->ssup_extra;
    1998      844464 :     VarString  *authoritative = DatumGetVarStringPP(original);
    1999      844464 :     char       *authoritative_data = VARDATA_ANY(authoritative);
    2000             : 
    2001             :     /* working state */
    2002             :     Datum       res;
    2003             :     char       *pres;
    2004             :     int         len;
    2005             :     uint32      hash;
    2006             : 
    2007      844464 :     pres = (char *) &res;
    2008             :     /* memset(), so any non-overwritten bytes are NUL */
    2009      844464 :     memset(pres, 0, max_prefix_bytes);
    2010      844464 :     len = VARSIZE_ANY_EXHDR(authoritative);
    2011             : 
    2012             :     /* Get number of bytes, ignoring trailing spaces */
    2013      844464 :     if (sss->typid == BPCHAROID)
    2014        1010 :         len = bpchartruelen(authoritative_data, len);
    2015             : 
    2016             :     /*
    2017             :      * If we're using the C collation, use memcpy(), rather than strxfrm(), to
    2018             :      * abbreviate keys.  The full comparator for the C locale is also
    2019             :      * memcmp().  This should be faster than strxfrm().
    2020             :      */
    2021      844464 :     if (sss->collate_c)
    2022      842628 :         memcpy(pres, authoritative_data, Min(len, max_prefix_bytes));
    2023             :     else
    2024             :     {
    2025             :         Size        bsize;
    2026             : 
    2027             :         /*
    2028             :          * We're not using the C collation, so fall back on strxfrm or ICU
    2029             :          * analogs.
    2030             :          */
    2031             : 
    2032             :         /* By convention, we use buffer 1 to store and NUL-terminate */
    2033        1836 :         if (len >= sss->buflen1)
    2034             :         {
    2035           0 :             sss->buflen1 = Max(len + 1, Min(sss->buflen1 * 2, MaxAllocSize));
    2036           0 :             sss->buf1 = repalloc(sss->buf1, sss->buflen1);
    2037             :         }
    2038             : 
    2039             :         /* Might be able to reuse strxfrm() blob from last call */
    2040        1836 :         if (sss->last_len1 == len && sss->cache_blob &&
    2041         918 :             memcmp(sss->buf1, authoritative_data, len) == 0)
    2042             :         {
    2043         168 :             memcpy(pres, sss->buf2, Min(max_prefix_bytes, sss->last_len2));
    2044             :             /* No change affecting cardinality, so no hashing required */
    2045         168 :             goto done;
    2046             :         }
    2047             : 
    2048        1668 :         memcpy(sss->buf1, authoritative_data, len);
    2049             : 
    2050             :         /*
    2051             :          * pg_strxfrm() and pg_strxfrm_prefix expect NUL-terminated strings.
    2052             :          */
    2053        1668 :         sss->buf1[len] = '\0';
    2054        1668 :         sss->last_len1 = len;
    2055             : 
    2056        1668 :         if (pg_strxfrm_prefix_enabled(sss->locale))
    2057             :         {
    2058        1668 :             if (sss->buflen2 < max_prefix_bytes)
    2059             :             {
    2060           0 :                 sss->buflen2 = Max(max_prefix_bytes,
    2061             :                                    Min(sss->buflen2 * 2, MaxAllocSize));
    2062           0 :                 sss->buf2 = repalloc(sss->buf2, sss->buflen2);
    2063             :             }
    2064             : 
    2065        1668 :             bsize = pg_strxfrm_prefix(sss->buf2, sss->buf1,
    2066             :                                       max_prefix_bytes, sss->locale);
    2067        1668 :             sss->last_len2 = bsize;
    2068             :         }
    2069             :         else
    2070             :         {
    2071             :             /*
    2072             :              * Loop: Call pg_strxfrm(), possibly enlarge buffer, and try
    2073             :              * again.  The pg_strxfrm() function leaves the result buffer
    2074             :              * content undefined if the result did not fit, so we need to
    2075             :              * retry until everything fits, even though we only need the first
    2076             :              * few bytes in the end.
    2077             :              */
    2078             :             for (;;)
    2079             :             {
    2080           0 :                 bsize = pg_strxfrm(sss->buf2, sss->buf1, sss->buflen2,
    2081             :                                    sss->locale);
    2082             : 
    2083           0 :                 sss->last_len2 = bsize;
    2084           0 :                 if (bsize < sss->buflen2)
    2085           0 :                     break;
    2086             : 
    2087             :                 /*
    2088             :                  * Grow buffer and retry.
    2089             :                  */
    2090           0 :                 sss->buflen2 = Max(bsize + 1,
    2091             :                                    Min(sss->buflen2 * 2, MaxAllocSize));
    2092           0 :                 sss->buf2 = repalloc(sss->buf2, sss->buflen2);
    2093             :             }
    2094             :         }
    2095             : 
    2096             :         /*
    2097             :          * Every Datum byte is always compared.  This is safe because the
    2098             :          * strxfrm() blob is itself NUL terminated, leaving no danger of
    2099             :          * misinterpreting any NUL bytes not intended to be interpreted as
    2100             :          * logically representing termination.
    2101             :          */
    2102        1668 :         memcpy(pres, sss->buf2, Min(max_prefix_bytes, bsize));
    2103             :     }
    2104             : 
    2105             :     /*
    2106             :      * Maintain approximate cardinality of both abbreviated keys and original,
    2107             :      * authoritative keys using HyperLogLog.  Used as cheap insurance against
    2108             :      * the worst case, where we do many string transformations for no saving
    2109             :      * in full strcoll()-based comparisons.  These statistics are used by
    2110             :      * varstr_abbrev_abort().
    2111             :      *
    2112             :      * First, Hash key proper, or a significant fraction of it.  Mix in length
    2113             :      * in order to compensate for cases where differences are past
    2114             :      * PG_CACHE_LINE_SIZE bytes, so as to limit the overhead of hashing.
    2115             :      */
    2116      844296 :     hash = DatumGetUInt32(hash_any((unsigned char *) authoritative_data,
    2117             :                                    Min(len, PG_CACHE_LINE_SIZE)));
    2118             : 
    2119      844296 :     if (len > PG_CACHE_LINE_SIZE)
    2120         192 :         hash ^= DatumGetUInt32(hash_uint32((uint32) len));
    2121             : 
    2122      844296 :     addHyperLogLog(&sss->full_card, hash);
    2123             : 
    2124             :     /* Hash abbreviated key */
    2125             :     {
    2126             :         uint32      tmp;
    2127             : 
    2128      844296 :         tmp = DatumGetUInt32(res) ^ (uint32) (DatumGetUInt64(res) >> 32);
    2129      844296 :         hash = DatumGetUInt32(hash_uint32(tmp));
    2130             :     }
    2131             : 
    2132      844296 :     addHyperLogLog(&sss->abbr_card, hash);
    2133             : 
    2134             :     /* Cache result, perhaps saving an expensive strxfrm() call next time */
    2135      844296 :     sss->cache_blob = true;
    2136      844464 : done:
    2137             : 
    2138             :     /*
    2139             :      * Byteswap on little-endian machines.
    2140             :      *
    2141             :      * This is needed so that ssup_datum_unsigned_cmp() (an unsigned integer
    2142             :      * 3-way comparator) works correctly on all platforms.  If we didn't do
    2143             :      * this, the comparator would have to call memcmp() with a pair of
    2144             :      * pointers to the first byte of each abbreviated key, which is slower.
    2145             :      */
    2146      844464 :     res = DatumBigEndianToNative(res);
    2147             : 
    2148             :     /* Don't leak memory here */
    2149      844464 :     if (PointerGetDatum(authoritative) != original)
    2150           2 :         pfree(authoritative);
    2151             : 
    2152      844464 :     return res;
    2153             : }
    2154             : 
    2155             : /*
    2156             :  * Callback for estimating effectiveness of abbreviated key optimization, using
    2157             :  * heuristic rules.  Returns value indicating if the abbreviation optimization
    2158             :  * should be aborted, based on its projected effectiveness.
    2159             :  */
    2160             : static bool
    2161        2378 : varstr_abbrev_abort(int memtupcount, SortSupport ssup)
    2162             : {
    2163        2378 :     VarStringSortSupport *sss = (VarStringSortSupport *) ssup->ssup_extra;
    2164             :     double      abbrev_distinct,
    2165             :                 key_distinct;
    2166             : 
    2167             :     Assert(ssup->abbreviate);
    2168             : 
    2169             :     /* Have a little patience */
    2170        2378 :     if (memtupcount < 100)
    2171        1380 :         return false;
    2172             : 
    2173         998 :     abbrev_distinct = estimateHyperLogLog(&sss->abbr_card);
    2174         998 :     key_distinct = estimateHyperLogLog(&sss->full_card);
    2175             : 
    2176             :     /*
    2177             :      * Clamp cardinality estimates to at least one distinct value.  While
    2178             :      * NULLs are generally disregarded, if only NULL values were seen so far,
    2179             :      * that might misrepresent costs if we failed to clamp.
    2180             :      */
    2181         998 :     if (abbrev_distinct < 1.0)
    2182           0 :         abbrev_distinct = 1.0;
    2183             : 
    2184         998 :     if (key_distinct < 1.0)
    2185           0 :         key_distinct = 1.0;
    2186             : 
    2187             :     /*
    2188             :      * In the worst case all abbreviated keys are identical, while at the same
    2189             :      * time there are differences within full key strings not captured in
    2190             :      * abbreviations.
    2191             :      */
    2192         998 :     if (trace_sort)
    2193             :     {
    2194           0 :         double      norm_abbrev_card = abbrev_distinct / (double) memtupcount;
    2195             : 
    2196           0 :         elog(LOG, "varstr_abbrev: abbrev_distinct after %d: %f "
    2197             :              "(key_distinct: %f, norm_abbrev_card: %f, prop_card: %f)",
    2198             :              memtupcount, abbrev_distinct, key_distinct, norm_abbrev_card,
    2199             :              sss->prop_card);
    2200             :     }
    2201             : 
    2202             :     /*
    2203             :      * If the number of distinct abbreviated keys approximately matches the
    2204             :      * number of distinct authoritative original keys, that's reason enough to
    2205             :      * proceed.  We can win even with a very low cardinality set if most
    2206             :      * tie-breakers only memcmp().  This is by far the most important
    2207             :      * consideration.
    2208             :      *
    2209             :      * While comparisons that are resolved at the abbreviated key level are
    2210             :      * considerably cheaper than tie-breakers resolved with memcmp(), both of
    2211             :      * those two outcomes are so much cheaper than a full strcoll() once
    2212             :      * sorting is underway that it doesn't seem worth it to weigh abbreviated
    2213             :      * cardinality against the overall size of the set in order to more
    2214             :      * accurately model costs.  Assume that an abbreviated comparison, and an
    2215             :      * abbreviated comparison with a cheap memcmp()-based authoritative
    2216             :      * resolution are equivalent.
    2217             :      */
    2218         998 :     if (abbrev_distinct > key_distinct * sss->prop_card)
    2219             :     {
    2220             :         /*
    2221             :          * When we have exceeded 10,000 tuples, decay required cardinality
    2222             :          * aggressively for next call.
    2223             :          *
    2224             :          * This is useful because the number of comparisons required on
    2225             :          * average increases at a linearithmic rate, and at roughly 10,000
    2226             :          * tuples that factor will start to dominate over the linear costs of
    2227             :          * string transformation (this is a conservative estimate).  The decay
    2228             :          * rate is chosen to be a little less aggressive than halving -- which
    2229             :          * (since we're called at points at which memtupcount has doubled)
    2230             :          * would never see the cost model actually abort past the first call
    2231             :          * following a decay.  This decay rate is mostly a precaution against
    2232             :          * a sudden, violent swing in how well abbreviated cardinality tracks
    2233             :          * full key cardinality.  The decay also serves to prevent a marginal
    2234             :          * case from being aborted too late, when too much has already been
    2235             :          * invested in string transformation.
    2236             :          *
    2237             :          * It's possible for sets of several million distinct strings with
    2238             :          * mere tens of thousands of distinct abbreviated keys to still
    2239             :          * benefit very significantly.  This will generally occur provided
    2240             :          * each abbreviated key is a proxy for a roughly uniform number of the
    2241             :          * set's full keys. If it isn't so, we hope to catch that early and
    2242             :          * abort.  If it isn't caught early, by the time the problem is
    2243             :          * apparent it's probably not worth aborting.
    2244             :          */
    2245         998 :         if (memtupcount > 10000)
    2246           4 :             sss->prop_card *= 0.65;
    2247             : 
    2248         998 :         return false;
    2249             :     }
    2250             : 
    2251             :     /*
    2252             :      * Abort abbreviation strategy.
    2253             :      *
    2254             :      * The worst case, where all abbreviated keys are identical while all
    2255             :      * original strings differ will typically only see a regression of about
    2256             :      * 10% in execution time for small to medium sized lists of strings.
    2257             :      * Whereas on modern CPUs where cache stalls are the dominant cost, we can
    2258             :      * often expect very large improvements, particularly with sets of strings
    2259             :      * of moderately high to high abbreviated cardinality.  There is little to
    2260             :      * lose but much to gain, which our strategy reflects.
    2261             :      */
    2262           0 :     if (trace_sort)
    2263           0 :         elog(LOG, "varstr_abbrev: aborted abbreviation at %d "
    2264             :              "(abbrev_distinct: %f, key_distinct: %f, prop_card: %f)",
    2265             :              memtupcount, abbrev_distinct, key_distinct, sss->prop_card);
    2266             : 
    2267           0 :     return true;
    2268             : }
    2269             : 
    2270             : /*
    2271             :  * Generic equalimage support function for character type's operator classes.
    2272             :  * Disables the use of deduplication with nondeterministic collations.
    2273             :  */
    2274             : Datum
    2275        9270 : btvarstrequalimage(PG_FUNCTION_ARGS)
    2276             : {
    2277             : #ifdef NOT_USED
    2278             :     Oid         opcintype = PG_GETARG_OID(0);
    2279             : #endif
    2280        9270 :     Oid         collid = PG_GET_COLLATION();
    2281             :     pg_locale_t locale;
    2282             : 
    2283        9270 :     check_collation_set(collid);
    2284             : 
    2285        9270 :     locale = pg_newlocale_from_collation(collid);
    2286             : 
    2287        9270 :     PG_RETURN_BOOL(locale->deterministic);
    2288             : }
    2289             : 
    2290             : Datum
    2291      229560 : text_larger(PG_FUNCTION_ARGS)
    2292             : {
    2293      229560 :     text       *arg1 = PG_GETARG_TEXT_PP(0);
    2294      229560 :     text       *arg2 = PG_GETARG_TEXT_PP(1);
    2295             :     text       *result;
    2296             : 
    2297      229560 :     result = ((text_cmp(arg1, arg2, PG_GET_COLLATION()) > 0) ? arg1 : arg2);
    2298             : 
    2299      229560 :     PG_RETURN_TEXT_P(result);
    2300             : }
    2301             : 
    2302             : Datum
    2303       86076 : text_smaller(PG_FUNCTION_ARGS)
    2304             : {
    2305       86076 :     text       *arg1 = PG_GETARG_TEXT_PP(0);
    2306       86076 :     text       *arg2 = PG_GETARG_TEXT_PP(1);
    2307             :     text       *result;
    2308             : 
    2309       86076 :     result = ((text_cmp(arg1, arg2, PG_GET_COLLATION()) < 0) ? arg1 : arg2);
    2310             : 
    2311       86076 :     PG_RETURN_TEXT_P(result);
    2312             : }
    2313             : 
    2314             : 
    2315             : /*
    2316             :  * Cross-type comparison functions for types text and name.
    2317             :  */
    2318             : 
    2319             : Datum
    2320      245920 : nameeqtext(PG_FUNCTION_ARGS)
    2321             : {
    2322      245920 :     Name        arg1 = PG_GETARG_NAME(0);
    2323      245920 :     text       *arg2 = PG_GETARG_TEXT_PP(1);
    2324      245920 :     size_t      len1 = strlen(NameStr(*arg1));
    2325      245920 :     size_t      len2 = VARSIZE_ANY_EXHDR(arg2);
    2326      245920 :     Oid         collid = PG_GET_COLLATION();
    2327             :     bool        result;
    2328             : 
    2329      245920 :     check_collation_set(collid);
    2330             : 
    2331      245920 :     if (collid == C_COLLATION_OID)
    2332      257140 :         result = (len1 == len2 &&
    2333      124386 :                   memcmp(NameStr(*arg1), VARDATA_ANY(arg2), len1) == 0);
    2334             :     else
    2335      113166 :         result = (varstr_cmp(NameStr(*arg1), len1,
    2336      113166 :                              VARDATA_ANY(arg2), len2,
    2337             :                              collid) == 0);
    2338             : 
    2339      245920 :     PG_FREE_IF_COPY(arg2, 1);
    2340             : 
    2341      245920 :     PG_RETURN_BOOL(result);
    2342             : }
    2343             : 
    2344             : Datum
    2345        8012 : texteqname(PG_FUNCTION_ARGS)
    2346             : {
    2347        8012 :     text       *arg1 = PG_GETARG_TEXT_PP(0);
    2348        8012 :     Name        arg2 = PG_GETARG_NAME(1);
    2349        8012 :     size_t      len1 = VARSIZE_ANY_EXHDR(arg1);
    2350        8012 :     size_t      len2 = strlen(NameStr(*arg2));
    2351        8012 :     Oid         collid = PG_GET_COLLATION();
    2352             :     bool        result;
    2353             : 
    2354        8012 :     check_collation_set(collid);
    2355             : 
    2356        8012 :     if (collid == C_COLLATION_OID)
    2357         568 :         result = (len1 == len2 &&
    2358         182 :                   memcmp(VARDATA_ANY(arg1), NameStr(*arg2), len1) == 0);
    2359             :     else
    2360        7626 :         result = (varstr_cmp(VARDATA_ANY(arg1), len1,
    2361        7626 :                              NameStr(*arg2), len2,
    2362             :                              collid) == 0);
    2363             : 
    2364        8012 :     PG_FREE_IF_COPY(arg1, 0);
    2365             : 
    2366        8012 :     PG_RETURN_BOOL(result);
    2367             : }
    2368             : 
    2369             : Datum
    2370          18 : namenetext(PG_FUNCTION_ARGS)
    2371             : {
    2372          18 :     Name        arg1 = PG_GETARG_NAME(0);
    2373          18 :     text       *arg2 = PG_GETARG_TEXT_PP(1);
    2374          18 :     size_t      len1 = strlen(NameStr(*arg1));
    2375          18 :     size_t      len2 = VARSIZE_ANY_EXHDR(arg2);
    2376          18 :     Oid         collid = PG_GET_COLLATION();
    2377             :     bool        result;
    2378             : 
    2379          18 :     check_collation_set(collid);
    2380             : 
    2381          18 :     if (collid == C_COLLATION_OID)
    2382           0 :         result = !(len1 == len2 &&
    2383           0 :                    memcmp(NameStr(*arg1), VARDATA_ANY(arg2), len1) == 0);
    2384             :     else
    2385          18 :         result = !(varstr_cmp(NameStr(*arg1), len1,
    2386          18 :                               VARDATA_ANY(arg2), len2,
    2387             :                               collid) == 0);
    2388             : 
    2389          18 :     PG_FREE_IF_COPY(arg2, 1);
    2390             : 
    2391          18 :     PG_RETURN_BOOL(result);
    2392             : }
    2393             : 
    2394             : Datum
    2395          18 : textnename(PG_FUNCTION_ARGS)
    2396             : {
    2397          18 :     text       *arg1 = PG_GETARG_TEXT_PP(0);
    2398          18 :     Name        arg2 = PG_GETARG_NAME(1);
    2399          18 :     size_t      len1 = VARSIZE_ANY_EXHDR(arg1);
    2400          18 :     size_t      len2 = strlen(NameStr(*arg2));
    2401          18 :     Oid         collid = PG_GET_COLLATION();
    2402             :     bool        result;
    2403             : 
    2404          18 :     check_collation_set(collid);
    2405             : 
    2406          18 :     if (collid == C_COLLATION_OID)
    2407           0 :         result = !(len1 == len2 &&
    2408           0 :                    memcmp(VARDATA_ANY(arg1), NameStr(*arg2), len1) == 0);
    2409             :     else
    2410          18 :         result = !(varstr_cmp(VARDATA_ANY(arg1), len1,
    2411          18 :                               NameStr(*arg2), len2,
    2412             :                               collid) == 0);
    2413             : 
    2414          18 :     PG_FREE_IF_COPY(arg1, 0);
    2415             : 
    2416          18 :     PG_RETURN_BOOL(result);
    2417             : }
    2418             : 
    2419             : Datum
    2420      141706 : btnametextcmp(PG_FUNCTION_ARGS)
    2421             : {
    2422      141706 :     Name        arg1 = PG_GETARG_NAME(0);
    2423      141706 :     text       *arg2 = PG_GETARG_TEXT_PP(1);
    2424             :     int32       result;
    2425             : 
    2426      141706 :     result = varstr_cmp(NameStr(*arg1), strlen(NameStr(*arg1)),
    2427      141706 :                         VARDATA_ANY(arg2), VARSIZE_ANY_EXHDR(arg2),
    2428             :                         PG_GET_COLLATION());
    2429             : 
    2430      141706 :     PG_FREE_IF_COPY(arg2, 1);
    2431             : 
    2432      141706 :     PG_RETURN_INT32(result);
    2433             : }
    2434             : 
    2435             : Datum
    2436          44 : bttextnamecmp(PG_FUNCTION_ARGS)
    2437             : {
    2438          44 :     text       *arg1 = PG_GETARG_TEXT_PP(0);
    2439          44 :     Name        arg2 = PG_GETARG_NAME(1);
    2440             :     int32       result;
    2441             : 
    2442          44 :     result = varstr_cmp(VARDATA_ANY(arg1), VARSIZE_ANY_EXHDR(arg1),
    2443          44 :                         NameStr(*arg2), strlen(NameStr(*arg2)),
    2444             :                         PG_GET_COLLATION());
    2445             : 
    2446          44 :     PG_FREE_IF_COPY(arg1, 0);
    2447             : 
    2448          44 :     PG_RETURN_INT32(result);
    2449             : }
    2450             : 
    2451             : #define CmpCall(cmpfunc) \
    2452             :     DatumGetInt32(DirectFunctionCall2Coll(cmpfunc, \
    2453             :                                           PG_GET_COLLATION(), \
    2454             :                                           PG_GETARG_DATUM(0), \
    2455             :                                           PG_GETARG_DATUM(1)))
    2456             : 
    2457             : Datum
    2458       68026 : namelttext(PG_FUNCTION_ARGS)
    2459             : {
    2460       68026 :     PG_RETURN_BOOL(CmpCall(btnametextcmp) < 0);
    2461             : }
    2462             : 
    2463             : Datum
    2464           0 : nameletext(PG_FUNCTION_ARGS)
    2465             : {
    2466           0 :     PG_RETURN_BOOL(CmpCall(btnametextcmp) <= 0);
    2467             : }
    2468             : 
    2469             : Datum
    2470           0 : namegttext(PG_FUNCTION_ARGS)
    2471             : {
    2472           0 :     PG_RETURN_BOOL(CmpCall(btnametextcmp) > 0);
    2473             : }
    2474             : 
    2475             : Datum
    2476       61384 : namegetext(PG_FUNCTION_ARGS)
    2477             : {
    2478       61384 :     PG_RETURN_BOOL(CmpCall(btnametextcmp) >= 0);
    2479             : }
    2480             : 
    2481             : Datum
    2482           0 : textltname(PG_FUNCTION_ARGS)
    2483             : {
    2484           0 :     PG_RETURN_BOOL(CmpCall(bttextnamecmp) < 0);
    2485             : }
    2486             : 
    2487             : Datum
    2488           0 : textlename(PG_FUNCTION_ARGS)
    2489             : {
    2490           0 :     PG_RETURN_BOOL(CmpCall(bttextnamecmp) <= 0);
    2491             : }
    2492             : 
    2493             : Datum
    2494           0 : textgtname(PG_FUNCTION_ARGS)
    2495             : {
    2496           0 :     PG_RETURN_BOOL(CmpCall(bttextnamecmp) > 0);
    2497             : }
    2498             : 
    2499             : Datum
    2500           0 : textgename(PG_FUNCTION_ARGS)
    2501             : {
    2502           0 :     PG_RETURN_BOOL(CmpCall(bttextnamecmp) >= 0);
    2503             : }
    2504             : 
    2505             : #undef CmpCall
    2506             : 
    2507             : 
    2508             : /*
    2509             :  * The following operators support character-by-character comparison
    2510             :  * of text datums, to allow building indexes suitable for LIKE clauses.
    2511             :  * Note that the regular texteq/textne comparison operators, and regular
    2512             :  * support functions 1 and 2 with "C" collation are assumed to be
    2513             :  * compatible with these!
    2514             :  */
    2515             : 
    2516             : static int
    2517      160444 : internal_text_pattern_compare(text *arg1, text *arg2)
    2518             : {
    2519             :     int         result;
    2520             :     int         len1,
    2521             :                 len2;
    2522             : 
    2523      160444 :     len1 = VARSIZE_ANY_EXHDR(arg1);
    2524      160444 :     len2 = VARSIZE_ANY_EXHDR(arg2);
    2525             : 
    2526      160444 :     result = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
    2527      160444 :     if (result != 0)
    2528      160312 :         return result;
    2529         132 :     else if (len1 < len2)
    2530           0 :         return -1;
    2531         132 :     else if (len1 > len2)
    2532          84 :         return 1;
    2533             :     else
    2534          48 :         return 0;
    2535             : }
    2536             : 
    2537             : 
    2538             : Datum
    2539       47866 : text_pattern_lt(PG_FUNCTION_ARGS)
    2540             : {
    2541       47866 :     text       *arg1 = PG_GETARG_TEXT_PP(0);
    2542       47866 :     text       *arg2 = PG_GETARG_TEXT_PP(1);
    2543             :     int         result;
    2544             : 
    2545       47866 :     result = internal_text_pattern_compare(arg1, arg2);
    2546             : 
    2547       47866 :     PG_FREE_IF_COPY(arg1, 0);
    2548       47866 :     PG_FREE_IF_COPY(arg2, 1);
    2549             : 
    2550       47866 :     PG_RETURN_BOOL(result < 0);
    2551             : }
    2552             : 
    2553             : 
    2554             : Datum
    2555       37510 : text_pattern_le(PG_FUNCTION_ARGS)
    2556             : {
    2557       37510 :     text       *arg1 = PG_GETARG_TEXT_PP(0);
    2558       37510 :     text       *arg2 = PG_GETARG_TEXT_PP(1);
    2559             :     int         result;
    2560             : 
    2561       37510 :     result = internal_text_pattern_compare(arg1, arg2);
    2562             : 
    2563       37510 :     PG_FREE_IF_COPY(arg1, 0);
    2564       37510 :     PG_FREE_IF_COPY(arg2, 1);
    2565             : 
    2566       37510 :     PG_RETURN_BOOL(result <= 0);
    2567             : }
    2568             : 
    2569             : 
    2570             : Datum
    2571       37534 : text_pattern_ge(PG_FUNCTION_ARGS)
    2572             : {
    2573       37534 :     text       *arg1 = PG_GETARG_TEXT_PP(0);
    2574       37534 :     text       *arg2 = PG_GETARG_TEXT_PP(1);
    2575             :     int         result;
    2576             : 
    2577       37534 :     result = internal_text_pattern_compare(arg1, arg2);
    2578             : 
    2579       37534 :     PG_FREE_IF_COPY(arg1, 0);
    2580       37534 :     PG_FREE_IF_COPY(arg2, 1);
    2581             : 
    2582       37534 :     PG_RETURN_BOOL(result >= 0);
    2583             : }
    2584             : 
    2585             : 
    2586             : Datum
    2587       37510 : text_pattern_gt(PG_FUNCTION_ARGS)
    2588             : {
    2589       37510 :     text       *arg1 = PG_GETARG_TEXT_PP(0);
    2590       37510 :     text       *arg2 = PG_GETARG_TEXT_PP(1);
    2591             :     int         result;
    2592             : 
    2593       37510 :     result = internal_text_pattern_compare(arg1, arg2);
    2594             : 
    2595       37510 :     PG_FREE_IF_COPY(arg1, 0);
    2596       37510 :     PG_FREE_IF_COPY(arg2, 1);
    2597             : 
    2598       37510 :     PG_RETURN_BOOL(result > 0);
    2599             : }
    2600             : 
    2601             : 
    2602             : Datum
    2603          24 : bttext_pattern_cmp(PG_FUNCTION_ARGS)
    2604             : {
    2605          24 :     text       *arg1 = PG_GETARG_TEXT_PP(0);
    2606          24 :     text       *arg2 = PG_GETARG_TEXT_PP(1);
    2607             :     int         result;
    2608             : 
    2609          24 :     result = internal_text_pattern_compare(arg1, arg2);
    2610             : 
    2611          24 :     PG_FREE_IF_COPY(arg1, 0);
    2612          24 :     PG_FREE_IF_COPY(arg2, 1);
    2613             : 
    2614          24 :     PG_RETURN_INT32(result);
    2615             : }
    2616             : 
    2617             : 
    2618             : Datum
    2619         116 : bttext_pattern_sortsupport(PG_FUNCTION_ARGS)
    2620             : {
    2621         116 :     SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
    2622             :     MemoryContext oldcontext;
    2623             : 
    2624         116 :     oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
    2625             : 
    2626             :     /* Use generic string SortSupport, forcing "C" collation */
    2627         116 :     varstr_sortsupport(ssup, TEXTOID, C_COLLATION_OID);
    2628             : 
    2629         116 :     MemoryContextSwitchTo(oldcontext);
    2630             : 
    2631         116 :     PG_RETURN_VOID();
    2632             : }
    2633             : 
    2634             : 
    2635             : /* text_name()
    2636             :  * Converts a text type to a Name type.
    2637             :  */
    2638             : Datum
    2639       30882 : text_name(PG_FUNCTION_ARGS)
    2640             : {
    2641       30882 :     text       *s = PG_GETARG_TEXT_PP(0);
    2642             :     Name        result;
    2643             :     int         len;
    2644             : 
    2645       30882 :     len = VARSIZE_ANY_EXHDR(s);
    2646             : 
    2647             :     /* Truncate oversize input */
    2648       30882 :     if (len >= NAMEDATALEN)
    2649           6 :         len = pg_mbcliplen(VARDATA_ANY(s), len, NAMEDATALEN - 1);
    2650             : 
    2651             :     /* We use palloc0 here to ensure result is zero-padded */
    2652       30882 :     result = (Name) palloc0(NAMEDATALEN);
    2653       30882 :     memcpy(NameStr(*result), VARDATA_ANY(s), len);
    2654             : 
    2655       30882 :     PG_RETURN_NAME(result);
    2656             : }
    2657             : 
    2658             : /* name_text()
    2659             :  * Converts a Name type to a text type.
    2660             :  */
    2661             : Datum
    2662      656686 : name_text(PG_FUNCTION_ARGS)
    2663             : {
    2664      656686 :     Name        s = PG_GETARG_NAME(0);
    2665             : 
    2666      656686 :     PG_RETURN_TEXT_P(cstring_to_text(NameStr(*s)));
    2667             : }
    2668             : 
    2669             : 
    2670             : /*
    2671             :  * textToQualifiedNameList - convert a text object to list of names
    2672             :  *
    2673             :  * This implements the input parsing needed by nextval() and other
    2674             :  * functions that take a text parameter representing a qualified name.
    2675             :  * We split the name at dots, downcase if not double-quoted, and
    2676             :  * truncate names if they're too long.
    2677             :  */
    2678             : List *
    2679        5432 : textToQualifiedNameList(text *textval)
    2680             : {
    2681             :     char       *rawname;
    2682        5432 :     List       *result = NIL;
    2683             :     List       *namelist;
    2684             :     ListCell   *l;
    2685             : 
    2686             :     /* Convert to C string (handles possible detoasting). */
    2687             :     /* Note we rely on being able to modify rawname below. */
    2688        5432 :     rawname = text_to_cstring(textval);
    2689             : 
    2690        5432 :     if (!SplitIdentifierString(rawname, '.', &namelist))
    2691           0 :         ereport(ERROR,
    2692             :                 (errcode(ERRCODE_INVALID_NAME),
    2693             :                  errmsg("invalid name syntax")));
    2694             : 
    2695        5432 :     if (namelist == NIL)
    2696           0 :         ereport(ERROR,
    2697             :                 (errcode(ERRCODE_INVALID_NAME),
    2698             :                  errmsg("invalid name syntax")));
    2699             : 
    2700       10980 :     foreach(l, namelist)
    2701             :     {
    2702        5548 :         char       *curname = (char *) lfirst(l);
    2703             : 
    2704        5548 :         result = lappend(result, makeString(pstrdup(curname)));
    2705             :     }
    2706             : 
    2707        5432 :     pfree(rawname);
    2708        5432 :     list_free(namelist);
    2709             : 
    2710        5432 :     return result;
    2711             : }
    2712             : 
    2713             : /*
    2714             :  * SplitIdentifierString --- parse a string containing identifiers
    2715             :  *
    2716             :  * This is the guts of textToQualifiedNameList, and is exported for use in
    2717             :  * other situations such as parsing GUC variables.  In the GUC case, it's
    2718             :  * important to avoid memory leaks, so the API is designed to minimize the
    2719             :  * amount of stuff that needs to be allocated and freed.
    2720             :  *
    2721             :  * Inputs:
    2722             :  *  rawstring: the input string; must be overwritable!  On return, it's
    2723             :  *             been modified to contain the separated identifiers.
    2724             :  *  separator: the separator punctuation expected between identifiers
    2725             :  *             (typically '.' or ',').  Whitespace may also appear around
    2726             :  *             identifiers.
    2727             :  * Outputs:
    2728             :  *  namelist: filled with a palloc'd list of pointers to identifiers within
    2729             :  *            rawstring.  Caller should list_free() this even on error return.
    2730             :  *
    2731             :  * Returns true if okay, false if there is a syntax error in the string.
    2732             :  *
    2733             :  * Note that an empty string is considered okay here, though not in
    2734             :  * textToQualifiedNameList.
    2735             :  */
    2736             : bool
    2737      383044 : SplitIdentifierString(char *rawstring, char separator,
    2738             :                       List **namelist)
    2739             : {
    2740      383044 :     char       *nextp = rawstring;
    2741      383044 :     bool        done = false;
    2742             : 
    2743      383044 :     *namelist = NIL;
    2744             : 
    2745      383050 :     while (scanner_isspace(*nextp))
    2746           6 :         nextp++;                /* skip leading whitespace */
    2747             : 
    2748      383044 :     if (*nextp == '\0')
    2749       31508 :         return true;            /* empty string represents empty list */
    2750             : 
    2751             :     /* At the top of the loop, we are at start of a new identifier. */
    2752             :     do
    2753             :     {
    2754             :         char       *curname;
    2755             :         char       *endp;
    2756             : 
    2757      657776 :         if (*nextp == '"')
    2758             :         {
    2759             :             /* Quoted name --- collapse quote-quote pairs, no downcasing */
    2760       42520 :             curname = nextp + 1;
    2761             :             for (;;)
    2762             :             {
    2763       42524 :                 endp = strchr(nextp + 1, '"');
    2764       42522 :                 if (endp == NULL)
    2765           0 :                     return false;   /* mismatched quotes */
    2766       42522 :                 if (endp[1] != '"')
    2767       42520 :                     break;      /* found end of quoted name */
    2768             :                 /* Collapse adjacent quotes into one quote, and look again */
    2769           2 :                 memmove(endp, endp + 1, strlen(endp));
    2770           2 :                 nextp = endp;
    2771             :             }
    2772             :             /* endp now points at the terminating quote */
    2773       42520 :             nextp = endp + 1;
    2774             :         }
    2775             :         else
    2776             :         {
    2777             :             /* Unquoted name --- extends to separator or whitespace */
    2778             :             char       *downname;
    2779             :             int         len;
    2780             : 
    2781      615256 :             curname = nextp;
    2782     5637346 :             while (*nextp && *nextp != separator &&
    2783     5022092 :                    !scanner_isspace(*nextp))
    2784     5022090 :                 nextp++;
    2785      615256 :             endp = nextp;
    2786      615256 :             if (curname == nextp)
    2787           0 :                 return false;   /* empty unquoted name not allowed */
    2788             : 
    2789             :             /*
    2790             :              * Downcase the identifier, using same code as main lexer does.
    2791             :              *
    2792             :              * XXX because we want to overwrite the input in-place, we cannot
    2793             :              * support a downcasing transformation that increases the string
    2794             :              * length.  This is not a problem given the current implementation
    2795             :              * of downcase_truncate_identifier, but we'll probably have to do
    2796             :              * something about this someday.
    2797             :              */
    2798      615256 :             len = endp - curname;
    2799      615256 :             downname = downcase_truncate_identifier(curname, len, false);
    2800             :             Assert(strlen(downname) <= len);
    2801      615256 :             strncpy(curname, downname, len);    /* strncpy is required here */
    2802      615256 :             pfree(downname);
    2803             :         }
    2804             : 
    2805      657778 :         while (scanner_isspace(*nextp))
    2806           2 :             nextp++;            /* skip trailing whitespace */
    2807             : 
    2808      657776 :         if (*nextp == separator)
    2809             :         {
    2810      306240 :             nextp++;
    2811      587056 :             while (scanner_isspace(*nextp))
    2812      280816 :                 nextp++;        /* skip leading whitespace for next */
    2813             :             /* we expect another name, so done remains false */
    2814             :         }
    2815      351536 :         else if (*nextp == '\0')
    2816      351534 :             done = true;
    2817             :         else
    2818           2 :             return false;       /* invalid syntax */
    2819             : 
    2820             :         /* Now safe to overwrite separator with a null */
    2821      657774 :         *endp = '\0';
    2822             : 
    2823             :         /* Truncate name if it's overlength */
    2824      657774 :         truncate_identifier(curname, strlen(curname), false);
    2825             : 
    2826             :         /*
    2827             :          * Finished isolating current name --- add it to list
    2828             :          */
    2829      657774 :         *namelist = lappend(*namelist, curname);
    2830             : 
    2831             :         /* Loop back if we didn't reach end of string */
    2832      657774 :     } while (!done);
    2833             : 
    2834      351534 :     return true;
    2835             : }
    2836             : 
    2837             : 
    2838             : /*
    2839             :  * SplitDirectoriesString --- parse a string containing file/directory names
    2840             :  *
    2841             :  * This works fine on file names too; the function name is historical.
    2842             :  *
    2843             :  * This is similar to SplitIdentifierString, except that the parsing
    2844             :  * rules are meant to handle pathnames instead of identifiers: there is
    2845             :  * no downcasing, embedded spaces are allowed, the max length is MAXPGPATH-1,
    2846             :  * and we apply canonicalize_path() to each extracted string.  Because of the
    2847             :  * last, the returned strings are separately palloc'd rather than being
    2848             :  * pointers into rawstring --- but we still scribble on rawstring.
    2849             :  *
    2850             :  * Inputs:
    2851             :  *  rawstring: the input string; must be modifiable!
    2852             :  *  separator: the separator punctuation expected between directories
    2853             :  *             (typically ',' or ';').  Whitespace may also appear around
    2854             :  *             directories.
    2855             :  * Outputs:
    2856             :  *  namelist: filled with a palloc'd list of directory names.
    2857             :  *            Caller should list_free_deep() this even on error return.
    2858             :  *
    2859             :  * Returns true if okay, false if there is a syntax error in the string.
    2860             :  *
    2861             :  * Note that an empty string is considered okay here.
    2862             :  */
    2863             : bool
    2864        1910 : SplitDirectoriesString(char *rawstring, char separator,
    2865             :                        List **namelist)
    2866             : {
    2867        1910 :     char       *nextp = rawstring;
    2868        1910 :     bool        done = false;
    2869             : 
    2870        1910 :     *namelist = NIL;
    2871             : 
    2872        1910 :     while (scanner_isspace(*nextp))
    2873           0 :         nextp++;                /* skip leading whitespace */
    2874             : 
    2875        1910 :     if (*nextp == '\0')
    2876           2 :         return true;            /* empty string represents empty list */
    2877             : 
    2878             :     /* At the top of the loop, we are at start of a new directory. */
    2879             :     do
    2880             :     {
    2881             :         char       *curname;
    2882             :         char       *endp;
    2883             : 
    2884        1918 :         if (*nextp == '"')
    2885             :         {
    2886             :             /* Quoted name --- collapse quote-quote pairs */
    2887           0 :             curname = nextp + 1;
    2888             :             for (;;)
    2889             :             {
    2890           0 :                 endp = strchr(nextp + 1, '"');
    2891           0 :                 if (endp == NULL)
    2892           0 :                     return false;   /* mismatched quotes */
    2893           0 :                 if (endp[1] != '"')
    2894           0 :                     break;      /* found end of quoted name */
    2895             :                 /* Collapse adjacent quotes into one quote, and look again */
    2896           0 :                 memmove(endp, endp + 1, strlen(endp));
    2897           0 :                 nextp = endp;
    2898             :             }
    2899             :             /* endp now points at the terminating quote */
    2900           0 :             nextp = endp + 1;
    2901             :         }
    2902             :         else
    2903             :         {
    2904             :             /* Unquoted name --- extends to separator or end of string */
    2905        1918 :             curname = endp = nextp;
    2906       32102 :             while (*nextp && *nextp != separator)
    2907             :             {
    2908             :                 /* trailing whitespace should not be included in name */
    2909       30184 :                 if (!scanner_isspace(*nextp))
    2910       30184 :                     endp = nextp + 1;
    2911       30184 :                 nextp++;
    2912             :             }
    2913        1918 :             if (curname == endp)
    2914           0 :                 return false;   /* empty unquoted name not allowed */
    2915             :         }
    2916             : 
    2917        1918 :         while (scanner_isspace(*nextp))
    2918           0 :             nextp++;            /* skip trailing whitespace */
    2919             : 
    2920        1918 :         if (*nextp == separator)
    2921             :         {
    2922          10 :             nextp++;
    2923          16 :             while (scanner_isspace(*nextp))
    2924           6 :                 nextp++;        /* skip leading whitespace for next */
    2925             :             /* we expect another name, so done remains false */
    2926             :         }
    2927        1908 :         else if (*nextp == '\0')
    2928        1908 :             done = true;
    2929             :         else
    2930           0 :             return false;       /* invalid syntax */
    2931             : 
    2932             :         /* Now safe to overwrite separator with a null */
    2933        1918 :         *endp = '\0';
    2934             : 
    2935             :         /* Truncate path if it's overlength */
    2936        1918 :         if (strlen(curname) >= MAXPGPATH)
    2937           0 :             curname[MAXPGPATH - 1] = '\0';
    2938             : 
    2939             :         /*
    2940             :          * Finished isolating current name --- add it to list
    2941             :          */
    2942        1918 :         curname = pstrdup(curname);
    2943        1918 :         canonicalize_path(curname);
    2944        1918 :         *namelist = lappend(*namelist, curname);
    2945             : 
    2946             :         /* Loop back if we didn't reach end of string */
    2947        1918 :     } while (!done);
    2948             : 
    2949        1908 :     return true;
    2950             : }
    2951             : 
    2952             : 
    2953             : /*
    2954             :  * SplitGUCList --- parse a string containing identifiers or file names
    2955             :  *
    2956             :  * This is used to split the value of a GUC_LIST_QUOTE GUC variable, without
    2957             :  * presuming whether the elements will be taken as identifiers or file names.
    2958             :  * We assume the input has already been through flatten_set_variable_args(),
    2959             :  * so that we need never downcase (if appropriate, that was done already).
    2960             :  * Nor do we ever truncate, since we don't know the correct max length.
    2961             :  * We disallow embedded whitespace for simplicity (it shouldn't matter,
    2962             :  * because any embedded whitespace should have led to double-quoting).
    2963             :  * Otherwise the API is identical to SplitIdentifierString.
    2964             :  *
    2965             :  * XXX it's annoying to have so many copies of this string-splitting logic.
    2966             :  * However, it's not clear that having one function with a bunch of option
    2967             :  * flags would be much better.
    2968             :  *
    2969             :  * XXX there is a version of this function in src/bin/pg_dump/dumputils.c.
    2970             :  * Be sure to update that if you have to change this.
    2971             :  *
    2972             :  * Inputs:
    2973             :  *  rawstring: the input string; must be overwritable!  On return, it's
    2974             :  *             been modified to contain the separated identifiers.
    2975             :  *  separator: the separator punctuation expected between identifiers
    2976             :  *             (typically '.' or ',').  Whitespace may also appear around
    2977             :  *             identifiers.
    2978             :  * Outputs:
    2979             :  *  namelist: filled with a palloc'd list of pointers to identifiers within
    2980             :  *            rawstring.  Caller should list_free() this even on error return.
    2981             :  *
    2982             :  * Returns true if okay, false if there is a syntax error in the string.
    2983             :  */
    2984             : bool
    2985        7400 : SplitGUCList(char *rawstring, char separator,
    2986             :              List **namelist)
    2987             : {
    2988        7400 :     char       *nextp = rawstring;
    2989        7400 :     bool        done = false;
    2990             : 
    2991        7400 :     *namelist = NIL;
    2992             : 
    2993        7400 :     while (scanner_isspace(*nextp))
    2994           0 :         nextp++;                /* skip leading whitespace */
    2995             : 
    2996        7400 :     if (*nextp == '\0')
    2997        4124 :         return true;            /* empty string represents empty list */
    2998             : 
    2999             :     /* At the top of the loop, we are at start of a new identifier. */
    3000             :     do
    3001             :     {
    3002             :         char       *curname;
    3003             :         char       *endp;
    3004             : 
    3005        3398 :         if (*nextp == '"')
    3006             :         {
    3007             :             /* Quoted name --- collapse quote-quote pairs */
    3008          24 :             curname = nextp + 1;
    3009             :             for (;;)
    3010             :             {
    3011          36 :                 endp = strchr(nextp + 1, '"');
    3012          30 :                 if (endp == NULL)
    3013           0 :                     return false;   /* mismatched quotes */
    3014          30 :                 if (endp[1] != '"')
    3015          24 :                     break;      /* found end of quoted name */
    3016             :                 /* Collapse adjacent quotes into one quote, and look again */
    3017           6 :                 memmove(endp, endp + 1, strlen(endp));
    3018           6 :                 nextp = endp;
    3019             :             }
    3020             :             /* endp now points at the terminating quote */
    3021          24 :             nextp = endp + 1;
    3022             :         }
    3023             :         else
    3024             :         {
    3025             :             /* Unquoted name --- extends to separator or whitespace */
    3026        3374 :             curname = nextp;
    3027       27352 :             while (*nextp && *nextp != separator &&
    3028       23978 :                    !scanner_isspace(*nextp))
    3029       23978 :                 nextp++;
    3030        3374 :             endp = nextp;
    3031        3374 :             if (curname == nextp)
    3032           0 :                 return false;   /* empty unquoted name not allowed */
    3033             :         }
    3034             : 
    3035        3398 :         while (scanner_isspace(*nextp))
    3036           0 :             nextp++;            /* skip trailing whitespace */
    3037             : 
    3038        3398 :         if (*nextp == separator)
    3039             :         {
    3040         122 :             nextp++;
    3041         236 :             while (scanner_isspace(*nextp))
    3042         114 :                 nextp++;        /* skip leading whitespace for next */
    3043             :             /* we expect another name, so done remains false */
    3044             :         }
    3045        3276 :         else if (*nextp == '\0')
    3046        3276 :             done = true;
    3047             :         else
    3048           0 :             return false;       /* invalid syntax */
    3049             : 
    3050             :         /* Now safe to overwrite separator with a null */
    3051        3398 :         *endp = '\0';
    3052             : 
    3053             :         /*
    3054             :          * Finished isolating current name --- add it to list
    3055             :          */
    3056        3398 :         *namelist = lappend(*namelist, curname);
    3057             : 
    3058             :         /* Loop back if we didn't reach end of string */
    3059        3398 :     } while (!done);
    3060             : 
    3061        3276 :     return true;
    3062             : }
    3063             : 
    3064             : /*
    3065             :  * appendStringInfoText
    3066             :  *
    3067             :  * Append a text to str.
    3068             :  * Like appendStringInfoString(str, text_to_cstring(t)) but faster.
    3069             :  */
    3070             : static void
    3071     2173034 : appendStringInfoText(StringInfo str, const text *t)
    3072             : {
    3073     2173034 :     appendBinaryStringInfo(str, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t));
    3074     2173034 : }
    3075             : 
    3076             : /*
    3077             :  * replace_text
    3078             :  * replace all occurrences of 'old_sub_str' in 'orig_str'
    3079             :  * with 'new_sub_str' to form 'new_str'
    3080             :  *
    3081             :  * returns 'orig_str' if 'old_sub_str' == '' or 'orig_str' == ''
    3082             :  * otherwise returns 'new_str'
    3083             :  */
    3084             : Datum
    3085        1568 : replace_text(PG_FUNCTION_ARGS)
    3086             : {
    3087        1568 :     text       *src_text = PG_GETARG_TEXT_PP(0);
    3088        1568 :     text       *from_sub_text = PG_GETARG_TEXT_PP(1);
    3089        1568 :     text       *to_sub_text = PG_GETARG_TEXT_PP(2);
    3090             :     int         src_text_len;
    3091             :     int         from_sub_text_len;
    3092             :     TextPositionState state;
    3093             :     text       *ret_text;
    3094             :     int         chunk_len;
    3095             :     char       *curr_ptr;
    3096             :     char       *start_ptr;
    3097             :     StringInfoData str;
    3098             :     bool        found;
    3099             : 
    3100        1568 :     src_text_len = VARSIZE_ANY_EXHDR(src_text);
    3101        1568 :     from_sub_text_len = VARSIZE_ANY_EXHDR(from_sub_text);
    3102             : 
    3103             :     /* Return unmodified source string if empty source or pattern */
    3104        1568 :     if (src_text_len < 1 || from_sub_text_len < 1)
    3105             :     {
    3106           0 :         PG_RETURN_TEXT_P(src_text);
    3107             :     }
    3108             : 
    3109        1568 :     text_position_setup(src_text, from_sub_text, PG_GET_COLLATION(), &state);
    3110             : 
    3111        1568 :     found = text_position_next(&state);
    3112             : 
    3113             :     /* When the from_sub_text is not found, there is nothing to do. */
    3114        1568 :     if (!found)
    3115             :     {
    3116         334 :         text_position_cleanup(&state);
    3117         334 :         PG_RETURN_TEXT_P(src_text);
    3118             :     }
    3119        1234 :     curr_ptr = text_position_get_match_ptr(&state);
    3120        1234 :     start_ptr = VARDATA_ANY(src_text);
    3121             : 
    3122        1234 :     initStringInfo(&str);
    3123             : 
    3124             :     do
    3125             :     {
    3126        7216 :         CHECK_FOR_INTERRUPTS();
    3127             : 
    3128             :         /* copy the data skipped over by last text_position_next() */
    3129        7216 :         chunk_len = curr_ptr - start_ptr;
    3130        7216 :         appendBinaryStringInfo(&str, start_ptr, chunk_len);
    3131             : 
    3132        7216 :         appendStringInfoText(&str, to_sub_text);
    3133             : 
    3134        7216 :         start_ptr = curr_ptr + state.last_match_len;
    3135             : 
    3136        7216 :         found = text_position_next(&state);
    3137        7216 :         if (found)
    3138        5982 :             curr_ptr = text_position_get_match_ptr(&state);
    3139             :     }
    3140        7216 :     while (found);
    3141             : 
    3142             :     /* copy trailing data */
    3143        1234 :     chunk_len = ((char *) src_text + VARSIZE_ANY(src_text)) - start_ptr;
    3144        1234 :     appendBinaryStringInfo(&str, start_ptr, chunk_len);
    3145             : 
    3146        1234 :     text_position_cleanup(&state);
    3147             : 
    3148        1234 :     ret_text = cstring_to_text_with_len(str.data, str.len);
    3149        1234 :     pfree(str.data);
    3150             : 
    3151        1234 :     PG_RETURN_TEXT_P(ret_text);
    3152             : }
    3153             : 
    3154             : /*
    3155             :  * check_replace_text_has_escape
    3156             :  *
    3157             :  * Returns 0 if text contains no backslashes that need processing.
    3158             :  * Returns 1 if text contains backslashes, but not regexp submatch specifiers.
    3159             :  * Returns 2 if text contains regexp submatch specifiers (\1 .. \9).
    3160             :  */
    3161             : static int
    3162       18804 : check_replace_text_has_escape(const text *replace_text)
    3163             : {
    3164       18804 :     int         result = 0;
    3165       18804 :     const char *p = VARDATA_ANY(replace_text);
    3166       18804 :     const char *p_end = p + VARSIZE_ANY_EXHDR(replace_text);
    3167             : 
    3168       37652 :     while (p < p_end)
    3169             :     {
    3170             :         /* Find next escape char, if any. */
    3171       17668 :         p = memchr(p, '\\', p_end - p);
    3172       17668 :         if (p == NULL)
    3173       16820 :             break;
    3174         848 :         p++;
    3175             :         /* Note: a backslash at the end doesn't require extra processing. */
    3176         848 :         if (p < p_end)
    3177             :         {
    3178         848 :             if (*p >= '1' && *p <= '9')
    3179         804 :                 return 2;       /* Found a submatch specifier, so done */
    3180          44 :             result = 1;         /* Found some other sequence, keep looking */
    3181          44 :             p++;
    3182             :         }
    3183             :     }
    3184       18000 :     return result;
    3185             : }
    3186             : 
    3187             : /*
    3188             :  * appendStringInfoRegexpSubstr
    3189             :  *
    3190             :  * Append replace_text to str, substituting regexp back references for
    3191             :  * \n escapes.  start_ptr is the start of the match in the source string,
    3192             :  * at logical character position data_pos.
    3193             :  */
    3194             : static void
    3195         254 : appendStringInfoRegexpSubstr(StringInfo str, text *replace_text,
    3196             :                              regmatch_t *pmatch,
    3197             :                              char *start_ptr, int data_pos)
    3198             : {
    3199         254 :     const char *p = VARDATA_ANY(replace_text);
    3200         254 :     const char *p_end = p + VARSIZE_ANY_EXHDR(replace_text);
    3201             : 
    3202         610 :     while (p < p_end)
    3203             :     {
    3204         536 :         const char *chunk_start = p;
    3205             :         int         so;
    3206             :         int         eo;
    3207             : 
    3208             :         /* Find next escape char, if any. */
    3209         536 :         p = memchr(p, '\\', p_end - p);
    3210         536 :         if (p == NULL)
    3211         174 :             p = p_end;
    3212             : 
    3213             :         /* Copy the text we just scanned over, if any. */
    3214         536 :         if (p > chunk_start)
    3215         318 :             appendBinaryStringInfo(str, chunk_start, p - chunk_start);
    3216             : 
    3217             :         /* Done if at end of string, else advance over escape char. */
    3218         536 :         if (p >= p_end)
    3219         174 :             break;
    3220         362 :         p++;
    3221             : 
    3222         362 :         if (p >= p_end)
    3223             :         {
    3224             :             /* Escape at very end of input.  Treat same as unexpected char */
    3225           6 :             appendStringInfoChar(str, '\\');
    3226           6 :             break;
    3227             :         }
    3228             : 
    3229         356 :         if (*p >= '1' && *p <= '9')
    3230         296 :         {
    3231             :             /* Use the back reference of regexp. */
    3232         296 :             int         idx = *p - '0';
    3233             : 
    3234         296 :             so = pmatch[idx].rm_so;
    3235         296 :             eo = pmatch[idx].rm_eo;
    3236         296 :             p++;
    3237             :         }
    3238          60 :         else if (*p == '&')
    3239             :         {
    3240             :             /* Use the entire matched string. */
    3241          18 :             so = pmatch[0].rm_so;
    3242          18 :             eo = pmatch[0].rm_eo;
    3243          18 :             p++;
    3244             :         }
    3245          42 :         else if (*p == '\\')
    3246             :         {
    3247             :             /* \\ means transfer one \ to output. */
    3248          36 :             appendStringInfoChar(str, '\\');
    3249          36 :             p++;
    3250          36 :             continue;
    3251             :         }
    3252             :         else
    3253             :         {
    3254             :             /*
    3255             :              * If escape char is not followed by any expected char, just treat
    3256             :              * it as ordinary data to copy.  (XXX would it be better to throw
    3257             :              * an error?)
    3258             :              */
    3259           6 :             appendStringInfoChar(str, '\\');
    3260           6 :             continue;
    3261             :         }
    3262             : 
    3263         314 :         if (so >= 0 && eo >= 0)
    3264             :         {
    3265             :             /*
    3266             :              * Copy the text that is back reference of regexp.  Note so and eo
    3267             :              * are counted in characters not bytes.
    3268             :              */
    3269             :             char       *chunk_start;
    3270             :             int         chunk_len;
    3271             : 
    3272             :             Assert(so >= data_pos);
    3273         314 :             chunk_start = start_ptr;
    3274         314 :             chunk_start += charlen_to_bytelen(chunk_start, so - data_pos);
    3275         314 :             chunk_len = charlen_to_bytelen(chunk_start, eo - so);
    3276         314 :             appendBinaryStringInfo(str, chunk_start, chunk_len);
    3277             :         }
    3278             :     }
    3279         254 : }
    3280             : 
    3281             : /*
    3282             :  * replace_text_regexp
    3283             :  *
    3284             :  * replace substring(s) in src_text that match pattern with replace_text.
    3285             :  * The replace_text can contain backslash markers to substitute
    3286             :  * (parts of) the matched text.
    3287             :  *
    3288             :  * cflags: regexp compile flags.
    3289             :  * collation: collation to use.
    3290             :  * search_start: the character (not byte) offset in src_text at which to
    3291             :  * begin searching.
    3292             :  * n: if 0, replace all matches; if > 0, replace only the N'th match.
    3293             :  */
    3294             : text *
    3295       18804 : replace_text_regexp(text *src_text, text *pattern_text,
    3296             :                     text *replace_text,
    3297             :                     int cflags, Oid collation,
    3298             :                     int search_start, int n)
    3299             : {
    3300             :     text       *ret_text;
    3301             :     regex_t    *re;
    3302       18804 :     int         src_text_len = VARSIZE_ANY_EXHDR(src_text);
    3303       18804 :     int         nmatches = 0;
    3304             :     StringInfoData buf;
    3305             :     regmatch_t  pmatch[10];     /* main match, plus \1 to \9 */
    3306       18804 :     int         nmatch = lengthof(pmatch);
    3307             :     pg_wchar   *data;
    3308             :     size_t      data_len;
    3309             :     int         data_pos;
    3310             :     char       *start_ptr;
    3311             :     int         escape_status;
    3312             : 
    3313       18804 :     initStringInfo(&buf);
    3314             : 
    3315             :     /* Convert data string to wide characters. */
    3316       18804 :     data = (pg_wchar *) palloc((src_text_len + 1) * sizeof(pg_wchar));
    3317       18804 :     data_len = pg_mb2wchar_with_len(VARDATA_ANY(src_text), data, src_text_len);
    3318             : 
    3319             :     /* Check whether replace_text has escapes, especially regexp submatches. */
    3320       18804 :     escape_status = check_replace_text_has_escape(replace_text);
    3321             : 
    3322             :     /* If no regexp submatches, we can use REG_NOSUB. */
    3323       18804 :     if (escape_status < 2)
    3324             :     {
    3325       18000 :         cflags |= REG_NOSUB;
    3326             :         /* Also tell pg_regexec we only want the whole-match location. */
    3327       18000 :         nmatch = 1;
    3328             :     }
    3329             : 
    3330             :     /* Prepare the regexp. */
    3331       18804 :     re = RE_compile_and_cache(pattern_text, cflags, collation);
    3332             : 
    3333             :     /* start_ptr points to the data_pos'th character of src_text */
    3334       18804 :     start_ptr = (char *) VARDATA_ANY(src_text);
    3335       18804 :     data_pos = 0;
    3336             : 
    3337       25224 :     while (search_start <= data_len)
    3338             :     {
    3339             :         int         regexec_result;
    3340             : 
    3341       25218 :         CHECK_FOR_INTERRUPTS();
    3342             : 
    3343       25218 :         regexec_result = pg_regexec(re,
    3344             :                                     data,
    3345             :                                     data_len,
    3346             :                                     search_start,
    3347             :                                     NULL,   /* no details */
    3348             :                                     nmatch,
    3349             :                                     pmatch,
    3350             :                                     0);
    3351             : 
    3352       25218 :         if (regexec_result == REG_NOMATCH)
    3353       16710 :             break;
    3354             : 
    3355        8508 :         if (regexec_result != REG_OKAY)
    3356             :         {
    3357             :             char        errMsg[100];
    3358             : 
    3359           0 :             pg_regerror(regexec_result, re, errMsg, sizeof(errMsg));
    3360           0 :             ereport(ERROR,
    3361             :                     (errcode(ERRCODE_INVALID_REGULAR_EXPRESSION),
    3362             :                      errmsg("regular expression failed: %s", errMsg)));
    3363             :         }
    3364             : 
    3365             :         /*
    3366             :          * Count matches, and decide whether to replace this match.
    3367             :          */
    3368        8508 :         nmatches++;
    3369        8508 :         if (n > 0 && nmatches != n)
    3370             :         {
    3371             :             /*
    3372             :              * No, so advance search_start, but not start_ptr/data_pos. (Thus,
    3373             :              * we treat the matched text as if it weren't matched, and copy it
    3374             :              * to the output later.)
    3375             :              */
    3376          60 :             search_start = pmatch[0].rm_eo;
    3377          60 :             if (pmatch[0].rm_so == pmatch[0].rm_eo)
    3378           0 :                 search_start++;
    3379          60 :             continue;
    3380             :         }
    3381             : 
    3382             :         /*
    3383             :          * Copy the text to the left of the match position.  Note we are given
    3384             :          * character not byte indexes.
    3385             :          */
    3386        8448 :         if (pmatch[0].rm_so - data_pos > 0)
    3387             :         {
    3388             :             int         chunk_len;
    3389             : 
    3390        8256 :             chunk_len = charlen_to_bytelen(start_ptr,
    3391        8256 :                                            pmatch[0].rm_so - data_pos);
    3392        8256 :             appendBinaryStringInfo(&buf, start_ptr, chunk_len);
    3393             : 
    3394             :             /*
    3395             :              * Advance start_ptr over that text, to avoid multiple rescans of
    3396             :              * it if the replace_text contains multiple back-references.
    3397             :              */
    3398        8256 :             start_ptr += chunk_len;
    3399        8256 :             data_pos = pmatch[0].rm_so;
    3400             :         }
    3401             : 
    3402             :         /*
    3403             :          * Copy the replace_text, processing escapes if any are present.
    3404             :          */
    3405        8448 :         if (escape_status > 0)
    3406         254 :             appendStringInfoRegexpSubstr(&buf, replace_text, pmatch,
    3407             :                                          start_ptr, data_pos);
    3408             :         else
    3409        8194 :             appendStringInfoText(&buf, replace_text);
    3410             : 
    3411             :         /* Advance start_ptr and data_pos over the matched text. */
    3412       16896 :         start_ptr += charlen_to_bytelen(start_ptr,
    3413        8448 :                                         pmatch[0].rm_eo - data_pos);
    3414        8448 :         data_pos = pmatch[0].rm_eo;
    3415             : 
    3416             :         /*
    3417             :          * If we only want to replace one occurrence, we're done.
    3418             :          */
    3419        8448 :         if (n > 0)
    3420        2088 :             break;
    3421             : 
    3422             :         /*
    3423             :          * Advance search position.  Normally we start the next search at the
    3424             :          * end of the previous match; but if the match was of zero length, we
    3425             :          * have to advance by one character, or we'd just find the same match
    3426             :          * again.
    3427             :          */
    3428        6360 :         search_start = data_pos;
    3429        6360 :         if (pmatch[0].rm_so == pmatch[0].rm_eo)
    3430          12 :             search_start++;
    3431             :     }
    3432             : 
    3433             :     /*
    3434             :      * Copy the text to the right of the last match.
    3435             :      */
    3436       18804 :     if (data_pos < data_len)
    3437             :     {
    3438             :         int         chunk_len;
    3439             : 
    3440       17908 :         chunk_len = ((char *) src_text + VARSIZE_ANY(src_text)) - start_ptr;
    3441       17908 :         appendBinaryStringInfo(&buf, start_ptr, chunk_len);
    3442             :     }
    3443             : 
    3444       18804 :     ret_text = cstring_to_text_with_len(buf.data, buf.len);
    3445       18804 :     pfree(buf.data);
    3446       18804 :     pfree(data);
    3447             : 
    3448       18804 :     return ret_text;
    3449             : }
    3450             : 
    3451             : /*
    3452             :  * split_part
    3453             :  * parse input string based on provided field separator
    3454             :  * return N'th item (1 based, negative counts from end)
    3455             :  */
    3456             : Datum
    3457         150 : split_part(PG_FUNCTION_ARGS)
    3458             : {
    3459         150 :     text       *inputstring = PG_GETARG_TEXT_PP(0);
    3460         150 :     text       *fldsep = PG_GETARG_TEXT_PP(1);
    3461         150 :     int         fldnum = PG_GETARG_INT32(2);
    3462             :     int         inputstring_len;
    3463             :     int         fldsep_len;
    3464             :     TextPositionState state;
    3465             :     char       *start_ptr;
    3466             :     char       *end_ptr;
    3467             :     text       *result_text;
    3468             :     bool        found;
    3469             : 
    3470             :     /* field number is 1 based */
    3471         150 :     if (fldnum == 0)
    3472           6 :         ereport(ERROR,
    3473             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    3474             :                  errmsg("field position must not be zero")));
    3475             : 
    3476         144 :     inputstring_len = VARSIZE_ANY_EXHDR(inputstring);
    3477         144 :     fldsep_len = VARSIZE_ANY_EXHDR(fldsep);
    3478             : 
    3479             :     /* return empty string for empty input string */
    3480         144 :     if (inputstring_len < 1)
    3481          12 :         PG_RETURN_TEXT_P(cstring_to_text(""));
    3482             : 
    3483             :     /* handle empty field separator */
    3484         132 :     if (fldsep_len < 1)
    3485             :     {
    3486             :         /* if first or last field, return input string, else empty string */
    3487          24 :         if (fldnum == 1 || fldnum == -1)
    3488          12 :             PG_RETURN_TEXT_P(inputstring);
    3489             :         else
    3490          12 :             PG_RETURN_TEXT_P(cstring_to_text(""));
    3491             :     }
    3492             : 
    3493             :     /* find the first field separator */
    3494         108 :     text_position_setup(inputstring, fldsep, PG_GET_COLLATION(), &state);
    3495             : 
    3496         108 :     found = text_position_next(&state);
    3497             : 
    3498             :     /* special case if fldsep not found at all */
    3499         108 :     if (!found)
    3500             :     {
    3501          24 :         text_position_cleanup(&state);
    3502             :         /* if first or last field, return input string, else empty string */
    3503          24 :         if (fldnum == 1 || fldnum == -1)
    3504          12 :             PG_RETURN_TEXT_P(inputstring);
    3505             :         else
    3506          12 :             PG_RETURN_TEXT_P(cstring_to_text(""));
    3507             :     }
    3508             : 
    3509             :     /*
    3510             :      * take care of a negative field number (i.e. count from the right) by
    3511             :      * converting to a positive field number; we need total number of fields
    3512             :      */
    3513          84 :     if (fldnum < 0)
    3514             :     {
    3515             :         /* we found a fldsep, so there are at least two fields */
    3516          42 :         int         numfields = 2;
    3517             : 
    3518          54 :         while (text_position_next(&state))
    3519          12 :             numfields++;
    3520             : 
    3521             :         /* special case of last field does not require an extra pass */
    3522          42 :         if (fldnum == -1)
    3523             :         {
    3524          24 :             start_ptr = text_position_get_match_ptr(&state) + state.last_match_len;
    3525          24 :             end_ptr = VARDATA_ANY(inputstring) + inputstring_len;
    3526          24 :             text_position_cleanup(&state);
    3527          24 :             PG_RETURN_TEXT_P(cstring_to_text_with_len(start_ptr,
    3528             :                                                       end_ptr - start_ptr));
    3529             :         }
    3530             : 
    3531             :         /* else, convert fldnum to positive notation */
    3532          18 :         fldnum += numfields + 1;
    3533             : 
    3534             :         /* if nonexistent field, return empty string */
    3535          18 :         if (fldnum <= 0)
    3536             :         {
    3537           6 :             text_position_cleanup(&state);
    3538           6 :             PG_RETURN_TEXT_P(cstring_to_text(""));
    3539             :         }
    3540             : 
    3541             :         /* reset to pointing at first match, but now with positive fldnum */
    3542          12 :         text_position_reset(&state);
    3543          12 :         found = text_position_next(&state);
    3544             :         Assert(found);
    3545             :     }
    3546             : 
    3547             :     /* identify bounds of first field */
    3548          54 :     start_ptr = VARDATA_ANY(inputstring);
    3549          54 :     end_ptr = text_position_get_match_ptr(&state);
    3550             : 
    3551         102 :     while (found && --fldnum > 0)
    3552             :     {
    3553             :         /* identify bounds of next field */
    3554          48 :         start_ptr = end_ptr + state.last_match_len;
    3555          48 :         found = text_position_next(&state);
    3556          48 :         if (found)
    3557          18 :             end_ptr = text_position_get_match_ptr(&state);
    3558             :     }
    3559             : 
    3560          54 :     text_position_cleanup(&state);
    3561             : 
    3562          54 :     if (fldnum > 0)
    3563             :     {
    3564             :         /* N'th field separator not found */
    3565             :         /* if last field requested, return it, else empty string */
    3566          30 :         if (fldnum == 1)
    3567             :         {
    3568          24 :             int         last_len = start_ptr - VARDATA_ANY(inputstring);
    3569             : 
    3570          24 :             result_text = cstring_to_text_with_len(start_ptr,
    3571             :                                                    inputstring_len - last_len);
    3572             :         }
    3573             :         else
    3574           6 :             result_text = cstring_to_text("");
    3575             :     }
    3576             :     else
    3577             :     {
    3578             :         /* non-last field requested */
    3579          24 :         result_text = cstring_to_text_with_len(start_ptr, end_ptr - start_ptr);
    3580             :     }
    3581             : 
    3582          54 :     PG_RETURN_TEXT_P(result_text);
    3583             : }
    3584             : 
    3585             : /*
    3586             :  * Convenience function to return true when two text params are equal.
    3587             :  */
    3588             : static bool
    3589         384 : text_isequal(text *txt1, text *txt2, Oid collid)
    3590             : {
    3591         384 :     return DatumGetBool(DirectFunctionCall2Coll(texteq,
    3592             :                                                 collid,
    3593             :                                                 PointerGetDatum(txt1),
    3594             :                                                 PointerGetDatum(txt2)));
    3595             : }
    3596             : 
    3597             : /*
    3598             :  * text_to_array
    3599             :  * parse input string and return text array of elements,
    3600             :  * based on provided field separator
    3601             :  */
    3602             : Datum
    3603         170 : text_to_array(PG_FUNCTION_ARGS)
    3604             : {
    3605             :     SplitTextOutputData tstate;
    3606             : 
    3607             :     /* For array output, tstate should start as all zeroes */
    3608         170 :     memset(&tstate, 0, sizeof(tstate));
    3609             : 
    3610         170 :     if (!split_text(fcinfo, &tstate))
    3611           6 :         PG_RETURN_NULL();
    3612             : 
    3613         164 :     if (tstate.astate == NULL)
    3614           6 :         PG_RETURN_ARRAYTYPE_P(construct_empty_array(TEXTOID));
    3615             : 
    3616         158 :     PG_RETURN_DATUM(makeArrayResult(tstate.astate,
    3617             :                                     CurrentMemoryContext));
    3618             : }
    3619             : 
    3620             : /*
    3621             :  * text_to_array_null
    3622             :  * parse input string and return text array of elements,
    3623             :  * based on provided field separator and null string
    3624             :  *
    3625             :  * This is a separate entry point only to prevent the regression tests from
    3626             :  * complaining about different argument sets for the same internal function.
    3627             :  */
    3628             : Datum
    3629          60 : text_to_array_null(PG_FUNCTION_ARGS)
    3630             : {
    3631          60 :     return text_to_array(fcinfo);
    3632             : }
    3633             : 
    3634             : /*
    3635             :  * text_to_table
    3636             :  * parse input string and return table of elements,
    3637             :  * based on provided field separator
    3638             :  */
    3639             : Datum
    3640          84 : text_to_table(PG_FUNCTION_ARGS)
    3641             : {
    3642          84 :     ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
    3643             :     SplitTextOutputData tstate;
    3644             : 
    3645          84 :     tstate.astate = NULL;
    3646          84 :     InitMaterializedSRF(fcinfo, MAT_SRF_USE_EXPECTED_DESC);
    3647          84 :     tstate.tupstore = rsi->setResult;
    3648          84 :     tstate.tupdesc = rsi->setDesc;
    3649             : 
    3650          84 :     (void) split_text(fcinfo, &tstate);
    3651             : 
    3652          84 :     return (Datum) 0;
    3653             : }
    3654             : 
    3655             : /*
    3656             :  * text_to_table_null
    3657             :  * parse input string and return table of elements,
    3658             :  * based on provided field separator and null string
    3659             :  *
    3660             :  * This is a separate entry point only to prevent the regression tests from
    3661             :  * complaining about different argument sets for the same internal function.
    3662             :  */
    3663             : Datum
    3664          24 : text_to_table_null(PG_FUNCTION_ARGS)
    3665             : {
    3666          24 :     return text_to_table(fcinfo);
    3667             : }
    3668             : 
    3669             : /*
    3670             :  * Common code for text_to_array, text_to_array_null, text_to_table
    3671             :  * and text_to_table_null functions.
    3672             :  *
    3673             :  * These are not strict so we have to test for null inputs explicitly.
    3674             :  * Returns false if result is to be null, else returns true.
    3675             :  *
    3676             :  * Note that if the result is valid but empty (zero elements), we return
    3677             :  * without changing *tstate --- caller must handle that case, too.
    3678             :  */
    3679             : static bool
    3680         254 : split_text(FunctionCallInfo fcinfo, SplitTextOutputData *tstate)
    3681             : {
    3682             :     text       *inputstring;
    3683             :     text       *fldsep;
    3684             :     text       *null_string;
    3685         254 :     Oid         collation = PG_GET_COLLATION();
    3686             :     int         inputstring_len;
    3687             :     int         fldsep_len;
    3688             :     char       *start_ptr;
    3689             :     text       *result_text;
    3690             : 
    3691             :     /* when input string is NULL, then result is NULL too */
    3692         254 :     if (PG_ARGISNULL(0))
    3693          12 :         return false;
    3694             : 
    3695         242 :     inputstring = PG_GETARG_TEXT_PP(0);
    3696             : 
    3697             :     /* fldsep can be NULL */
    3698         242 :     if (!PG_ARGISNULL(1))
    3699         212 :         fldsep = PG_GETARG_TEXT_PP(1);
    3700             :     else
    3701          30 :         fldsep = NULL;
    3702             : 
    3703             :     /* null_string can be NULL or omitted */
    3704         242 :     if (PG_NARGS() > 2 && !PG_ARGISNULL(2))
    3705          84 :         null_string = PG_GETARG_TEXT_PP(2);
    3706             :     else
    3707         158 :         null_string = NULL;
    3708             : 
    3709         242 :     if (fldsep != NULL)
    3710             :     {
    3711             :         /*
    3712             :          * Normal case with non-null fldsep.  Use the text_position machinery
    3713             :          * to search for occurrences of fldsep.
    3714             :          */
    3715             :         TextPositionState state;
    3716             : 
    3717         212 :         inputstring_len = VARSIZE_ANY_EXHDR(inputstring);
    3718         212 :         fldsep_len = VARSIZE_ANY_EXHDR(fldsep);
    3719             : 
    3720             :         /* return empty set for empty input string */
    3721         212 :         if (inputstring_len < 1)
    3722          60 :             return true;
    3723             : 
    3724             :         /* empty field separator: return input string as a one-element set */
    3725         200 :         if (fldsep_len < 1)
    3726             :         {
    3727          48 :             split_text_accum_result(tstate, inputstring,
    3728             :                                     null_string, collation);
    3729          48 :             return true;
    3730             :         }
    3731             : 
    3732         152 :         text_position_setup(inputstring, fldsep, collation, &state);
    3733             : 
    3734         152 :         start_ptr = VARDATA_ANY(inputstring);
    3735             : 
    3736             :         for (;;)
    3737         512 :         {
    3738             :             bool        found;
    3739             :             char       *end_ptr;
    3740             :             int         chunk_len;
    3741             : 
    3742         664 :             CHECK_FOR_INTERRUPTS();
    3743             : 
    3744         664 :             found = text_position_next(&state);
    3745         664 :             if (!found)
    3746             :             {
    3747             :                 /* fetch last field */
    3748         152 :                 chunk_len = ((char *) inputstring + VARSIZE_ANY(inputstring)) - start_ptr;
    3749         152 :                 end_ptr = NULL; /* not used, but some compilers complain */
    3750             :             }
    3751             :             else
    3752             :             {
    3753             :                 /* fetch non-last field */
    3754         512 :                 end_ptr = text_position_get_match_ptr(&state);
    3755         512 :                 chunk_len = end_ptr - start_ptr;
    3756             :             }
    3757             : 
    3758             :             /* build a temp text datum to pass to split_text_accum_result */
    3759         664 :             result_text = cstring_to_text_with_len(start_ptr, chunk_len);
    3760             : 
    3761             :             /* stash away this field */
    3762         664 :             split_text_accum_result(tstate, result_text,
    3763             :                                     null_string, collation);
    3764             : 
    3765         664 :             pfree(result_text);
    3766             : 
    3767         664 :             if (!found)
    3768         152 :                 break;
    3769             : 
    3770         512 :             start_ptr = end_ptr + state.last_match_len;
    3771             :         }
    3772             : 
    3773         152 :         text_position_cleanup(&state);
    3774             :     }
    3775             :     else
    3776             :     {
    3777             :         const char *end_ptr;
    3778             : 
    3779             :         /*
    3780             :          * When fldsep is NULL, each character in the input string becomes a
    3781             :          * separate element in the result set.  The separator is effectively
    3782             :          * the space between characters.
    3783             :          */
    3784          30 :         inputstring_len = VARSIZE_ANY_EXHDR(inputstring);
    3785             : 
    3786          30 :         start_ptr = VARDATA_ANY(inputstring);
    3787          30 :         end_ptr = start_ptr + inputstring_len;
    3788             : 
    3789         252 :         while (inputstring_len > 0)
    3790             :         {
    3791         222 :             int         chunk_len = pg_mblen_range(start_ptr, end_ptr);
    3792             : 
    3793         222 :             CHECK_FOR_INTERRUPTS();
    3794             : 
    3795             :             /* build a temp text datum to pass to split_text_accum_result */
    3796         222 :             result_text = cstring_to_text_with_len(start_ptr, chunk_len);
    3797             : 
    3798             :             /* stash away this field */
    3799         222 :             split_text_accum_result(tstate, result_text,
    3800             :                                     null_string, collation);
    3801             : 
    3802         222 :             pfree(result_text);
    3803             : 
    3804         222 :             start_ptr += chunk_len;
    3805         222 :             inputstring_len -= chunk_len;
    3806             :         }
    3807             :     }
    3808             : 
    3809         182 :     return true;
    3810             : }
    3811             : 
    3812             : /*
    3813             :  * Add text item to result set (table or array).
    3814             :  *
    3815             :  * This is also responsible for checking to see if the item matches
    3816             :  * the null_string, in which case we should emit NULL instead.
    3817             :  */
    3818             : static void
    3819         934 : split_text_accum_result(SplitTextOutputData *tstate,
    3820             :                         text *field_value,
    3821             :                         text *null_string,
    3822             :                         Oid collation)
    3823             : {
    3824         934 :     bool        is_null = false;
    3825             : 
    3826         934 :     if (null_string && text_isequal(field_value, null_string, collation))
    3827          72 :         is_null = true;
    3828             : 
    3829         934 :     if (tstate->tupstore)
    3830             :     {
    3831             :         Datum       values[1];
    3832             :         bool        nulls[1];
    3833             : 
    3834         228 :         values[0] = PointerGetDatum(field_value);
    3835         228 :         nulls[0] = is_null;
    3836             : 
    3837         228 :         tuplestore_putvalues(tstate->tupstore,
    3838             :                              tstate->tupdesc,
    3839             :                              values,
    3840             :                              nulls);
    3841             :     }
    3842             :     else
    3843             :     {
    3844         706 :         tstate->astate = accumArrayResult(tstate->astate,
    3845             :                                           PointerGetDatum(field_value),
    3846             :                                           is_null,
    3847             :                                           TEXTOID,
    3848             :                                           CurrentMemoryContext);
    3849             :     }
    3850         934 : }
    3851             : 
    3852             : /*
    3853             :  * array_to_text
    3854             :  * concatenate Cstring representation of input array elements
    3855             :  * using provided field separator
    3856             :  */
    3857             : Datum
    3858       77118 : array_to_text(PG_FUNCTION_ARGS)
    3859             : {
    3860       77118 :     ArrayType  *v = PG_GETARG_ARRAYTYPE_P(0);
    3861       77118 :     char       *fldsep = text_to_cstring(PG_GETARG_TEXT_PP(1));
    3862             : 
    3863       77118 :     PG_RETURN_TEXT_P(array_to_text_internal(fcinfo, v, fldsep, NULL));
    3864             : }
    3865             : 
    3866             : /*
    3867             :  * array_to_text_null
    3868             :  * concatenate Cstring representation of input array elements
    3869             :  * using provided field separator and null string
    3870             :  *
    3871             :  * This version is not strict so we have to test for null inputs explicitly.
    3872             :  */
    3873             : Datum
    3874          12 : array_to_text_null(PG_FUNCTION_ARGS)
    3875             : {
    3876             :     ArrayType  *v;
    3877             :     char       *fldsep;
    3878             :     char       *null_string;
    3879             : 
    3880             :     /* returns NULL when first or second parameter is NULL */
    3881          12 :     if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
    3882           0 :         PG_RETURN_NULL();
    3883             : 
    3884          12 :     v = PG_GETARG_ARRAYTYPE_P(0);
    3885          12 :     fldsep = text_to_cstring(PG_GETARG_TEXT_PP(1));
    3886             : 
    3887             :     /* NULL null string is passed through as a null pointer */
    3888          12 :     if (!PG_ARGISNULL(2))
    3889           6 :         null_string = text_to_cstring(PG_GETARG_TEXT_PP(2));
    3890             :     else
    3891           6 :         null_string = NULL;
    3892             : 
    3893          12 :     PG_RETURN_TEXT_P(array_to_text_internal(fcinfo, v, fldsep, null_string));
    3894             : }
    3895             : 
    3896             : /*
    3897             :  * common code for array_to_text and array_to_text_null functions
    3898             :  */
    3899             : static text *
    3900       77148 : array_to_text_internal(FunctionCallInfo fcinfo, ArrayType *v,
    3901             :                        const char *fldsep, const char *null_string)
    3902             : {
    3903             :     text       *result;
    3904             :     int         nitems,
    3905             :                *dims,
    3906             :                 ndims;
    3907             :     Oid         element_type;
    3908             :     int         typlen;
    3909             :     bool        typbyval;
    3910             :     char        typalign;
    3911             :     uint8       typalignby;
    3912             :     StringInfoData buf;
    3913       77148 :     bool        printed = false;
    3914             :     char       *p;
    3915             :     bits8      *bitmap;
    3916             :     int         bitmask;
    3917             :     int         i;
    3918             :     ArrayMetaState *my_extra;
    3919             : 
    3920       77148 :     ndims = ARR_NDIM(v);
    3921       77148 :     dims = ARR_DIMS(v);
    3922       77148 :     nitems = ArrayGetNItems(ndims, dims);
    3923             : 
    3924             :     /* if there are no elements, return an empty string */
    3925       77148 :     if (nitems == 0)
    3926       51930 :         return cstring_to_text_with_len("", 0);
    3927             : 
    3928       25218 :     element_type = ARR_ELEMTYPE(v);
    3929       25218 :     initStringInfo(&buf);
    3930             : 
    3931             :     /*
    3932             :      * We arrange to look up info about element type, including its output
    3933             :      * conversion proc, only once per series of calls, assuming the element
    3934             :      * type doesn't change underneath us.
    3935             :      */
    3936       25218 :     my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
    3937       25218 :     if (my_extra == NULL)
    3938             :     {
    3939        1420 :         fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
    3940             :                                                       sizeof(ArrayMetaState));
    3941        1420 :         my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
    3942        1420 :         my_extra->element_type = ~element_type;
    3943             :     }
    3944             : 
    3945       25218 :     if (my_extra->element_type != element_type)
    3946             :     {
    3947             :         /*
    3948             :          * Get info about element type, including its output conversion proc
    3949             :          */
    3950        1420 :         get_type_io_data(element_type, IOFunc_output,
    3951             :                          &my_extra->typlen, &my_extra->typbyval,
    3952             :                          &my_extra->typalign, &my_extra->typdelim,
    3953             :                          &my_extra->typioparam, &my_extra->typiofunc);
    3954        1420 :         fmgr_info_cxt(my_extra->typiofunc, &my_extra->proc,
    3955        1420 :                       fcinfo->flinfo->fn_mcxt);
    3956        1420 :         my_extra->element_type = element_type;
    3957             :     }
    3958       25218 :     typlen = my_extra->typlen;
    3959       25218 :     typbyval = my_extra->typbyval;
    3960       25218 :     typalign = my_extra->typalign;
    3961       25218 :     typalignby = typalign_to_alignby(typalign);
    3962             : 
    3963       25218 :     p = ARR_DATA_PTR(v);
    3964       25218 :     bitmap = ARR_NULLBITMAP(v);
    3965       25218 :     bitmask = 1;
    3966             : 
    3967       85718 :     for (i = 0; i < nitems; i++)
    3968             :     {
    3969             :         Datum       itemvalue;
    3970             :         char       *value;
    3971             : 
    3972             :         /* Get source element, checking for NULL */
    3973       60500 :         if (bitmap && (*bitmap & bitmask) == 0)
    3974             :         {
    3975             :             /* if null_string is NULL, we just ignore null elements */
    3976          18 :             if (null_string != NULL)
    3977             :             {
    3978           6 :                 if (printed)
    3979           6 :                     appendStringInfo(&buf, "%s%s", fldsep, null_string);
    3980             :                 else
    3981           0 :                     appendStringInfoString(&buf, null_string);
    3982           6 :                 printed = true;
    3983             :             }
    3984             :         }
    3985             :         else
    3986             :         {
    3987       60482 :             itemvalue = fetch_att(p, typbyval, typlen);
    3988             : 
    3989       60482 :             value = OutputFunctionCall(&my_extra->proc, itemvalue);
    3990             : 
    3991       60482 :             if (printed)
    3992       35264 :                 appendStringInfo(&buf, "%s%s", fldsep, value);
    3993             :             else
    3994       25218 :                 appendStringInfoString(&buf, value);
    3995       60482 :             printed = true;
    3996             : 
    3997       60482 :             p = att_addlength_pointer(p, typlen, p);
    3998       60482 :             p = (char *) att_nominal_alignby(p, typalignby);
    3999             :         }
    4000             : 
    4001             :         /* advance bitmap pointer if any */
    4002       60500 :         if (bitmap)
    4003             :         {
    4004         108 :             bitmask <<= 1;
    4005         108 :             if (bitmask == 0x100)
    4006             :             {
    4007           0 :                 bitmap++;
    4008           0 :                 bitmask = 1;
    4009             :             }
    4010             :         }
    4011             :     }
    4012             : 
    4013       25218 :     result = cstring_to_text_with_len(buf.data, buf.len);
    4014       25218 :     pfree(buf.data);
    4015             : 
    4016       25218 :     return result;
    4017             : }
    4018             : 
    4019             : /*
    4020             :  * Workhorse for to_bin, to_oct, and to_hex.  Note that base must be > 1 and <=
    4021             :  * 16.
    4022             :  */
    4023             : static inline text *
    4024       38750 : convert_to_base(uint64 value, int base)
    4025             : {
    4026       38750 :     const char *digits = "0123456789abcdef";
    4027             : 
    4028             :     /* We size the buffer for to_bin's longest possible return value. */
    4029             :     char        buf[sizeof(uint64) * BITS_PER_BYTE];
    4030       38750 :     char       *const end = buf + sizeof(buf);
    4031       38750 :     char       *ptr = end;
    4032             : 
    4033             :     Assert(base > 1);
    4034             :     Assert(base <= 16);
    4035             : 
    4036             :     do
    4037             :     {
    4038       75974 :         *--ptr = digits[value % base];
    4039       75974 :         value /= base;
    4040       75974 :     } while (ptr > buf && value);
    4041             : 
    4042       38750 :     return cstring_to_text_with_len(ptr, end - ptr);
    4043             : }
    4044             : 
    4045             : /*
    4046             :  * Convert an integer to a string containing a base-2 (binary) representation
    4047             :  * of the number.
    4048             :  */
    4049             : Datum
    4050          12 : to_bin32(PG_FUNCTION_ARGS)
    4051             : {
    4052          12 :     uint64      value = (uint32) PG_GETARG_INT32(0);
    4053             : 
    4054          12 :     PG_RETURN_TEXT_P(convert_to_base(value, 2));
    4055             : }
    4056             : Datum
    4057          12 : to_bin64(PG_FUNCTION_ARGS)
    4058             : {
    4059          12 :     uint64      value = (uint64) PG_GETARG_INT64(0);
    4060             : 
    4061          12 :     PG_RETURN_TEXT_P(convert_to_base(value, 2));
    4062             : }
    4063             : 
    4064             : /*
    4065             :  * Convert an integer to a string containing a base-8 (oct) representation of
    4066             :  * the number.
    4067             :  */
    4068             : Datum
    4069          12 : to_oct32(PG_FUNCTION_ARGS)
    4070             : {
    4071          12 :     uint64      value = (uint32) PG_GETARG_INT32(0);
    4072             : 
    4073          12 :     PG_RETURN_TEXT_P(convert_to_base(value, 8));
    4074             : }
    4075             : Datum
    4076          12 : to_oct64(PG_FUNCTION_ARGS)
    4077             : {
    4078          12 :     uint64      value = (uint64) PG_GETARG_INT64(0);
    4079             : 
    4080          12 :     PG_RETURN_TEXT_P(convert_to_base(value, 8));
    4081             : }
    4082             : 
    4083             : /*
    4084             :  * Convert an integer to a string containing a base-16 (hex) representation of
    4085             :  * the number.
    4086             :  */
    4087             : Datum
    4088       38690 : to_hex32(PG_FUNCTION_ARGS)
    4089             : {
    4090       38690 :     uint64      value = (uint32) PG_GETARG_INT32(0);
    4091             : 
    4092       38690 :     PG_RETURN_TEXT_P(convert_to_base(value, 16));
    4093             : }
    4094             : Datum
    4095          12 : to_hex64(PG_FUNCTION_ARGS)
    4096             : {
    4097          12 :     uint64      value = (uint64) PG_GETARG_INT64(0);
    4098             : 
    4099          12 :     PG_RETURN_TEXT_P(convert_to_base(value, 16));
    4100             : }
    4101             : 
    4102             : /*
    4103             :  * Return the size of a datum, possibly compressed
    4104             :  *
    4105             :  * Works on any data type
    4106             :  */
    4107             : Datum
    4108         122 : pg_column_size(PG_FUNCTION_ARGS)
    4109             : {
    4110         122 :     Datum       value = PG_GETARG_DATUM(0);
    4111             :     int32       result;
    4112             :     int         typlen;
    4113             : 
    4114             :     /* On first call, get the input type's typlen, and save at *fn_extra */
    4115         122 :     if (fcinfo->flinfo->fn_extra == NULL)
    4116             :     {
    4117             :         /* Lookup the datatype of the supplied argument */
    4118         122 :         Oid         argtypeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
    4119             : 
    4120         122 :         typlen = get_typlen(argtypeid);
    4121         122 :         if (typlen == 0)        /* should not happen */
    4122           0 :             elog(ERROR, "cache lookup failed for type %u", argtypeid);
    4123             : 
    4124         122 :         fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
    4125             :                                                       sizeof(int));
    4126         122 :         *((int *) fcinfo->flinfo->fn_extra) = typlen;
    4127             :     }
    4128             :     else
    4129           0 :         typlen = *((int *) fcinfo->flinfo->fn_extra);
    4130             : 
    4131         122 :     if (typlen == -1)
    4132             :     {
    4133             :         /* varlena type, possibly toasted */
    4134         122 :         result = toast_datum_size(value);
    4135             :     }
    4136           0 :     else if (typlen == -2)
    4137             :     {
    4138             :         /* cstring */
    4139           0 :         result = strlen(DatumGetCString(value)) + 1;
    4140             :     }
    4141             :     else
    4142             :     {
    4143             :         /* ordinary fixed-width type */
    4144           0 :         result = typlen;
    4145             :     }
    4146             : 
    4147         122 :     PG_RETURN_INT32(result);
    4148             : }
    4149             : 
    4150             : /*
    4151             :  * Return the compression method stored in the compressed attribute.  Return
    4152             :  * NULL for non varlena type or uncompressed data.
    4153             :  */
    4154             : Datum
    4155         192 : pg_column_compression(PG_FUNCTION_ARGS)
    4156             : {
    4157             :     int         typlen;
    4158             :     char       *result;
    4159             :     ToastCompressionId cmid;
    4160             : 
    4161             :     /* On first call, get the input type's typlen, and save at *fn_extra */
    4162         192 :     if (fcinfo->flinfo->fn_extra == NULL)
    4163             :     {
    4164             :         /* Lookup the datatype of the supplied argument */
    4165         156 :         Oid         argtypeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
    4166             : 
    4167         156 :         typlen = get_typlen(argtypeid);
    4168         156 :         if (typlen == 0)        /* should not happen */
    4169           0 :             elog(ERROR, "cache lookup failed for type %u", argtypeid);
    4170             : 
    4171         156 :         fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
    4172             :                                                       sizeof(int));
    4173         156 :         *((int *) fcinfo->flinfo->fn_extra) = typlen;
    4174             :     }
    4175             :     else
    4176          36 :         typlen = *((int *) fcinfo->flinfo->fn_extra);
    4177             : 
    4178         192 :     if (typlen != -1)
    4179           0 :         PG_RETURN_NULL();
    4180             : 
    4181             :     /* get the compression method id stored in the compressed varlena */
    4182         192 :     cmid = toast_get_compression_id((varlena *)
    4183         192 :                                     DatumGetPointer(PG_GETARG_DATUM(0)));
    4184         192 :     if (cmid == TOAST_INVALID_COMPRESSION_ID)
    4185          42 :         PG_RETURN_NULL();
    4186             : 
    4187             :     /* convert compression method id to compression method name */
    4188         150 :     switch (cmid)
    4189             :     {
    4190          84 :         case TOAST_PGLZ_COMPRESSION_ID:
    4191          84 :             result = "pglz";
    4192          84 :             break;
    4193          66 :         case TOAST_LZ4_COMPRESSION_ID:
    4194          66 :             result = "lz4";
    4195          66 :             break;
    4196           0 :         default:
    4197           0 :             elog(ERROR, "invalid compression method id %d", cmid);
    4198             :     }
    4199             : 
    4200         150 :     PG_RETURN_TEXT_P(cstring_to_text(result));
    4201             : }
    4202             : 
    4203             : /*
    4204             :  * Return the chunk_id of the on-disk TOASTed value.  Return NULL if the value
    4205             :  * is un-TOASTed or not on-disk.
    4206             :  */
    4207             : Datum
    4208          52 : pg_column_toast_chunk_id(PG_FUNCTION_ARGS)
    4209             : {
    4210             :     int         typlen;
    4211             :     varlena    *attr;
    4212             :     varatt_external toast_pointer;
    4213             : 
    4214             :     /* On first call, get the input type's typlen, and save at *fn_extra */
    4215          52 :     if (fcinfo->flinfo->fn_extra == NULL)
    4216             :     {
    4217             :         /* Lookup the datatype of the supplied argument */
    4218          40 :         Oid         argtypeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
    4219             : 
    4220          40 :         typlen = get_typlen(argtypeid);
    4221          40 :         if (typlen == 0)        /* should not happen */
    4222           0 :             elog(ERROR, "cache lookup failed for type %u", argtypeid);
    4223             : 
    4224          40 :         fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
    4225             :                                                       sizeof(int));
    4226          40 :         *((int *) fcinfo->flinfo->fn_extra) = typlen;
    4227             :     }
    4228             :     else
    4229          12 :         typlen = *((int *) fcinfo->flinfo->fn_extra);
    4230             : 
    4231          52 :     if (typlen != -1)
    4232           0 :         PG_RETURN_NULL();
    4233             : 
    4234          52 :     attr = (varlena *) DatumGetPointer(PG_GETARG_DATUM(0));
    4235             : 
    4236          52 :     if (!VARATT_IS_EXTERNAL_ONDISK(attr))
    4237          12 :         PG_RETURN_NULL();
    4238             : 
    4239          40 :     VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr);
    4240             : 
    4241          40 :     PG_RETURN_OID(toast_pointer.va_valueid);
    4242             : }
    4243             : 
    4244             : /*
    4245             :  * string_agg - Concatenates values and returns string.
    4246             :  *
    4247             :  * Syntax: string_agg(value text, delimiter text) RETURNS text
    4248             :  *
    4249             :  * Note: Any NULL values are ignored. The first-call delimiter isn't
    4250             :  * actually used at all, and on subsequent calls the delimiter precedes
    4251             :  * the associated value.
    4252             :  */
    4253             : 
    4254             : /* subroutine to initialize state */
    4255             : static StringInfo
    4256        2372 : makeStringAggState(FunctionCallInfo fcinfo)
    4257             : {
    4258             :     StringInfo  state;
    4259             :     MemoryContext aggcontext;
    4260             :     MemoryContext oldcontext;
    4261             : 
    4262        2372 :     if (!AggCheckCallContext(fcinfo, &aggcontext))
    4263             :     {
    4264             :         /* cannot be called directly because of internal-type argument */
    4265           0 :         elog(ERROR, "string_agg_transfn called in non-aggregate context");
    4266             :     }
    4267             : 
    4268             :     /*
    4269             :      * Create state in aggregate context.  It'll stay there across subsequent
    4270             :      * calls.
    4271             :      */
    4272        2372 :     oldcontext = MemoryContextSwitchTo(aggcontext);
    4273        2372 :     state = makeStringInfo();
    4274        2372 :     MemoryContextSwitchTo(oldcontext);
    4275             : 
    4276        2372 :     return state;
    4277             : }
    4278             : 
    4279             : Datum
    4280     1093860 : string_agg_transfn(PG_FUNCTION_ARGS)
    4281             : {
    4282             :     StringInfo  state;
    4283             : 
    4284     1093860 :     state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
    4285             : 
    4286             :     /* Append the value unless null, preceding it with the delimiter. */
    4287     1093860 :     if (!PG_ARGISNULL(1))
    4288             :     {
    4289     1078812 :         text       *value = PG_GETARG_TEXT_PP(1);
    4290     1078812 :         bool        isfirst = false;
    4291             : 
    4292             :         /*
    4293             :          * You might think we can just throw away the first delimiter, however
    4294             :          * we must keep it as we may be a parallel worker doing partial
    4295             :          * aggregation building a state to send to the main process.  We need
    4296             :          * to keep the delimiter of every aggregation so that the combine
    4297             :          * function can properly join up the strings of two separately
    4298             :          * partially aggregated results.  The first delimiter is only stripped
    4299             :          * off in the final function.  To know how much to strip off the front
    4300             :          * of the string, we store the length of the first delimiter in the
    4301             :          * StringInfo's cursor field, which we don't otherwise need here.
    4302             :          */
    4303     1078812 :         if (state == NULL)
    4304             :         {
    4305        2052 :             state = makeStringAggState(fcinfo);
    4306        2052 :             isfirst = true;
    4307             :         }
    4308             : 
    4309     1078812 :         if (!PG_ARGISNULL(2))
    4310             :         {
    4311     1078812 :             text       *delim = PG_GETARG_TEXT_PP(2);
    4312             : 
    4313     1078812 :             appendStringInfoText(state, delim);
    4314     1078812 :             if (isfirst)
    4315        2052 :                 state->cursor = VARSIZE_ANY_EXHDR(delim);
    4316             :         }
    4317             : 
    4318     1078812 :         appendStringInfoText(state, value);
    4319             :     }
    4320             : 
    4321             :     /*
    4322             :      * The transition type for string_agg() is declared to be "internal",
    4323             :      * which is a pass-by-value type the same size as a pointer.
    4324             :      */
    4325     1093860 :     if (state)
    4326     1093772 :         PG_RETURN_POINTER(state);
    4327          88 :     PG_RETURN_NULL();
    4328             : }
    4329             : 
    4330             : /*
    4331             :  * string_agg_combine
    4332             :  *      Aggregate combine function for string_agg(text) and string_agg(bytea)
    4333             :  */
    4334             : Datum
    4335         200 : string_agg_combine(PG_FUNCTION_ARGS)
    4336             : {
    4337             :     StringInfo  state1;
    4338             :     StringInfo  state2;
    4339             :     MemoryContext agg_context;
    4340             : 
    4341         200 :     if (!AggCheckCallContext(fcinfo, &agg_context))
    4342           0 :         elog(ERROR, "aggregate function called in non-aggregate context");
    4343             : 
    4344         200 :     state1 = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
    4345         200 :     state2 = PG_ARGISNULL(1) ? NULL : (StringInfo) PG_GETARG_POINTER(1);
    4346             : 
    4347         200 :     if (state2 == NULL)
    4348             :     {
    4349             :         /*
    4350             :          * NULL state2 is easy, just return state1, which we know is already
    4351             :          * in the agg_context
    4352             :          */
    4353           0 :         if (state1 == NULL)
    4354           0 :             PG_RETURN_NULL();
    4355           0 :         PG_RETURN_POINTER(state1);
    4356             :     }
    4357             : 
    4358         200 :     if (state1 == NULL)
    4359             :     {
    4360             :         /* We must copy state2's data into the agg_context */
    4361             :         MemoryContext old_context;
    4362             : 
    4363         120 :         old_context = MemoryContextSwitchTo(agg_context);
    4364         120 :         state1 = makeStringAggState(fcinfo);
    4365         120 :         appendBinaryStringInfo(state1, state2->data, state2->len);
    4366         120 :         state1->cursor = state2->cursor;
    4367         120 :         MemoryContextSwitchTo(old_context);
    4368             :     }
    4369          80 :     else if (state2->len > 0)
    4370             :     {
    4371             :         /* Combine ... state1->cursor does not change in this case */
    4372          80 :         appendBinaryStringInfo(state1, state2->data, state2->len);
    4373             :     }
    4374             : 
    4375         200 :     PG_RETURN_POINTER(state1);
    4376             : }
    4377             : 
    4378             : /*
    4379             :  * string_agg_serialize
    4380             :  *      Aggregate serialize function for string_agg(text) and string_agg(bytea)
    4381             :  *
    4382             :  * This is strict, so we need not handle NULL input
    4383             :  */
    4384             : Datum
    4385         200 : string_agg_serialize(PG_FUNCTION_ARGS)
    4386             : {
    4387             :     StringInfo  state;
    4388             :     StringInfoData buf;
    4389             :     bytea      *result;
    4390             : 
    4391             :     /* cannot be called directly because of internal-type argument */
    4392             :     Assert(AggCheckCallContext(fcinfo, NULL));
    4393             : 
    4394         200 :     state = (StringInfo) PG_GETARG_POINTER(0);
    4395             : 
    4396         200 :     pq_begintypsend(&buf);
    4397             : 
    4398             :     /* cursor */
    4399         200 :     pq_sendint(&buf, state->cursor, 4);
    4400             : 
    4401             :     /* data */
    4402         200 :     pq_sendbytes(&buf, state->data, state->len);
    4403             : 
    4404         200 :     result = pq_endtypsend(&buf);
    4405             : 
    4406         200 :     PG_RETURN_BYTEA_P(result);
    4407             : }
    4408             : 
    4409             : /*
    4410             :  * string_agg_deserialize
    4411             :  *      Aggregate deserial function for string_agg(text) and string_agg(bytea)
    4412             :  *
    4413             :  * This is strict, so we need not handle NULL input
    4414             :  */
    4415             : Datum
    4416         200 : string_agg_deserialize(PG_FUNCTION_ARGS)
    4417             : {
    4418             :     bytea      *sstate;
    4419             :     StringInfo  result;
    4420             :     StringInfoData buf;
    4421             :     char       *data;
    4422             :     int         datalen;
    4423             : 
    4424             :     /* cannot be called directly because of internal-type argument */
    4425             :     Assert(AggCheckCallContext(fcinfo, NULL));
    4426             : 
    4427         200 :     sstate = PG_GETARG_BYTEA_PP(0);
    4428             : 
    4429             :     /*
    4430             :      * Initialize a StringInfo so that we can "receive" it using the standard
    4431             :      * recv-function infrastructure.
    4432             :      */
    4433         200 :     initReadOnlyStringInfo(&buf, VARDATA_ANY(sstate),
    4434         200 :                            VARSIZE_ANY_EXHDR(sstate));
    4435             : 
    4436         200 :     result = makeStringAggState(fcinfo);
    4437             : 
    4438             :     /* cursor */
    4439         200 :     result->cursor = pq_getmsgint(&buf, 4);
    4440             : 
    4441             :     /* data */
    4442         200 :     datalen = VARSIZE_ANY_EXHDR(sstate) - 4;
    4443         200 :     data = (char *) pq_getmsgbytes(&buf, datalen);
    4444         200 :     appendBinaryStringInfo(result, data, datalen);
    4445             : 
    4446         200 :     pq_getmsgend(&buf);
    4447             : 
    4448         200 :     PG_RETURN_POINTER(result);
    4449             : }
    4450             : 
    4451             : Datum
    4452        2096 : string_agg_finalfn(PG_FUNCTION_ARGS)
    4453             : {
    4454             :     StringInfo  state;
    4455             : 
    4456             :     /* cannot be called directly because of internal-type argument */
    4457             :     Assert(AggCheckCallContext(fcinfo, NULL));
    4458             : 
    4459        2096 :     state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
    4460             : 
    4461        2096 :     if (state != NULL)
    4462             :     {
    4463             :         /* As per comment in transfn, strip data before the cursor position */
    4464        2012 :         PG_RETURN_TEXT_P(cstring_to_text_with_len(&state->data[state->cursor],
    4465             :                                                   state->len - state->cursor));
    4466             :     }
    4467             :     else
    4468          84 :         PG_RETURN_NULL();
    4469             : }
    4470             : 
    4471             : /*
    4472             :  * Prepare cache with fmgr info for the output functions of the datatypes of
    4473             :  * the arguments of a concat-like function, beginning with argument "argidx".
    4474             :  * (Arguments before that will have corresponding slots in the resulting
    4475             :  * FmgrInfo array, but we don't fill those slots.)
    4476             :  */
    4477             : static FmgrInfo *
    4478         106 : build_concat_foutcache(FunctionCallInfo fcinfo, int argidx)
    4479             : {
    4480             :     FmgrInfo   *foutcache;
    4481             :     int         i;
    4482             : 
    4483             :     /* We keep the info in fn_mcxt so it survives across calls */
    4484         106 :     foutcache = (FmgrInfo *) MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
    4485         106 :                                                 PG_NARGS() * sizeof(FmgrInfo));
    4486             : 
    4487         400 :     for (i = argidx; i < PG_NARGS(); i++)
    4488             :     {
    4489             :         Oid         valtype;
    4490             :         Oid         typOutput;
    4491             :         bool        typIsVarlena;
    4492             : 
    4493         294 :         valtype = get_fn_expr_argtype(fcinfo->flinfo, i);
    4494         294 :         if (!OidIsValid(valtype))
    4495           0 :             elog(ERROR, "could not determine data type of concat() input");
    4496             : 
    4497         294 :         getTypeOutputInfo(valtype, &typOutput, &typIsVarlena);
    4498         294 :         fmgr_info_cxt(typOutput, &foutcache[i], fcinfo->flinfo->fn_mcxt);
    4499             :     }
    4500             : 
    4501         106 :     fcinfo->flinfo->fn_extra = foutcache;
    4502             : 
    4503         106 :     return foutcache;
    4504             : }
    4505             : 
    4506             : /*
    4507             :  * Implementation of both concat() and concat_ws().
    4508             :  *
    4509             :  * sepstr is the separator string to place between values.
    4510             :  * argidx identifies the first argument to concatenate (counting from zero);
    4511             :  * note that this must be constant across any one series of calls.
    4512             :  *
    4513             :  * Returns NULL if result should be NULL, else text value.
    4514             :  */
    4515             : static text *
    4516         264 : concat_internal(const char *sepstr, int argidx,
    4517             :                 FunctionCallInfo fcinfo)
    4518             : {
    4519             :     text       *result;
    4520             :     StringInfoData str;
    4521             :     FmgrInfo   *foutcache;
    4522         264 :     bool        first_arg = true;
    4523             :     int         i;
    4524             : 
    4525             :     /*
    4526             :      * concat(VARIADIC some-array) is essentially equivalent to
    4527             :      * array_to_text(), ie concat the array elements with the given separator.
    4528             :      * So we just pass the case off to that code.
    4529             :      */
    4530         264 :     if (get_fn_expr_variadic(fcinfo->flinfo))
    4531             :     {
    4532             :         ArrayType  *arr;
    4533             : 
    4534             :         /* Should have just the one argument */
    4535             :         Assert(argidx == PG_NARGS() - 1);
    4536             : 
    4537             :         /* concat(VARIADIC NULL) is defined as NULL */
    4538          30 :         if (PG_ARGISNULL(argidx))
    4539          12 :             return NULL;
    4540             : 
    4541             :         /*
    4542             :          * Non-null argument had better be an array.  We assume that any call
    4543             :          * context that could let get_fn_expr_variadic return true will have
    4544             :          * checked that a VARIADIC-labeled parameter actually is an array.  So
    4545             :          * it should be okay to just Assert that it's an array rather than
    4546             :          * doing a full-fledged error check.
    4547             :          */
    4548             :         Assert(OidIsValid(get_base_element_type(get_fn_expr_argtype(fcinfo->flinfo, argidx))));
    4549             : 
    4550             :         /* OK, safe to fetch the array value */
    4551          18 :         arr = PG_GETARG_ARRAYTYPE_P(argidx);
    4552             : 
    4553             :         /*
    4554             :          * And serialize the array.  We tell array_to_text to ignore null
    4555             :          * elements, which matches the behavior of the loop below.
    4556             :          */
    4557          18 :         return array_to_text_internal(fcinfo, arr, sepstr, NULL);
    4558             :     }
    4559             : 
    4560             :     /* Normal case without explicit VARIADIC marker */
    4561         234 :     initStringInfo(&str);
    4562             : 
    4563             :     /* Get output function info, building it if first time through */
    4564         234 :     foutcache = (FmgrInfo *) fcinfo->flinfo->fn_extra;
    4565         234 :     if (foutcache == NULL)
    4566         106 :         foutcache = build_concat_foutcache(fcinfo, argidx);
    4567             : 
    4568         822 :     for (i = argidx; i < PG_NARGS(); i++)
    4569             :     {
    4570         588 :         if (!PG_ARGISNULL(i))
    4571             :         {
    4572         510 :             Datum       value = PG_GETARG_DATUM(i);
    4573             : 
    4574             :             /* add separator if appropriate */
    4575         510 :             if (first_arg)
    4576         228 :                 first_arg = false;
    4577             :             else
    4578         282 :                 appendStringInfoString(&str, sepstr);
    4579             : 
    4580             :             /* call the appropriate type output function, append the result */
    4581         510 :             appendStringInfoString(&str,
    4582         510 :                                    OutputFunctionCall(&foutcache[i], value));
    4583             :         }
    4584             :     }
    4585             : 
    4586         234 :     result = cstring_to_text_with_len(str.data, str.len);
    4587         234 :     pfree(str.data);
    4588             : 
    4589         234 :     return result;
    4590             : }
    4591             : 
    4592             : /*
    4593             :  * Concatenate all arguments. NULL arguments are ignored.
    4594             :  */
    4595             : Datum
    4596         186 : text_concat(PG_FUNCTION_ARGS)
    4597             : {
    4598             :     text       *result;
    4599             : 
    4600         186 :     result = concat_internal("", 0, fcinfo);
    4601         186 :     if (result == NULL)
    4602           6 :         PG_RETURN_NULL();
    4603         180 :     PG_RETURN_TEXT_P(result);
    4604             : }
    4605             : 
    4606             : /*
    4607             :  * Concatenate all but first argument value with separators. The first
    4608             :  * parameter is used as the separator. NULL arguments are ignored.
    4609             :  */
    4610             : Datum
    4611          84 : text_concat_ws(PG_FUNCTION_ARGS)
    4612             : {
    4613             :     char       *sep;
    4614             :     text       *result;
    4615             : 
    4616             :     /* return NULL when separator is NULL */
    4617          84 :     if (PG_ARGISNULL(0))
    4618           6 :         PG_RETURN_NULL();
    4619          78 :     sep = text_to_cstring(PG_GETARG_TEXT_PP(0));
    4620             : 
    4621          78 :     result = concat_internal(sep, 1, fcinfo);
    4622          78 :     if (result == NULL)
    4623           6 :         PG_RETURN_NULL();
    4624          72 :     PG_RETURN_TEXT_P(result);
    4625             : }
    4626             : 
    4627             : /*
    4628             :  * Return first n characters in the string. When n is negative,
    4629             :  * return all but last |n| characters.
    4630             :  */
    4631             : Datum
    4632        2148 : text_left(PG_FUNCTION_ARGS)
    4633             : {
    4634        2148 :     int         n = PG_GETARG_INT32(1);
    4635             : 
    4636        2148 :     if (n < 0)
    4637             :     {
    4638          30 :         text       *str = PG_GETARG_TEXT_PP(0);
    4639          30 :         const char *p = VARDATA_ANY(str);
    4640          30 :         int         len = VARSIZE_ANY_EXHDR(str);
    4641             :         int         rlen;
    4642             : 
    4643          30 :         n = pg_mbstrlen_with_len(p, len) + n;
    4644          30 :         rlen = pg_mbcharcliplen(p, len, n);
    4645          30 :         PG_RETURN_TEXT_P(cstring_to_text_with_len(p, rlen));
    4646             :     }
    4647             :     else
    4648        2118 :         PG_RETURN_TEXT_P(text_substring(PG_GETARG_DATUM(0), 1, n, false));
    4649             : }
    4650             : 
    4651             : /*
    4652             :  * Return last n characters in the string. When n is negative,
    4653             :  * return all but first |n| characters.
    4654             :  */
    4655             : Datum
    4656          66 : text_right(PG_FUNCTION_ARGS)
    4657             : {
    4658          66 :     text       *str = PG_GETARG_TEXT_PP(0);
    4659          66 :     const char *p = VARDATA_ANY(str);
    4660          66 :     int         len = VARSIZE_ANY_EXHDR(str);
    4661          66 :     int         n = PG_GETARG_INT32(1);
    4662             :     int         off;
    4663             : 
    4664          66 :     if (n < 0)
    4665          30 :         n = -n;
    4666             :     else
    4667          36 :         n = pg_mbstrlen_with_len(p, len) - n;
    4668          66 :     off = pg_mbcharcliplen(p, len, n);
    4669             : 
    4670          66 :     PG_RETURN_TEXT_P(cstring_to_text_with_len(p + off, len - off));
    4671             : }
    4672             : 
    4673             : /*
    4674             :  * Return reversed string
    4675             :  */
    4676             : Datum
    4677          42 : text_reverse(PG_FUNCTION_ARGS)
    4678             : {
    4679          42 :     text       *str = PG_GETARG_TEXT_PP(0);
    4680          42 :     const char *p = VARDATA_ANY(str);
    4681          42 :     int         len = VARSIZE_ANY_EXHDR(str);
    4682          42 :     const char *endp = p + len;
    4683             :     text       *result;
    4684             :     char       *dst;
    4685             : 
    4686          42 :     result = palloc(len + VARHDRSZ);
    4687          42 :     dst = (char *) VARDATA(result) + len;
    4688          42 :     SET_VARSIZE(result, len + VARHDRSZ);
    4689             : 
    4690          42 :     if (pg_database_encoding_max_length() > 1)
    4691             :     {
    4692             :         /* multibyte version */
    4693         324 :         while (p < endp)
    4694             :         {
    4695             :             int         sz;
    4696             : 
    4697         288 :             sz = pg_mblen_range(p, endp);
    4698         282 :             dst -= sz;
    4699         282 :             memcpy(dst, p, sz);
    4700         282 :             p += sz;
    4701             :         }
    4702             :     }
    4703             :     else
    4704             :     {
    4705             :         /* single byte version */
    4706           0 :         while (p < endp)
    4707           0 :             *(--dst) = *p++;
    4708             :     }
    4709             : 
    4710          36 :     PG_RETURN_TEXT_P(result);
    4711             : }
    4712             : 
    4713             : 
    4714             : /*
    4715             :  * Support macros for text_format()
    4716             :  */
    4717             : #define TEXT_FORMAT_FLAG_MINUS  0x0001  /* is minus flag present? */
    4718             : 
    4719             : #define ADVANCE_PARSE_POINTER(ptr,end_ptr) \
    4720             :     do { \
    4721             :         if (++(ptr) >= (end_ptr)) \
    4722             :             ereport(ERROR, \
    4723             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE), \
    4724             :                      errmsg("unterminated format() type specifier"), \
    4725             :                      errhint("For a single \"%%\" use \"%%%%\"."))); \
    4726             :     } while (0)
    4727             : 
    4728             : /*
    4729             :  * Returns a formatted string
    4730             :  */
    4731             : Datum
    4732       33238 : text_format(PG_FUNCTION_ARGS)
    4733             : {
    4734             :     text       *fmt;
    4735             :     StringInfoData str;
    4736             :     const char *cp;
    4737             :     const char *start_ptr;
    4738             :     const char *end_ptr;
    4739             :     text       *result;
    4740             :     int         arg;
    4741             :     bool        funcvariadic;
    4742             :     int         nargs;
    4743       33238 :     Datum      *elements = NULL;
    4744       33238 :     bool       *nulls = NULL;
    4745       33238 :     Oid         element_type = InvalidOid;
    4746       33238 :     Oid         prev_type = InvalidOid;
    4747       33238 :     Oid         prev_width_type = InvalidOid;
    4748             :     FmgrInfo    typoutputfinfo;
    4749             :     FmgrInfo    typoutputinfo_width;
    4750             : 
    4751             :     /* When format string is null, immediately return null */
    4752       33238 :     if (PG_ARGISNULL(0))
    4753           6 :         PG_RETURN_NULL();
    4754             : 
    4755             :     /* If argument is marked VARIADIC, expand array into elements */
    4756       33232 :     if (get_fn_expr_variadic(fcinfo->flinfo))
    4757             :     {
    4758             :         ArrayType  *arr;
    4759             :         int16       elmlen;
    4760             :         bool        elmbyval;
    4761             :         char        elmalign;
    4762             :         int         nitems;
    4763             : 
    4764             :         /* Should have just the one argument */
    4765             :         Assert(PG_NARGS() == 2);
    4766             : 
    4767             :         /* If argument is NULL, we treat it as zero-length array */
    4768          48 :         if (PG_ARGISNULL(1))
    4769           6 :             nitems = 0;
    4770             :         else
    4771             :         {
    4772             :             /*
    4773             :              * Non-null argument had better be an array.  We assume that any
    4774             :              * call context that could let get_fn_expr_variadic return true
    4775             :              * will have checked that a VARIADIC-labeled parameter actually is
    4776             :              * an array.  So it should be okay to just Assert that it's an
    4777             :              * array rather than doing a full-fledged error check.
    4778             :              */
    4779             :             Assert(OidIsValid(get_base_element_type(get_fn_expr_argtype(fcinfo->flinfo, 1))));
    4780             : 
    4781             :             /* OK, safe to fetch the array value */
    4782          42 :             arr = PG_GETARG_ARRAYTYPE_P(1);
    4783             : 
    4784             :             /* Get info about array element type */
    4785          42 :             element_type = ARR_ELEMTYPE(arr);
    4786          42 :             get_typlenbyvalalign(element_type,
    4787             :                                  &elmlen, &elmbyval, &elmalign);
    4788             : 
    4789             :             /* Extract all array elements */
    4790          42 :             deconstruct_array(arr, element_type, elmlen, elmbyval, elmalign,
    4791             :                               &elements, &nulls, &nitems);
    4792             :         }
    4793             : 
    4794          48 :         nargs = nitems + 1;
    4795          48 :         funcvariadic = true;
    4796             :     }
    4797             :     else
    4798             :     {
    4799             :         /* Non-variadic case, we'll process the arguments individually */
    4800       33184 :         nargs = PG_NARGS();
    4801       33184 :         funcvariadic = false;
    4802             :     }
    4803             : 
    4804             :     /* Setup for main loop. */
    4805       33232 :     fmt = PG_GETARG_TEXT_PP(0);
    4806       33232 :     start_ptr = VARDATA_ANY(fmt);
    4807       33232 :     end_ptr = start_ptr + VARSIZE_ANY_EXHDR(fmt);
    4808       33232 :     initStringInfo(&str);
    4809       33232 :     arg = 1;                    /* next argument position to print */
    4810             : 
    4811             :     /* Scan format string, looking for conversion specifiers. */
    4812     1013604 :     for (cp = start_ptr; cp < end_ptr; cp++)
    4813             :     {
    4814             :         int         argpos;
    4815             :         int         widthpos;
    4816             :         int         flags;
    4817             :         int         width;
    4818             :         Datum       value;
    4819             :         bool        isNull;
    4820             :         Oid         typid;
    4821             : 
    4822             :         /*
    4823             :          * If it's not the start of a conversion specifier, just copy it to
    4824             :          * the output buffer.
    4825             :          */
    4826      980432 :         if (*cp != '%')
    4827             :         {
    4828      914542 :             appendStringInfoCharMacro(&str, *cp);
    4829      914560 :             continue;
    4830             :         }
    4831             : 
    4832       65890 :         ADVANCE_PARSE_POINTER(cp, end_ptr);
    4833             : 
    4834             :         /* Easy case: %% outputs a single % */
    4835       65890 :         if (*cp == '%')
    4836             :         {
    4837          18 :             appendStringInfoCharMacro(&str, *cp);
    4838          18 :             continue;
    4839             :         }
    4840             : 
    4841             :         /* Parse the optional portions of the format specifier */
    4842       65872 :         cp = text_format_parse_format(cp, end_ptr,
    4843             :                                       &argpos, &widthpos,
    4844             :                                       &flags, &width);
    4845             : 
    4846             :         /*
    4847             :          * Next we should see the main conversion specifier.  Whether or not
    4848             :          * an argument position was present, it's known that at least one
    4849             :          * character remains in the string at this point.  Experience suggests
    4850             :          * that it's worth checking that that character is one of the expected
    4851             :          * ones before we try to fetch arguments, so as to produce the least
    4852             :          * confusing response to a mis-formatted specifier.
    4853             :          */
    4854       65848 :         if (strchr("sIL", *cp) == NULL)
    4855           6 :             ereport(ERROR,
    4856             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    4857             :                      errmsg("unrecognized format() type specifier \"%.*s\"",
    4858             :                             pg_mblen_range(cp, end_ptr), cp),
    4859             :                      errhint("For a single \"%%\" use \"%%%%\".")));
    4860             : 
    4861             :         /* If indirect width was specified, get its value */
    4862       65842 :         if (widthpos >= 0)
    4863             :         {
    4864             :             /* Collect the specified or next argument position */
    4865          42 :             if (widthpos > 0)
    4866          36 :                 arg = widthpos;
    4867          42 :             if (arg >= nargs)
    4868           0 :                 ereport(ERROR,
    4869             :                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    4870             :                          errmsg("too few arguments for format()")));
    4871             : 
    4872             :             /* Get the value and type of the selected argument */
    4873          42 :             if (!funcvariadic)
    4874             :             {
    4875          42 :                 value = PG_GETARG_DATUM(arg);
    4876          42 :                 isNull = PG_ARGISNULL(arg);
    4877          42 :                 typid = get_fn_expr_argtype(fcinfo->flinfo, arg);
    4878             :             }
    4879             :             else
    4880             :             {
    4881           0 :                 value = elements[arg - 1];
    4882           0 :                 isNull = nulls[arg - 1];
    4883           0 :                 typid = element_type;
    4884             :             }
    4885          42 :             if (!OidIsValid(typid))
    4886           0 :                 elog(ERROR, "could not determine data type of format() input");
    4887             : 
    4888          42 :             arg++;
    4889             : 
    4890             :             /* We can treat NULL width the same as zero */
    4891          42 :             if (isNull)
    4892           6 :                 width = 0;
    4893          36 :             else if (typid == INT4OID)
    4894          36 :                 width = DatumGetInt32(value);
    4895           0 :             else if (typid == INT2OID)
    4896           0 :                 width = DatumGetInt16(value);
    4897             :             else
    4898             :             {
    4899             :                 /* For less-usual datatypes, convert to text then to int */
    4900             :                 char       *str;
    4901             : 
    4902           0 :                 if (typid != prev_width_type)
    4903             :                 {
    4904             :                     Oid         typoutputfunc;
    4905             :                     bool        typIsVarlena;
    4906             : 
    4907           0 :                     getTypeOutputInfo(typid, &typoutputfunc, &typIsVarlena);
    4908           0 :                     fmgr_info(typoutputfunc, &typoutputinfo_width);
    4909           0 :                     prev_width_type = typid;
    4910             :                 }
    4911             : 
    4912           0 :                 str = OutputFunctionCall(&typoutputinfo_width, value);
    4913             : 
    4914             :                 /* pg_strtoint32 will complain about bad data or overflow */
    4915           0 :                 width = pg_strtoint32(str);
    4916             : 
    4917           0 :                 pfree(str);
    4918             :             }
    4919             :         }
    4920             : 
    4921             :         /* Collect the specified or next argument position */
    4922       65842 :         if (argpos > 0)
    4923         132 :             arg = argpos;
    4924       65842 :         if (arg >= nargs)
    4925          24 :             ereport(ERROR,
    4926             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    4927             :                      errmsg("too few arguments for format()")));
    4928             : 
    4929             :         /* Get the value and type of the selected argument */
    4930       65818 :         if (!funcvariadic)
    4931             :         {
    4932       64546 :             value = PG_GETARG_DATUM(arg);
    4933       64546 :             isNull = PG_ARGISNULL(arg);
    4934       64546 :             typid = get_fn_expr_argtype(fcinfo->flinfo, arg);
    4935             :         }
    4936             :         else
    4937             :         {
    4938        1272 :             value = elements[arg - 1];
    4939        1272 :             isNull = nulls[arg - 1];
    4940        1272 :             typid = element_type;
    4941             :         }
    4942       65818 :         if (!OidIsValid(typid))
    4943           0 :             elog(ERROR, "could not determine data type of format() input");
    4944             : 
    4945       65818 :         arg++;
    4946             : 
    4947             :         /*
    4948             :          * Get the appropriate typOutput function, reusing previous one if
    4949             :          * same type as previous argument.  That's particularly useful in the
    4950             :          * variadic-array case, but often saves work even for ordinary calls.
    4951             :          */
    4952       65818 :         if (typid != prev_type)
    4953             :         {
    4954             :             Oid         typoutputfunc;
    4955             :             bool        typIsVarlena;
    4956             : 
    4957       34300 :             getTypeOutputInfo(typid, &typoutputfunc, &typIsVarlena);
    4958       34300 :             fmgr_info(typoutputfunc, &typoutputfinfo);
    4959       34300 :             prev_type = typid;
    4960             :         }
    4961             : 
    4962             :         /*
    4963             :          * And now we can format the value.
    4964             :          */
    4965       65818 :         switch (*cp)
    4966             :         {
    4967       65818 :             case 's':
    4968             :             case 'I':
    4969             :             case 'L':
    4970       65818 :                 text_format_string_conversion(&str, *cp, &typoutputfinfo,
    4971             :                                               value, isNull,
    4972             :                                               flags, width);
    4973       65812 :                 break;
    4974           0 :             default:
    4975             :                 /* should not get here, because of previous check */
    4976           0 :                 ereport(ERROR,
    4977             :                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    4978             :                          errmsg("unrecognized format() type specifier \"%.*s\"",
    4979             :                                 pg_mblen_range(cp, end_ptr), cp),
    4980             :                          errhint("For a single \"%%\" use \"%%%%\".")));
    4981             :                 break;
    4982             :         }
    4983             :     }
    4984             : 
    4985             :     /* Don't need deconstruct_array results anymore. */
    4986       33172 :     if (elements != NULL)
    4987          42 :         pfree(elements);
    4988       33172 :     if (nulls != NULL)
    4989          42 :         pfree(nulls);
    4990             : 
    4991             :     /* Generate results. */
    4992       33172 :     result = cstring_to_text_with_len(str.data, str.len);
    4993       33172 :     pfree(str.data);
    4994             : 
    4995       33172 :     PG_RETURN_TEXT_P(result);
    4996             : }
    4997             : 
    4998             : /*
    4999             :  * Parse contiguous digits as a decimal number.
    5000             :  *
    5001             :  * Returns true if some digits could be parsed.
    5002             :  * The value is returned into *value, and *ptr is advanced to the next
    5003             :  * character to be parsed.
    5004             :  *
    5005             :  * Note parsing invariant: at least one character is known available before
    5006             :  * string end (end_ptr) at entry, and this is still true at exit.
    5007             :  */
    5008             : static bool
    5009      131708 : text_format_parse_digits(const char **ptr, const char *end_ptr, int *value)
    5010             : {
    5011      131708 :     bool        found = false;
    5012      131708 :     const char *cp = *ptr;
    5013      131708 :     int         val = 0;
    5014             : 
    5015      132020 :     while (*cp >= '0' && *cp <= '9')
    5016             :     {
    5017         318 :         int8        digit = (*cp - '0');
    5018             : 
    5019         318 :         if (unlikely(pg_mul_s32_overflow(val, 10, &val)) ||
    5020         318 :             unlikely(pg_add_s32_overflow(val, digit, &val)))
    5021           0 :             ereport(ERROR,
    5022             :                     (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
    5023             :                      errmsg("number is out of range")));
    5024         318 :         ADVANCE_PARSE_POINTER(cp, end_ptr);
    5025         312 :         found = true;
    5026             :     }
    5027             : 
    5028      131702 :     *ptr = cp;
    5029      131702 :     *value = val;
    5030             : 
    5031      131702 :     return found;
    5032             : }
    5033             : 
    5034             : /*
    5035             :  * Parse a format specifier (generally following the SUS printf spec).
    5036             :  *
    5037             :  * We have already advanced over the initial '%', and we are looking for
    5038             :  * [argpos][flags][width]type (but the type character is not consumed here).
    5039             :  *
    5040             :  * Inputs are start_ptr (the position after '%') and end_ptr (string end + 1).
    5041             :  * Output parameters:
    5042             :  *  argpos: argument position for value to be printed.  -1 means unspecified.
    5043             :  *  widthpos: argument position for width.  Zero means the argument position
    5044             :  *          was unspecified (ie, take the next arg) and -1 means no width
    5045             :  *          argument (width was omitted or specified as a constant).
    5046             :  *  flags: bitmask of flags.
    5047             :  *  width: directly-specified width value.  Zero means the width was omitted
    5048             :  *          (note it's not necessary to distinguish this case from an explicit
    5049             :  *          zero width value).
    5050             :  *
    5051             :  * The function result is the next character position to be parsed, ie, the
    5052             :  * location where the type character is/should be.
    5053             :  *
    5054             :  * Note parsing invariant: at least one character is known available before
    5055             :  * string end (end_ptr) at entry, and this is still true at exit.
    5056             :  */
    5057             : static const char *
    5058       65872 : text_format_parse_format(const char *start_ptr, const char *end_ptr,
    5059             :                          int *argpos, int *widthpos,
    5060             :                          int *flags, int *width)
    5061             : {
    5062       65872 :     const char *cp = start_ptr;
    5063             :     int         n;
    5064             : 
    5065             :     /* set defaults for output parameters */
    5066       65872 :     *argpos = -1;
    5067       65872 :     *widthpos = -1;
    5068       65872 :     *flags = 0;
    5069       65872 :     *width = 0;
    5070             : 
    5071             :     /* try to identify first number */
    5072       65872 :     if (text_format_parse_digits(&cp, end_ptr, &n))
    5073             :     {
    5074         174 :         if (*cp != '$')
    5075             :         {
    5076             :             /* Must be just a width and a type, so we're done */
    5077          24 :             *width = n;
    5078          24 :             return cp;
    5079             :         }
    5080             :         /* The number was argument position */
    5081         150 :         *argpos = n;
    5082             :         /* Explicit 0 for argument index is immediately refused */
    5083         150 :         if (n == 0)
    5084           6 :             ereport(ERROR,
    5085             :                     (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    5086             :                      errmsg("format specifies argument 0, but arguments are numbered from 1")));
    5087         144 :         ADVANCE_PARSE_POINTER(cp, end_ptr);
    5088             :     }
    5089             : 
    5090             :     /* Handle flags (only minus is supported now) */
    5091       65866 :     while (*cp == '-')
    5092             :     {
    5093          30 :         *flags |= TEXT_FORMAT_FLAG_MINUS;
    5094          30 :         ADVANCE_PARSE_POINTER(cp, end_ptr);
    5095             :     }
    5096             : 
    5097       65836 :     if (*cp == '*')
    5098             :     {
    5099             :         /* Handle indirect width */
    5100          48 :         ADVANCE_PARSE_POINTER(cp, end_ptr);
    5101          48 :         if (text_format_parse_digits(&cp, end_ptr, &n))
    5102             :         {
    5103             :             /* number in this position must be closed by $ */
    5104          42 :             if (*cp != '$')
    5105           0 :                 ereport(ERROR,
    5106             :                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    5107             :                          errmsg("width argument position must be ended by \"$\"")));
    5108             :             /* The number was width argument position */
    5109          42 :             *widthpos = n;
    5110             :             /* Explicit 0 for argument index is immediately refused */
    5111          42 :             if (n == 0)
    5112           6 :                 ereport(ERROR,
    5113             :                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    5114             :                          errmsg("format specifies argument 0, but arguments are numbered from 1")));
    5115          36 :             ADVANCE_PARSE_POINTER(cp, end_ptr);
    5116             :         }
    5117             :         else
    5118           6 :             *widthpos = 0;      /* width's argument position is unspecified */
    5119             :     }
    5120             :     else
    5121             :     {
    5122             :         /* Check for direct width specification */
    5123       65788 :         if (text_format_parse_digits(&cp, end_ptr, &n))
    5124          30 :             *width = n;
    5125             :     }
    5126             : 
    5127             :     /* cp should now be pointing at type character */
    5128       65824 :     return cp;
    5129             : }
    5130             : 
    5131             : /*
    5132             :  * Format a %s, %I, or %L conversion
    5133             :  */
    5134             : static void
    5135       65818 : text_format_string_conversion(StringInfo buf, char conversion,
    5136             :                               FmgrInfo *typOutputInfo,
    5137             :                               Datum value, bool isNull,
    5138             :                               int flags, int width)
    5139             : {
    5140             :     char       *str;
    5141             : 
    5142             :     /* Handle NULL arguments before trying to stringify the value. */
    5143       65818 :     if (isNull)
    5144             :     {
    5145         342 :         if (conversion == 's')
    5146         270 :             text_format_append_string(buf, "", flags, width);
    5147          72 :         else if (conversion == 'L')
    5148          66 :             text_format_append_string(buf, "NULL", flags, width);
    5149           6 :         else if (conversion == 'I')
    5150           6 :             ereport(ERROR,
    5151             :                     (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
    5152             :                      errmsg("null values cannot be formatted as an SQL identifier")));
    5153         336 :         return;
    5154             :     }
    5155             : 
    5156             :     /* Stringify. */
    5157       65476 :     str = OutputFunctionCall(typOutputInfo, value);
    5158             : 
    5159             :     /* Escape. */
    5160       65476 :     if (conversion == 'I')
    5161             :     {
    5162             :         /* quote_identifier may or may not allocate a new string. */
    5163        4906 :         text_format_append_string(buf, quote_identifier(str), flags, width);
    5164             :     }
    5165       60570 :     else if (conversion == 'L')
    5166             :     {
    5167        3252 :         char       *qstr = quote_literal_cstr(str);
    5168             : 
    5169        3252 :         text_format_append_string(buf, qstr, flags, width);
    5170             :         /* quote_literal_cstr() always allocates a new string */
    5171        3252 :         pfree(qstr);
    5172             :     }
    5173             :     else
    5174       57318 :         text_format_append_string(buf, str, flags, width);
    5175             : 
    5176             :     /* Cleanup. */
    5177       65476 :     pfree(str);
    5178             : }
    5179             : 
    5180             : /*
    5181             :  * Append str to buf, padding as directed by flags/width
    5182             :  */
    5183             : static void
    5184       65812 : text_format_append_string(StringInfo buf, const char *str,
    5185             :                           int flags, int width)
    5186             : {
    5187       65812 :     bool        align_to_left = false;
    5188             :     int         len;
    5189             : 
    5190             :     /* fast path for typical easy case */
    5191       65812 :     if (width == 0)
    5192             :     {
    5193       65728 :         appendStringInfoString(buf, str);
    5194       65728 :         return;
    5195             :     }
    5196             : 
    5197          84 :     if (width < 0)
    5198             :     {
    5199             :         /* Negative width: implicit '-' flag, then take absolute value */
    5200           6 :         align_to_left = true;
    5201             :         /* -INT_MIN is undefined */
    5202           6 :         if (width <= INT_MIN)
    5203           0 :             ereport(ERROR,
    5204             :                     (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
    5205             :                      errmsg("number is out of range")));
    5206           6 :         width = -width;
    5207             :     }
    5208          78 :     else if (flags & TEXT_FORMAT_FLAG_MINUS)
    5209          24 :         align_to_left = true;
    5210             : 
    5211          84 :     len = pg_mbstrlen(str);
    5212          84 :     if (align_to_left)
    5213             :     {
    5214             :         /* left justify */
    5215          30 :         appendStringInfoString(buf, str);
    5216          30 :         if (len < width)
    5217          30 :             appendStringInfoSpaces(buf, width - len);
    5218             :     }
    5219             :     else
    5220             :     {
    5221             :         /* right justify */
    5222          54 :         if (len < width)
    5223          54 :             appendStringInfoSpaces(buf, width - len);
    5224          54 :         appendStringInfoString(buf, str);
    5225             :     }
    5226             : }
    5227             : 
    5228             : /*
    5229             :  * text_format_nv - nonvariadic wrapper for text_format function.
    5230             :  *
    5231             :  * note: this wrapper is necessary to pass the sanity check in opr_sanity,
    5232             :  * which checks that all built-in functions that share the implementing C
    5233             :  * function take the same number of arguments.
    5234             :  */
    5235             : Datum
    5236        3810 : text_format_nv(PG_FUNCTION_ARGS)
    5237             : {
    5238        3810 :     return text_format(fcinfo);
    5239             : }
    5240             : 
    5241             : /*
    5242             :  * Helper function for Levenshtein distance functions. Faster than memcmp(),
    5243             :  * for this use case.
    5244             :  */
    5245             : static inline bool
    5246           0 : rest_of_char_same(const char *s1, const char *s2, int len)
    5247             : {
    5248           0 :     while (len > 0)
    5249             :     {
    5250           0 :         len--;
    5251           0 :         if (s1[len] != s2[len])
    5252           0 :             return false;
    5253             :     }
    5254           0 :     return true;
    5255             : }
    5256             : 
    5257             : /* Expand each Levenshtein distance variant */
    5258             : #include "levenshtein.c"
    5259             : #define LEVENSHTEIN_LESS_EQUAL
    5260             : #include "levenshtein.c"
    5261             : 
    5262             : 
    5263             : /*
    5264             :  * The following *ClosestMatch() functions can be used to determine whether a
    5265             :  * user-provided string resembles any known valid values, which is useful for
    5266             :  * providing hints in log messages, among other things.  Use these functions
    5267             :  * like so:
    5268             :  *
    5269             :  *      initClosestMatch(&state, source_string, max_distance);
    5270             :  *
    5271             :  *      for (int i = 0; i < num_valid_strings; i++)
    5272             :  *          updateClosestMatch(&state, valid_strings[i]);
    5273             :  *
    5274             :  *      closestMatch = getClosestMatch(&state);
    5275             :  */
    5276             : 
    5277             : /*
    5278             :  * Initialize the given state with the source string and maximum Levenshtein
    5279             :  * distance to consider.
    5280             :  */
    5281             : void
    5282          78 : initClosestMatch(ClosestMatchState *state, const char *source, int max_d)
    5283             : {
    5284             :     Assert(state);
    5285             :     Assert(max_d >= 0);
    5286             : 
    5287          78 :     state->source = source;
    5288          78 :     state->min_d = -1;
    5289          78 :     state->max_d = max_d;
    5290          78 :     state->match = NULL;
    5291          78 : }
    5292             : 
    5293             : /*
    5294             :  * If the candidate string is a closer match than the current one saved (or
    5295             :  * there is no match saved), save it as the closest match.
    5296             :  *
    5297             :  * If the source or candidate string is NULL, empty, or too long, this function
    5298             :  * takes no action.  Likewise, if the Levenshtein distance exceeds the maximum
    5299             :  * allowed or more than half the characters are different, no action is taken.
    5300             :  */
    5301             : void
    5302         804 : updateClosestMatch(ClosestMatchState *state, const char *candidate)
    5303             : {
    5304             :     int         dist;
    5305             : 
    5306             :     Assert(state);
    5307             : 
    5308         804 :     if (state->source == NULL || state->source[0] == '\0' ||
    5309         804 :         candidate == NULL || candidate[0] == '\0')
    5310           0 :         return;
    5311             : 
    5312             :     /*
    5313             :      * To avoid ERROR-ing, we check the lengths here instead of setting
    5314             :      * 'trusted' to false in the call to varstr_levenshtein_less_equal().
    5315             :      */
    5316         804 :     if (strlen(state->source) > MAX_LEVENSHTEIN_STRLEN ||
    5317         804 :         strlen(candidate) > MAX_LEVENSHTEIN_STRLEN)
    5318           0 :         return;
    5319             : 
    5320         804 :     dist = varstr_levenshtein_less_equal(state->source, strlen(state->source),
    5321         804 :                                          candidate, strlen(candidate), 1, 1, 1,
    5322             :                                          state->max_d, true);
    5323         804 :     if (dist <= state->max_d &&
    5324          62 :         dist <= strlen(state->source) / 2 &&
    5325          14 :         (state->min_d == -1 || dist < state->min_d))
    5326             :     {
    5327          14 :         state->min_d = dist;
    5328          14 :         state->match = candidate;
    5329             :     }
    5330             : }
    5331             : 
    5332             : /*
    5333             :  * Return the closest match.  If no suitable candidates were provided via
    5334             :  * updateClosestMatch(), return NULL.
    5335             :  */
    5336             : const char *
    5337          78 : getClosestMatch(ClosestMatchState *state)
    5338             : {
    5339             :     Assert(state);
    5340             : 
    5341          78 :     return state->match;
    5342             : }
    5343             : 
    5344             : 
    5345             : /*
    5346             :  * Unicode support
    5347             :  */
    5348             : 
    5349             : static UnicodeNormalizationForm
    5350         210 : unicode_norm_form_from_string(const char *formstr)
    5351             : {
    5352         210 :     UnicodeNormalizationForm form = -1;
    5353             : 
    5354             :     /*
    5355             :      * Might as well check this while we're here.
    5356             :      */
    5357         210 :     if (GetDatabaseEncoding() != PG_UTF8)
    5358           0 :         ereport(ERROR,
    5359             :                 (errcode(ERRCODE_SYNTAX_ERROR),
    5360             :                  errmsg("Unicode normalization can only be performed if server encoding is UTF8")));
    5361             : 
    5362         210 :     if (pg_strcasecmp(formstr, "NFC") == 0)
    5363          66 :         form = UNICODE_NFC;
    5364         144 :     else if (pg_strcasecmp(formstr, "NFD") == 0)
    5365          60 :         form = UNICODE_NFD;
    5366          84 :     else if (pg_strcasecmp(formstr, "NFKC") == 0)
    5367          36 :         form = UNICODE_NFKC;
    5368          48 :     else if (pg_strcasecmp(formstr, "NFKD") == 0)
    5369          36 :         form = UNICODE_NFKD;
    5370             :     else
    5371          12 :         ereport(ERROR,
    5372             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    5373             :                  errmsg("invalid normalization form: %s", formstr)));
    5374             : 
    5375         198 :     return form;
    5376             : }
    5377             : 
    5378             : /*
    5379             :  * Returns version of Unicode used by Postgres in "major.minor" format (the
    5380             :  * same format as the Unicode version reported by ICU). The third component
    5381             :  * ("update version") never involves additions to the character repertoire and
    5382             :  * is unimportant for most purposes.
    5383             :  *
    5384             :  * See: https://unicode.org/versions/
    5385             :  */
    5386             : Datum
    5387          34 : unicode_version(PG_FUNCTION_ARGS)
    5388             : {
    5389          34 :     PG_RETURN_TEXT_P(cstring_to_text(PG_UNICODE_VERSION));
    5390             : }
    5391             : 
    5392             : /*
    5393             :  * Returns version of Unicode used by ICU, if enabled; otherwise NULL.
    5394             :  */
    5395             : Datum
    5396           2 : icu_unicode_version(PG_FUNCTION_ARGS)
    5397             : {
    5398           2 :     const char *version = pg_icu_unicode_version();
    5399             : 
    5400           2 :     if (version)
    5401           2 :         PG_RETURN_TEXT_P(cstring_to_text(version));
    5402             :     else
    5403           0 :         PG_RETURN_NULL();
    5404             : }
    5405             : 
    5406             : /*
    5407             :  * Check whether the string contains only assigned Unicode code
    5408             :  * points. Requires that the database encoding is UTF-8.
    5409             :  */
    5410             : Datum
    5411          12 : unicode_assigned(PG_FUNCTION_ARGS)
    5412             : {
    5413          12 :     text       *input = PG_GETARG_TEXT_PP(0);
    5414             :     unsigned char *p;
    5415             :     int         size;
    5416             : 
    5417          12 :     if (GetDatabaseEncoding() != PG_UTF8)
    5418           0 :         ereport(ERROR,
    5419             :                 (errmsg("Unicode categorization can only be performed if server encoding is UTF8")));
    5420             : 
    5421             :     /* convert to char32_t */
    5422          12 :     size = pg_mbstrlen_with_len(VARDATA_ANY(input), VARSIZE_ANY_EXHDR(input));
    5423          12 :     p = (unsigned char *) VARDATA_ANY(input);
    5424          48 :     for (int i = 0; i < size; i++)
    5425             :     {
    5426          42 :         char32_t    uchar = utf8_to_unicode(p);
    5427          42 :         int         category = unicode_category(uchar);
    5428             : 
    5429          42 :         if (category == PG_U_UNASSIGNED)
    5430           6 :             PG_RETURN_BOOL(false);
    5431             : 
    5432          36 :         p += pg_utf_mblen(p);
    5433             :     }
    5434             : 
    5435           6 :     PG_RETURN_BOOL(true);
    5436             : }
    5437             : 
    5438             : Datum
    5439          72 : unicode_normalize_func(PG_FUNCTION_ARGS)
    5440             : {
    5441          72 :     text       *input = PG_GETARG_TEXT_PP(0);
    5442          72 :     char       *formstr = text_to_cstring(PG_GETARG_TEXT_PP(1));
    5443             :     UnicodeNormalizationForm form;
    5444             :     int         size;
    5445             :     char32_t   *input_chars;
    5446             :     char32_t   *output_chars;
    5447             :     unsigned char *p;
    5448             :     text       *result;
    5449             :     int         i;
    5450             : 
    5451          72 :     form = unicode_norm_form_from_string(formstr);
    5452             : 
    5453             :     /* convert to char32_t */
    5454          66 :     size = pg_mbstrlen_with_len(VARDATA_ANY(input), VARSIZE_ANY_EXHDR(input));
    5455          66 :     input_chars = palloc((size + 1) * sizeof(char32_t));
    5456          66 :     p = (unsigned char *) VARDATA_ANY(input);
    5457         288 :     for (i = 0; i < size; i++)
    5458             :     {
    5459         222 :         input_chars[i] = utf8_to_unicode(p);
    5460         222 :         p += pg_utf_mblen(p);
    5461             :     }
    5462          66 :     input_chars[i] = (char32_t) '\0';
    5463             :     Assert((char *) p == VARDATA_ANY(input) + VARSIZE_ANY_EXHDR(input));
    5464             : 
    5465             :     /* action */
    5466          66 :     output_chars = unicode_normalize(form, input_chars);
    5467             : 
    5468             :     /* convert back to UTF-8 string */
    5469          66 :     size = 0;
    5470         306 :     for (char32_t *wp = output_chars; *wp; wp++)
    5471             :     {
    5472             :         unsigned char buf[4];
    5473             : 
    5474         240 :         unicode_to_utf8(*wp, buf);
    5475         240 :         size += pg_utf_mblen(buf);
    5476             :     }
    5477             : 
    5478          66 :     result = palloc(size + VARHDRSZ);
    5479          66 :     SET_VARSIZE(result, size + VARHDRSZ);
    5480             : 
    5481          66 :     p = (unsigned char *) VARDATA_ANY(result);
    5482         306 :     for (char32_t *wp = output_chars; *wp; wp++)
    5483             :     {
    5484         240 :         unicode_to_utf8(*wp, p);
    5485         240 :         p += pg_utf_mblen(p);
    5486             :     }
    5487             :     Assert((char *) p == (char *) result + size + VARHDRSZ);
    5488             : 
    5489          66 :     PG_RETURN_TEXT_P(result);
    5490             : }
    5491             : 
    5492             : /*
    5493             :  * Check whether the string is in the specified Unicode normalization form.
    5494             :  *
    5495             :  * This is done by converting the string to the specified normal form and then
    5496             :  * comparing that to the original string.  To speed that up, we also apply the
    5497             :  * "quick check" algorithm specified in UAX #15, which can give a yes or no
    5498             :  * answer for many strings by just scanning the string once.
    5499             :  *
    5500             :  * This function should generally be optimized for the case where the string
    5501             :  * is in fact normalized.  In that case, we'll end up looking at the entire
    5502             :  * string, so it's probably not worth doing any incremental conversion etc.
    5503             :  */
    5504             : Datum
    5505         138 : unicode_is_normalized(PG_FUNCTION_ARGS)
    5506             : {
    5507         138 :     text       *input = PG_GETARG_TEXT_PP(0);
    5508         138 :     char       *formstr = text_to_cstring(PG_GETARG_TEXT_PP(1));
    5509             :     UnicodeNormalizationForm form;
    5510             :     int         size;
    5511             :     char32_t   *input_chars;
    5512             :     char32_t   *output_chars;
    5513             :     unsigned char *p;
    5514             :     int         i;
    5515             :     UnicodeNormalizationQC quickcheck;
    5516             :     int         output_size;
    5517             :     bool        result;
    5518             : 
    5519         138 :     form = unicode_norm_form_from_string(formstr);
    5520             : 
    5521             :     /* convert to char32_t */
    5522         132 :     size = pg_mbstrlen_with_len(VARDATA_ANY(input), VARSIZE_ANY_EXHDR(input));
    5523         132 :     input_chars = palloc((size + 1) * sizeof(char32_t));
    5524         132 :     p = (unsigned char *) VARDATA_ANY(input);
    5525         504 :     for (i = 0; i < size; i++)
    5526             :     {
    5527         372 :         input_chars[i] = utf8_to_unicode(p);
    5528         372 :         p += pg_utf_mblen(p);
    5529             :     }
    5530         132 :     input_chars[i] = (char32_t) '\0';
    5531             :     Assert((char *) p == VARDATA_ANY(input) + VARSIZE_ANY_EXHDR(input));
    5532             : 
    5533             :     /* quick check (see UAX #15) */
    5534         132 :     quickcheck = unicode_is_normalized_quickcheck(form, input_chars);
    5535         132 :     if (quickcheck == UNICODE_NORM_QC_YES)
    5536          42 :         PG_RETURN_BOOL(true);
    5537          90 :     else if (quickcheck == UNICODE_NORM_QC_NO)
    5538          12 :         PG_RETURN_BOOL(false);
    5539             : 
    5540             :     /* normalize and compare with original */
    5541          78 :     output_chars = unicode_normalize(form, input_chars);
    5542             : 
    5543          78 :     output_size = 0;
    5544         324 :     for (char32_t *wp = output_chars; *wp; wp++)
    5545         246 :         output_size++;
    5546             : 
    5547         114 :     result = (size == output_size) &&
    5548          36 :         (memcmp(input_chars, output_chars, size * sizeof(char32_t)) == 0);
    5549             : 
    5550          78 :     PG_RETURN_BOOL(result);
    5551             : }
    5552             : 
    5553             : /*
    5554             :  * Check if first n chars are hexadecimal digits
    5555             :  */
    5556             : static bool
    5557         156 : isxdigits_n(const char *instr, size_t n)
    5558             : {
    5559         660 :     for (size_t i = 0; i < n; i++)
    5560         570 :         if (!isxdigit((unsigned char) instr[i]))
    5561          66 :             return false;
    5562             : 
    5563          90 :     return true;
    5564             : }
    5565             : 
    5566             : static unsigned int
    5567         504 : hexval(unsigned char c)
    5568             : {
    5569         504 :     if (c >= '0' && c <= '9')
    5570         384 :         return c - '0';
    5571         120 :     if (c >= 'a' && c <= 'f')
    5572          60 :         return c - 'a' + 0xA;
    5573          60 :     if (c >= 'A' && c <= 'F')
    5574          60 :         return c - 'A' + 0xA;
    5575           0 :     elog(ERROR, "invalid hexadecimal digit");
    5576             :     return 0;                   /* not reached */
    5577             : }
    5578             : 
    5579             : /*
    5580             :  * Translate string with hexadecimal digits to number
    5581             :  */
    5582             : static unsigned int
    5583          90 : hexval_n(const char *instr, size_t n)
    5584             : {
    5585          90 :     unsigned int result = 0;
    5586             : 
    5587         594 :     for (size_t i = 0; i < n; i++)
    5588         504 :         result += hexval(instr[i]) << (4 * (n - i - 1));
    5589             : 
    5590          90 :     return result;
    5591             : }
    5592             : 
    5593             : /*
    5594             :  * Replaces Unicode escape sequences by Unicode characters
    5595             :  */
    5596             : Datum
    5597          66 : unistr(PG_FUNCTION_ARGS)
    5598             : {
    5599          66 :     text       *input_text = PG_GETARG_TEXT_PP(0);
    5600             :     char       *instr;
    5601             :     int         len;
    5602             :     StringInfoData str;
    5603             :     text       *result;
    5604          66 :     char16_t    pair_first = 0;
    5605             :     char        cbuf[MAX_UNICODE_EQUIVALENT_STRING + 1];
    5606             : 
    5607          66 :     instr = VARDATA_ANY(input_text);
    5608          66 :     len = VARSIZE_ANY_EXHDR(input_text);
    5609             : 
    5610          66 :     initStringInfo(&str);
    5611             : 
    5612         510 :     while (len > 0)
    5613             :     {
    5614         486 :         if (instr[0] == '\\')
    5615             :         {
    5616         102 :             if (len >= 2 &&
    5617         102 :                 instr[1] == '\\')
    5618             :             {
    5619           6 :                 if (pair_first)
    5620           0 :                     goto invalid_pair;
    5621           6 :                 appendStringInfoChar(&str, '\\');
    5622           6 :                 instr += 2;
    5623           6 :                 len -= 2;
    5624             :             }
    5625          96 :             else if ((len >= 5 && isxdigits_n(instr + 1, 4)) ||
    5626          66 :                      (len >= 6 && instr[1] == 'u' && isxdigits_n(instr + 2, 4)))
    5627          30 :             {
    5628             :                 char32_t    unicode;
    5629          42 :                 int         offset = instr[1] == 'u' ? 2 : 1;
    5630             : 
    5631          42 :                 unicode = hexval_n(instr + offset, 4);
    5632             : 
    5633          42 :                 if (!is_valid_unicode_codepoint(unicode))
    5634           0 :                     ereport(ERROR,
    5635             :                             errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    5636             :                             errmsg("invalid Unicode code point: %04X", unicode));
    5637             : 
    5638          42 :                 if (pair_first)
    5639             :                 {
    5640          12 :                     if (is_utf16_surrogate_second(unicode))
    5641             :                     {
    5642           0 :                         unicode = surrogate_pair_to_codepoint(pair_first, unicode);
    5643           0 :                         pair_first = 0;
    5644             :                     }
    5645             :                     else
    5646          12 :                         goto invalid_pair;
    5647             :                 }
    5648          30 :                 else if (is_utf16_surrogate_second(unicode))
    5649           0 :                     goto invalid_pair;
    5650             : 
    5651          30 :                 if (is_utf16_surrogate_first(unicode))
    5652          18 :                     pair_first = unicode;
    5653             :                 else
    5654             :                 {
    5655          12 :                     pg_unicode_to_server(unicode, (unsigned char *) cbuf);
    5656          12 :                     appendStringInfoString(&str, cbuf);
    5657             :                 }
    5658             : 
    5659          30 :                 instr += 4 + offset;
    5660          30 :                 len -= 4 + offset;
    5661             :             }
    5662          54 :             else if (len >= 8 && instr[1] == '+' && isxdigits_n(instr + 2, 6))
    5663          12 :             {
    5664             :                 char32_t    unicode;
    5665             : 
    5666          24 :                 unicode = hexval_n(instr + 2, 6);
    5667             : 
    5668          24 :                 if (!is_valid_unicode_codepoint(unicode))
    5669           6 :                     ereport(ERROR,
    5670             :                             errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    5671             :                             errmsg("invalid Unicode code point: %04X", unicode));
    5672             : 
    5673          18 :                 if (pair_first)
    5674             :                 {
    5675           6 :                     if (is_utf16_surrogate_second(unicode))
    5676             :                     {
    5677           0 :                         unicode = surrogate_pair_to_codepoint(pair_first, unicode);
    5678           0 :                         pair_first = 0;
    5679             :                     }
    5680             :                     else
    5681           6 :                         goto invalid_pair;
    5682             :                 }
    5683          12 :                 else if (is_utf16_surrogate_second(unicode))
    5684           0 :                     goto invalid_pair;
    5685             : 
    5686          12 :                 if (is_utf16_surrogate_first(unicode))
    5687           6 :                     pair_first = unicode;
    5688             :                 else
    5689             :                 {
    5690           6 :                     pg_unicode_to_server(unicode, (unsigned char *) cbuf);
    5691           6 :                     appendStringInfoString(&str, cbuf);
    5692             :                 }
    5693             : 
    5694          12 :                 instr += 8;
    5695          12 :                 len -= 8;
    5696             :             }
    5697          30 :             else if (len >= 10 && instr[1] == 'U' && isxdigits_n(instr + 2, 8))
    5698          12 :             {
    5699             :                 char32_t    unicode;
    5700             : 
    5701          24 :                 unicode = hexval_n(instr + 2, 8);
    5702             : 
    5703          24 :                 if (!is_valid_unicode_codepoint(unicode))
    5704           6 :                     ereport(ERROR,
    5705             :                             errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    5706             :                             errmsg("invalid Unicode code point: %04X", unicode));
    5707             : 
    5708          18 :                 if (pair_first)
    5709             :                 {
    5710           6 :                     if (is_utf16_surrogate_second(unicode))
    5711             :                     {
    5712           0 :                         unicode = surrogate_pair_to_codepoint(pair_first, unicode);
    5713           0 :                         pair_first = 0;
    5714             :                     }
    5715             :                     else
    5716           6 :                         goto invalid_pair;
    5717             :                 }
    5718          12 :                 else if (is_utf16_surrogate_second(unicode))
    5719           0 :                     goto invalid_pair;
    5720             : 
    5721          12 :                 if (is_utf16_surrogate_first(unicode))
    5722           6 :                     pair_first = unicode;
    5723             :                 else
    5724             :                 {
    5725           6 :                     pg_unicode_to_server(unicode, (unsigned char *) cbuf);
    5726           6 :                     appendStringInfoString(&str, cbuf);
    5727             :                 }
    5728             : 
    5729          12 :                 instr += 10;
    5730          12 :                 len -= 10;
    5731             :             }
    5732             :             else
    5733           6 :                 ereport(ERROR,
    5734             :                         (errcode(ERRCODE_SYNTAX_ERROR),
    5735             :                          errmsg("invalid Unicode escape"),
    5736             :                          errhint("Unicode escapes must be \\XXXX, \\+XXXXXX, \\uXXXX, or \\UXXXXXXXX.")));
    5737             :         }
    5738             :         else
    5739             :         {
    5740         384 :             if (pair_first)
    5741           0 :                 goto invalid_pair;
    5742             : 
    5743         384 :             appendStringInfoChar(&str, *instr++);
    5744         384 :             len--;
    5745             :         }
    5746             :     }
    5747             : 
    5748             :     /* unfinished surrogate pair? */
    5749          24 :     if (pair_first)
    5750           6 :         goto invalid_pair;
    5751             : 
    5752          18 :     result = cstring_to_text_with_len(str.data, str.len);
    5753          18 :     pfree(str.data);
    5754             : 
    5755          18 :     PG_RETURN_TEXT_P(result);
    5756             : 
    5757          30 : invalid_pair:
    5758          30 :     ereport(ERROR,
    5759             :             (errcode(ERRCODE_SYNTAX_ERROR),
    5760             :              errmsg("invalid Unicode surrogate pair")));
    5761             :     PG_RETURN_NULL();           /* keep compiler quiet */
    5762             : }

Generated by: LCOV version 1.16