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

Generated by: LCOV version 2.0-1