PostgreSQL Source Code git master
Loading...
Searching...
No Matches
varlena.c File Reference
#include "postgres.h"
#include <ctype.h>
#include <limits.h>
#include "access/detoast.h"
#include "access/toast_compression.h"
#include "catalog/pg_collation.h"
#include "catalog/pg_type.h"
#include "common/hashfn.h"
#include "common/int.h"
#include "common/unicode_category.h"
#include "common/unicode_norm.h"
#include "common/unicode_version.h"
#include "funcapi.h"
#include "lib/hyperloglog.h"
#include "libpq/pqformat.h"
#include "miscadmin.h"
#include "nodes/execnodes.h"
#include "parser/scansup.h"
#include "port/pg_bswap.h"
#include "regex/regex.h"
#include "utils/builtins.h"
#include "utils/guc.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/pg_locale.h"
#include "utils/sortsupport.h"
#include "utils/varlena.h"
#include "levenshtein.c"
Include dependency graph for varlena.c:

Go to the source code of this file.

Data Structures

struct  TextPositionState
 
struct  VarStringSortSupport
 
struct  SplitTextOutputData
 

Macros

#define TEXTBUFLEN   1024
 
#define DatumGetVarStringP(X)   ((VarString *) PG_DETOAST_DATUM(X))
 
#define DatumGetVarStringPP(X)   ((VarString *) PG_DETOAST_DATUM_PACKED(X))
 
#define CmpCall(cmpfunc)
 
#define TEXT_FORMAT_FLAG_MINUS   0x0001 /* is minus flag present? */
 
#define ADVANCE_PARSE_POINTER(ptr, end_ptr)
 
#define LEVENSHTEIN_LESS_EQUAL
 

Typedefs

typedef struct varlena VarString
 

Functions

static int varstrfastcmp_c (Datum x, Datum y, SortSupport ssup)
 
static int bpcharfastcmp_c (Datum x, Datum y, SortSupport ssup)
 
static int namefastcmp_c (Datum x, Datum y, SortSupport ssup)
 
static int varlenafastcmp_locale (Datum x, Datum y, SortSupport ssup)
 
static int namefastcmp_locale (Datum x, Datum y, SortSupport ssup)
 
static int varstrfastcmp_locale (char *a1p, int len1, char *a2p, int len2, SortSupport ssup)
 
static Datum varstr_abbrev_convert (Datum original, SortSupport ssup)
 
static bool varstr_abbrev_abort (int memtupcount, SortSupport ssup)
 
static int32 text_length (Datum str)
 
static texttext_catenate (text *t1, text *t2)
 
static texttext_substring (Datum str, int32 start, int32 length, bool length_not_specified)
 
static texttext_overlay (text *t1, text *t2, int sp, int sl)
 
static int text_position (text *t1, text *t2, Oid collid)
 
static void text_position_setup (text *t1, text *t2, Oid collid, TextPositionState *state)
 
static bool text_position_next (TextPositionState *state)
 
static chartext_position_next_internal (char *start_ptr, TextPositionState *state)
 
static chartext_position_get_match_ptr (TextPositionState *state)
 
static int text_position_get_match_pos (TextPositionState *state)
 
static void text_position_cleanup (TextPositionState *state)
 
static void check_collation_set (Oid collid)
 
static int text_cmp (text *arg1, text *arg2, Oid collid)
 
static void appendStringInfoText (StringInfo str, const text *t)
 
static bool split_text (FunctionCallInfo fcinfo, SplitTextOutputData *tstate)
 
static void split_text_accum_result (SplitTextOutputData *tstate, text *field_value, text *null_string, Oid collation)
 
static textarray_to_text_internal (FunctionCallInfo fcinfo, ArrayType *v, const char *fldsep, const char *null_string)
 
static StringInfo makeStringAggState (FunctionCallInfo fcinfo)
 
static bool text_format_parse_digits (const char **ptr, const char *end_ptr, int *value)
 
static const chartext_format_parse_format (const char *start_ptr, const char *end_ptr, int *argpos, int *widthpos, int *flags, int *width)
 
static void text_format_string_conversion (StringInfo buf, char conversion, FmgrInfo *typOutputInfo, Datum value, bool isNull, int flags, int width)
 
static void text_format_append_string (StringInfo buf, const char *str, int flags, int width)
 
textcstring_to_text (const char *s)
 
textcstring_to_text_with_len (const char *s, int len)
 
chartext_to_cstring (const text *t)
 
void text_to_cstring_buffer (const text *src, char *dst, size_t dst_len)
 
Datum textin (PG_FUNCTION_ARGS)
 
Datum textout (PG_FUNCTION_ARGS)
 
Datum textrecv (PG_FUNCTION_ARGS)
 
Datum textsend (PG_FUNCTION_ARGS)
 
Datum unknownin (PG_FUNCTION_ARGS)
 
Datum unknownout (PG_FUNCTION_ARGS)
 
Datum unknownrecv (PG_FUNCTION_ARGS)
 
Datum unknownsend (PG_FUNCTION_ARGS)
 
Datum textlen (PG_FUNCTION_ARGS)
 
Datum textoctetlen (PG_FUNCTION_ARGS)
 
Datum textcat (PG_FUNCTION_ARGS)
 
static int charlen_to_bytelen (const char *p, int n)
 
Datum text_substr (PG_FUNCTION_ARGS)
 
Datum text_substr_no_len (PG_FUNCTION_ARGS)
 
Datum textoverlay (PG_FUNCTION_ARGS)
 
Datum textoverlay_no_len (PG_FUNCTION_ARGS)
 
Datum textpos (PG_FUNCTION_ARGS)
 
static void text_position_reset (TextPositionState *state)
 
int varstr_cmp (const char *arg1, int len1, const char *arg2, int len2, Oid collid)
 
Datum texteq (PG_FUNCTION_ARGS)
 
Datum textne (PG_FUNCTION_ARGS)
 
Datum text_lt (PG_FUNCTION_ARGS)
 
Datum text_le (PG_FUNCTION_ARGS)
 
Datum text_gt (PG_FUNCTION_ARGS)
 
Datum text_ge (PG_FUNCTION_ARGS)
 
Datum text_starts_with (PG_FUNCTION_ARGS)
 
Datum bttextcmp (PG_FUNCTION_ARGS)
 
Datum bttextsortsupport (PG_FUNCTION_ARGS)
 
void varstr_sortsupport (SortSupport ssup, Oid typid, Oid collid)
 
Datum btvarstrequalimage (PG_FUNCTION_ARGS)
 
Datum text_larger (PG_FUNCTION_ARGS)
 
Datum text_smaller (PG_FUNCTION_ARGS)
 
Datum nameeqtext (PG_FUNCTION_ARGS)
 
Datum texteqname (PG_FUNCTION_ARGS)
 
Datum namenetext (PG_FUNCTION_ARGS)
 
Datum textnename (PG_FUNCTION_ARGS)
 
Datum btnametextcmp (PG_FUNCTION_ARGS)
 
Datum bttextnamecmp (PG_FUNCTION_ARGS)
 
Datum namelttext (PG_FUNCTION_ARGS)
 
Datum nameletext (PG_FUNCTION_ARGS)
 
Datum namegttext (PG_FUNCTION_ARGS)
 
Datum namegetext (PG_FUNCTION_ARGS)
 
Datum textltname (PG_FUNCTION_ARGS)
 
Datum textlename (PG_FUNCTION_ARGS)
 
Datum textgtname (PG_FUNCTION_ARGS)
 
Datum textgename (PG_FUNCTION_ARGS)
 
static int internal_text_pattern_compare (text *arg1, text *arg2)
 
Datum text_pattern_lt (PG_FUNCTION_ARGS)
 
Datum text_pattern_le (PG_FUNCTION_ARGS)
 
Datum text_pattern_ge (PG_FUNCTION_ARGS)
 
Datum text_pattern_gt (PG_FUNCTION_ARGS)
 
Datum bttext_pattern_cmp (PG_FUNCTION_ARGS)
 
Datum bttext_pattern_sortsupport (PG_FUNCTION_ARGS)
 
Datum text_name (PG_FUNCTION_ARGS)
 
Datum name_text (PG_FUNCTION_ARGS)
 
ListtextToQualifiedNameList (text *textval)
 
bool SplitIdentifierString (char *rawstring, char separator, List **namelist)
 
bool SplitDirectoriesString (char *rawstring, char separator, List **namelist)
 
bool SplitGUCList (char *rawstring, char separator, List **namelist)
 
Datum replace_text (PG_FUNCTION_ARGS)
 
static int check_replace_text_has_escape (const text *replace_text)
 
static void appendStringInfoRegexpSubstr (StringInfo str, text *replace_text, regmatch_t *pmatch, char *start_ptr, int data_pos)
 
textreplace_text_regexp (text *src_text, text *pattern_text, text *replace_text, int cflags, Oid collation, int search_start, int n)
 
Datum split_part (PG_FUNCTION_ARGS)
 
static bool text_isequal (text *txt1, text *txt2, Oid collid)
 
Datum text_to_array (PG_FUNCTION_ARGS)
 
Datum text_to_array_null (PG_FUNCTION_ARGS)
 
Datum text_to_table (PG_FUNCTION_ARGS)
 
Datum text_to_table_null (PG_FUNCTION_ARGS)
 
Datum array_to_text (PG_FUNCTION_ARGS)
 
Datum array_to_text_null (PG_FUNCTION_ARGS)
 
static textconvert_to_base (uint64 value, int base)
 
Datum to_bin32 (PG_FUNCTION_ARGS)
 
Datum to_bin64 (PG_FUNCTION_ARGS)
 
Datum to_oct32 (PG_FUNCTION_ARGS)
 
Datum to_oct64 (PG_FUNCTION_ARGS)
 
Datum to_hex32 (PG_FUNCTION_ARGS)
 
Datum to_hex64 (PG_FUNCTION_ARGS)
 
Datum pg_column_size (PG_FUNCTION_ARGS)
 
Datum pg_column_compression (PG_FUNCTION_ARGS)
 
Datum pg_column_toast_chunk_id (PG_FUNCTION_ARGS)
 
Datum string_agg_transfn (PG_FUNCTION_ARGS)
 
Datum string_agg_combine (PG_FUNCTION_ARGS)
 
Datum string_agg_serialize (PG_FUNCTION_ARGS)
 
Datum string_agg_deserialize (PG_FUNCTION_ARGS)
 
Datum string_agg_finalfn (PG_FUNCTION_ARGS)
 
static FmgrInfobuild_concat_foutcache (FunctionCallInfo fcinfo, int argidx)
 
static textconcat_internal (const char *sepstr, int argidx, FunctionCallInfo fcinfo)
 
Datum text_concat (PG_FUNCTION_ARGS)
 
Datum text_concat_ws (PG_FUNCTION_ARGS)
 
Datum text_left (PG_FUNCTION_ARGS)
 
Datum text_right (PG_FUNCTION_ARGS)
 
Datum text_reverse (PG_FUNCTION_ARGS)
 
Datum text_format (PG_FUNCTION_ARGS)
 
Datum text_format_nv (PG_FUNCTION_ARGS)
 
static bool rest_of_char_same (const char *s1, const char *s2, int len)
 
void initClosestMatch (ClosestMatchState *state, const char *source, int max_d)
 
void updateClosestMatch (ClosestMatchState *state, const char *candidate)
 
const chargetClosestMatch (ClosestMatchState *state)
 
static UnicodeNormalizationForm unicode_norm_form_from_string (const char *formstr)
 
Datum unicode_version (PG_FUNCTION_ARGS)
 
Datum icu_unicode_version (PG_FUNCTION_ARGS)
 
Datum unicode_assigned (PG_FUNCTION_ARGS)
 
Datum unicode_normalize_func (PG_FUNCTION_ARGS)
 
Datum unicode_is_normalized (PG_FUNCTION_ARGS)
 
static bool isxdigits_n (const char *instr, size_t n)
 
static unsigned int hexval (unsigned char c)
 
static unsigned int hexval_n (const char *instr, size_t n)
 
Datum unistr (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ ADVANCE_PARSE_POINTER

#define ADVANCE_PARSE_POINTER (   ptr,
  end_ptr 
)
Value:
do { \
if (++(ptr) >= (end_ptr)) \
errmsg("unterminated format() type specifier"), \
errhint("For a single \"%%\" use \"%%%%\"."))); \
} while (0)
int errhint(const char *fmt,...)
Definition elog.c:1330
int errcode(int sqlerrcode)
Definition elog.c:863
int errmsg(const char *fmt,...)
Definition elog.c:1080
#define ERROR
Definition elog.h:39
static int fb(int x)

Definition at line 4707 of file varlena.c.

4708 { \
4709 if (++(ptr) >= (end_ptr)) \
4710 ereport(ERROR, \
4712 errmsg("unterminated format() type specifier"), \
4713 errhint("For a single \"%%\" use \"%%%%\"."))); \
4714 } while (0)

◆ CmpCall

#define CmpCall (   cmpfunc)
Value:
Datum DirectFunctionCall2Coll(PGFunction func, Oid collation, Datum arg1, Datum arg2)
Definition fmgr.c:813
#define PG_GETARG_DATUM(n)
Definition fmgr.h:268
#define PG_GET_COLLATION()
Definition fmgr.h:198
static int32 DatumGetInt32(Datum X)
Definition postgres.h:212

Definition at line 2444 of file varlena.c.

◆ DatumGetVarStringP

#define DatumGetVarStringP (   X)    ((VarString *) PG_DETOAST_DATUM(X))

Definition at line 119 of file varlena.c.

◆ DatumGetVarStringPP

#define DatumGetVarStringPP (   X)    ((VarString *) PG_DETOAST_DATUM_PACKED(X))

Definition at line 120 of file varlena.c.

◆ LEVENSHTEIN_LESS_EQUAL

#define LEVENSHTEIN_LESS_EQUAL

Definition at line 5247 of file varlena.c.

◆ TEXT_FORMAT_FLAG_MINUS

#define TEXT_FORMAT_FLAG_MINUS   0x0001 /* is minus flag present? */

Definition at line 4705 of file varlena.c.

◆ TEXTBUFLEN

#define TEXTBUFLEN   1024

Definition at line 117 of file varlena.c.

Typedef Documentation

◆ VarString

Definition at line 45 of file varlena.c.

Function Documentation

◆ appendStringInfoRegexpSubstr()

static void appendStringInfoRegexpSubstr ( StringInfo  str,
text replace_text,
regmatch_t pmatch,
char start_ptr,
int  data_pos 
)
static

Definition at line 3188 of file varlena.c.

3191{
3192 const char *p = VARDATA_ANY(replace_text);
3193 const char *p_end = p + VARSIZE_ANY_EXHDR(replace_text);
3194
3195 while (p < p_end)
3196 {
3197 const char *chunk_start = p;
3198 int so;
3199 int eo;
3200
3201 /* Find next escape char, if any. */
3202 p = memchr(p, '\\', p_end - p);
3203 if (p == NULL)
3204 p = p_end;
3205
3206 /* Copy the text we just scanned over, if any. */
3207 if (p > chunk_start)
3209
3210 /* Done if at end of string, else advance over escape char. */
3211 if (p >= p_end)
3212 break;
3213 p++;
3214
3215 if (p >= p_end)
3216 {
3217 /* Escape at very end of input. Treat same as unexpected char */
3219 break;
3220 }
3221
3222 if (*p >= '1' && *p <= '9')
3223 {
3224 /* Use the back reference of regexp. */
3225 int idx = *p - '0';
3226
3227 so = pmatch[idx].rm_so;
3228 eo = pmatch[idx].rm_eo;
3229 p++;
3230 }
3231 else if (*p == '&')
3232 {
3233 /* Use the entire matched string. */
3234 so = pmatch[0].rm_so;
3235 eo = pmatch[0].rm_eo;
3236 p++;
3237 }
3238 else if (*p == '\\')
3239 {
3240 /* \\ means transfer one \ to output. */
3242 p++;
3243 continue;
3244 }
3245 else
3246 {
3247 /*
3248 * If escape char is not followed by any expected char, just treat
3249 * it as ordinary data to copy. (XXX would it be better to throw
3250 * an error?)
3251 */
3253 continue;
3254 }
3255
3256 if (so >= 0 && eo >= 0)
3257 {
3258 /*
3259 * Copy the text that is back reference of regexp. Note so and eo
3260 * are counted in characters not bytes.
3261 */
3262 char *chunk_start;
3263 int chunk_len;
3264
3265 Assert(so >= data_pos);
3270 }
3271 }
3272}
Datum idx(PG_FUNCTION_ARGS)
Definition _int_op.c:262
#define Assert(condition)
Definition c.h:873
const char * str
void appendBinaryStringInfo(StringInfo str, const void *data, int datalen)
Definition stringinfo.c:281
void appendStringInfoChar(StringInfo str, char ch)
Definition stringinfo.c:242
static Size VARSIZE_ANY_EXHDR(const void *PTR)
Definition varatt.h:472
static char * VARDATA_ANY(const void *PTR)
Definition varatt.h:486
static int charlen_to_bytelen(const char *p, int n)
Definition varlena.c:501
Datum replace_text(PG_FUNCTION_ARGS)
Definition varlena.c:3078

References appendBinaryStringInfo(), appendStringInfoChar(), Assert, charlen_to_bytelen(), fb(), idx(), replace_text(), str, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

Referenced by replace_text_regexp().

◆ appendStringInfoText()

static void appendStringInfoText ( StringInfo  str,
const text t 
)
static

◆ array_to_text()

Datum array_to_text ( PG_FUNCTION_ARGS  )

Definition at line 3848 of file varlena.c.

3849{
3852
3854}
#define PG_GETARG_ARRAYTYPE_P(n)
Definition array.h:263
#define PG_GETARG_TEXT_PP(n)
Definition fmgr.h:310
#define PG_RETURN_TEXT_P(x)
Definition fmgr.h:374
static text * array_to_text_internal(FunctionCallInfo fcinfo, ArrayType *v, const char *fldsep, const char *null_string)
Definition varlena.c:3890
char * text_to_cstring(const text *t)
Definition varlena.c:214

References array_to_text_internal(), fb(), PG_GETARG_ARRAYTYPE_P, PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, and text_to_cstring().

◆ array_to_text_internal()

static text * array_to_text_internal ( FunctionCallInfo  fcinfo,
ArrayType v,
const char fldsep,
const char null_string 
)
static

Definition at line 3890 of file varlena.c.

3892{
3893 text *result;
3894 int nitems,
3895 *dims,
3896 ndims;
3897 Oid element_type;
3898 int typlen;
3899 bool typbyval;
3900 char typalign;
3902 bool printed = false;
3903 char *p;
3904 bits8 *bitmap;
3905 int bitmask;
3906 int i;
3908
3909 ndims = ARR_NDIM(v);
3910 dims = ARR_DIMS(v);
3911 nitems = ArrayGetNItems(ndims, dims);
3912
3913 /* if there are no elements, return an empty string */
3914 if (nitems == 0)
3915 return cstring_to_text_with_len("", 0);
3916
3917 element_type = ARR_ELEMTYPE(v);
3919
3920 /*
3921 * We arrange to look up info about element type, including its output
3922 * conversion proc, only once per series of calls, assuming the element
3923 * type doesn't change underneath us.
3924 */
3925 my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
3926 if (my_extra == NULL)
3927 {
3928 fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
3929 sizeof(ArrayMetaState));
3930 my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
3931 my_extra->element_type = ~element_type;
3932 }
3933
3934 if (my_extra->element_type != element_type)
3935 {
3936 /*
3937 * Get info about element type, including its output conversion proc
3938 */
3939 get_type_io_data(element_type, IOFunc_output,
3940 &my_extra->typlen, &my_extra->typbyval,
3941 &my_extra->typalign, &my_extra->typdelim,
3942 &my_extra->typioparam, &my_extra->typiofunc);
3943 fmgr_info_cxt(my_extra->typiofunc, &my_extra->proc,
3944 fcinfo->flinfo->fn_mcxt);
3945 my_extra->element_type = element_type;
3946 }
3947 typlen = my_extra->typlen;
3948 typbyval = my_extra->typbyval;
3949 typalign = my_extra->typalign;
3950
3951 p = ARR_DATA_PTR(v);
3952 bitmap = ARR_NULLBITMAP(v);
3953 bitmask = 1;
3954
3955 for (i = 0; i < nitems; i++)
3956 {
3958 char *value;
3959
3960 /* Get source element, checking for NULL */
3961 if (bitmap && (*bitmap & bitmask) == 0)
3962 {
3963 /* if null_string is NULL, we just ignore null elements */
3964 if (null_string != NULL)
3965 {
3966 if (printed)
3968 else
3970 printed = true;
3971 }
3972 }
3973 else
3974 {
3975 itemvalue = fetch_att(p, typbyval, typlen);
3976
3978
3979 if (printed)
3980 appendStringInfo(&buf, "%s%s", fldsep, value);
3981 else
3983 printed = true;
3984
3985 p = att_addlength_pointer(p, typlen, p);
3986 p = (char *) att_align_nominal(p, typalign);
3987 }
3988
3989 /* advance bitmap pointer if any */
3990 if (bitmap)
3991 {
3992 bitmask <<= 1;
3993 if (bitmask == 0x100)
3994 {
3995 bitmap++;
3996 bitmask = 1;
3997 }
3998 }
3999 }
4000
4001 result = cstring_to_text_with_len(buf.data, buf.len);
4002 pfree(buf.data);
4003
4004 return result;
4005}
#define ARR_NDIM(a)
Definition array.h:290
#define ARR_DATA_PTR(a)
Definition array.h:322
#define ARR_NULLBITMAP(a)
Definition array.h:300
#define ARR_ELEMTYPE(a)
Definition array.h:292
#define ARR_DIMS(a)
Definition array.h:294
int ArrayGetNItems(int ndim, const int *dims)
Definition arrayutils.c:57
uint8 bits8
Definition c.h:553
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition fmgr.c:138
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition fmgr.c:1683
#define nitems(x)
Definition indent.h:31
static struct @172 value
int i
Definition isn.c:77
void get_type_io_data(Oid typid, IOFuncSelector which_func, int16 *typlen, bool *typbyval, char *typalign, char *typdelim, Oid *typioparam, Oid *func)
Definition lsyscache.c:2475
@ IOFunc_output
Definition lsyscache.h:37
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition mcxt.c:1232
void pfree(void *pointer)
Definition mcxt.c:1616
static char buf[DEFAULT_XLOG_SEG_SIZE]
char typalign
Definition pg_type.h:176
uint64_t Datum
Definition postgres.h:70
unsigned int Oid
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition stringinfo.c:145
void appendStringInfoString(StringInfo str, const char *s)
Definition stringinfo.c:230
void initStringInfo(StringInfo str)
Definition stringinfo.c:97
void * fn_extra
Definition fmgr.h:64
MemoryContext fn_mcxt
Definition fmgr.h:65
FmgrInfo * flinfo
Definition fmgr.h:87
Definition c.h:706
#define att_align_nominal(cur_offset, attalign)
Definition tupmacs.h:145
#define att_addlength_pointer(cur_offset, attlen, attptr)
Definition tupmacs.h:180
static Datum fetch_att(const void *T, bool attbyval, int attlen)
Definition tupmacs.h:50
text * cstring_to_text_with_len(const char *s, int len)
Definition varlena.c:193

References appendStringInfo(), appendStringInfoString(), ARR_DATA_PTR, ARR_DIMS, ARR_ELEMTYPE, ARR_NDIM, ARR_NULLBITMAP, ArrayGetNItems(), att_addlength_pointer, att_align_nominal, buf, cstring_to_text_with_len(), fb(), fetch_att(), FunctionCallInfoBaseData::flinfo, fmgr_info_cxt(), FmgrInfo::fn_extra, FmgrInfo::fn_mcxt, get_type_io_data(), i, initStringInfo(), IOFunc_output, MemoryContextAlloc(), nitems, OutputFunctionCall(), pfree(), typalign, and value.

Referenced by array_to_text(), array_to_text_null(), and concat_internal().

◆ array_to_text_null()

Datum array_to_text_null ( PG_FUNCTION_ARGS  )

Definition at line 3864 of file varlena.c.

3865{
3866 ArrayType *v;
3867 char *fldsep;
3868 char *null_string;
3869
3870 /* returns NULL when first or second parameter is NULL */
3871 if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
3873
3874 v = PG_GETARG_ARRAYTYPE_P(0);
3876
3877 /* NULL null string is passed through as a null pointer */
3878 if (!PG_ARGISNULL(2))
3880 else
3881 null_string = NULL;
3882
3884}
#define PG_ARGISNULL(n)
Definition fmgr.h:209
#define PG_RETURN_NULL()
Definition fmgr.h:346

References array_to_text_internal(), fb(), PG_ARGISNULL, PG_GETARG_ARRAYTYPE_P, PG_GETARG_TEXT_PP, PG_RETURN_NULL, PG_RETURN_TEXT_P, and text_to_cstring().

◆ bpcharfastcmp_c()

static int bpcharfastcmp_c ( Datum  x,
Datum  y,
SortSupport  ssup 
)
static

Definition at line 1797 of file varlena.c.

1798{
1801 char *a1p,
1802 *a2p;
1803 int len1,
1804 len2,
1805 result;
1806
1807 a1p = VARDATA_ANY(arg1);
1808 a2p = VARDATA_ANY(arg2);
1809
1812
1813 result = memcmp(a1p, a2p, Min(len1, len2));
1814 if ((result == 0) && (len1 != len2))
1815 result = (len1 < len2) ? -1 : 1;
1816
1817 /* We can't afford to leak memory here. */
1818 if (PointerGetDatum(arg1) != x)
1819 pfree(arg1);
1820 if (PointerGetDatum(arg2) != y)
1821 pfree(arg2);
1822
1823 return result;
1824}
#define Min(x, y)
Definition c.h:997
#define DatumGetBpCharPP(X)
Definition fmgr.h:294
int y
Definition isn.c:76
int x
Definition isn.c:75
static Datum PointerGetDatum(const void *X)
Definition postgres.h:352
int bpchartruelen(char *s, int len)
Definition varchar.c:676

References bpchartruelen(), DatumGetBpCharPP, fb(), Min, pfree(), PointerGetDatum(), VARDATA_ANY(), VARSIZE_ANY_EXHDR(), x, and y.

Referenced by varstr_sortsupport().

◆ btnametextcmp()

Datum btnametextcmp ( PG_FUNCTION_ARGS  )

Definition at line 2413 of file varlena.c.

2414{
2417 int32 result;
2418
2419 result = varstr_cmp(NameStr(*arg1), strlen(NameStr(*arg1)),
2422
2424
2425 PG_RETURN_INT32(result);
2426}
#define NameStr(name)
Definition c.h:765
int32_t int32
Definition c.h:542
#define PG_FREE_IF_COPY(ptr, n)
Definition fmgr.h:260
#define PG_GETARG_NAME(n)
Definition fmgr.h:279
#define PG_RETURN_INT32(x)
Definition fmgr.h:355
Definition c.h:760
int varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid)
Definition varlena.c:1308

References fb(), NameStr, PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_NAME, PG_GETARG_TEXT_PP, PG_RETURN_INT32, VARDATA_ANY(), VARSIZE_ANY_EXHDR(), and varstr_cmp().

Referenced by namegetext(), namegttext(), nameletext(), and namelttext().

◆ bttext_pattern_cmp()

Datum bttext_pattern_cmp ( PG_FUNCTION_ARGS  )

Definition at line 2596 of file varlena.c.

2597{
2600 int result;
2601
2603
2606
2607 PG_RETURN_INT32(result);
2608}
static int internal_text_pattern_compare(text *arg1, text *arg2)
Definition varlena.c:2510

References fb(), internal_text_pattern_compare(), PG_FREE_IF_COPY, PG_GETARG_TEXT_PP, and PG_RETURN_INT32.

◆ bttext_pattern_sortsupport()

Datum bttext_pattern_sortsupport ( PG_FUNCTION_ARGS  )

Definition at line 2612 of file varlena.c.

2613{
2615 MemoryContext oldcontext;
2616
2617 oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
2618
2619 /* Use generic string SortSupport, forcing "C" collation */
2621
2622 MemoryContextSwitchTo(oldcontext);
2623
2625}
#define PG_RETURN_VOID()
Definition fmgr.h:350
#define PG_GETARG_POINTER(n)
Definition fmgr.h:277
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
struct SortSupportData * SortSupport
Definition sortsupport.h:58
MemoryContext ssup_cxt
Definition sortsupport.h:66
void varstr_sortsupport(SortSupport ssup, Oid typid, Oid collid)
Definition varlena.c:1624

References fb(), MemoryContextSwitchTo(), PG_GETARG_POINTER, PG_RETURN_VOID, SortSupportData::ssup_cxt, and varstr_sortsupport().

◆ bttextcmp()

Datum bttextcmp ( PG_FUNCTION_ARGS  )

Definition at line 1584 of file varlena.c.

1585{
1588 int32 result;
1589
1590 result = text_cmp(arg1, arg2, PG_GET_COLLATION());
1591
1594
1595 PG_RETURN_INT32(result);
1596}
static int text_cmp(text *arg1, text *arg2, Oid collid)
Definition varlena.c:1356

References fb(), PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_TEXT_PP, PG_RETURN_INT32, and text_cmp().

Referenced by gbt_text_ssup_cmp(), and gbt_textcmp().

◆ bttextnamecmp()

Datum bttextnamecmp ( PG_FUNCTION_ARGS  )

◆ bttextsortsupport()

Datum bttextsortsupport ( PG_FUNCTION_ARGS  )

Definition at line 1599 of file varlena.c.

1600{
1602 Oid collid = ssup->ssup_collation;
1603 MemoryContext oldcontext;
1604
1605 oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
1606
1607 /* Use generic string SortSupport */
1609
1610 MemoryContextSwitchTo(oldcontext);
1611
1613}
Oid collid

References collid, fb(), MemoryContextSwitchTo(), PG_GETARG_POINTER, PG_RETURN_VOID, SortSupportData::ssup_collation, SortSupportData::ssup_cxt, and varstr_sortsupport().

◆ btvarstrequalimage()

Datum btvarstrequalimage ( PG_FUNCTION_ARGS  )

Definition at line 2268 of file varlena.c.

2269{
2270#ifdef NOT_USED
2271 Oid opcintype = PG_GETARG_OID(0);
2272#endif
2274 pg_locale_t locale;
2275
2277
2279
2281}
#define PG_GETARG_OID(n)
Definition fmgr.h:275
#define PG_RETURN_BOOL(x)
Definition fmgr.h:360
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition pg_locale.c:1189
static void check_collation_set(Oid collid)
Definition varlena.c:1279

References check_collation_set(), collid, pg_locale_struct::deterministic, PG_GET_COLLATION, PG_GETARG_OID, pg_newlocale_from_collation(), and PG_RETURN_BOOL.

◆ build_concat_foutcache()

static FmgrInfo * build_concat_foutcache ( FunctionCallInfo  fcinfo,
int  argidx 
)
static

Definition at line 4466 of file varlena.c.

4467{
4469 int i;
4470
4471 /* We keep the info in fn_mcxt so it survives across calls */
4473 PG_NARGS() * sizeof(FmgrInfo));
4474
4475 for (i = argidx; i < PG_NARGS(); i++)
4476 {
4477 Oid valtype;
4478 Oid typOutput;
4479 bool typIsVarlena;
4480
4481 valtype = get_fn_expr_argtype(fcinfo->flinfo, i);
4482 if (!OidIsValid(valtype))
4483 elog(ERROR, "could not determine data type of concat() input");
4484
4487 }
4488
4489 fcinfo->flinfo->fn_extra = foutcache;
4490
4491 return foutcache;
4492}
#define OidIsValid(objectId)
Definition c.h:788
#define elog(elevel,...)
Definition elog.h:226
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition fmgr.c:1875
#define PG_NARGS()
Definition fmgr.h:203
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition lsyscache.c:3057

References elog, ERROR, fb(), FunctionCallInfoBaseData::flinfo, fmgr_info_cxt(), FmgrInfo::fn_extra, FmgrInfo::fn_mcxt, get_fn_expr_argtype(), getTypeOutputInfo(), i, MemoryContextAlloc(), OidIsValid, and PG_NARGS.

Referenced by concat_internal().

◆ charlen_to_bytelen()

static int charlen_to_bytelen ( const char p,
int  n 
)
static

Definition at line 501 of file varlena.c.

502{
504 {
505 /* Optimization for single-byte encodings */
506 return n;
507 }
508 else
509 {
510 const char *s;
511
512 for (s = p; n > 0; n--)
513 s += pg_mblen(s);
514
515 return s - p;
516 }
517}
int pg_database_encoding_max_length(void)
Definition mbutils.c:1549
int pg_mblen(const char *mbstr)
Definition mbutils.c:1026

References pg_database_encoding_max_length(), and pg_mblen().

Referenced by appendStringInfoRegexpSubstr(), and replace_text_regexp().

◆ check_collation_set()

static void check_collation_set ( Oid  collid)
static

Definition at line 1279 of file varlena.c.

1280{
1281 if (!OidIsValid(collid))
1282 {
1283 /*
1284 * This typically means that the parser could not resolve a conflict
1285 * of implicit collations, so report it that way.
1286 */
1287 ereport(ERROR,
1289 errmsg("could not determine which collation to use for string comparison"),
1290 errhint("Use the COLLATE clause to set the collation explicitly.")));
1291 }
1292}
#define ereport(elevel,...)
Definition elog.h:150

References collid, ereport, errcode(), errhint(), errmsg(), ERROR, fb(), and OidIsValid.

Referenced by btvarstrequalimage(), nameeqtext(), namenetext(), text_position(), text_position_setup(), text_starts_with(), texteq(), texteqname(), textne(), textnename(), varstr_cmp(), and varstr_sortsupport().

◆ check_replace_text_has_escape()

static int check_replace_text_has_escape ( const text replace_text)
static

Definition at line 3155 of file varlena.c.

3156{
3157 int result = 0;
3158 const char *p = VARDATA_ANY(replace_text);
3159 const char *p_end = p + VARSIZE_ANY_EXHDR(replace_text);
3160
3161 while (p < p_end)
3162 {
3163 /* Find next escape char, if any. */
3164 p = memchr(p, '\\', p_end - p);
3165 if (p == NULL)
3166 break;
3167 p++;
3168 /* Note: a backslash at the end doesn't require extra processing. */
3169 if (p < p_end)
3170 {
3171 if (*p >= '1' && *p <= '9')
3172 return 2; /* Found a submatch specifier, so done */
3173 result = 1; /* Found some other sequence, keep looking */
3174 p++;
3175 }
3176 }
3177 return result;
3178}

References fb(), replace_text(), VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

Referenced by replace_text_regexp().

◆ concat_internal()

static text * concat_internal ( const char sepstr,
int  argidx,
FunctionCallInfo  fcinfo 
)
static

Definition at line 4504 of file varlena.c.

4506{
4507 text *result;
4510 bool first_arg = true;
4511 int i;
4512
4513 /*
4514 * concat(VARIADIC some-array) is essentially equivalent to
4515 * array_to_text(), ie concat the array elements with the given separator.
4516 * So we just pass the case off to that code.
4517 */
4518 if (get_fn_expr_variadic(fcinfo->flinfo))
4519 {
4520 ArrayType *arr;
4521
4522 /* Should have just the one argument */
4523 Assert(argidx == PG_NARGS() - 1);
4524
4525 /* concat(VARIADIC NULL) is defined as NULL */
4526 if (PG_ARGISNULL(argidx))
4527 return NULL;
4528
4529 /*
4530 * Non-null argument had better be an array. We assume that any call
4531 * context that could let get_fn_expr_variadic return true will have
4532 * checked that a VARIADIC-labeled parameter actually is an array. So
4533 * it should be okay to just Assert that it's an array rather than
4534 * doing a full-fledged error check.
4535 */
4537
4538 /* OK, safe to fetch the array value */
4540
4541 /*
4542 * And serialize the array. We tell array_to_text to ignore null
4543 * elements, which matches the behavior of the loop below.
4544 */
4545 return array_to_text_internal(fcinfo, arr, sepstr, NULL);
4546 }
4547
4548 /* Normal case without explicit VARIADIC marker */
4550
4551 /* Get output function info, building it if first time through */
4552 foutcache = (FmgrInfo *) fcinfo->flinfo->fn_extra;
4553 if (foutcache == NULL)
4555
4556 for (i = argidx; i < PG_NARGS(); i++)
4557 {
4558 if (!PG_ARGISNULL(i))
4559 {
4561
4562 /* add separator if appropriate */
4563 if (first_arg)
4564 first_arg = false;
4565 else
4567
4568 /* call the appropriate type output function, append the result */
4571 }
4572 }
4573
4574 result = cstring_to_text_with_len(str.data, str.len);
4575 pfree(str.data);
4576
4577 return result;
4578}
bool get_fn_expr_variadic(FmgrInfo *flinfo)
Definition fmgr.c:2009
Oid get_base_element_type(Oid typid)
Definition lsyscache.c:2982
static FmgrInfo * build_concat_foutcache(FunctionCallInfo fcinfo, int argidx)
Definition varlena.c:4466

References appendStringInfoString(), array_to_text_internal(), Assert, build_concat_foutcache(), cstring_to_text_with_len(), fb(), FunctionCallInfoBaseData::flinfo, FmgrInfo::fn_extra, get_base_element_type(), get_fn_expr_argtype(), get_fn_expr_variadic(), i, initStringInfo(), OidIsValid, OutputFunctionCall(), pfree(), PG_ARGISNULL, PG_GETARG_ARRAYTYPE_P, PG_GETARG_DATUM, PG_NARGS, str, and value.

Referenced by text_concat(), and text_concat_ws().

◆ convert_to_base()

static text * convert_to_base ( uint64  value,
int  base 
)
inlinestatic

Definition at line 4012 of file varlena.c.

4013{
4014 const char *digits = "0123456789abcdef";
4015
4016 /* We size the buffer for to_bin's longest possible return value. */
4017 char buf[sizeof(uint64) * BITS_PER_BYTE];
4018 char *const end = buf + sizeof(buf);
4019 char *ptr = end;
4020
4021 Assert(base > 1);
4022 Assert(base <= 16);
4023
4024 do
4025 {
4026 *--ptr = digits[value % base];
4027 value /= base;
4028 } while (ptr > buf && value);
4029
4030 return cstring_to_text_with_len(ptr, end - ptr);
4031}
uint64_t uint64
Definition c.h:547
int digits
Definition informix.c:691
#define BITS_PER_BYTE

References Assert, BITS_PER_BYTE, buf, cstring_to_text_with_len(), digits, and value.

Referenced by to_bin32(), to_bin64(), to_hex32(), to_hex64(), to_oct32(), and to_oct64().

◆ cstring_to_text()

text * cstring_to_text ( const char s)

Definition at line 181 of file varlena.c.

182{
183 return cstring_to_text_with_len(s, strlen(s));
184}

References cstring_to_text_with_len(), and fb().

Referenced by array_dims(), ASN1_STRING_to_text(), booltext(), brin_minmax_multi_summary_out(), brin_page_type(), build_test_info_result(), build_test_match_result(), casefold(), cidr_abbrev(), convert_charset(), cstring_to_xmltype(), current_query(), cvt_name_text(), datetime_to_char_body(), dblink_build_sql_delete(), dblink_build_sql_insert(), dblink_build_sql_update(), dblink_cancel_query(), dblink_close(), dblink_connect(), dblink_disconnect(), dblink_error_message(), dblink_exec(), dblink_open(), dmetaphone(), dmetaphone_alt(), exec_assign_c_string(), executeDateTimeMethod(), filter_list_to_array(), format_type(), get_command_tag(), get_command_type(), get_scalar(), get_val_in_hash(), hash_page_type(), icu_unicode_version(), inet_abbrev(), initcap(), injection_points_list(), json_in(), json_typeof(), jsonb_get_element(), jsonb_typeof(), JsonbValueAsText(), lower(), md5_bytea(), md5_text(), metaphone(), name_bpchar(), name_text(), network_host(), network_show(), oidvectortypes(), ParameterAclCreate(), ParameterAclLookup(), pg_collation_actual_version(), pg_collation_for(), pg_column_compression(), pg_crypt(), pg_current_logfile(), pg_database_collation_actual_version(), pg_describe_object(), pg_export_snapshot(), pg_get_statisticsobjdef_expressions(), pg_get_wal_replay_pause_state(), pg_parameter_aclmask(), pg_relation_filepath(), pg_size_pretty(), pg_size_pretty_numeric(), pg_stat_get_backend_activity(), pg_stat_get_backend_wait_event(), pg_stat_get_backend_wait_event_type(), pg_stat_get_slru(), pg_stats_ext_mcvlist_items(), pg_tablespace_location(), pg_walfile_name(), pg_xact_status(), pgsql_version(), pgxml_result_to_text(), quote_ident(), quote_ident_cstr(), quote_nullable(), sepgsql_getcon(), sepgsql_mcstrans_in(), sepgsql_mcstrans_out(), set_config_by_name(), show_all_file_settings(), show_config_by_name(), show_config_by_name_missing_ok(), ShowAllGUCConfig(), soundex(), split_part(), ssl_cipher(), ssl_client_dn(), ssl_issuer_dn(), ssl_version(), string_to_text(), test_canonicalize_path(), test_custom_stats_var_report(), test_slru_page_read(), test_slru_page_readonly(), text_substring(), textin(), timeofday(), timetz_at_local(), tsquerytree(), unaccent_dict(), unicode_version(), upper(), xml_encode_special_chars(), xml_in(), and xml_recv().

◆ cstring_to_text_with_len()

text * cstring_to_text_with_len ( const char s,
int  len 
)

Definition at line 193 of file varlena.c.

194{
195 text *result = (text *) palloc(len + VARHDRSZ);
196
197 SET_VARSIZE(result, len + VARHDRSZ);
198 memcpy(VARDATA(result), s, len);
199
200 return result;
201}
#define VARHDRSZ
Definition c.h:711
void * palloc(Size size)
Definition mcxt.c:1387
const void size_t len
static char * VARDATA(const void *PTR)
Definition varatt.h:305
static void SET_VARSIZE(void *PTR, Size len)
Definition varatt.h:432

References fb(), len, palloc(), SET_VARSIZE(), VARDATA(), and VARHDRSZ.

Referenced by array_to_json(), array_to_json_pretty(), array_to_text_internal(), brin_minmax_multi_summary_out(), build_regexp_match_result(), build_regexp_split_result(), build_test_match_result(), cash_words(), compute_tsvector_stats(), concat_internal(), convert_to_base(), cstring_to_text(), daitch_mokotoff_coding(), datum_to_json(), do_text_output_multiline(), dotrim(), each_object_field_end(), each_worker_jsonb(), elements_array_element_end(), ExecEvalMergeSupportFunc(), ExecEvalXmlExpr(), executeDateTimeMethod(), executeLikeRegex(), find_or_create_child_node(), fsm_page_contents(), get_array_element_end(), get_array_end(), get_object_end(), get_object_field_end(), get_scalar(), gin_extract_tsquery(), gin_extract_tsvector(), hstore_akeys(), hstore_avals(), hstore_each(), hstore_fetchval(), hstore_skeys(), hstore_slice_to_array(), hstore_subscript_fetch(), hstore_svals(), hstore_to_array_internal(), hstore_to_json(), hstore_to_json_loose(), json_build_array_noargs(), json_build_array_worker(), json_build_object_noargs(), json_build_object_worker(), json_object(), json_object_two_arg(), json_recv(), json_strip_nulls(), jsonb_pretty(), JsonbValueAsText(), leftmostvalue_text(), LogicalOutputWrite(), parse_ident(), pg_gen_salt(), pg_gen_salt_rounds(), pg_get_function_sqlbody(), replace_text(), replace_text_regexp(), row_to_json(), row_to_json_pretty(), serialize_deflist(), split_part(), split_text(), ssl_extension_info(), string_agg_finalfn(), stringinfo_to_xmltype(), text_format(), text_left(), text_right(), textrecv(), transform_json_string_values(), tsquerytree(), tsvector_to_array(), tsvector_unnest(), unistr(), varchar(), varchar_input(), xmltext(), xmltotext_with_options(), and xslt_process().

◆ getClosestMatch()

const char * getClosestMatch ( ClosestMatchState state)

Definition at line 5325 of file varlena.c.

5326{
5327 Assert(state);
5328
5329 return state->match;
5330}

References Assert.

Referenced by dblink_fdw_validator(), file_fdw_validator(), postgres_fdw_validator(), and postgresql_fdw_validator().

◆ hexval()

static unsigned int hexval ( unsigned char  c)
static

Definition at line 5555 of file varlena.c.

5556{
5557 if (c >= '0' && c <= '9')
5558 return c - '0';
5559 if (c >= 'a' && c <= 'f')
5560 return c - 'a' + 0xA;
5561 if (c >= 'A' && c <= 'F')
5562 return c - 'A' + 0xA;
5563 elog(ERROR, "invalid hexadecimal digit");
5564 return 0; /* not reached */
5565}
char * c

References elog, and ERROR.

Referenced by hexval_n().

◆ hexval_n()

static unsigned int hexval_n ( const char instr,
size_t  n 
)
static

Definition at line 5571 of file varlena.c.

5572{
5573 unsigned int result = 0;
5574
5575 for (size_t i = 0; i < n; i++)
5576 result += hexval(instr[i]) << (4 * (n - i - 1));
5577
5578 return result;
5579}
static unsigned int hexval(unsigned char c)
Definition varlena.c:5555

References hexval(), and i.

Referenced by unistr().

◆ icu_unicode_version()

Datum icu_unicode_version ( PG_FUNCTION_ARGS  )

Definition at line 5384 of file varlena.c.

5385{
5386 const char *version = pg_icu_unicode_version();
5387
5388 if (version)
5390 else
5392}
const char * pg_icu_unicode_version()
Definition pg_locale.c:1653
text * cstring_to_text(const char *s)
Definition varlena.c:181

References cstring_to_text(), pg_icu_unicode_version(), PG_RETURN_NULL, and PG_RETURN_TEXT_P.

Referenced by main().

◆ initClosestMatch()

void initClosestMatch ( ClosestMatchState state,
const char source,
int  max_d 
)

Definition at line 5270 of file varlena.c.

5271{
5272 Assert(state);
5273 Assert(max_d >= 0);
5274
5275 state->source = source;
5276 state->min_d = -1;
5277 state->max_d = max_d;
5278 state->match = NULL;
5279}
static rewind_source * source
Definition pg_rewind.c:89

References Assert, fb(), and source.

Referenced by dblink_fdw_validator(), file_fdw_validator(), postgres_fdw_validator(), and postgresql_fdw_validator().

◆ internal_text_pattern_compare()

static int internal_text_pattern_compare ( text arg1,
text arg2 
)
static

Definition at line 2510 of file varlena.c.

2511{
2512 int result;
2513 int len1,
2514 len2;
2515
2516 len1 = VARSIZE_ANY_EXHDR(arg1);
2517 len2 = VARSIZE_ANY_EXHDR(arg2);
2518
2519 result = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
2520 if (result != 0)
2521 return result;
2522 else if (len1 < len2)
2523 return -1;
2524 else if (len1 > len2)
2525 return 1;
2526 else
2527 return 0;
2528}

References fb(), Min, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

Referenced by bttext_pattern_cmp(), text_pattern_ge(), text_pattern_gt(), text_pattern_le(), and text_pattern_lt().

◆ isxdigits_n()

static bool isxdigits_n ( const char instr,
size_t  n 
)
static

Definition at line 5545 of file varlena.c.

5546{
5547 for (size_t i = 0; i < n; i++)
5548 if (!isxdigit((unsigned char) instr[i]))
5549 return false;
5550
5551 return true;
5552}

References fb(), and i.

Referenced by unistr().

◆ makeStringAggState()

static StringInfo makeStringAggState ( FunctionCallInfo  fcinfo)
static

Definition at line 4244 of file varlena.c.

4245{
4247 MemoryContext aggcontext;
4248 MemoryContext oldcontext;
4249
4250 if (!AggCheckCallContext(fcinfo, &aggcontext))
4251 {
4252 /* cannot be called directly because of internal-type argument */
4253 elog(ERROR, "string_agg_transfn called in non-aggregate context");
4254 }
4255
4256 /*
4257 * Create state in aggregate context. It'll stay there across subsequent
4258 * calls.
4259 */
4260 oldcontext = MemoryContextSwitchTo(aggcontext);
4262 MemoryContextSwitchTo(oldcontext);
4263
4264 return state;
4265}
int AggCheckCallContext(FunctionCallInfo fcinfo, MemoryContext *aggcontext)
Definition nodeAgg.c:4607
StringInfo makeStringInfo(void)
Definition stringinfo.c:72

References AggCheckCallContext(), elog, ERROR, makeStringInfo(), and MemoryContextSwitchTo().

Referenced by string_agg_combine(), string_agg_deserialize(), and string_agg_transfn().

◆ name_text()

Datum name_text ( PG_FUNCTION_ARGS  )

Definition at line 2655 of file varlena.c.

2656{
2657 Name s = PG_GETARG_NAME(0);
2658
2660}

References cstring_to_text(), NameStr, PG_GETARG_NAME, and PG_RETURN_TEXT_P.

Referenced by nameiclike(), and nameicnlike().

◆ nameeqtext()

Datum nameeqtext ( PG_FUNCTION_ARGS  )

Definition at line 2313 of file varlena.c.

2314{
2317 size_t len1 = strlen(NameStr(*arg1));
2318 size_t len2 = VARSIZE_ANY_EXHDR(arg2);
2320 bool result;
2321
2323
2324 if (collid == C_COLLATION_OID)
2325 result = (len1 == len2 &&
2326 memcmp(NameStr(*arg1), VARDATA_ANY(arg2), len1) == 0);
2327 else
2328 result = (varstr_cmp(NameStr(*arg1), len1,
2329 VARDATA_ANY(arg2), len2,
2330 collid) == 0);
2331
2333
2334 PG_RETURN_BOOL(result);
2335}

References check_collation_set(), collid, fb(), NameStr, PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_NAME, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, VARDATA_ANY(), VARSIZE_ANY_EXHDR(), and varstr_cmp().

◆ namefastcmp_c()

static int namefastcmp_c ( Datum  x,
Datum  y,
SortSupport  ssup 
)
static

Definition at line 1830 of file varlena.c.

1831{
1834
1836}
#define NAMEDATALEN
static Name DatumGetName(Datum X)
Definition postgres.h:390

References DatumGetName(), fb(), NAMEDATALEN, NameStr, x, and y.

Referenced by varstr_sortsupport().

◆ namefastcmp_locale()

static int namefastcmp_locale ( Datum  x,
Datum  y,
SortSupport  ssup 
)
static

Definition at line 1873 of file varlena.c.

1874{
1877
1880 ssup);
1881}
static int varstrfastcmp_locale(char *a1p, int len1, char *a2p, int len2, SortSupport ssup)
Definition varlena.c:1887

References DatumGetName(), fb(), NameStr, varstrfastcmp_locale(), x, and y.

Referenced by varstr_sortsupport().

◆ namegetext()

Datum namegetext ( PG_FUNCTION_ARGS  )

Definition at line 2469 of file varlena.c.

2470{
2472}
#define CmpCall(cmpfunc)
Definition varlena.c:2444
Datum btnametextcmp(PG_FUNCTION_ARGS)
Definition varlena.c:2413

References btnametextcmp(), CmpCall, and PG_RETURN_BOOL.

◆ namegttext()

Datum namegttext ( PG_FUNCTION_ARGS  )

Definition at line 2463 of file varlena.c.

2464{
2466}

References btnametextcmp(), CmpCall, and PG_RETURN_BOOL.

◆ nameletext()

Datum nameletext ( PG_FUNCTION_ARGS  )

Definition at line 2457 of file varlena.c.

2458{
2460}

References btnametextcmp(), CmpCall, and PG_RETURN_BOOL.

◆ namelttext()

Datum namelttext ( PG_FUNCTION_ARGS  )

Definition at line 2451 of file varlena.c.

2452{
2454}

References btnametextcmp(), CmpCall, and PG_RETURN_BOOL.

◆ namenetext()

Datum namenetext ( PG_FUNCTION_ARGS  )

Definition at line 2363 of file varlena.c.

2364{
2367 size_t len1 = strlen(NameStr(*arg1));
2368 size_t len2 = VARSIZE_ANY_EXHDR(arg2);
2370 bool result;
2371
2373
2374 if (collid == C_COLLATION_OID)
2375 result = !(len1 == len2 &&
2376 memcmp(NameStr(*arg1), VARDATA_ANY(arg2), len1) == 0);
2377 else
2378 result = !(varstr_cmp(NameStr(*arg1), len1,
2379 VARDATA_ANY(arg2), len2,
2380 collid) == 0);
2381
2383
2384 PG_RETURN_BOOL(result);
2385}

References check_collation_set(), collid, fb(), NameStr, PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_NAME, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, VARDATA_ANY(), VARSIZE_ANY_EXHDR(), and varstr_cmp().

◆ pg_column_compression()

Datum pg_column_compression ( PG_FUNCTION_ARGS  )

Definition at line 4143 of file varlena.c.

4144{
4145 int typlen;
4146 char *result;
4148
4149 /* On first call, get the input type's typlen, and save at *fn_extra */
4150 if (fcinfo->flinfo->fn_extra == NULL)
4151 {
4152 /* Lookup the datatype of the supplied argument */
4153 Oid argtypeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
4154
4155 typlen = get_typlen(argtypeid);
4156 if (typlen == 0) /* should not happen */
4157 elog(ERROR, "cache lookup failed for type %u", argtypeid);
4158
4159 fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
4160 sizeof(int));
4161 *((int *) fcinfo->flinfo->fn_extra) = typlen;
4162 }
4163 else
4164 typlen = *((int *) fcinfo->flinfo->fn_extra);
4165
4166 if (typlen != -1)
4168
4169 /* get the compression method id stored in the compressed varlena */
4174
4175 /* convert compression method id to compression method name */
4176 switch (cmid)
4177 {
4179 result = "pglz";
4180 break;
4182 result = "lz4";
4183 break;
4184 default:
4185 elog(ERROR, "invalid compression method id %d", cmid);
4186 }
4187
4189}
int16 get_typlen(Oid typid)
Definition lsyscache.c:2347
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:342
ToastCompressionId toast_get_compression_id(struct varlena *attr)
ToastCompressionId
@ TOAST_INVALID_COMPRESSION_ID
@ TOAST_LZ4_COMPRESSION_ID
@ TOAST_PGLZ_COMPRESSION_ID

References cstring_to_text(), DatumGetPointer(), elog, ERROR, fb(), get_fn_expr_argtype(), get_typlen(), MemoryContextAlloc(), PG_GETARG_DATUM, PG_RETURN_NULL, PG_RETURN_TEXT_P, toast_get_compression_id(), TOAST_INVALID_COMPRESSION_ID, TOAST_LZ4_COMPRESSION_ID, and TOAST_PGLZ_COMPRESSION_ID.

◆ pg_column_size()

Datum pg_column_size ( PG_FUNCTION_ARGS  )

Definition at line 4096 of file varlena.c.

4097{
4099 int32 result;
4100 int typlen;
4101
4102 /* On first call, get the input type's typlen, and save at *fn_extra */
4103 if (fcinfo->flinfo->fn_extra == NULL)
4104 {
4105 /* Lookup the datatype of the supplied argument */
4106 Oid argtypeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
4107
4108 typlen = get_typlen(argtypeid);
4109 if (typlen == 0) /* should not happen */
4110 elog(ERROR, "cache lookup failed for type %u", argtypeid);
4111
4112 fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
4113 sizeof(int));
4114 *((int *) fcinfo->flinfo->fn_extra) = typlen;
4115 }
4116 else
4117 typlen = *((int *) fcinfo->flinfo->fn_extra);
4118
4119 if (typlen == -1)
4120 {
4121 /* varlena type, possibly toasted */
4122 result = toast_datum_size(value);
4123 }
4124 else if (typlen == -2)
4125 {
4126 /* cstring */
4127 result = strlen(DatumGetCString(value)) + 1;
4128 }
4129 else
4130 {
4131 /* ordinary fixed-width type */
4132 result = typlen;
4133 }
4134
4135 PG_RETURN_INT32(result);
4136}
Size toast_datum_size(Datum value)
Definition detoast.c:601
static char * DatumGetCString(Datum X)
Definition postgres.h:365

References DatumGetCString(), elog, ERROR, fb(), get_fn_expr_argtype(), get_typlen(), MemoryContextAlloc(), PG_GETARG_DATUM, PG_RETURN_INT32, toast_datum_size(), and value.

◆ pg_column_toast_chunk_id()

Datum pg_column_toast_chunk_id ( PG_FUNCTION_ARGS  )

Definition at line 4196 of file varlena.c.

4197{
4198 int typlen;
4199 struct varlena *attr;
4200 struct varatt_external toast_pointer;
4201
4202 /* On first call, get the input type's typlen, and save at *fn_extra */
4203 if (fcinfo->flinfo->fn_extra == NULL)
4204 {
4205 /* Lookup the datatype of the supplied argument */
4206 Oid argtypeid = get_fn_expr_argtype(fcinfo->flinfo, 0);
4207
4208 typlen = get_typlen(argtypeid);
4209 if (typlen == 0) /* should not happen */
4210 elog(ERROR, "cache lookup failed for type %u", argtypeid);
4211
4212 fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
4213 sizeof(int));
4214 *((int *) fcinfo->flinfo->fn_extra) = typlen;
4215 }
4216 else
4217 typlen = *((int *) fcinfo->flinfo->fn_extra);
4218
4219 if (typlen != -1)
4221
4222 attr = (struct varlena *) DatumGetPointer(PG_GETARG_DATUM(0));
4223
4224 if (!VARATT_IS_EXTERNAL_ONDISK(attr))
4226
4227 VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr);
4228
4229 PG_RETURN_OID(toast_pointer.va_valueid);
4230}
#define VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr)
Definition detoast.h:22
#define PG_RETURN_OID(x)
Definition fmgr.h:361
static bool VARATT_IS_EXTERNAL_ONDISK(const void *PTR)
Definition varatt.h:361

References DatumGetPointer(), elog, ERROR, fb(), get_fn_expr_argtype(), get_typlen(), MemoryContextAlloc(), PG_GETARG_DATUM, PG_RETURN_NULL, PG_RETURN_OID, varatt_external::va_valueid, VARATT_EXTERNAL_GET_POINTER, and VARATT_IS_EXTERNAL_ONDISK().

◆ replace_text()

Datum replace_text ( PG_FUNCTION_ARGS  )

Definition at line 3078 of file varlena.c.

3079{
3083 int src_text_len;
3086 text *ret_text;
3087 int chunk_len;
3088 char *curr_ptr;
3089 char *start_ptr;
3091 bool found;
3092
3095
3096 /* Return unmodified source string if empty source or pattern */
3097 if (src_text_len < 1 || from_sub_text_len < 1)
3098 {
3100 }
3101
3103
3104 found = text_position_next(&state);
3105
3106 /* When the from_sub_text is not found, there is nothing to do. */
3107 if (!found)
3108 {
3111 }
3114
3116
3117 do
3118 {
3120
3121 /* copy the data skipped over by last text_position_next() */
3124
3126
3127 start_ptr = curr_ptr + state.last_match_len;
3128
3129 found = text_position_next(&state);
3130 if (found)
3132 }
3133 while (found);
3134
3135 /* copy trailing data */
3138
3140
3142 pfree(str.data);
3143
3145}
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:123
static Size VARSIZE_ANY(const void *PTR)
Definition varatt.h:460
static void text_position_setup(text *t1, text *t2, Oid collid, TextPositionState *state)
Definition varlena.c:917
static void text_position_cleanup(TextPositionState *state)
Definition varlena.c:1272
static void appendStringInfoText(StringInfo str, const text *t)
Definition varlena.c:3064
static char * text_position_get_match_ptr(TextPositionState *state)
Definition varlena.c:1237
static bool text_position_next(TextPositionState *state)
Definition varlena.c:1038

References appendBinaryStringInfo(), appendStringInfoText(), CHECK_FOR_INTERRUPTS, cstring_to_text_with_len(), fb(), initStringInfo(), pfree(), PG_GET_COLLATION, PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, str, text_position_cleanup(), text_position_get_match_ptr(), text_position_next(), text_position_setup(), VARDATA_ANY(), VARSIZE_ANY(), and VARSIZE_ANY_EXHDR().

Referenced by appendStringInfoRegexpSubstr(), check_replace_text_has_escape(), execute_extension_script(), and replace_text_regexp().

◆ replace_text_regexp()

text * replace_text_regexp ( text src_text,
text pattern_text,
text replace_text,
int  cflags,
Oid  collation,
int  search_start,
int  n 
)

Definition at line 3288 of file varlena.c.

3292{
3293 text *ret_text;
3294 regex_t *re;
3296 int nmatches = 0;
3298 regmatch_t pmatch[10]; /* main match, plus \1 to \9 */
3299 int nmatch = lengthof(pmatch);
3300 pg_wchar *data;
3301 size_t data_len;
3302 int data_pos;
3303 char *start_ptr;
3304 int escape_status;
3305
3307
3308 /* Convert data string to wide characters. */
3309 data = (pg_wchar *) palloc((src_text_len + 1) * sizeof(pg_wchar));
3311
3312 /* Check whether replace_text has escapes, especially regexp submatches. */
3314
3315 /* If no regexp submatches, we can use REG_NOSUB. */
3316 if (escape_status < 2)
3317 {
3318 cflags |= REG_NOSUB;
3319 /* Also tell pg_regexec we only want the whole-match location. */
3320 nmatch = 1;
3321 }
3322
3323 /* Prepare the regexp. */
3324 re = RE_compile_and_cache(pattern_text, cflags, collation);
3325
3326 /* start_ptr points to the data_pos'th character of src_text */
3327 start_ptr = (char *) VARDATA_ANY(src_text);
3328 data_pos = 0;
3329
3330 while (search_start <= data_len)
3331 {
3332 int regexec_result;
3333
3335
3337 data,
3338 data_len,
3339 search_start,
3340 NULL, /* no details */
3341 nmatch,
3342 pmatch,
3343 0);
3344
3346 break;
3347
3348 if (regexec_result != REG_OKAY)
3349 {
3350 char errMsg[100];
3351
3352 pg_regerror(regexec_result, re, errMsg, sizeof(errMsg));
3353 ereport(ERROR,
3355 errmsg("regular expression failed: %s", errMsg)));
3356 }
3357
3358 /*
3359 * Count matches, and decide whether to replace this match.
3360 */
3361 nmatches++;
3362 if (n > 0 && nmatches != n)
3363 {
3364 /*
3365 * No, so advance search_start, but not start_ptr/data_pos. (Thus,
3366 * we treat the matched text as if it weren't matched, and copy it
3367 * to the output later.)
3368 */
3369 search_start = pmatch[0].rm_eo;
3370 if (pmatch[0].rm_so == pmatch[0].rm_eo)
3371 search_start++;
3372 continue;
3373 }
3374
3375 /*
3376 * Copy the text to the left of the match position. Note we are given
3377 * character not byte indexes.
3378 */
3379 if (pmatch[0].rm_so - data_pos > 0)
3380 {
3381 int chunk_len;
3382
3384 pmatch[0].rm_so - data_pos);
3386
3387 /*
3388 * Advance start_ptr over that text, to avoid multiple rescans of
3389 * it if the replace_text contains multiple back-references.
3390 */
3392 data_pos = pmatch[0].rm_so;
3393 }
3394
3395 /*
3396 * Copy the replace_text, processing escapes if any are present.
3397 */
3398 if (escape_status > 0)
3401 else
3403
3404 /* Advance start_ptr and data_pos over the matched text. */
3406 pmatch[0].rm_eo - data_pos);
3407 data_pos = pmatch[0].rm_eo;
3408
3409 /*
3410 * If we only want to replace one occurrence, we're done.
3411 */
3412 if (n > 0)
3413 break;
3414
3415 /*
3416 * Advance search position. Normally we start the next search at the
3417 * end of the previous match; but if the match was of zero length, we
3418 * have to advance by one character, or we'd just find the same match
3419 * again.
3420 */
3421 search_start = data_pos;
3422 if (pmatch[0].rm_so == pmatch[0].rm_eo)
3423 search_start++;
3424 }
3425
3426 /*
3427 * Copy the text to the right of the last match.
3428 */
3429 if (data_pos < data_len)
3430 {
3431 int chunk_len;
3432
3435 }
3436
3438 pfree(buf.data);
3439 pfree(data);
3440
3441 return ret_text;
3442}
#define lengthof(array)
Definition c.h:803
unsigned int pg_wchar
Definition mbprint.c:31
int pg_mb2wchar_with_len(const char *from, pg_wchar *to, int len)
Definition mbutils.c:989
const void * data
size_t pg_regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
Definition regerror.c:60
#define REG_NOMATCH
Definition regex.h:216
#define regmatch_t
Definition regex.h:246
#define REG_OKAY
Definition regex.h:215
#define REG_NOSUB
Definition regex.h:185
#define regex_t
Definition regex.h:245
int pg_regexec(regex_t *re, const chr *string, size_t len, size_t search_start, rm_detail_t *details, size_t nmatch, regmatch_t pmatch[], int flags)
Definition regexec.c:185
regex_t * RE_compile_and_cache(text *text_re, int cflags, Oid collation)
Definition regexp.c:141
static int check_replace_text_has_escape(const text *replace_text)
Definition varlena.c:3155
static void appendStringInfoRegexpSubstr(StringInfo str, text *replace_text, regmatch_t *pmatch, char *start_ptr, int data_pos)
Definition varlena.c:3188

References appendBinaryStringInfo(), appendStringInfoRegexpSubstr(), appendStringInfoText(), buf, charlen_to_bytelen(), CHECK_FOR_INTERRUPTS, check_replace_text_has_escape(), cstring_to_text_with_len(), data, ereport, errcode(), errmsg(), ERROR, fb(), initStringInfo(), lengthof, palloc(), pfree(), pg_mb2wchar_with_len(), pg_regerror(), pg_regexec(), RE_compile_and_cache(), REG_NOMATCH, REG_NOSUB, REG_OKAY, regex_t, regmatch_t, replace_text(), VARDATA_ANY(), VARSIZE_ANY(), and VARSIZE_ANY_EXHDR().

Referenced by textregexreplace(), textregexreplace_extended(), and textregexreplace_noopt().

◆ rest_of_char_same()

static bool rest_of_char_same ( const char s1,
const char s2,
int  len 
)
inlinestatic

Definition at line 5234 of file varlena.c.

5235{
5236 while (len > 0)
5237 {
5238 len--;
5239 if (s1[len] != s2[len])
5240 return false;
5241 }
5242 return true;
5243}
char * s1
char * s2

References len, s1, and s2.

Referenced by varstr_levenshtein().

◆ split_part()

Datum split_part ( PG_FUNCTION_ARGS  )

Definition at line 3450 of file varlena.c.

3451{
3454 int fldnum = PG_GETARG_INT32(2);
3455 int inputstring_len;
3456 int fldsep_len;
3458 char *start_ptr;
3459 char *end_ptr;
3461 bool found;
3462
3463 /* field number is 1 based */
3464 if (fldnum == 0)
3465 ereport(ERROR,
3467 errmsg("field position must not be zero")));
3468
3471
3472 /* return empty string for empty input string */
3473 if (inputstring_len < 1)
3475
3476 /* handle empty field separator */
3477 if (fldsep_len < 1)
3478 {
3479 /* if first or last field, return input string, else empty string */
3480 if (fldnum == 1 || fldnum == -1)
3482 else
3484 }
3485
3486 /* find the first field separator */
3488
3489 found = text_position_next(&state);
3490
3491 /* special case if fldsep not found at all */
3492 if (!found)
3493 {
3495 /* if first or last field, return input string, else empty string */
3496 if (fldnum == 1 || fldnum == -1)
3498 else
3500 }
3501
3502 /*
3503 * take care of a negative field number (i.e. count from the right) by
3504 * converting to a positive field number; we need total number of fields
3505 */
3506 if (fldnum < 0)
3507 {
3508 /* we found a fldsep, so there are at least two fields */
3509 int numfields = 2;
3510
3511 while (text_position_next(&state))
3512 numfields++;
3513
3514 /* special case of last field does not require an extra pass */
3515 if (fldnum == -1)
3516 {
3517 start_ptr = text_position_get_match_ptr(&state) + state.last_match_len;
3521 end_ptr - start_ptr));
3522 }
3523
3524 /* else, convert fldnum to positive notation */
3525 fldnum += numfields + 1;
3526
3527 /* if nonexistent field, return empty string */
3528 if (fldnum <= 0)
3529 {
3532 }
3533
3534 /* reset to pointing at first match, but now with positive fldnum */
3536 found = text_position_next(&state);
3537 Assert(found);
3538 }
3539
3540 /* identify bounds of first field */
3543
3544 while (found && --fldnum > 0)
3545 {
3546 /* identify bounds of next field */
3547 start_ptr = end_ptr + state.last_match_len;
3548 found = text_position_next(&state);
3549 if (found)
3551 }
3552
3554
3555 if (fldnum > 0)
3556 {
3557 /* N'th field separator not found */
3558 /* if last field requested, return it, else empty string */
3559 if (fldnum == 1)
3560 {
3562
3565 }
3566 else
3568 }
3569 else
3570 {
3571 /* non-last field requested */
3573 }
3574
3576}
#define PG_GETARG_INT32(n)
Definition fmgr.h:269
static void text_position_reset(TextPositionState *state)
Definition varlena.c:1264

References Assert, cstring_to_text(), cstring_to_text_with_len(), ereport, errcode(), errmsg(), ERROR, fb(), PG_GET_COLLATION, PG_GETARG_INT32, PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, text_position_cleanup(), text_position_get_match_ptr(), text_position_next(), text_position_reset(), text_position_setup(), VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

◆ split_text()

static bool split_text ( FunctionCallInfo  fcinfo,
SplitTextOutputData tstate 
)
static

Definition at line 3673 of file varlena.c.

3674{
3676 text *fldsep;
3678 Oid collation = PG_GET_COLLATION();
3679 int inputstring_len;
3680 int fldsep_len;
3681 char *start_ptr;
3683
3684 /* when input string is NULL, then result is NULL too */
3685 if (PG_ARGISNULL(0))
3686 return false;
3687
3689
3690 /* fldsep can be NULL */
3691 if (!PG_ARGISNULL(1))
3693 else
3694 fldsep = NULL;
3695
3696 /* null_string can be NULL or omitted */
3697 if (PG_NARGS() > 2 && !PG_ARGISNULL(2))
3699 else
3700 null_string = NULL;
3701
3702 if (fldsep != NULL)
3703 {
3704 /*
3705 * Normal case with non-null fldsep. Use the text_position machinery
3706 * to search for occurrences of fldsep.
3707 */
3709
3712
3713 /* return empty set for empty input string */
3714 if (inputstring_len < 1)
3715 return true;
3716
3717 /* empty field separator: return input string as a one-element set */
3718 if (fldsep_len < 1)
3719 {
3721 null_string, collation);
3722 return true;
3723 }
3724
3726
3728
3729 for (;;)
3730 {
3731 bool found;
3732 char *end_ptr;
3733 int chunk_len;
3734
3736
3737 found = text_position_next(&state);
3738 if (!found)
3739 {
3740 /* fetch last field */
3742 end_ptr = NULL; /* not used, but some compilers complain */
3743 }
3744 else
3745 {
3746 /* fetch non-last field */
3749 }
3750
3751 /* build a temp text datum to pass to split_text_accum_result */
3753
3754 /* stash away this field */
3756 null_string, collation);
3757
3759
3760 if (!found)
3761 break;
3762
3763 start_ptr = end_ptr + state.last_match_len;
3764 }
3765
3767 }
3768 else
3769 {
3770 /*
3771 * When fldsep is NULL, each character in the input string becomes a
3772 * separate element in the result set. The separator is effectively
3773 * the space between characters.
3774 */
3776
3778
3779 while (inputstring_len > 0)
3780 {
3782
3784
3785 /* build a temp text datum to pass to split_text_accum_result */
3787
3788 /* stash away this field */
3790 null_string, collation);
3791
3793
3796 }
3797 }
3798
3799 return true;
3800}
static void split_text_accum_result(SplitTextOutputData *tstate, text *field_value, text *null_string, Oid collation)
Definition varlena.c:3809

References CHECK_FOR_INTERRUPTS, cstring_to_text_with_len(), fb(), pfree(), PG_ARGISNULL, PG_GET_COLLATION, PG_GETARG_TEXT_PP, pg_mblen(), PG_NARGS, split_text_accum_result(), text_position_cleanup(), text_position_get_match_ptr(), text_position_next(), text_position_setup(), VARDATA_ANY(), VARSIZE_ANY(), and VARSIZE_ANY_EXHDR().

Referenced by text_to_array(), and text_to_table().

◆ split_text_accum_result()

static void split_text_accum_result ( SplitTextOutputData tstate,
text field_value,
text null_string,
Oid  collation 
)
static

Definition at line 3809 of file varlena.c.

3813{
3814 bool is_null = false;
3815
3816 if (null_string && text_isequal(field_value, null_string, collation))
3817 is_null = true;
3818
3819 if (tstate->tupstore)
3820 {
3821 Datum values[1];
3822 bool nulls[1];
3823
3825 nulls[0] = is_null;
3826
3827 tuplestore_putvalues(tstate->tupstore,
3828 tstate->tupdesc,
3829 values,
3830 nulls);
3831 }
3832 else
3833 {
3834 tstate->astate = accumArrayResult(tstate->astate,
3836 is_null,
3837 TEXTOID,
3839 }
3840}
ArrayBuildState * accumArrayResult(ArrayBuildState *astate, Datum dvalue, bool disnull, Oid element_type, MemoryContext rcontext)
static Datum values[MAXATTR]
Definition bootstrap.c:155
MemoryContext CurrentMemoryContext
Definition mcxt.c:160
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, const Datum *values, const bool *isnull)
Definition tuplestore.c:784
static bool text_isequal(text *txt1, text *txt2, Oid collid)
Definition varlena.c:3582

References accumArrayResult(), CurrentMemoryContext, fb(), PointerGetDatum(), text_isequal(), tuplestore_putvalues(), and values.

Referenced by split_text().

◆ SplitDirectoriesString()

bool SplitDirectoriesString ( char rawstring,
char  separator,
List **  namelist 
)

Definition at line 2857 of file varlena.c.

2859{
2860 char *nextp = rawstring;
2861 bool done = false;
2862
2863 *namelist = NIL;
2864
2865 while (scanner_isspace(*nextp))
2866 nextp++; /* skip leading whitespace */
2867
2868 if (*nextp == '\0')
2869 return true; /* empty string represents empty list */
2870
2871 /* At the top of the loop, we are at start of a new directory. */
2872 do
2873 {
2874 char *curname;
2875 char *endp;
2876
2877 if (*nextp == '"')
2878 {
2879 /* Quoted name --- collapse quote-quote pairs */
2880 curname = nextp + 1;
2881 for (;;)
2882 {
2883 endp = strchr(nextp + 1, '"');
2884 if (endp == NULL)
2885 return false; /* mismatched quotes */
2886 if (endp[1] != '"')
2887 break; /* found end of quoted name */
2888 /* Collapse adjacent quotes into one quote, and look again */
2889 memmove(endp, endp + 1, strlen(endp));
2890 nextp = endp;
2891 }
2892 /* endp now points at the terminating quote */
2893 nextp = endp + 1;
2894 }
2895 else
2896 {
2897 /* Unquoted name --- extends to separator or end of string */
2898 curname = endp = nextp;
2899 while (*nextp && *nextp != separator)
2900 {
2901 /* trailing whitespace should not be included in name */
2902 if (!scanner_isspace(*nextp))
2903 endp = nextp + 1;
2904 nextp++;
2905 }
2906 if (curname == endp)
2907 return false; /* empty unquoted name not allowed */
2908 }
2909
2910 while (scanner_isspace(*nextp))
2911 nextp++; /* skip trailing whitespace */
2912
2913 if (*nextp == separator)
2914 {
2915 nextp++;
2916 while (scanner_isspace(*nextp))
2917 nextp++; /* skip leading whitespace for next */
2918 /* we expect another name, so done remains false */
2919 }
2920 else if (*nextp == '\0')
2921 done = true;
2922 else
2923 return false; /* invalid syntax */
2924
2925 /* Now safe to overwrite separator with a null */
2926 *endp = '\0';
2927
2928 /* Truncate path if it's overlength */
2929 if (strlen(curname) >= MAXPGPATH)
2930 curname[MAXPGPATH - 1] = '\0';
2931
2932 /*
2933 * Finished isolating current name --- add it to list
2934 */
2938
2939 /* Loop back if we didn't reach end of string */
2940 } while (!done);
2941
2942 return true;
2943}
List * lappend(List *list, void *datum)
Definition list.c:339
char * pstrdup(const char *in)
Definition mcxt.c:1781
#define MAXPGPATH
#define NIL
Definition pg_list.h:68
void canonicalize_path(char *path)
Definition path.c:337
bool scanner_isspace(char ch)
Definition scansup.c:105

References canonicalize_path(), fb(), lappend(), MAXPGPATH, NIL, pstrdup(), and scanner_isspace().

Referenced by check_oauth_validator(), load_libraries(), and PostmasterMain().

◆ SplitGUCList()

bool SplitGUCList ( char rawstring,
char  separator,
List **  namelist 
)

Definition at line 2978 of file varlena.c.

2980{
2981 char *nextp = rawstring;
2982 bool done = false;
2983
2984 *namelist = NIL;
2985
2986 while (scanner_isspace(*nextp))
2987 nextp++; /* skip leading whitespace */
2988
2989 if (*nextp == '\0')
2990 return true; /* empty string represents empty list */
2991
2992 /* At the top of the loop, we are at start of a new identifier. */
2993 do
2994 {
2995 char *curname;
2996 char *endp;
2997
2998 if (*nextp == '"')
2999 {
3000 /* Quoted name --- collapse quote-quote pairs */
3001 curname = nextp + 1;
3002 for (;;)
3003 {
3004 endp = strchr(nextp + 1, '"');
3005 if (endp == NULL)
3006 return false; /* mismatched quotes */
3007 if (endp[1] != '"')
3008 break; /* found end of quoted name */
3009 /* Collapse adjacent quotes into one quote, and look again */
3010 memmove(endp, endp + 1, strlen(endp));
3011 nextp = endp;
3012 }
3013 /* endp now points at the terminating quote */
3014 nextp = endp + 1;
3015 }
3016 else
3017 {
3018 /* Unquoted name --- extends to separator or whitespace */
3019 curname = nextp;
3020 while (*nextp && *nextp != separator &&
3022 nextp++;
3023 endp = nextp;
3024 if (curname == nextp)
3025 return false; /* empty unquoted name not allowed */
3026 }
3027
3028 while (scanner_isspace(*nextp))
3029 nextp++; /* skip trailing whitespace */
3030
3031 if (*nextp == separator)
3032 {
3033 nextp++;
3034 while (scanner_isspace(*nextp))
3035 nextp++; /* skip leading whitespace for next */
3036 /* we expect another name, so done remains false */
3037 }
3038 else if (*nextp == '\0')
3039 done = true;
3040 else
3041 return false; /* invalid syntax */
3042
3043 /* Now safe to overwrite separator with a null */
3044 *endp = '\0';
3045
3046 /*
3047 * Finished isolating current name --- add it to list
3048 */
3050
3051 /* Loop back if we didn't reach end of string */
3052 } while (!done);
3053
3054 return true;
3055}

References fb(), lappend(), NIL, and scanner_isspace().

Referenced by check_debug_io_direct(), dumpFunc(), parse_hba_auth_opt(), pg_get_functiondef(), and PostmasterMain().

◆ SplitIdentifierString()

bool SplitIdentifierString ( char rawstring,
char  separator,
List **  namelist 
)

Definition at line 2730 of file varlena.c.

2732{
2733 char *nextp = rawstring;
2734 bool done = false;
2735
2736 *namelist = NIL;
2737
2738 while (scanner_isspace(*nextp))
2739 nextp++; /* skip leading whitespace */
2740
2741 if (*nextp == '\0')
2742 return true; /* empty string represents empty list */
2743
2744 /* At the top of the loop, we are at start of a new identifier. */
2745 do
2746 {
2747 char *curname;
2748 char *endp;
2749
2750 if (*nextp == '"')
2751 {
2752 /* Quoted name --- collapse quote-quote pairs, no downcasing */
2753 curname = nextp + 1;
2754 for (;;)
2755 {
2756 endp = strchr(nextp + 1, '"');
2757 if (endp == NULL)
2758 return false; /* mismatched quotes */
2759 if (endp[1] != '"')
2760 break; /* found end of quoted name */
2761 /* Collapse adjacent quotes into one quote, and look again */
2762 memmove(endp, endp + 1, strlen(endp));
2763 nextp = endp;
2764 }
2765 /* endp now points at the terminating quote */
2766 nextp = endp + 1;
2767 }
2768 else
2769 {
2770 /* Unquoted name --- extends to separator or whitespace */
2771 char *downname;
2772 int len;
2773
2774 curname = nextp;
2775 while (*nextp && *nextp != separator &&
2777 nextp++;
2778 endp = nextp;
2779 if (curname == nextp)
2780 return false; /* empty unquoted name not allowed */
2781
2782 /*
2783 * Downcase the identifier, using same code as main lexer does.
2784 *
2785 * XXX because we want to overwrite the input in-place, we cannot
2786 * support a downcasing transformation that increases the string
2787 * length. This is not a problem given the current implementation
2788 * of downcase_truncate_identifier, but we'll probably have to do
2789 * something about this someday.
2790 */
2791 len = endp - curname;
2794 strncpy(curname, downname, len); /* strncpy is required here */
2795 pfree(downname);
2796 }
2797
2798 while (scanner_isspace(*nextp))
2799 nextp++; /* skip trailing whitespace */
2800
2801 if (*nextp == separator)
2802 {
2803 nextp++;
2804 while (scanner_isspace(*nextp))
2805 nextp++; /* skip leading whitespace for next */
2806 /* we expect another name, so done remains false */
2807 }
2808 else if (*nextp == '\0')
2809 done = true;
2810 else
2811 return false; /* invalid syntax */
2812
2813 /* Now safe to overwrite separator with a null */
2814 *endp = '\0';
2815
2816 /* Truncate name if it's overlength */
2818
2819 /*
2820 * Finished isolating current name --- add it to list
2821 */
2823
2824 /* Loop back if we didn't reach end of string */
2825 } while (!done);
2826
2827 return true;
2828}
void truncate_identifier(char *ident, int len, bool warn)
Definition scansup.c:81
char * downcase_truncate_identifier(const char *ident, int len, bool warn)
Definition scansup.c:38

References Assert, downcase_truncate_identifier(), fb(), lappend(), len, NIL, pfree(), scanner_isspace(), and truncate_identifier().

Referenced by check_createrole_self_grant(), check_datestyle(), check_log_connections(), check_log_destination(), check_restrict_nonsystem_relation_kind(), check_search_path(), check_temp_tablespaces(), check_wal_consistency_checking(), ExtractExtensionList(), parse_extension_control_file(), parse_output_parameters(), parse_publication_options(), plpgsql_extra_checks_check_hook(), PrepareTempTablespaces(), preprocessNamespacePath(), stringToQualifiedNameList(), textToQualifiedNameList(), and validate_sync_standby_slots().

◆ string_agg_combine()

Datum string_agg_combine ( PG_FUNCTION_ARGS  )

Definition at line 4323 of file varlena.c.

4324{
4327 MemoryContext agg_context;
4328
4329 if (!AggCheckCallContext(fcinfo, &agg_context))
4330 elog(ERROR, "aggregate function called in non-aggregate context");
4331
4334
4335 if (state2 == NULL)
4336 {
4337 /*
4338 * NULL state2 is easy, just return state1, which we know is already
4339 * in the agg_context
4340 */
4341 if (state1 == NULL)
4344 }
4345
4346 if (state1 == NULL)
4347 {
4348 /* We must copy state2's data into the agg_context */
4350
4351 old_context = MemoryContextSwitchTo(agg_context);
4352 state1 = makeStringAggState(fcinfo);
4354 state1->cursor = state2->cursor;
4356 }
4357 else if (state2->len > 0)
4358 {
4359 /* Combine ... state1->cursor does not change in this case */
4361 }
4362
4364}
#define PG_RETURN_POINTER(x)
Definition fmgr.h:363
struct StringInfoData * StringInfo
Definition string.h:15
static StringInfo makeStringAggState(FunctionCallInfo fcinfo)
Definition varlena.c:4244

References AggCheckCallContext(), appendBinaryStringInfo(), elog, ERROR, fb(), makeStringAggState(), MemoryContextSwitchTo(), PG_ARGISNULL, PG_GETARG_POINTER, PG_RETURN_NULL, and PG_RETURN_POINTER.

◆ string_agg_deserialize()

Datum string_agg_deserialize ( PG_FUNCTION_ARGS  )

Definition at line 4404 of file varlena.c.

4405{
4406 bytea *sstate;
4407 StringInfo result;
4409 char *data;
4410 int datalen;
4411
4412 /* cannot be called directly because of internal-type argument */
4414
4415 sstate = PG_GETARG_BYTEA_PP(0);
4416
4417 /*
4418 * Initialize a StringInfo so that we can "receive" it using the standard
4419 * recv-function infrastructure.
4420 */
4422 VARSIZE_ANY_EXHDR(sstate));
4423
4424 result = makeStringAggState(fcinfo);
4425
4426 /* cursor */
4427 result->cursor = pq_getmsgint(&buf, 4);
4428
4429 /* data */
4430 datalen = VARSIZE_ANY_EXHDR(sstate) - 4;
4431 data = (char *) pq_getmsgbytes(&buf, datalen);
4432 appendBinaryStringInfo(result, data, datalen);
4433
4434 pq_getmsgend(&buf);
4435
4436 PG_RETURN_POINTER(result);
4437}
#define PG_GETARG_BYTEA_PP(n)
Definition fmgr.h:309
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition pqformat.c:414
void pq_getmsgend(StringInfo msg)
Definition pqformat.c:634
const char * pq_getmsgbytes(StringInfo msg, int datalen)
Definition pqformat.c:507
static void initReadOnlyStringInfo(StringInfo str, char *data, int len)
Definition stringinfo.h:157

References AggCheckCallContext(), appendBinaryStringInfo(), Assert, buf, StringInfoData::cursor, data, fb(), initReadOnlyStringInfo(), makeStringAggState(), PG_GETARG_BYTEA_PP, PG_RETURN_POINTER, pq_getmsgbytes(), pq_getmsgend(), pq_getmsgint(), VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

◆ string_agg_finalfn()

Datum string_agg_finalfn ( PG_FUNCTION_ARGS  )

Definition at line 4440 of file varlena.c.

4441{
4443
4444 /* cannot be called directly because of internal-type argument */
4446
4448
4449 if (state != NULL)
4450 {
4451 /* As per comment in transfn, strip data before the cursor position */
4453 state->len - state->cursor));
4454 }
4455 else
4457}

References AggCheckCallContext(), Assert, cstring_to_text_with_len(), fb(), PG_ARGISNULL, PG_GETARG_POINTER, PG_RETURN_NULL, and PG_RETURN_TEXT_P.

◆ string_agg_serialize()

Datum string_agg_serialize ( PG_FUNCTION_ARGS  )

Definition at line 4373 of file varlena.c.

4374{
4377 bytea *result;
4378
4379 /* cannot be called directly because of internal-type argument */
4381
4383
4385
4386 /* cursor */
4387 pq_sendint(&buf, state->cursor, 4);
4388
4389 /* data */
4390 pq_sendbytes(&buf, state->data, state->len);
4391
4392 result = pq_endtypsend(&buf);
4393
4394 PG_RETURN_BYTEA_P(result);
4395}
#define PG_RETURN_BYTEA_P(x)
Definition fmgr.h:373
void pq_sendbytes(StringInfo buf, const void *data, int datalen)
Definition pqformat.c:126
void pq_begintypsend(StringInfo buf)
Definition pqformat.c:325
bytea * pq_endtypsend(StringInfo buf)
Definition pqformat.c:345
static void pq_sendint(StringInfo buf, uint32 i, int b)
Definition pqformat.h:171

References AggCheckCallContext(), Assert, buf, fb(), PG_GETARG_POINTER, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), pq_sendbytes(), and pq_sendint().

◆ string_agg_transfn()

Datum string_agg_transfn ( PG_FUNCTION_ARGS  )

Definition at line 4268 of file varlena.c.

4269{
4271
4273
4274 /* Append the value unless null, preceding it with the delimiter. */
4275 if (!PG_ARGISNULL(1))
4276 {
4278 bool isfirst = false;
4279
4280 /*
4281 * You might think we can just throw away the first delimiter, however
4282 * we must keep it as we may be a parallel worker doing partial
4283 * aggregation building a state to send to the main process. We need
4284 * to keep the delimiter of every aggregation so that the combine
4285 * function can properly join up the strings of two separately
4286 * partially aggregated results. The first delimiter is only stripped
4287 * off in the final function. To know how much to strip off the front
4288 * of the string, we store the length of the first delimiter in the
4289 * StringInfo's cursor field, which we don't otherwise need here.
4290 */
4291 if (state == NULL)
4292 {
4293 state = makeStringAggState(fcinfo);
4294 isfirst = true;
4295 }
4296
4297 if (!PG_ARGISNULL(2))
4298 {
4299 text *delim = PG_GETARG_TEXT_PP(2);
4300
4302 if (isfirst)
4303 state->cursor = VARSIZE_ANY_EXHDR(delim);
4304 }
4305
4307 }
4308
4309 /*
4310 * The transition type for string_agg() is declared to be "internal",
4311 * which is a pass-by-value type the same size as a pointer.
4312 */
4313 if (state)
4316}

References appendStringInfoText(), fb(), makeStringAggState(), PG_ARGISNULL, PG_GETARG_POINTER, PG_GETARG_TEXT_PP, PG_RETURN_NULL, PG_RETURN_POINTER, value, and VARSIZE_ANY_EXHDR().

◆ text_catenate()

static text * text_catenate ( text t1,
text t2 
)
static

Definition at line 460 of file varlena.c.

461{
462 text *result;
463 int len1,
464 len2,
465 len;
466 char *ptr;
467
468 len1 = VARSIZE_ANY_EXHDR(t1);
469 len2 = VARSIZE_ANY_EXHDR(t2);
470
471 /* paranoia ... probably should throw error instead? */
472 if (len1 < 0)
473 len1 = 0;
474 if (len2 < 0)
475 len2 = 0;
476
477 len = len1 + len2 + VARHDRSZ;
478 result = (text *) palloc(len);
479
480 /* Set size of result string... */
481 SET_VARSIZE(result, len);
482
483 /* Fill data field of result string... */
484 ptr = VARDATA(result);
485 if (len1 > 0)
486 memcpy(ptr, VARDATA_ANY(t1), len1);
487 if (len2 > 0)
488 memcpy(ptr + len1, VARDATA_ANY(t2), len2);
489
490 return result;
491}

References fb(), len, palloc(), SET_VARSIZE(), VARDATA(), VARDATA_ANY(), VARHDRSZ, and VARSIZE_ANY_EXHDR().

Referenced by text_overlay(), and textcat().

◆ text_cmp()

static int text_cmp ( text arg1,
text arg2,
Oid  collid 
)
static

Definition at line 1356 of file varlena.c.

1357{
1358 char *a1p,
1359 *a2p;
1360 int len1,
1361 len2;
1362
1363 a1p = VARDATA_ANY(arg1);
1364 a2p = VARDATA_ANY(arg2);
1365
1366 len1 = VARSIZE_ANY_EXHDR(arg1);
1367 len2 = VARSIZE_ANY_EXHDR(arg2);
1368
1369 return varstr_cmp(a1p, len1, a2p, len2, collid);
1370}

References collid, fb(), VARDATA_ANY(), VARSIZE_ANY_EXHDR(), and varstr_cmp().

Referenced by bttextcmp(), text_ge(), text_gt(), text_larger(), text_le(), text_lt(), text_smaller(), texteq(), and textne().

◆ text_concat()

Datum text_concat ( PG_FUNCTION_ARGS  )

Definition at line 4584 of file varlena.c.

4585{
4586 text *result;
4587
4588 result = concat_internal("", 0, fcinfo);
4589 if (result == NULL)
4591 PG_RETURN_TEXT_P(result);
4592}
static text * concat_internal(const char *sepstr, int argidx, FunctionCallInfo fcinfo)
Definition varlena.c:4504

References concat_internal(), fb(), PG_RETURN_NULL, and PG_RETURN_TEXT_P.

◆ text_concat_ws()

Datum text_concat_ws ( PG_FUNCTION_ARGS  )

Definition at line 4599 of file varlena.c.

4600{
4601 char *sep;
4602 text *result;
4603
4604 /* return NULL when separator is NULL */
4605 if (PG_ARGISNULL(0))
4608
4609 result = concat_internal(sep, 1, fcinfo);
4610 if (result == NULL)
4612 PG_RETURN_TEXT_P(result);
4613}

References concat_internal(), fb(), PG_ARGISNULL, PG_GETARG_TEXT_PP, PG_RETURN_NULL, PG_RETURN_TEXT_P, and text_to_cstring().

◆ text_format()

Datum text_format ( PG_FUNCTION_ARGS  )

Definition at line 4720 of file varlena.c.

4721{
4722 text *fmt;
4724 const char *cp;
4725 const char *start_ptr;
4726 const char *end_ptr;
4727 text *result;
4728 int arg;
4729 bool funcvariadic;
4730 int nargs;
4731 Datum *elements = NULL;
4732 bool *nulls = NULL;
4733 Oid element_type = InvalidOid;
4738
4739 /* When format string is null, immediately return null */
4740 if (PG_ARGISNULL(0))
4742
4743 /* If argument is marked VARIADIC, expand array into elements */
4744 if (get_fn_expr_variadic(fcinfo->flinfo))
4745 {
4746 ArrayType *arr;
4747 int16 elmlen;
4748 bool elmbyval;
4749 char elmalign;
4750 int nitems;
4751
4752 /* Should have just the one argument */
4753 Assert(PG_NARGS() == 2);
4754
4755 /* If argument is NULL, we treat it as zero-length array */
4756 if (PG_ARGISNULL(1))
4757 nitems = 0;
4758 else
4759 {
4760 /*
4761 * Non-null argument had better be an array. We assume that any
4762 * call context that could let get_fn_expr_variadic return true
4763 * will have checked that a VARIADIC-labeled parameter actually is
4764 * an array. So it should be okay to just Assert that it's an
4765 * array rather than doing a full-fledged error check.
4766 */
4768
4769 /* OK, safe to fetch the array value */
4770 arr = PG_GETARG_ARRAYTYPE_P(1);
4771
4772 /* Get info about array element type */
4773 element_type = ARR_ELEMTYPE(arr);
4774 get_typlenbyvalalign(element_type,
4775 &elmlen, &elmbyval, &elmalign);
4776
4777 /* Extract all array elements */
4778 deconstruct_array(arr, element_type, elmlen, elmbyval, elmalign,
4779 &elements, &nulls, &nitems);
4780 }
4781
4782 nargs = nitems + 1;
4783 funcvariadic = true;
4784 }
4785 else
4786 {
4787 /* Non-variadic case, we'll process the arguments individually */
4788 nargs = PG_NARGS();
4789 funcvariadic = false;
4790 }
4791
4792 /* Setup for main loop. */
4797 arg = 1; /* next argument position to print */
4798
4799 /* Scan format string, looking for conversion specifiers. */
4800 for (cp = start_ptr; cp < end_ptr; cp++)
4801 {
4802 int argpos;
4803 int widthpos;
4804 int flags;
4805 int width;
4806 Datum value;
4807 bool isNull;
4808 Oid typid;
4809
4810 /*
4811 * If it's not the start of a conversion specifier, just copy it to
4812 * the output buffer.
4813 */
4814 if (*cp != '%')
4815 {
4817 continue;
4818 }
4819
4821
4822 /* Easy case: %% outputs a single % */
4823 if (*cp == '%')
4824 {
4826 continue;
4827 }
4828
4829 /* Parse the optional portions of the format specifier */
4831 &argpos, &widthpos,
4832 &flags, &width);
4833
4834 /*
4835 * Next we should see the main conversion specifier. Whether or not
4836 * an argument position was present, it's known that at least one
4837 * character remains in the string at this point. Experience suggests
4838 * that it's worth checking that that character is one of the expected
4839 * ones before we try to fetch arguments, so as to produce the least
4840 * confusing response to a mis-formatted specifier.
4841 */
4842 if (strchr("sIL", *cp) == NULL)
4843 ereport(ERROR,
4845 errmsg("unrecognized format() type specifier \"%.*s\"",
4846 pg_mblen(cp), cp),
4847 errhint("For a single \"%%\" use \"%%%%\".")));
4848
4849 /* If indirect width was specified, get its value */
4850 if (widthpos >= 0)
4851 {
4852 /* Collect the specified or next argument position */
4853 if (widthpos > 0)
4854 arg = widthpos;
4855 if (arg >= nargs)
4856 ereport(ERROR,
4858 errmsg("too few arguments for format()")));
4859
4860 /* Get the value and type of the selected argument */
4861 if (!funcvariadic)
4862 {
4864 isNull = PG_ARGISNULL(arg);
4865 typid = get_fn_expr_argtype(fcinfo->flinfo, arg);
4866 }
4867 else
4868 {
4869 value = elements[arg - 1];
4870 isNull = nulls[arg - 1];
4871 typid = element_type;
4872 }
4873 if (!OidIsValid(typid))
4874 elog(ERROR, "could not determine data type of format() input");
4875
4876 arg++;
4877
4878 /* We can treat NULL width the same as zero */
4879 if (isNull)
4880 width = 0;
4881 else if (typid == INT4OID)
4882 width = DatumGetInt32(value);
4883 else if (typid == INT2OID)
4884 width = DatumGetInt16(value);
4885 else
4886 {
4887 /* For less-usual datatypes, convert to text then to int */
4888 char *str;
4889
4890 if (typid != prev_width_type)
4891 {
4893 bool typIsVarlena;
4894
4897 prev_width_type = typid;
4898 }
4899
4901
4902 /* pg_strtoint32 will complain about bad data or overflow */
4903 width = pg_strtoint32(str);
4904
4905 pfree(str);
4906 }
4907 }
4908
4909 /* Collect the specified or next argument position */
4910 if (argpos > 0)
4911 arg = argpos;
4912 if (arg >= nargs)
4913 ereport(ERROR,
4915 errmsg("too few arguments for format()")));
4916
4917 /* Get the value and type of the selected argument */
4918 if (!funcvariadic)
4919 {
4921 isNull = PG_ARGISNULL(arg);
4922 typid = get_fn_expr_argtype(fcinfo->flinfo, arg);
4923 }
4924 else
4925 {
4926 value = elements[arg - 1];
4927 isNull = nulls[arg - 1];
4928 typid = element_type;
4929 }
4930 if (!OidIsValid(typid))
4931 elog(ERROR, "could not determine data type of format() input");
4932
4933 arg++;
4934
4935 /*
4936 * Get the appropriate typOutput function, reusing previous one if
4937 * same type as previous argument. That's particularly useful in the
4938 * variadic-array case, but often saves work even for ordinary calls.
4939 */
4940 if (typid != prev_type)
4941 {
4943 bool typIsVarlena;
4944
4947 prev_type = typid;
4948 }
4949
4950 /*
4951 * And now we can format the value.
4952 */
4953 switch (*cp)
4954 {
4955 case 's':
4956 case 'I':
4957 case 'L':
4959 value, isNull,
4960 flags, width);
4961 break;
4962 default:
4963 /* should not get here, because of previous check */
4964 ereport(ERROR,
4966 errmsg("unrecognized format() type specifier \"%.*s\"",
4967 pg_mblen(cp), cp),
4968 errhint("For a single \"%%\" use \"%%%%\".")));
4969 break;
4970 }
4971 }
4972
4973 /* Don't need deconstruct_array results anymore. */
4974 if (elements != NULL)
4975 pfree(elements);
4976 if (nulls != NULL)
4977 pfree(nulls);
4978
4979 /* Generate results. */
4980 result = cstring_to_text_with_len(str.data, str.len);
4981 pfree(str.data);
4982
4983 PG_RETURN_TEXT_P(result);
4984}
void deconstruct_array(const ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
int16_t int16
Definition c.h:541
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition fmgr.c:128
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
Definition lsyscache.c:2421
int32 pg_strtoint32(const char *s)
Definition numutils.c:382
void * arg
static int16 DatumGetInt16(Datum X)
Definition postgres.h:172
#define InvalidOid
#define appendStringInfoCharMacro(str, ch)
Definition stringinfo.h:231
static void text_format_string_conversion(StringInfo buf, char conversion, FmgrInfo *typOutputInfo, Datum value, bool isNull, int flags, int width)
Definition varlena.c:5123
static const char * text_format_parse_format(const char *start_ptr, const char *end_ptr, int *argpos, int *widthpos, int *flags, int *width)
Definition varlena.c:5046
#define ADVANCE_PARSE_POINTER(ptr, end_ptr)
Definition varlena.c:4707

References ADVANCE_PARSE_POINTER, appendStringInfoCharMacro, arg, ARR_ELEMTYPE, Assert, cstring_to_text_with_len(), DatumGetInt16(), DatumGetInt32(), deconstruct_array(), elog, ereport, errcode(), errhint(), errmsg(), ERROR, fb(), fmgr_info(), get_base_element_type(), get_fn_expr_argtype(), get_fn_expr_variadic(), get_typlenbyvalalign(), getTypeOutputInfo(), initStringInfo(), InvalidOid, nitems, OidIsValid, OutputFunctionCall(), pfree(), PG_ARGISNULL, PG_GETARG_ARRAYTYPE_P, PG_GETARG_DATUM, PG_GETARG_TEXT_PP, pg_mblen(), PG_NARGS, PG_RETURN_NULL, PG_RETURN_TEXT_P, pg_strtoint32(), str, text_format_parse_format(), text_format_string_conversion(), value, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

Referenced by text_format_nv().

◆ text_format_append_string()

static void text_format_append_string ( StringInfo  buf,
const char str,
int  flags,
int  width 
)
static

Definition at line 5172 of file varlena.c.

5174{
5175 bool align_to_left = false;
5176 int len;
5177
5178 /* fast path for typical easy case */
5179 if (width == 0)
5180 {
5182 return;
5183 }
5184
5185 if (width < 0)
5186 {
5187 /* Negative width: implicit '-' flag, then take absolute value */
5188 align_to_left = true;
5189 /* -INT_MIN is undefined */
5190 if (width <= INT_MIN)
5191 ereport(ERROR,
5193 errmsg("number is out of range")));
5194 width = -width;
5195 }
5196 else if (flags & TEXT_FORMAT_FLAG_MINUS)
5197 align_to_left = true;
5198
5199 len = pg_mbstrlen(str);
5200 if (align_to_left)
5201 {
5202 /* left justify */
5204 if (len < width)
5205 appendStringInfoSpaces(buf, width - len);
5206 }
5207 else
5208 {
5209 /* right justify */
5210 if (len < width)
5211 appendStringInfoSpaces(buf, width - len);
5213 }
5214}
int pg_mbstrlen(const char *mbstr)
Definition mbutils.c:1040
void appendStringInfoSpaces(StringInfo str, int count)
Definition stringinfo.c:260
#define TEXT_FORMAT_FLAG_MINUS
Definition varlena.c:4705

References appendStringInfoSpaces(), appendStringInfoString(), buf, ereport, errcode(), errmsg(), ERROR, fb(), len, pg_mbstrlen(), str, and TEXT_FORMAT_FLAG_MINUS.

Referenced by text_format_string_conversion().

◆ text_format_nv()

Datum text_format_nv ( PG_FUNCTION_ARGS  )

Definition at line 5224 of file varlena.c.

5225{
5226 return text_format(fcinfo);
5227}
Datum text_format(PG_FUNCTION_ARGS)
Definition varlena.c:4720

References text_format().

◆ text_format_parse_digits()

static bool text_format_parse_digits ( const char **  ptr,
const char end_ptr,
int value 
)
static

Definition at line 4997 of file varlena.c.

4998{
4999 bool found = false;
5000 const char *cp = *ptr;
5001 int val = 0;
5002
5003 while (*cp >= '0' && *cp <= '9')
5004 {
5005 int8 digit = (*cp - '0');
5006
5007 if (unlikely(pg_mul_s32_overflow(val, 10, &val)) ||
5009 ereport(ERROR,
5011 errmsg("number is out of range")));
5013 found = true;
5014 }
5015
5016 *ptr = cp;
5017 *value = val;
5018
5019 return found;
5020}
int8_t int8
Definition c.h:540
#define unlikely(x)
Definition c.h:412
long val
Definition informix.c:689
static bool pg_mul_s32_overflow(int32 a, int32 b, int32 *result)
Definition int.h:187
static bool pg_add_s32_overflow(int32 a, int32 b, int32 *result)
Definition int.h:151

References ADVANCE_PARSE_POINTER, ereport, errcode(), errmsg(), ERROR, fb(), pg_add_s32_overflow(), pg_mul_s32_overflow(), unlikely, val, and value.

Referenced by text_format_parse_format().

◆ text_format_parse_format()

static const char * text_format_parse_format ( const char start_ptr,
const char end_ptr,
int argpos,
int widthpos,
int flags,
int width 
)
static

Definition at line 5046 of file varlena.c.

5049{
5050 const char *cp = start_ptr;
5051 int n;
5052
5053 /* set defaults for output parameters */
5054 *argpos = -1;
5055 *widthpos = -1;
5056 *flags = 0;
5057 *width = 0;
5058
5059 /* try to identify first number */
5061 {
5062 if (*cp != '$')
5063 {
5064 /* Must be just a width and a type, so we're done */
5065 *width = n;
5066 return cp;
5067 }
5068 /* The number was argument position */
5069 *argpos = n;
5070 /* Explicit 0 for argument index is immediately refused */
5071 if (n == 0)
5072 ereport(ERROR,
5074 errmsg("format specifies argument 0, but arguments are numbered from 1")));
5076 }
5077
5078 /* Handle flags (only minus is supported now) */
5079 while (*cp == '-')
5080 {
5081 *flags |= TEXT_FORMAT_FLAG_MINUS;
5083 }
5084
5085 if (*cp == '*')
5086 {
5087 /* Handle indirect width */
5090 {
5091 /* number in this position must be closed by $ */
5092 if (*cp != '$')
5093 ereport(ERROR,
5095 errmsg("width argument position must be ended by \"$\"")));
5096 /* The number was width argument position */
5097 *widthpos = n;
5098 /* Explicit 0 for argument index is immediately refused */
5099 if (n == 0)
5100 ereport(ERROR,
5102 errmsg("format specifies argument 0, but arguments are numbered from 1")));
5104 }
5105 else
5106 *widthpos = 0; /* width's argument position is unspecified */
5107 }
5108 else
5109 {
5110 /* Check for direct width specification */
5112 *width = n;
5113 }
5114
5115 /* cp should now be pointing at type character */
5116 return cp;
5117}
static bool text_format_parse_digits(const char **ptr, const char *end_ptr, int *value)
Definition varlena.c:4997

References ADVANCE_PARSE_POINTER, ereport, errcode(), errmsg(), ERROR, fb(), TEXT_FORMAT_FLAG_MINUS, and text_format_parse_digits().

Referenced by text_format().

◆ text_format_string_conversion()

static void text_format_string_conversion ( StringInfo  buf,
char  conversion,
FmgrInfo typOutputInfo,
Datum  value,
bool  isNull,
int  flags,
int  width 
)
static

Definition at line 5123 of file varlena.c.

5127{
5128 char *str;
5129
5130 /* Handle NULL arguments before trying to stringify the value. */
5131 if (isNull)
5132 {
5133 if (conversion == 's')
5134 text_format_append_string(buf, "", flags, width);
5135 else if (conversion == 'L')
5136 text_format_append_string(buf, "NULL", flags, width);
5137 else if (conversion == 'I')
5138 ereport(ERROR,
5140 errmsg("null values cannot be formatted as an SQL identifier")));
5141 return;
5142 }
5143
5144 /* Stringify. */
5146
5147 /* Escape. */
5148 if (conversion == 'I')
5149 {
5150 /* quote_identifier may or may not allocate a new string. */
5152 }
5153 else if (conversion == 'L')
5154 {
5155 char *qstr = quote_literal_cstr(str);
5156
5157 text_format_append_string(buf, qstr, flags, width);
5158 /* quote_literal_cstr() always allocates a new string */
5159 pfree(qstr);
5160 }
5161 else
5162 text_format_append_string(buf, str, flags, width);
5163
5164 /* Cleanup. */
5165 pfree(str);
5166}
char * quote_literal_cstr(const char *rawstr)
Definition quote.c:101
const char * quote_identifier(const char *ident)
static void text_format_append_string(StringInfo buf, const char *str, int flags, int width)
Definition varlena.c:5172

References buf, ereport, errcode(), errmsg(), ERROR, fb(), OutputFunctionCall(), pfree(), quote_identifier(), quote_literal_cstr(), str, text_format_append_string(), and value.

Referenced by text_format().

◆ text_ge()

Datum text_ge ( PG_FUNCTION_ARGS  )

Definition at line 1530 of file varlena.c.

1531{
1534 bool result;
1535
1536 result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) >= 0);
1537
1540
1541 PG_RETURN_BOOL(result);
1542}

References fb(), PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, and text_cmp().

Referenced by gbt_textge().

◆ text_gt()

Datum text_gt ( PG_FUNCTION_ARGS  )

Definition at line 1515 of file varlena.c.

1516{
1519 bool result;
1520
1521 result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) > 0);
1522
1525
1526 PG_RETURN_BOOL(result);
1527}

References fb(), PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, and text_cmp().

Referenced by gbt_textgt().

◆ text_isequal()

static bool text_isequal ( text txt1,
text txt2,
Oid  collid 
)
static

Definition at line 3582 of file varlena.c.

3583{
3585 collid,
3588}
static bool DatumGetBool(Datum X)
Definition postgres.h:100
Datum texteq(PG_FUNCTION_ARGS)
Definition varlena.c:1381

References collid, DatumGetBool(), DirectFunctionCall2Coll(), fb(), PointerGetDatum(), and texteq().

Referenced by split_text_accum_result().

◆ text_larger()

Datum text_larger ( PG_FUNCTION_ARGS  )

Definition at line 2284 of file varlena.c.

2285{
2288 text *result;
2289
2290 result = ((text_cmp(arg1, arg2, PG_GET_COLLATION()) > 0) ? arg1 : arg2);
2291
2292 PG_RETURN_TEXT_P(result);
2293}

References fb(), PG_GET_COLLATION, PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, and text_cmp().

◆ text_le()

Datum text_le ( PG_FUNCTION_ARGS  )

Definition at line 1500 of file varlena.c.

1501{
1504 bool result;
1505
1506 result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) <= 0);
1507
1510
1511 PG_RETURN_BOOL(result);
1512}

References fb(), PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, and text_cmp().

Referenced by gbt_textle().

◆ text_left()

Datum text_left ( PG_FUNCTION_ARGS  )

Definition at line 4620 of file varlena.c.

4621{
4622 int n = PG_GETARG_INT32(1);
4623
4624 if (n < 0)
4625 {
4627 const char *p = VARDATA_ANY(str);
4628 int len = VARSIZE_ANY_EXHDR(str);
4629 int rlen;
4630
4631 n = pg_mbstrlen_with_len(p, len) + n;
4632 rlen = pg_mbcharcliplen(p, len, n);
4634 }
4635 else
4637}
int pg_mbstrlen_with_len(const char *mbstr, int limit)
Definition mbutils.c:1060
int pg_mbcharcliplen(const char *mbstr, int len, int limit)
Definition mbutils.c:1128
static text * text_substring(Datum str, int32 start, int32 length, bool length_not_specified)
Definition varlena.c:580

References cstring_to_text_with_len(), fb(), len, PG_GETARG_DATUM, PG_GETARG_INT32, PG_GETARG_TEXT_PP, pg_mbcharcliplen(), pg_mbstrlen_with_len(), PG_RETURN_TEXT_P, str, text_substring(), VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

◆ text_length()

static int32 text_length ( Datum  str)
static

Definition at line 407 of file varlena.c.

408{
409 /* fastpath when max encoding length is one */
412 else
413 {
414 text *t = DatumGetTextPP(str);
415
417 }
418}
Size toast_raw_datum_size(Datum value)
Definition detoast.c:545
#define DatumGetTextPP(X)
Definition fmgr.h:293

References DatumGetTextPP, pg_database_encoding_max_length(), pg_mbstrlen_with_len(), str, toast_raw_datum_size(), VARDATA_ANY(), VARHDRSZ, and VARSIZE_ANY_EXHDR().

Referenced by textlen(), and textoverlay_no_len().

◆ text_lt()

Datum text_lt ( PG_FUNCTION_ARGS  )

Definition at line 1485 of file varlena.c.

1486{
1489 bool result;
1490
1491 result = (text_cmp(arg1, arg2, PG_GET_COLLATION()) < 0);
1492
1495
1496 PG_RETURN_BOOL(result);
1497}

References fb(), PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, and text_cmp().

Referenced by gbt_textlt().

◆ text_name()

Datum text_name ( PG_FUNCTION_ARGS  )

Definition at line 2632 of file varlena.c.

2633{
2634 text *s = PG_GETARG_TEXT_PP(0);
2635 Name result;
2636 int len;
2637
2639
2640 /* Truncate oversize input */
2641 if (len >= NAMEDATALEN)
2643
2644 /* We use palloc0 here to ensure result is zero-padded */
2645 result = (Name) palloc0(NAMEDATALEN);
2646 memcpy(NameStr(*result), VARDATA_ANY(s), len);
2647
2648 PG_RETURN_NAME(result);
2649}
NameData * Name
Definition c.h:763
#define PG_RETURN_NAME(x)
Definition fmgr.h:365
int pg_mbcliplen(const char *mbstr, int len, int limit)
Definition mbutils.c:1086
void * palloc0(Size size)
Definition mcxt.c:1417

References fb(), len, NAMEDATALEN, NameStr, palloc0(), PG_GETARG_TEXT_PP, pg_mbcliplen(), PG_RETURN_NAME, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

◆ text_overlay()

static text * text_overlay ( text t1,
text t2,
int  sp,
int  sl 
)
static

Definition at line 811 of file varlena.c.

812{
813 text *result;
814 text *s1;
815 text *s2;
816 int sp_pl_sl;
817
818 /*
819 * Check for possible integer-overflow cases. For negative sp, throw a
820 * "substring length" error because that's what should be expected
821 * according to the spec's definition of OVERLAY().
822 */
823 if (sp <= 0)
826 errmsg("negative substring length not allowed")));
830 errmsg("integer out of range")));
831
832 s1 = text_substring(PointerGetDatum(t1), 1, sp - 1, false);
834 result = text_catenate(s1, t2);
835 result = text_catenate(result, s2);
836
837 return result;
838}
static text * text_catenate(text *t1, text *t2)
Definition varlena.c:460

References ereport, errcode(), errmsg(), ERROR, fb(), pg_add_s32_overflow(), PointerGetDatum(), s1, s2, text_catenate(), and text_substring().

Referenced by textoverlay(), and textoverlay_no_len().

◆ text_pattern_ge()

Datum text_pattern_ge ( PG_FUNCTION_ARGS  )

Definition at line 2564 of file varlena.c.

2565{
2568 int result;
2569
2571
2574
2575 PG_RETURN_BOOL(result >= 0);
2576}

References fb(), internal_text_pattern_compare(), PG_FREE_IF_COPY, PG_GETARG_TEXT_PP, and PG_RETURN_BOOL.

◆ text_pattern_gt()

Datum text_pattern_gt ( PG_FUNCTION_ARGS  )

Definition at line 2580 of file varlena.c.

2581{
2584 int result;
2585
2587
2590
2591 PG_RETURN_BOOL(result > 0);
2592}

References fb(), internal_text_pattern_compare(), PG_FREE_IF_COPY, PG_GETARG_TEXT_PP, and PG_RETURN_BOOL.

◆ text_pattern_le()

Datum text_pattern_le ( PG_FUNCTION_ARGS  )

Definition at line 2548 of file varlena.c.

2549{
2552 int result;
2553
2555
2558
2559 PG_RETURN_BOOL(result <= 0);
2560}

References fb(), internal_text_pattern_compare(), PG_FREE_IF_COPY, PG_GETARG_TEXT_PP, and PG_RETURN_BOOL.

◆ text_pattern_lt()

Datum text_pattern_lt ( PG_FUNCTION_ARGS  )

Definition at line 2532 of file varlena.c.

2533{
2536 int result;
2537
2539
2542
2543 PG_RETURN_BOOL(result < 0);
2544}

References fb(), internal_text_pattern_compare(), PG_FREE_IF_COPY, PG_GETARG_TEXT_PP, and PG_RETURN_BOOL.

◆ text_position()

static int text_position ( text t1,
text t2,
Oid  collid 
)
static

Definition at line 871 of file varlena.c.

872{
874 int result;
875
877
878 /* Empty needle always matches at position 1 */
879 if (VARSIZE_ANY_EXHDR(t2) < 1)
880 return 1;
881
882 /* Otherwise, can't match if haystack is shorter than needle */
884 pg_newlocale_from_collation(collid)->deterministic)
885 return 0;
886
888 /* don't need greedy mode here */
889 state.greedy = false;
890
892 result = 0;
893 else
896 return result;
897}
static int text_position_get_match_pos(TextPositionState *state)
Definition varlena.c:1248

References check_collation_set(), collid, fb(), pg_newlocale_from_collation(), text_position_cleanup(), text_position_get_match_pos(), text_position_next(), text_position_setup(), and VARSIZE_ANY_EXHDR().

Referenced by textpos().

◆ text_position_cleanup()

static void text_position_cleanup ( TextPositionState state)
static

Definition at line 1272 of file varlena.c.

1273{
1274 /* no cleanup needed */
1275}

Referenced by replace_text(), split_part(), split_text(), and text_position().

◆ text_position_get_match_pos()

static int text_position_get_match_pos ( TextPositionState state)
static

Definition at line 1248 of file varlena.c.

1249{
1250 /* Convert the byte position to char position. */
1251 state->refpos += pg_mbstrlen_with_len(state->refpoint,
1252 state->last_match - state->refpoint);
1253 state->refpoint = state->last_match;
1254 return state->refpos + 1;
1255}

References pg_mbstrlen_with_len().

Referenced by text_position().

◆ text_position_get_match_ptr()

static char * text_position_get_match_ptr ( TextPositionState state)
static

Definition at line 1237 of file varlena.c.

1238{
1239 return state->last_match;
1240}

Referenced by replace_text(), split_part(), and split_text().

◆ text_position_next()

static bool text_position_next ( TextPositionState state)
static

Definition at line 1038 of file varlena.c.

1039{
1040 int needle_len = state->len2;
1041 char *start_ptr;
1042 char *matchptr;
1043
1044 if (needle_len <= 0)
1045 return false; /* result for empty pattern */
1046
1047 /* Start from the point right after the previous match. */
1048 if (state->last_match)
1049 start_ptr = state->last_match + state->last_match_len;
1050 else
1051 start_ptr = state->str1;
1052
1053retry:
1055
1056 if (!matchptr)
1057 return false;
1058
1059 /*
1060 * Found a match for the byte sequence. If this is a multibyte encoding,
1061 * where one character's byte sequence can appear inside a longer
1062 * multi-byte character, we need to verify that the match was at a
1063 * character boundary, not in the middle of a multi-byte character.
1064 */
1065 if (state->is_multibyte_char_in_char && state->locale->deterministic)
1066 {
1067 /* Walk one character at a time, until we reach the match. */
1068
1069 /* the search should never move backwards. */
1070 Assert(state->refpoint <= matchptr);
1071
1072 while (state->refpoint < matchptr)
1073 {
1074 /* step to next character. */
1075 state->refpoint += pg_mblen(state->refpoint);
1076 state->refpos++;
1077
1078 /*
1079 * If we stepped over the match's start position, then it was a
1080 * false positive, where the byte sequence appeared in the middle
1081 * of a multi-byte character. Skip it, and continue the search at
1082 * the next character boundary.
1083 */
1084 if (state->refpoint > matchptr)
1085 {
1086 start_ptr = state->refpoint;
1087 goto retry;
1088 }
1089 }
1090 }
1091
1092 state->last_match = matchptr;
1093 state->last_match_len = state->last_match_len_tmp;
1094 return true;
1095}
static char * text_position_next_internal(char *start_ptr, TextPositionState *state)
Definition varlena.c:1103

References Assert, fb(), pg_mblen(), and text_position_next_internal().

Referenced by replace_text(), split_part(), split_text(), and text_position().

◆ text_position_next_internal()

static char * text_position_next_internal ( char start_ptr,
TextPositionState state 
)
static

Definition at line 1103 of file varlena.c.

1104{
1105 int haystack_len = state->len1;
1106 int needle_len = state->len2;
1107 int skiptablemask = state->skiptablemask;
1108 const char *haystack = state->str1;
1109 const char *needle = state->str2;
1110 const char *haystack_end = &haystack[haystack_len];
1111 const char *hptr;
1112
1114 Assert(needle_len > 0);
1115
1116 state->last_match_len_tmp = needle_len;
1117
1118 if (!state->locale->deterministic)
1119 {
1120 /*
1121 * With a nondeterministic collation, we have to use an unoptimized
1122 * route. We walk through the haystack and see if at each position
1123 * there is a substring of the remaining string that is equal to the
1124 * needle under the given collation.
1125 *
1126 * Note, the found substring could have a different length than the
1127 * needle. Callers that want to skip over the found string need to
1128 * read the length of the found substring from last_match_len rather
1129 * than just using the length of their needle.
1130 *
1131 * Most callers will require "greedy" semantics, meaning that we need
1132 * to find the longest such substring, not the shortest. For callers
1133 * that don't need greedy semantics, we can finish on the first match.
1134 *
1135 * This loop depends on the assumption that the needle is nonempty and
1136 * any matching substring must also be nonempty. (Even if the
1137 * collation would accept an empty match, returning one would send
1138 * callers that search for successive matches into an infinite loop.)
1139 */
1140 const char *result_hptr = NULL;
1141
1142 hptr = start_ptr;
1143 while (hptr < haystack_end)
1144 {
1145 const char *test_end;
1146
1147 /*
1148 * First check the common case that there is a match in the
1149 * haystack of exactly the length of the needle.
1150 */
1151 if (!state->greedy &&
1154 return (char *) hptr;
1155
1156 /*
1157 * Else check if any of the non-empty substrings starting at hptr
1158 * compare equal to the needle.
1159 */
1160 test_end = hptr;
1161 do
1162 {
1164 if (pg_strncoll(hptr, (test_end - hptr), needle, needle_len, state->locale) == 0)
1165 {
1166 state->last_match_len_tmp = (test_end - hptr);
1167 result_hptr = hptr;
1168 if (!state->greedy)
1169 break;
1170 }
1171 } while (test_end < haystack_end);
1172
1173 if (result_hptr)
1174 break;
1175
1176 hptr += pg_mblen(hptr);
1177 }
1178
1179 return (char *) result_hptr;
1180 }
1181 else if (needle_len == 1)
1182 {
1183 /* No point in using B-M-H for a one-character needle */
1184 char nchar = *needle;
1185
1186 hptr = start_ptr;
1187 while (hptr < haystack_end)
1188 {
1189 if (*hptr == nchar)
1190 return (char *) hptr;
1191 hptr++;
1192 }
1193 }
1194 else
1195 {
1196 const char *needle_last = &needle[needle_len - 1];
1197
1198 /* Start at startpos plus the length of the needle */
1199 hptr = start_ptr + needle_len - 1;
1200 while (hptr < haystack_end)
1201 {
1202 /* Match the needle scanning *backward* */
1203 const char *nptr;
1204 const char *p;
1205
1206 nptr = needle_last;
1207 p = hptr;
1208 while (*nptr == *p)
1209 {
1210 /* Matched it all? If so, return 1-based position */
1211 if (nptr == needle)
1212 return (char *) p;
1213 nptr--, p--;
1214 }
1215
1216 /*
1217 * No match, so use the haystack char at hptr to decide how far to
1218 * advance. If the needle had any occurrence of that character
1219 * (or more precisely, one sharing the same skiptable entry)
1220 * before its last character, then we advance far enough to align
1221 * the last such needle character with that haystack position.
1222 * Otherwise we can advance by the whole needle length.
1223 */
1224 hptr += state->skiptable[(unsigned char) *hptr & skiptablemask];
1225 }
1226 }
1227
1228 return 0; /* not found */
1229}
int pg_strncoll(const char *arg1, ssize_t len1, const char *arg2, ssize_t len2, pg_locale_t locale)
Definition pg_locale.c:1404

References Assert, fb(), pg_mblen(), and pg_strncoll().

Referenced by text_position_next().

◆ text_position_reset()

static void text_position_reset ( TextPositionState state)
static

Definition at line 1264 of file varlena.c.

1265{
1266 state->last_match = NULL;
1267 state->refpoint = state->str1;
1268 state->refpos = 0;
1269}

References fb().

Referenced by split_part().

◆ text_position_setup()

static void text_position_setup ( text t1,
text t2,
Oid  collid,
TextPositionState state 
)
static

Definition at line 917 of file varlena.c.

918{
919 int len1 = VARSIZE_ANY_EXHDR(t1);
920 int len2 = VARSIZE_ANY_EXHDR(t2);
921
923
925
926 /*
927 * Most callers need greedy mode, but some might want to unset this to
928 * optimize.
929 */
930 state->greedy = true;
931
932 Assert(len2 > 0);
933
934 /*
935 * Even with a multi-byte encoding, we perform the search using the raw
936 * byte sequence, ignoring multibyte issues. For UTF-8, that works fine,
937 * because in UTF-8 the byte sequence of one character cannot contain
938 * another character. For other multi-byte encodings, we do the search
939 * initially as a simple byte search, ignoring multibyte issues, but
940 * verify afterwards that the match we found is at a character boundary,
941 * and continue the search if it was a false match.
942 */
944 state->is_multibyte_char_in_char = false;
945 else if (GetDatabaseEncoding() == PG_UTF8)
946 state->is_multibyte_char_in_char = false;
947 else
948 state->is_multibyte_char_in_char = true;
949
950 state->str1 = VARDATA_ANY(t1);
951 state->str2 = VARDATA_ANY(t2);
952 state->len1 = len1;
953 state->len2 = len2;
954 state->last_match = NULL;
955 state->refpoint = state->str1;
956 state->refpos = 0;
957
958 /*
959 * Prepare the skip table for Boyer-Moore-Horspool searching. In these
960 * notes we use the terminology that the "haystack" is the string to be
961 * searched (t1) and the "needle" is the pattern being sought (t2).
962 *
963 * If the needle is empty or bigger than the haystack then there is no
964 * point in wasting cycles initializing the table. We also choose not to
965 * use B-M-H for needles of length 1, since the skip table can't possibly
966 * save anything in that case.
967 *
968 * (With nondeterministic collations, the search is already
969 * multibyte-aware, so we don't need this.)
970 */
971 if (len1 >= len2 && len2 > 1 && state->locale->deterministic)
972 {
973 int searchlength = len1 - len2;
974 int skiptablemask;
975 int last;
976 int i;
977 const char *str2 = state->str2;
978
979 /*
980 * First we must determine how much of the skip table to use. The
981 * declaration of TextPositionState allows up to 256 elements, but for
982 * short search problems we don't really want to have to initialize so
983 * many elements --- it would take too long in comparison to the
984 * actual search time. So we choose a useful skip table size based on
985 * the haystack length minus the needle length. The closer the needle
986 * length is to the haystack length the less useful skipping becomes.
987 *
988 * Note: since we use bit-masking to select table elements, the skip
989 * table size MUST be a power of 2, and so the mask must be 2^N-1.
990 */
991 if (searchlength < 16)
992 skiptablemask = 3;
993 else if (searchlength < 64)
994 skiptablemask = 7;
995 else if (searchlength < 128)
996 skiptablemask = 15;
997 else if (searchlength < 512)
998 skiptablemask = 31;
999 else if (searchlength < 2048)
1000 skiptablemask = 63;
1001 else if (searchlength < 4096)
1002 skiptablemask = 127;
1003 else
1004 skiptablemask = 255;
1005 state->skiptablemask = skiptablemask;
1006
1007 /*
1008 * Initialize the skip table. We set all elements to the needle
1009 * length, since this is the correct skip distance for any character
1010 * not found in the needle.
1011 */
1012 for (i = 0; i <= skiptablemask; i++)
1013 state->skiptable[i] = len2;
1014
1015 /*
1016 * Now examine the needle. For each character except the last one,
1017 * set the corresponding table element to the appropriate skip
1018 * distance. Note that when two characters share the same skip table
1019 * entry, the one later in the needle must determine the skip
1020 * distance.
1021 */
1022 last = len2 - 1;
1023
1024 for (i = 0; i < last; i++)
1025 state->skiptable[(unsigned char) str2[i] & skiptablemask] = last - i;
1026 }
1027}
#define PG_UTF8
Definition mbprint.c:43
int GetDatabaseEncoding(void)
Definition mbutils.c:1264

References Assert, check_collation_set(), collid, fb(), GetDatabaseEncoding(), i, pg_database_encoding_max_length(), pg_newlocale_from_collation(), PG_UTF8, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

Referenced by replace_text(), split_part(), split_text(), and text_position().

◆ text_reverse()

Datum text_reverse ( PG_FUNCTION_ARGS  )

Definition at line 4665 of file varlena.c.

4666{
4668 const char *p = VARDATA_ANY(str);
4669 int len = VARSIZE_ANY_EXHDR(str);
4670 const char *endp = p + len;
4671 text *result;
4672 char *dst;
4673
4674 result = palloc(len + VARHDRSZ);
4675 dst = (char *) VARDATA(result) + len;
4676 SET_VARSIZE(result, len + VARHDRSZ);
4677
4679 {
4680 /* multibyte version */
4681 while (p < endp)
4682 {
4683 int sz;
4684
4685 sz = pg_mblen(p);
4686 dst -= sz;
4687 memcpy(dst, p, sz);
4688 p += sz;
4689 }
4690 }
4691 else
4692 {
4693 /* single byte version */
4694 while (p < endp)
4695 *(--dst) = *p++;
4696 }
4697
4698 PG_RETURN_TEXT_P(result);
4699}

References fb(), len, palloc(), pg_database_encoding_max_length(), PG_GETARG_TEXT_PP, pg_mblen(), PG_RETURN_TEXT_P, SET_VARSIZE(), str, VARDATA(), VARDATA_ANY(), VARHDRSZ, and VARSIZE_ANY_EXHDR().

◆ text_right()

Datum text_right ( PG_FUNCTION_ARGS  )

Definition at line 4644 of file varlena.c.

4645{
4647 const char *p = VARDATA_ANY(str);
4648 int len = VARSIZE_ANY_EXHDR(str);
4649 int n = PG_GETARG_INT32(1);
4650 int off;
4651
4652 if (n < 0)
4653 n = -n;
4654 else
4655 n = pg_mbstrlen_with_len(p, len) - n;
4656 off = pg_mbcharcliplen(p, len, n);
4657
4659}

References cstring_to_text_with_len(), len, PG_GETARG_INT32, PG_GETARG_TEXT_PP, pg_mbcharcliplen(), pg_mbstrlen_with_len(), PG_RETURN_TEXT_P, str, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

◆ text_smaller()

Datum text_smaller ( PG_FUNCTION_ARGS  )

Definition at line 2296 of file varlena.c.

2297{
2300 text *result;
2301
2302 result = ((text_cmp(arg1, arg2, PG_GET_COLLATION()) < 0) ? arg1 : arg2);
2303
2304 PG_RETURN_TEXT_P(result);
2305}

References fb(), PG_GET_COLLATION, PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, and text_cmp().

◆ text_starts_with()

Datum text_starts_with ( PG_FUNCTION_ARGS  )

Definition at line 1545 of file varlena.c.

1546{
1551 bool result;
1552 Size len1,
1553 len2;
1554
1556
1558
1559 if (!mylocale->deterministic)
1560 ereport(ERROR,
1562 errmsg("nondeterministic collations are not supported for substring searches")));
1563
1564 len1 = toast_raw_datum_size(arg1);
1565 len2 = toast_raw_datum_size(arg2);
1566 if (len2 > len1)
1567 result = false;
1568 else
1569 {
1570 text *targ1 = text_substring(arg1, 1, len2, false);
1572
1574 VARSIZE_ANY_EXHDR(targ2)) == 0);
1575
1578 }
1579
1580 PG_RETURN_BOOL(result);
1581}
size_t Size
Definition c.h:619

References check_collation_set(), collid, DatumGetTextPP, ereport, errcode(), errmsg(), ERROR, fb(), PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_DATUM, pg_newlocale_from_collation(), PG_RETURN_BOOL, text_substring(), toast_raw_datum_size(), VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

Referenced by spg_text_leaf_consistent(), and t_starts_with().

◆ text_substr()

◆ text_substr_no_len()

Datum text_substr_no_len ( PG_FUNCTION_ARGS  )

Definition at line 561 of file varlena.c.

562{
565 -1, true));
566}

References PG_GETARG_DATUM, PG_GETARG_INT32, PG_RETURN_TEXT_P, and text_substring().

◆ text_substring()

static text * text_substring ( Datum  str,
int32  start,
int32  length,
bool  length_not_specified 
)
static

Definition at line 580 of file varlena.c.

581{
583 int32 S = start; /* start position */
584 int32 S1; /* adjusted start position */
585 int32 L1; /* adjusted substring length */
586 int32 E; /* end position */
587
588 /*
589 * SQL99 says S can be zero or negative (which we don't document), but we
590 * still must fetch from the start of the string.
591 * https://www.postgresql.org/message-id/170905442373.643.11536838320909376197%40wrigleys.postgresql.org
592 */
593 S1 = Max(S, 1);
594
595 /* life is easy if the encoding max length is 1 */
596 if (eml == 1)
597 {
598 if (length_not_specified) /* special case - get length to end of
599 * string */
600 L1 = -1;
601 else if (length < 0)
602 {
603 /* SQL99 says to throw an error for E < S, i.e., negative length */
606 errmsg("negative substring length not allowed")));
607 L1 = -1; /* silence stupider compilers */
608 }
609 else if (pg_add_s32_overflow(S, length, &E))
610 {
611 /*
612 * L could be large enough for S + L to overflow, in which case
613 * the substring must run to end of string.
614 */
615 L1 = -1;
616 }
617 else
618 {
619 /*
620 * A zero or negative value for the end position can happen if the
621 * start was negative or one. SQL99 says to return a zero-length
622 * string.
623 */
624 if (E < 1)
625 return cstring_to_text("");
626
627 L1 = E - S1;
628 }
629
630 /*
631 * If the start position is past the end of the string, SQL99 says to
632 * return a zero-length string -- DatumGetTextPSlice() will do that
633 * for us. We need only convert S1 to zero-based starting position.
634 */
635 return DatumGetTextPSlice(str, S1 - 1, L1);
636 }
637 else if (eml > 1)
638 {
639 /*
640 * When encoding max length is > 1, we can't get LC without
641 * detoasting, so we'll grab a conservatively large slice now and go
642 * back later to do the right thing
643 */
647 text *slice;
648 int32 E1;
649 int32 i;
650 char *p;
651 char *s;
652 text *ret;
653
654 /*
655 * We need to start at position zero because there is no way to know
656 * in advance which byte offset corresponds to the supplied start
657 * position.
658 */
659 slice_start = 0;
660
661 if (length_not_specified) /* special case - get length to end of
662 * string */
663 slice_size = L1 = -1;
664 else if (length < 0)
665 {
666 /* SQL99 says to throw an error for E < S, i.e., negative length */
669 errmsg("negative substring length not allowed")));
670 slice_size = L1 = -1; /* silence stupider compilers */
671 }
672 else if (pg_add_s32_overflow(S, length, &E))
673 {
674 /*
675 * L could be large enough for S + L to overflow, in which case
676 * the substring must run to end of string.
677 */
678 slice_size = L1 = -1;
679 }
680 else
681 {
682 /*
683 * A zero or negative value for the end position can happen if the
684 * start was negative or one. SQL99 says to return a zero-length
685 * string.
686 */
687 if (E < 1)
688 return cstring_to_text("");
689
690 /*
691 * if E is past the end of the string, the tuple toaster will
692 * truncate the length for us
693 */
694 L1 = E - S1;
695
696 /*
697 * Total slice size in bytes can't be any longer than the start
698 * position plus substring length times the encoding max length.
699 * If that overflows, we can just use -1.
700 */
701 if (pg_mul_s32_overflow(E, eml, &slice_size))
702 slice_size = -1;
703 }
704
705 /*
706 * If we're working with an untoasted source, no need to do an extra
707 * copying step.
708 */
712 else
713 slice = (text *) DatumGetPointer(str);
714
715 /* see if we got back an empty string */
716 if (VARSIZE_ANY_EXHDR(slice) == 0)
717 {
718 if (slice != (text *) DatumGetPointer(str))
719 pfree(slice);
720 return cstring_to_text("");
721 }
722
723 /* Now we can get the actual length of the slice in MB characters */
725 VARSIZE_ANY_EXHDR(slice));
726
727 /*
728 * Check that the start position wasn't > slice_strlen. If so, SQL99
729 * says to return a zero-length string.
730 */
731 if (S1 > slice_strlen)
732 {
733 if (slice != (text *) DatumGetPointer(str))
734 pfree(slice);
735 return cstring_to_text("");
736 }
737
738 /*
739 * Adjust L1 and E1 now that we know the slice string length. Again
740 * remember that S1 is one based, and slice_start is zero based.
741 */
742 if (L1 > -1)
743 E1 = Min(S1 + L1, slice_start + 1 + slice_strlen);
744 else
746
747 /*
748 * Find the start position in the slice; remember S1 is not zero based
749 */
750 p = VARDATA_ANY(slice);
751 for (i = 0; i < S1 - 1; i++)
752 p += pg_mblen(p);
753
754 /* hang onto a pointer to our start position */
755 s = p;
756
757 /*
758 * Count the actual bytes used by the substring of the requested
759 * length.
760 */
761 for (i = S1; i < E1; i++)
762 p += pg_mblen(p);
763
764 ret = (text *) palloc(VARHDRSZ + (p - s));
765 SET_VARSIZE(ret, VARHDRSZ + (p - s));
766 memcpy(VARDATA(ret), s, (p - s));
767
768 if (slice != (text *) DatumGetPointer(str))
769 pfree(slice);
770
771 return ret;
772 }
773 else
774 elog(ERROR, "invalid backend encoding: encoding max length < 1");
775
776 /* not reached: suppress compiler warning */
777 return NULL;
778}
#define Max(x, y)
Definition c.h:991
#define DatumGetTextPSlice(X, m, n)
Definition fmgr.h:305
return str start
#define S(n, x)
Definition sha1.c:73
static bool VARATT_IS_EXTERNAL(const void *PTR)
Definition varatt.h:354
static bool VARATT_IS_COMPRESSED(const void *PTR)
Definition varatt.h:347

References cstring_to_text(), DatumGetPointer(), DatumGetTextPSlice, elog, ereport, errcode(), errmsg(), ERROR, fb(), i, Max, Min, palloc(), pfree(), pg_add_s32_overflow(), pg_database_encoding_max_length(), pg_mblen(), pg_mbstrlen_with_len(), pg_mul_s32_overflow(), S, SET_VARSIZE(), start, str, VARATT_IS_COMPRESSED(), VARATT_IS_EXTERNAL(), VARDATA(), VARDATA_ANY(), VARHDRSZ, and VARSIZE_ANY_EXHDR().

Referenced by text_left(), text_overlay(), text_starts_with(), text_substr(), and text_substr_no_len().

◆ text_to_array()

Datum text_to_array ( PG_FUNCTION_ARGS  )

Definition at line 3596 of file varlena.c.

3597{
3599
3600 /* For array output, tstate should start as all zeroes */
3601 memset(&tstate, 0, sizeof(tstate));
3602
3603 if (!split_text(fcinfo, &tstate))
3605
3606 if (tstate.astate == NULL)
3608
3611}
#define PG_RETURN_ARRAYTYPE_P(x)
Definition array.h:265
ArrayType * construct_empty_array(Oid elmtype)
Datum makeArrayResult(ArrayBuildState *astate, MemoryContext rcontext)
#define PG_RETURN_DATUM(x)
Definition fmgr.h:354
static bool split_text(FunctionCallInfo fcinfo, SplitTextOutputData *tstate)
Definition varlena.c:3673

References construct_empty_array(), CurrentMemoryContext, fb(), makeArrayResult(), PG_RETURN_ARRAYTYPE_P, PG_RETURN_DATUM, PG_RETURN_NULL, and split_text().

Referenced by text_to_array_null().

◆ text_to_array_null()

Datum text_to_array_null ( PG_FUNCTION_ARGS  )

Definition at line 3622 of file varlena.c.

3623{
3624 return text_to_array(fcinfo);
3625}
Datum text_to_array(PG_FUNCTION_ARGS)
Definition varlena.c:3596

References text_to_array().

◆ text_to_cstring()

char * text_to_cstring ( const text t)

Definition at line 214 of file varlena.c.

215{
216 /* must cast away the const, unfortunately */
219 char *result;
220
221 result = (char *) palloc(len + 1);
222 memcpy(result, VARDATA_ANY(tunpacked), len);
223 result[len] = '\0';
224
225 if (tunpacked != t)
227
228 return result;
229}
#define unconstify(underlying_type, expr)
Definition c.h:1240
struct varlena * pg_detoast_datum_packed(struct varlena *datum)
Definition fmgr.c:1829

References fb(), len, palloc(), pfree(), pg_detoast_datum_packed(), unconstify, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

Referenced by array_to_text(), array_to_text_null(), binary_upgrade_add_sub_rel_state(), binary_upgrade_create_empty_extension(), binary_upgrade_replorigin_advance(), binary_upgrade_set_missing_value(), connectby_text(), connectby_text_serial(), convert_and_check_filename(), convert_any_priv_string(), convert_column_name(), convert_database_name(), convert_foreign_data_wrapper_name(), convert_function_name(), convert_language_name(), convert_schema_name(), convert_server_name(), convert_tablespace_name(), convert_type_name(), crosstab(), crosstab_hash(), cursor_to_xml(), cursor_to_xmlschema(), daitch_mokotoff(), database_to_xml(), database_to_xml_and_xmlschema(), database_to_xmlschema(), datetime_to_char_body(), dblink_cancel_query(), dblink_close(), dblink_connect(), dblink_disconnect(), dblink_error_message(), dblink_exec(), dblink_fetch(), dblink_get_notify(), dblink_is_busy(), dblink_open(), dblink_record_internal(), dblink_send_query(), deserialize_deflist(), difference(), dmetaphone(), dmetaphone_alt(), do_to_timestamp(), errno_from_string(), ExecAlterExtensionStmt(), execute_extension_script(), executeDateTimeMethod(), get_raw_page_fork(), get_raw_page_fork_1_9(), has_param_priv_byname(), has_sequence_privilege_id_name(), has_sequence_privilege_name(), has_sequence_privilege_name_name(), injection_points_attach(), injection_points_attach_func(), injection_points_cached(), injection_points_detach(), injection_points_load(), injection_points_run(), injection_points_wakeup(), json_object_field(), json_object_field_text(), jsonb_set_lax(), JsonItemFromDatum(), ltree_addtext(), ltree_textadd(), NUM_cache(), parse_datetime(), parse_ident(), pg_backup_start(), pg_create_restore_point(), pg_crypt(), pg_current_logfile(), pg_get_expr_worker(), pg_get_logical_snapshot_info(), pg_get_logical_snapshot_meta(), pg_get_publication_sequences(), pg_get_serial_sequence(), pg_index_column_has_property(), pg_index_has_property(), pg_indexam_has_property(), pg_input_is_valid_common(), pg_logical_emit_message_bytea(), pg_ls_replslotdir(), pg_notify(), pg_prewarm(), pg_relation_size(), pg_replication_origin_advance(), pg_replication_origin_create(), pg_replication_origin_drop(), pg_replication_origin_oid(), pg_replication_origin_progress(), pg_replication_origin_session_setup(), pg_size_bytes(), pg_split_walfile_name(), pg_stat_get_progress_info(), pg_stat_get_replication_slot(), pg_stat_have_stats(), pg_stat_reset_replication_slot(), pg_stat_reset_shared(), pg_stat_reset_slru(), pgxml_texttoxmlchar(), phraseto_tsquery_byid(), plainto_tsquery_byid(), postgres_fdw_disconnect(), query_to_xml(), query_to_xml_and_xmlschema(), query_to_xmlschema(), quote_ident(), quote_ident_cstr(), range_constructor3(), regress_setenv(), replorigin_by_oid(), schema_to_xml(), schema_to_xml_and_xmlschema(), schema_to_xmlschema(), sepgsql_mcstrans_in(), sepgsql_mcstrans_out(), soundex(), table_to_xml(), table_to_xml_and_xmlschema(), table_to_xmlschema(), test_canonicalize_path(), test_custom_stats_var_create(), test_custom_stats_var_drop(), test_custom_stats_var_report(), test_custom_stats_var_update(), test_predtest(), test_slru_page_write(), text2ltree(), text_concat_ws(), textToQualifiedNameList(), to_date(), to_regclass(), to_regcollation(), to_regdatabase(), to_regnamespace(), to_regoper(), to_regoperator(), to_regproc(), to_regprocedure(), to_regrole(), to_regtype(), to_regtypemod(), to_tsquery_byid(), ts_stat_sql(), tsquery_rewrite_query(), tuple_data_split(), unicode_is_normalized(), unicode_normalize_func(), verify_heapam(), websearch_to_tsquery_byid(), X509_NAME_field_to_text(), xml_out_internal(), xmlconcat(), xmlpi(), xmlroot(), and xpath_table().

◆ text_to_cstring_buffer()

void text_to_cstring_buffer ( const text src,
char dst,
size_t  dst_len 
)

Definition at line 245 of file varlena.c.

246{
247 /* must cast away the const, unfortunately */
250
251 if (dst_len > 0)
252 {
253 dst_len--;
254 if (dst_len >= src_len)
256 else /* ensure truncation is encoding-safe */
259 dst[dst_len] = '\0';
260 }
261
262 if (srcunpacked != src)
264}

References fb(), pfree(), pg_detoast_datum_packed(), pg_mbcliplen(), unconstify, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

Referenced by be_lo_export(), lo_import_internal(), lookup_timezone(), parse_sane_timezone(), pg_gen_salt(), pg_gen_salt_rounds(), timestamp_zone(), timestamptz_zone(), and timetz_zone().

◆ text_to_table()

Datum text_to_table ( PG_FUNCTION_ARGS  )

Definition at line 3633 of file varlena.c.

3634{
3635 ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
3637
3638 tstate.astate = NULL;
3640 tstate.tupstore = rsi->setResult;
3641 tstate.tupdesc = rsi->setDesc;
3642
3643 (void) split_text(fcinfo, &tstate);
3644
3645 return (Datum) 0;
3646}
void InitMaterializedSRF(FunctionCallInfo fcinfo, bits32 flags)
Definition funcapi.c:76
#define MAT_SRF_USE_EXPECTED_DESC
Definition funcapi.h:296
ArrayBuildState * astate
Definition varlena.c:108

References SplitTextOutputData::astate, fb(), InitMaterializedSRF(), MAT_SRF_USE_EXPECTED_DESC, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, and split_text().

Referenced by text_to_table_null().

◆ text_to_table_null()

Datum text_to_table_null ( PG_FUNCTION_ARGS  )

Definition at line 3657 of file varlena.c.

3658{
3659 return text_to_table(fcinfo);
3660}
Datum text_to_table(PG_FUNCTION_ARGS)
Definition varlena.c:3633

References text_to_table().

◆ textcat()

Datum textcat ( PG_FUNCTION_ARGS  )

Definition at line 445 of file varlena.c.

446{
449
451}

References fb(), PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, and text_catenate().

◆ texteq()

Datum texteq ( PG_FUNCTION_ARGS  )

Definition at line 1381 of file varlena.c.

1382{
1385 bool result;
1386
1388
1390
1391 if (mylocale->deterministic)
1392 {
1395 Size len1,
1396 len2;
1397
1398 /*
1399 * Since we only care about equality or not-equality, we can avoid all
1400 * the expense of strcoll() here, and just do bitwise comparison. In
1401 * fact, we don't even have to do a bitwise comparison if we can show
1402 * the lengths of the strings are unequal; which might save us from
1403 * having to detoast one or both values.
1404 */
1405 len1 = toast_raw_datum_size(arg1);
1406 len2 = toast_raw_datum_size(arg2);
1407 if (len1 != len2)
1408 result = false;
1409 else
1410 {
1413
1415 len1 - VARHDRSZ) == 0);
1416
1419 }
1420 }
1421 else
1422 {
1425
1426 result = (text_cmp(arg1, arg2, collid) == 0);
1427
1430 }
1431
1432 PG_RETURN_BOOL(result);
1433}

References check_collation_set(), collid, DatumGetTextPP, fb(), PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_DATUM, PG_GETARG_TEXT_PP, pg_newlocale_from_collation(), PG_RETURN_BOOL, text_cmp(), toast_raw_datum_size(), VARDATA_ANY(), and VARHDRSZ.

Referenced by gbt_texteq(), text_isequal(), and texteqfast().

◆ texteqname()

Datum texteqname ( PG_FUNCTION_ARGS  )

Definition at line 2338 of file varlena.c.

2339{
2342 size_t len1 = VARSIZE_ANY_EXHDR(arg1);
2343 size_t len2 = strlen(NameStr(*arg2));
2345 bool result;
2346
2348
2349 if (collid == C_COLLATION_OID)
2350 result = (len1 == len2 &&
2351 memcmp(VARDATA_ANY(arg1), NameStr(*arg2), len1) == 0);
2352 else
2353 result = (varstr_cmp(VARDATA_ANY(arg1), len1,
2354 NameStr(*arg2), len2,
2355 collid) == 0);
2356
2358
2359 PG_RETURN_BOOL(result);
2360}

References check_collation_set(), collid, fb(), NameStr, PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_NAME, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, VARDATA_ANY(), VARSIZE_ANY_EXHDR(), and varstr_cmp().

◆ textgename()

Datum textgename ( PG_FUNCTION_ARGS  )

Definition at line 2493 of file varlena.c.

2494{
2496}
Datum bttextnamecmp(PG_FUNCTION_ARGS)
Definition varlena.c:2429

References bttextnamecmp(), CmpCall, and PG_RETURN_BOOL.

◆ textgtname()

Datum textgtname ( PG_FUNCTION_ARGS  )

Definition at line 2487 of file varlena.c.

2488{
2490}

References bttextnamecmp(), CmpCall, and PG_RETURN_BOOL.

◆ textin()

Datum textin ( PG_FUNCTION_ARGS  )

Definition at line 275 of file varlena.c.

276{
277 char *inputText = PG_GETARG_CSTRING(0);
278
280}
#define PG_GETARG_CSTRING(n)
Definition fmgr.h:278

References cstring_to_text(), fb(), PG_GETARG_CSTRING, and PG_RETURN_TEXT_P.

Referenced by ExecEvalJsonExprPath().

◆ textlen()

Datum textlen ( PG_FUNCTION_ARGS  )

Definition at line 389 of file varlena.c.

390{
392
393 /* try to avoid decompressing argument */
395}
static int32 text_length(Datum str)
Definition varlena.c:407

References PG_GETARG_DATUM, PG_RETURN_INT32, str, and text_length().

◆ textlename()

Datum textlename ( PG_FUNCTION_ARGS  )

Definition at line 2481 of file varlena.c.

2482{
2484}

References bttextnamecmp(), CmpCall, and PG_RETURN_BOOL.

◆ textltname()

Datum textltname ( PG_FUNCTION_ARGS  )

Definition at line 2475 of file varlena.c.

2476{
2478}

References bttextnamecmp(), CmpCall, and PG_RETURN_BOOL.

◆ textne()

Datum textne ( PG_FUNCTION_ARGS  )

Definition at line 1436 of file varlena.c.

1437{
1440 bool result;
1441
1443
1445
1446 if (mylocale->deterministic)
1447 {
1450 Size len1,
1451 len2;
1452
1453 /* See comment in texteq() */
1454 len1 = toast_raw_datum_size(arg1);
1455 len2 = toast_raw_datum_size(arg2);
1456 if (len1 != len2)
1457 result = true;
1458 else
1459 {
1462
1464 len1 - VARHDRSZ) != 0);
1465
1468 }
1469 }
1470 else
1471 {
1474
1475 result = (text_cmp(arg1, arg2, collid) != 0);
1476
1479 }
1480
1481 PG_RETURN_BOOL(result);
1482}

References check_collation_set(), collid, DatumGetTextPP, fb(), PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_DATUM, PG_GETARG_TEXT_PP, pg_newlocale_from_collation(), PG_RETURN_BOOL, text_cmp(), toast_raw_datum_size(), VARDATA_ANY(), and VARHDRSZ.

◆ textnename()

Datum textnename ( PG_FUNCTION_ARGS  )

Definition at line 2388 of file varlena.c.

2389{
2392 size_t len1 = VARSIZE_ANY_EXHDR(arg1);
2393 size_t len2 = strlen(NameStr(*arg2));
2395 bool result;
2396
2398
2399 if (collid == C_COLLATION_OID)
2400 result = !(len1 == len2 &&
2401 memcmp(VARDATA_ANY(arg1), NameStr(*arg2), len1) == 0);
2402 else
2403 result = !(varstr_cmp(VARDATA_ANY(arg1), len1,
2404 NameStr(*arg2), len2,
2405 collid) == 0);
2406
2408
2409 PG_RETURN_BOOL(result);
2410}

References check_collation_set(), collid, fb(), NameStr, PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_NAME, PG_GETARG_TEXT_PP, PG_RETURN_BOOL, VARDATA_ANY(), VARSIZE_ANY_EXHDR(), and varstr_cmp().

◆ textoctetlen()

Datum textoctetlen ( PG_FUNCTION_ARGS  )

Definition at line 426 of file varlena.c.

427{
429
430 /* We need not detoast the input at all */
432}

References PG_GETARG_DATUM, PG_RETURN_INT32, str, toast_raw_datum_size(), and VARHDRSZ.

◆ textout()

Datum textout ( PG_FUNCTION_ARGS  )

Definition at line 286 of file varlena.c.

287{
289
291}
#define TextDatumGetCString(d)
Definition builtins.h:98
#define PG_RETURN_CSTRING(x)
Definition fmgr.h:364

References fb(), PG_GETARG_DATUM, PG_RETURN_CSTRING, and TextDatumGetCString.

Referenced by pg_node_tree_out(), plsample_func_handler(), and plsample_trigger_handler().

◆ textoverlay()

Datum textoverlay ( PG_FUNCTION_ARGS  )

Definition at line 788 of file varlena.c.

789{
792 int sp = PG_GETARG_INT32(2); /* substring start position */
793 int sl = PG_GETARG_INT32(3); /* substring length */
794
796}
static text * text_overlay(text *t1, text *t2, int sp, int sl)
Definition varlena.c:811

References fb(), PG_GETARG_INT32, PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, and text_overlay().

◆ textoverlay_no_len()

Datum textoverlay_no_len ( PG_FUNCTION_ARGS  )

Definition at line 799 of file varlena.c.

800{
803 int sp = PG_GETARG_INT32(2); /* substring start position */
804 int sl;
805
806 sl = text_length(PointerGetDatum(t2)); /* defaults to length(t2) */
808}

References fb(), PG_GETARG_INT32, PG_GETARG_TEXT_PP, PG_RETURN_TEXT_P, PointerGetDatum(), text_length(), and text_overlay().

◆ textpos()

Datum textpos ( PG_FUNCTION_ARGS  )

Definition at line 848 of file varlena.c.

849{
852
854}
static int text_position(text *t1, text *t2, Oid collid)
Definition varlena.c:871

References fb(), PG_GET_COLLATION, PG_GETARG_TEXT_PP, PG_RETURN_INT32, str, and text_position().

◆ textrecv()

Datum textrecv ( PG_FUNCTION_ARGS  )

Definition at line 297 of file varlena.c.

298{
300 text *result;
301 char *str;
302 int nbytes;
303
304 str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
305
306 result = cstring_to_text_with_len(str, nbytes);
307 pfree(str);
308 PG_RETURN_TEXT_P(result);
309}
char * pq_getmsgtext(StringInfo msg, int rawbytes, int *nbytes)
Definition pqformat.c:545

References buf, cstring_to_text_with_len(), pfree(), PG_GETARG_POINTER, PG_RETURN_TEXT_P, pq_getmsgtext(), and str.

◆ textsend()

Datum textsend ( PG_FUNCTION_ARGS  )

Definition at line 315 of file varlena.c.

316{
317 text *t = PG_GETARG_TEXT_PP(0);
319
323}
void pq_sendtext(StringInfo buf, const char *str, int slen)
Definition pqformat.c:172

References buf, PG_GETARG_TEXT_PP, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), pq_sendtext(), VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

Referenced by bpcharsend(), pg_node_tree_send(), and varcharsend().

◆ textToQualifiedNameList()

List * textToQualifiedNameList ( text textval)

Definition at line 2672 of file varlena.c.

2673{
2674 char *rawname;
2675 List *result = NIL;
2676 List *namelist;
2677 ListCell *l;
2678
2679 /* Convert to C string (handles possible detoasting). */
2680 /* Note we rely on being able to modify rawname below. */
2682
2684 ereport(ERROR,
2686 errmsg("invalid name syntax")));
2687
2688 if (namelist == NIL)
2689 ereport(ERROR,
2691 errmsg("invalid name syntax")));
2692
2693 foreach(l, namelist)
2694 {
2695 char *curname = (char *) lfirst(l);
2696
2697 result = lappend(result, makeString(pstrdup(curname)));
2698 }
2699
2700 pfree(rawname);
2702
2703 return result;
2704}
void list_free(List *list)
Definition list.c:1546
#define lfirst(lc)
Definition pg_list.h:172
Definition pg_list.h:54
String * makeString(char *str)
Definition value.c:63
bool SplitIdentifierString(char *rawstring, char separator, List **namelist)
Definition varlena.c:2730

References ereport, errcode(), errmsg(), ERROR, fb(), lappend(), lfirst, list_free(), makeString(), NIL, pfree(), pstrdup(), SplitIdentifierString(), and text_to_cstring().

Referenced by bt_metap(), bt_multi_page_stats(), bt_page_items_internal(), bt_page_stats_internal(), convert_table_name(), currtid_byrelname(), get_raw_page_internal(), get_rel_from_relname(), nextval(), pg_get_serial_sequence(), pg_get_viewdef_name(), pg_get_viewdef_name_ext(), pg_relpages(), pg_relpages_v1_5(), pgrowlocks(), pgstatindex(), pgstatindex_v1_5(), pgstattuple(), pgstattuple_v1_5(), row_security_active_name(), text_regclass(), ts_parse_byname(), and ts_token_type_byname().

◆ to_bin32()

Datum to_bin32 ( PG_FUNCTION_ARGS  )

Definition at line 4038 of file varlena.c.

4039{
4041
4043}
uint32_t uint32
Definition c.h:546
static text * convert_to_base(uint64 value, int base)
Definition varlena.c:4012

References convert_to_base(), PG_GETARG_INT32, PG_RETURN_TEXT_P, and value.

◆ to_bin64()

Datum to_bin64 ( PG_FUNCTION_ARGS  )

Definition at line 4045 of file varlena.c.

4046{
4048
4050}
#define PG_GETARG_INT64(n)
Definition fmgr.h:284

References convert_to_base(), PG_GETARG_INT64, PG_RETURN_TEXT_P, and value.

◆ to_hex32()

Datum to_hex32 ( PG_FUNCTION_ARGS  )

Definition at line 4076 of file varlena.c.

4077{
4079
4081}

References convert_to_base(), PG_GETARG_INT32, PG_RETURN_TEXT_P, and value.

Referenced by gin_page_opaque_info(), and gist_page_opaque_info().

◆ to_hex64()

Datum to_hex64 ( PG_FUNCTION_ARGS  )

Definition at line 4083 of file varlena.c.

4084{
4086
4088}

References convert_to_base(), PG_GETARG_INT64, PG_RETURN_TEXT_P, and value.

◆ to_oct32()

Datum to_oct32 ( PG_FUNCTION_ARGS  )

Definition at line 4057 of file varlena.c.

4058{
4060
4062}

References convert_to_base(), PG_GETARG_INT32, PG_RETURN_TEXT_P, and value.

◆ to_oct64()

Datum to_oct64 ( PG_FUNCTION_ARGS  )

Definition at line 4064 of file varlena.c.

4065{
4067
4069}

References convert_to_base(), PG_GETARG_INT64, PG_RETURN_TEXT_P, and value.

◆ unicode_assigned()

Datum unicode_assigned ( PG_FUNCTION_ARGS  )

Definition at line 5399 of file varlena.c.

5400{
5402 unsigned char *p;
5403 int size;
5404
5406 ereport(ERROR,
5407 (errmsg("Unicode categorization can only be performed if server encoding is UTF8")));
5408
5409 /* convert to char32_t */
5411 p = (unsigned char *) VARDATA_ANY(input);
5412 for (int i = 0; i < size; i++)
5413 {
5414 char32_t uchar = utf8_to_unicode(p);
5415 int category = unicode_category(uchar);
5416
5417 if (category == PG_U_UNASSIGNED)
5418 PG_RETURN_BOOL(false);
5419
5420 p += pg_utf_mblen(p);
5421 }
5422
5423 PG_RETURN_BOOL(true);
5424}
FILE * input
static char32_t utf8_to_unicode(const unsigned char *c)
Definition mbprint.c:53
#define pg_utf_mblen
Definition pg_wchar.h:633
pg_unicode_category unicode_category(char32_t code)
@ PG_U_UNASSIGNED

References ereport, errmsg(), ERROR, fb(), GetDatabaseEncoding(), i, input, PG_GETARG_TEXT_PP, pg_mbstrlen_with_len(), PG_RETURN_BOOL, PG_U_UNASSIGNED, PG_UTF8, pg_utf_mblen, unicode_category(), utf8_to_unicode(), VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

◆ unicode_is_normalized()

Datum unicode_is_normalized ( PG_FUNCTION_ARGS  )

Definition at line 5493 of file varlena.c.

5494{
5498 int size;
5499 char32_t *input_chars;
5500 char32_t *output_chars;
5501 unsigned char *p;
5502 int i;
5503 UnicodeNormalizationQC quickcheck;
5504 int output_size;
5505 bool result;
5506
5508
5509 /* convert to char32_t */
5511 input_chars = palloc((size + 1) * sizeof(char32_t));
5512 p = (unsigned char *) VARDATA_ANY(input);
5513 for (i = 0; i < size; i++)
5514 {
5516 p += pg_utf_mblen(p);
5517 }
5518 input_chars[i] = (char32_t) '\0';
5519 Assert((char *) p == VARDATA_ANY(input) + VARSIZE_ANY_EXHDR(input));
5520
5521 /* quick check (see UAX #15) */
5523 if (quickcheck == UNICODE_NORM_QC_YES)
5524 PG_RETURN_BOOL(true);
5525 else if (quickcheck == UNICODE_NORM_QC_NO)
5526 PG_RETURN_BOOL(false);
5527
5528 /* normalize and compare with original */
5530
5531 output_size = 0;
5532 for (char32_t *wp = output_chars; *wp; wp++)
5533 output_size++;
5534
5535 result = (size == output_size) &&
5536 (memcmp(input_chars, output_chars, size * sizeof(char32_t)) == 0);
5537
5538 PG_RETURN_BOOL(result);
5539}
uint32_t char32_t
Definition c.h:1406
UnicodeNormalizationQC unicode_is_normalized_quickcheck(UnicodeNormalizationForm form, const char32_t *input)
char32_t * unicode_normalize(UnicodeNormalizationForm form, const char32_t *input)
UnicodeNormalizationForm
UnicodeNormalizationQC
@ UNICODE_NORM_QC_YES
@ UNICODE_NORM_QC_NO
static UnicodeNormalizationForm unicode_norm_form_from_string(const char *formstr)
Definition varlena.c:5338

References Assert, fb(), i, input, palloc(), PG_GETARG_TEXT_PP, pg_mbstrlen_with_len(), PG_RETURN_BOOL, pg_utf_mblen, text_to_cstring(), unicode_is_normalized_quickcheck(), unicode_norm_form_from_string(), UNICODE_NORM_QC_NO, UNICODE_NORM_QC_YES, unicode_normalize(), utf8_to_unicode(), VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

◆ unicode_norm_form_from_string()

static UnicodeNormalizationForm unicode_norm_form_from_string ( const char formstr)
static

Definition at line 5338 of file varlena.c.

5339{
5341
5342 /*
5343 * Might as well check this while we're here.
5344 */
5346 ereport(ERROR,
5348 errmsg("Unicode normalization can only be performed if server encoding is UTF8")));
5349
5350 if (pg_strcasecmp(formstr, "NFC") == 0)
5351 form = UNICODE_NFC;
5352 else if (pg_strcasecmp(formstr, "NFD") == 0)
5353 form = UNICODE_NFD;
5354 else if (pg_strcasecmp(formstr, "NFKC") == 0)
5356 else if (pg_strcasecmp(formstr, "NFKD") == 0)
5358 else
5359 ereport(ERROR,
5361 errmsg("invalid normalization form: %s", formstr)));
5362
5363 return form;
5364}
int pg_strcasecmp(const char *s1, const char *s2)
@ UNICODE_NFKD
@ UNICODE_NFD
@ UNICODE_NFC
@ UNICODE_NFKC

References ereport, errcode(), errmsg(), ERROR, fb(), GetDatabaseEncoding(), pg_strcasecmp(), PG_UTF8, UNICODE_NFC, UNICODE_NFD, UNICODE_NFKC, and UNICODE_NFKD.

Referenced by unicode_is_normalized(), and unicode_normalize_func().

◆ unicode_normalize_func()

Datum unicode_normalize_func ( PG_FUNCTION_ARGS  )

Definition at line 5427 of file varlena.c.

5428{
5432 int size;
5433 char32_t *input_chars;
5434 char32_t *output_chars;
5435 unsigned char *p;
5436 text *result;
5437 int i;
5438
5440
5441 /* convert to char32_t */
5443 input_chars = palloc((size + 1) * sizeof(char32_t));
5444 p = (unsigned char *) VARDATA_ANY(input);
5445 for (i = 0; i < size; i++)
5446 {
5448 p += pg_utf_mblen(p);
5449 }
5450 input_chars[i] = (char32_t) '\0';
5451 Assert((char *) p == VARDATA_ANY(input) + VARSIZE_ANY_EXHDR(input));
5452
5453 /* action */
5455
5456 /* convert back to UTF-8 string */
5457 size = 0;
5458 for (char32_t *wp = output_chars; *wp; wp++)
5459 {
5460 unsigned char buf[4];
5461
5463 size += pg_utf_mblen(buf);
5464 }
5465
5466 result = palloc(size + VARHDRSZ);
5467 SET_VARSIZE(result, size + VARHDRSZ);
5468
5469 p = (unsigned char *) VARDATA_ANY(result);
5470 for (char32_t *wp = output_chars; *wp; wp++)
5471 {
5472 unicode_to_utf8(*wp, p);
5473 p += pg_utf_mblen(p);
5474 }
5475 Assert((char *) p == (char *) result + size + VARHDRSZ);
5476
5477 PG_RETURN_TEXT_P(result);
5478}
static unsigned char * unicode_to_utf8(char32_t c, unsigned char *utf8string)
Definition pg_wchar.h:575

References Assert, buf, fb(), i, input, palloc(), PG_GETARG_TEXT_PP, pg_mbstrlen_with_len(), PG_RETURN_TEXT_P, pg_utf_mblen, SET_VARSIZE(), text_to_cstring(), unicode_norm_form_from_string(), unicode_normalize(), unicode_to_utf8(), utf8_to_unicode(), VARDATA_ANY(), VARHDRSZ, and VARSIZE_ANY_EXHDR().

◆ unicode_version()

Datum unicode_version ( PG_FUNCTION_ARGS  )

Definition at line 5375 of file varlena.c.

5376{
5378}
#define PG_UNICODE_VERSION

References cstring_to_text(), PG_RETURN_TEXT_P, and PG_UNICODE_VERSION.

◆ unistr()

Datum unistr ( PG_FUNCTION_ARGS  )

Definition at line 5585 of file varlena.c.

5586{
5588 char *instr;
5589 int len;
5591 text *result;
5592 char16_t pair_first = 0;
5594
5595 instr = VARDATA_ANY(input_text);
5597
5599
5600 while (len > 0)
5601 {
5602 if (instr[0] == '\\')
5603 {
5604 if (len >= 2 &&
5605 instr[1] == '\\')
5606 {
5607 if (pair_first)
5608 goto invalid_pair;
5609 appendStringInfoChar(&str, '\\');
5610 instr += 2;
5611 len -= 2;
5612 }
5613 else if ((len >= 5 && isxdigits_n(instr + 1, 4)) ||
5614 (len >= 6 && instr[1] == 'u' && isxdigits_n(instr + 2, 4)))
5615 {
5616 char32_t unicode;
5617 int offset = instr[1] == 'u' ? 2 : 1;
5618
5619 unicode = hexval_n(instr + offset, 4);
5620
5622 ereport(ERROR,
5624 errmsg("invalid Unicode code point: %04X", unicode));
5625
5626 if (pair_first)
5627 {
5629 {
5631 pair_first = 0;
5632 }
5633 else
5634 goto invalid_pair;
5635 }
5637 goto invalid_pair;
5638
5641 else
5642 {
5643 pg_unicode_to_server(unicode, (unsigned char *) cbuf);
5645 }
5646
5647 instr += 4 + offset;
5648 len -= 4 + offset;
5649 }
5650 else if (len >= 8 && instr[1] == '+' && isxdigits_n(instr + 2, 6))
5651 {
5652 char32_t unicode;
5653
5654 unicode = hexval_n(instr + 2, 6);
5655
5657 ereport(ERROR,
5659 errmsg("invalid Unicode code point: %04X", unicode));
5660
5661 if (pair_first)
5662 {
5664 {
5666 pair_first = 0;
5667 }
5668 else
5669 goto invalid_pair;
5670 }
5672 goto invalid_pair;
5673
5676 else
5677 {
5678 pg_unicode_to_server(unicode, (unsigned char *) cbuf);
5680 }
5681
5682 instr += 8;
5683 len -= 8;
5684 }
5685 else if (len >= 10 && instr[1] == 'U' && isxdigits_n(instr + 2, 8))
5686 {
5687 char32_t unicode;
5688
5689 unicode = hexval_n(instr + 2, 8);
5690
5692 ereport(ERROR,
5694 errmsg("invalid Unicode code point: %04X", unicode));
5695
5696 if (pair_first)
5697 {
5699 {
5701 pair_first = 0;
5702 }
5703 else
5704 goto invalid_pair;
5705 }
5707 goto invalid_pair;
5708
5711 else
5712 {
5713 pg_unicode_to_server(unicode, (unsigned char *) cbuf);
5715 }
5716
5717 instr += 10;
5718 len -= 10;
5719 }
5720 else
5721 ereport(ERROR,
5723 errmsg("invalid Unicode escape"),
5724 errhint("Unicode escapes must be \\XXXX, \\+XXXXXX, \\uXXXX, or \\UXXXXXXXX.")));
5725 }
5726 else
5727 {
5728 if (pair_first)
5729 goto invalid_pair;
5730
5731 appendStringInfoChar(&str, *instr++);
5732 len--;
5733 }
5734 }
5735
5736 /* unfinished surrogate pair? */
5737 if (pair_first)
5738 goto invalid_pair;
5739
5740 result = cstring_to_text_with_len(str.data, str.len);
5741 pfree(str.data);
5742
5743 PG_RETURN_TEXT_P(result);
5744
5746 ereport(ERROR,
5748 errmsg("invalid Unicode surrogate pair")));
5749 PG_RETURN_NULL(); /* keep compiler quiet */
5750}
void pg_unicode_to_server(char32_t c, unsigned char *s)
Definition mbutils.c:867
static bool is_utf16_surrogate_first(char32_t c)
Definition pg_wchar.h:525
static bool is_utf16_surrogate_second(char32_t c)
Definition pg_wchar.h:531
static char32_t surrogate_pair_to_codepoint(char16_t first, char16_t second)
Definition pg_wchar.h:537
#define MAX_UNICODE_EQUIVALENT_STRING
Definition pg_wchar.h:329
static bool is_valid_unicode_codepoint(char32_t c)
Definition pg_wchar.h:519
static unsigned int hexval_n(const char *instr, size_t n)
Definition varlena.c:5571
static bool isxdigits_n(const char *instr, size_t n)
Definition varlena.c:5545

References appendStringInfoChar(), appendStringInfoString(), cstring_to_text_with_len(), ereport, errcode(), errhint(), errmsg(), ERROR, fb(), hexval_n(), initStringInfo(), is_utf16_surrogate_first(), is_utf16_surrogate_second(), is_valid_unicode_codepoint(), isxdigits_n(), len, MAX_UNICODE_EQUIVALENT_STRING, pfree(), PG_GETARG_TEXT_PP, PG_RETURN_NULL, PG_RETURN_TEXT_P, pg_unicode_to_server(), str, surrogate_pair_to_codepoint(), VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

◆ unknownin()

Datum unknownin ( PG_FUNCTION_ARGS  )

Definition at line 330 of file varlena.c.

331{
332 char *str = PG_GETARG_CSTRING(0);
333
334 /* representation is same as cstring */
336}

References PG_GETARG_CSTRING, PG_RETURN_CSTRING, pstrdup(), and str.

◆ unknownout()

Datum unknownout ( PG_FUNCTION_ARGS  )

Definition at line 342 of file varlena.c.

343{
344 /* representation is same as cstring */
345 char *str = PG_GETARG_CSTRING(0);
346
348}

References PG_GETARG_CSTRING, PG_RETURN_CSTRING, pstrdup(), and str.

◆ unknownrecv()

Datum unknownrecv ( PG_FUNCTION_ARGS  )

Definition at line 354 of file varlena.c.

355{
357 char *str;
358 int nbytes;
359
360 str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
361 /* representation is same as cstring */
363}

References buf, PG_GETARG_POINTER, PG_RETURN_CSTRING, pq_getmsgtext(), and str.

◆ unknownsend()

Datum unknownsend ( PG_FUNCTION_ARGS  )

Definition at line 369 of file varlena.c.

370{
371 /* representation is same as cstring */
372 char *str = PG_GETARG_CSTRING(0);
374
378}

References buf, fb(), PG_GETARG_CSTRING, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), pq_sendtext(), and str.

◆ updateClosestMatch()

void updateClosestMatch ( ClosestMatchState state,
const char candidate 
)

Definition at line 5290 of file varlena.c.

5291{
5292 int dist;
5293
5294 Assert(state);
5295
5296 if (state->source == NULL || state->source[0] == '\0' ||
5297 candidate == NULL || candidate[0] == '\0')
5298 return;
5299
5300 /*
5301 * To avoid ERROR-ing, we check the lengths here instead of setting
5302 * 'trusted' to false in the call to varstr_levenshtein_less_equal().
5303 */
5304 if (strlen(state->source) > MAX_LEVENSHTEIN_STRLEN ||
5306 return;
5307
5309 candidate, strlen(candidate), 1, 1, 1,
5310 state->max_d, true);
5311 if (dist <= state->max_d &&
5312 dist <= strlen(state->source) / 2 &&
5313 (state->min_d == -1 || dist < state->min_d))
5314 {
5315 state->min_d = dist;
5316 state->match = candidate;
5317 }
5318}
#define MAX_LEVENSHTEIN_STRLEN
Definition levenshtein.c:26
int varstr_levenshtein_less_equal(const char *source, int slen, const char *target, int tlen, int ins_c, int del_c, int sub_c, int max_d, bool trusted)

References Assert, fb(), MAX_LEVENSHTEIN_STRLEN, and varstr_levenshtein_less_equal().

Referenced by dblink_fdw_validator(), file_fdw_validator(), postgres_fdw_validator(), and postgresql_fdw_validator().

◆ varlenafastcmp_locale()

static int varlenafastcmp_locale ( Datum  x,
Datum  y,
SortSupport  ssup 
)
static

Definition at line 1842 of file varlena.c.

1843{
1846 char *a1p,
1847 *a2p;
1848 int len1,
1849 len2,
1850 result;
1851
1852 a1p = VARDATA_ANY(arg1);
1853 a2p = VARDATA_ANY(arg2);
1854
1855 len1 = VARSIZE_ANY_EXHDR(arg1);
1856 len2 = VARSIZE_ANY_EXHDR(arg2);
1857
1858 result = varstrfastcmp_locale(a1p, len1, a2p, len2, ssup);
1859
1860 /* We can't afford to leak memory here. */
1861 if (PointerGetDatum(arg1) != x)
1862 pfree(arg1);
1863 if (PointerGetDatum(arg2) != y)
1864 pfree(arg2);
1865
1866 return result;
1867}
#define DatumGetVarStringPP(X)
Definition varlena.c:120

References DatumGetVarStringPP, fb(), pfree(), PointerGetDatum(), VARDATA_ANY(), VARSIZE_ANY_EXHDR(), varstrfastcmp_locale(), x, and y.

Referenced by varstr_sortsupport().

◆ varstr_abbrev_abort()

static bool varstr_abbrev_abort ( int  memtupcount,
SortSupport  ssup 
)
static

Definition at line 2154 of file varlena.c.

2155{
2157 double abbrev_distinct,
2159
2160 Assert(ssup->abbreviate);
2161
2162 /* Have a little patience */
2163 if (memtupcount < 100)
2164 return false;
2165
2167 key_distinct = estimateHyperLogLog(&sss->full_card);
2168
2169 /*
2170 * Clamp cardinality estimates to at least one distinct value. While
2171 * NULLs are generally disregarded, if only NULL values were seen so far,
2172 * that might misrepresent costs if we failed to clamp.
2173 */
2174 if (abbrev_distinct < 1.0)
2175 abbrev_distinct = 1.0;
2176
2177 if (key_distinct < 1.0)
2178 key_distinct = 1.0;
2179
2180 /*
2181 * In the worst case all abbreviated keys are identical, while at the same
2182 * time there are differences within full key strings not captured in
2183 * abbreviations.
2184 */
2185 if (trace_sort)
2186 {
2187 double norm_abbrev_card = abbrev_distinct / (double) memtupcount;
2188
2189 elog(LOG, "varstr_abbrev: abbrev_distinct after %d: %f "
2190 "(key_distinct: %f, norm_abbrev_card: %f, prop_card: %f)",
2192 sss->prop_card);
2193 }
2194
2195 /*
2196 * If the number of distinct abbreviated keys approximately matches the
2197 * number of distinct authoritative original keys, that's reason enough to
2198 * proceed. We can win even with a very low cardinality set if most
2199 * tie-breakers only memcmp(). This is by far the most important
2200 * consideration.
2201 *
2202 * While comparisons that are resolved at the abbreviated key level are
2203 * considerably cheaper than tie-breakers resolved with memcmp(), both of
2204 * those two outcomes are so much cheaper than a full strcoll() once
2205 * sorting is underway that it doesn't seem worth it to weigh abbreviated
2206 * cardinality against the overall size of the set in order to more
2207 * accurately model costs. Assume that an abbreviated comparison, and an
2208 * abbreviated comparison with a cheap memcmp()-based authoritative
2209 * resolution are equivalent.
2210 */
2211 if (abbrev_distinct > key_distinct * sss->prop_card)
2212 {
2213 /*
2214 * When we have exceeded 10,000 tuples, decay required cardinality
2215 * aggressively for next call.
2216 *
2217 * This is useful because the number of comparisons required on
2218 * average increases at a linearithmic rate, and at roughly 10,000
2219 * tuples that factor will start to dominate over the linear costs of
2220 * string transformation (this is a conservative estimate). The decay
2221 * rate is chosen to be a little less aggressive than halving -- which
2222 * (since we're called at points at which memtupcount has doubled)
2223 * would never see the cost model actually abort past the first call
2224 * following a decay. This decay rate is mostly a precaution against
2225 * a sudden, violent swing in how well abbreviated cardinality tracks
2226 * full key cardinality. The decay also serves to prevent a marginal
2227 * case from being aborted too late, when too much has already been
2228 * invested in string transformation.
2229 *
2230 * It's possible for sets of several million distinct strings with
2231 * mere tens of thousands of distinct abbreviated keys to still
2232 * benefit very significantly. This will generally occur provided
2233 * each abbreviated key is a proxy for a roughly uniform number of the
2234 * set's full keys. If it isn't so, we hope to catch that early and
2235 * abort. If it isn't caught early, by the time the problem is
2236 * apparent it's probably not worth aborting.
2237 */
2238 if (memtupcount > 10000)
2239 sss->prop_card *= 0.65;
2240
2241 return false;
2242 }
2243
2244 /*
2245 * Abort abbreviation strategy.
2246 *
2247 * The worst case, where all abbreviated keys are identical while all
2248 * original strings differ will typically only see a regression of about
2249 * 10% in execution time for small to medium sized lists of strings.
2250 * Whereas on modern CPUs where cache stalls are the dominant cost, we can
2251 * often expect very large improvements, particularly with sets of strings
2252 * of moderately high to high abbreviated cardinality. There is little to
2253 * lose but much to gain, which our strategy reflects.
2254 */
2255 if (trace_sort)
2256 elog(LOG, "varstr_abbrev: aborted abbreviation at %d "
2257 "(abbrev_distinct: %f, key_distinct: %f, prop_card: %f)",
2258 memtupcount, abbrev_distinct, key_distinct, sss->prop_card);
2259
2260 return true;
2261}
#define LOG
Definition elog.h:31
double estimateHyperLogLog(hyperLogLogState *cState)
bool trace_sort
Definition tuplesort.c:122

References SortSupportData::abbreviate, Assert, elog, estimateHyperLogLog(), fb(), LOG, SortSupportData::ssup_extra, and trace_sort.

Referenced by varstr_sortsupport().

◆ varstr_abbrev_convert()

static Datum varstr_abbrev_convert ( Datum  original,
SortSupport  ssup 
)
static

Definition at line 1987 of file varlena.c.

1988{
1989 const size_t max_prefix_bytes = sizeof(Datum);
1993
1994 /* working state */
1995 Datum res;
1996 char *pres;
1997 int len;
1998 uint32 hash;
1999
2000 pres = (char *) &res;
2001 /* memset(), so any non-overwritten bytes are NUL */
2004
2005 /* Get number of bytes, ignoring trailing spaces */
2006 if (sss->typid == BPCHAROID)
2008
2009 /*
2010 * If we're using the C collation, use memcpy(), rather than strxfrm(), to
2011 * abbreviate keys. The full comparator for the C locale is also
2012 * memcmp(). This should be faster than strxfrm().
2013 */
2014 if (sss->collate_c)
2016 else
2017 {
2018 Size bsize;
2019
2020 /*
2021 * We're not using the C collation, so fall back on strxfrm or ICU
2022 * analogs.
2023 */
2024
2025 /* By convention, we use buffer 1 to store and NUL-terminate */
2026 if (len >= sss->buflen1)
2027 {
2028 sss->buflen1 = Max(len + 1, Min(sss->buflen1 * 2, MaxAllocSize));
2029 sss->buf1 = repalloc(sss->buf1, sss->buflen1);
2030 }
2031
2032 /* Might be able to reuse strxfrm() blob from last call */
2033 if (sss->last_len1 == len && sss->cache_blob &&
2034 memcmp(sss->buf1, authoritative_data, len) == 0)
2035 {
2036 memcpy(pres, sss->buf2, Min(max_prefix_bytes, sss->last_len2));
2037 /* No change affecting cardinality, so no hashing required */
2038 goto done;
2039 }
2040
2042
2043 /*
2044 * pg_strxfrm() and pg_strxfrm_prefix expect NUL-terminated strings.
2045 */
2046 sss->buf1[len] = '\0';
2047 sss->last_len1 = len;
2048
2049 if (pg_strxfrm_prefix_enabled(sss->locale))
2050 {
2051 if (sss->buflen2 < max_prefix_bytes)
2052 {
2053 sss->buflen2 = Max(max_prefix_bytes,
2054 Min(sss->buflen2 * 2, MaxAllocSize));
2055 sss->buf2 = repalloc(sss->buf2, sss->buflen2);
2056 }
2057
2058 bsize = pg_strxfrm_prefix(sss->buf2, sss->buf1,
2059 max_prefix_bytes, sss->locale);
2060 sss->last_len2 = bsize;
2061 }
2062 else
2063 {
2064 /*
2065 * Loop: Call pg_strxfrm(), possibly enlarge buffer, and try
2066 * again. The pg_strxfrm() function leaves the result buffer
2067 * content undefined if the result did not fit, so we need to
2068 * retry until everything fits, even though we only need the first
2069 * few bytes in the end.
2070 */
2071 for (;;)
2072 {
2073 bsize = pg_strxfrm(sss->buf2, sss->buf1, sss->buflen2,
2074 sss->locale);
2075
2076 sss->last_len2 = bsize;
2077 if (bsize < sss->buflen2)
2078 break;
2079
2080 /*
2081 * Grow buffer and retry.
2082 */
2083 sss->buflen2 = Max(bsize + 1,
2084 Min(sss->buflen2 * 2, MaxAllocSize));
2085 sss->buf2 = repalloc(sss->buf2, sss->buflen2);
2086 }
2087 }
2088
2089 /*
2090 * Every Datum byte is always compared. This is safe because the
2091 * strxfrm() blob is itself NUL terminated, leaving no danger of
2092 * misinterpreting any NUL bytes not intended to be interpreted as
2093 * logically representing termination.
2094 */
2096 }
2097
2098 /*
2099 * Maintain approximate cardinality of both abbreviated keys and original,
2100 * authoritative keys using HyperLogLog. Used as cheap insurance against
2101 * the worst case, where we do many string transformations for no saving
2102 * in full strcoll()-based comparisons. These statistics are used by
2103 * varstr_abbrev_abort().
2104 *
2105 * First, Hash key proper, or a significant fraction of it. Mix in length
2106 * in order to compensate for cases where differences are past
2107 * PG_CACHE_LINE_SIZE bytes, so as to limit the overhead of hashing.
2108 */
2109 hash = DatumGetUInt32(hash_any((unsigned char *) authoritative_data,
2111
2112 if (len > PG_CACHE_LINE_SIZE)
2114
2115 addHyperLogLog(&sss->full_card, hash);
2116
2117 /* Hash abbreviated key */
2118 {
2119 uint32 tmp;
2120
2121 tmp = DatumGetUInt32(res) ^ (uint32) (DatumGetUInt64(res) >> 32);
2123 }
2124
2125 addHyperLogLog(&sss->abbr_card, hash);
2126
2127 /* Cache result, perhaps saving an expensive strxfrm() call next time */
2128 sss->cache_blob = true;
2129done:
2130
2131 /*
2132 * Byteswap on little-endian machines.
2133 *
2134 * This is needed so that ssup_datum_unsigned_cmp() (an unsigned integer
2135 * 3-way comparator) works correctly on all platforms. If we didn't do
2136 * this, the comparator would have to call memcmp() with a pair of
2137 * pointers to the first byte of each abbreviated key, which is slower.
2138 */
2139 res = DatumBigEndianToNative(res);
2140
2141 /* Don't leak memory here */
2142 if (PointerGetDatum(authoritative) != original)
2144
2145 return res;
2146}
#define MaxAllocSize
Definition fe_memutils.h:22
static Datum hash_uint32(uint32 k)
Definition hashfn.h:43
static Datum hash_any(const unsigned char *k, int keylen)
Definition hashfn.h:31
void addHyperLogLog(hyperLogLogState *cState, uint32 hash)
void * repalloc(void *pointer, Size size)
Definition mcxt.c:1632
#define DatumBigEndianToNative(x)
Definition pg_bswap.h:145
#define PG_CACHE_LINE_SIZE
bool pg_strxfrm_prefix_enabled(pg_locale_t locale)
Definition pg_locale.c:1470
size_t pg_strxfrm(char *dest, const char *src, size_t destsize, pg_locale_t locale)
Definition pg_locale.c:1434
size_t pg_strxfrm_prefix(char *dest, const char *src, size_t destsize, pg_locale_t locale)
Definition pg_locale.c:1481
static uint32 DatumGetUInt32(Datum X)
Definition postgres.h:232
static uint64 DatumGetUInt64(Datum X)
Definition postgres.h:433
static unsigned hash(unsigned *uv, int n)
Definition rege_dfa.c:715

References addHyperLogLog(), bpchartruelen(), DatumBigEndianToNative, DatumGetUInt32(), DatumGetUInt64(), DatumGetVarStringPP, fb(), hash(), hash_any(), hash_uint32(), len, Max, MaxAllocSize, Min, pfree(), PG_CACHE_LINE_SIZE, pg_strxfrm(), pg_strxfrm_prefix(), pg_strxfrm_prefix_enabled(), PointerGetDatum(), repalloc(), SortSupportData::ssup_extra, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

Referenced by varstr_sortsupport().

◆ varstr_cmp()

int varstr_cmp ( const char arg1,
int  len1,
const char arg2,
int  len2,
Oid  collid 
)

Definition at line 1308 of file varlena.c.

1309{
1310 int result;
1312
1314
1316
1317 if (mylocale->collate_is_c)
1318 {
1319 result = memcmp(arg1, arg2, Min(len1, len2));
1320 if ((result == 0) && (len1 != len2))
1321 result = (len1 < len2) ? -1 : 1;
1322 }
1323 else
1324 {
1325 /*
1326 * memcmp() can't tell us which of two unequal strings sorts first,
1327 * but it's a cheap way to tell if they're equal. Testing shows that
1328 * memcmp() followed by strcoll() is only trivially slower than
1329 * strcoll() by itself, so we don't lose much if this doesn't work out
1330 * very often, and if it does - for example, because there are many
1331 * equal strings in the input - then we win big by avoiding expensive
1332 * collation-aware comparisons.
1333 */
1334 if (len1 == len2 && memcmp(arg1, arg2, len1) == 0)
1335 return 0;
1336
1337 result = pg_strncoll(arg1, len1, arg2, len2, mylocale);
1338
1339 /* Break tie if necessary. */
1340 if (result == 0 && mylocale->deterministic)
1341 {
1342 result = memcmp(arg1, arg2, Min(len1, len2));
1343 if ((result == 0) && (len1 != len2))
1344 result = (len1 < len2) ? -1 : 1;
1345 }
1346 }
1347
1348 return result;
1349}

References check_collation_set(), collid, fb(), Min, pg_newlocale_from_collation(), and pg_strncoll().

Referenced by bpchar_larger(), bpchar_smaller(), bpcharcmp(), bpchareq(), bpcharge(), bpchargt(), bpcharle(), bpcharlt(), bpcharne(), btnametextcmp(), bttextnamecmp(), citextcmp(), compareJsonbScalarValue(), gin_compare_jsonb(), make_greater_string(), namecmp(), nameeqtext(), namenetext(), spg_text_leaf_consistent(), text_cmp(), texteqname(), and textnename().

◆ varstr_sortsupport()

void varstr_sortsupport ( SortSupport  ssup,
Oid  typid,
Oid  collid 
)

Definition at line 1624 of file varlena.c.

1625{
1626 bool abbreviate = ssup->abbreviate;
1627 bool collate_c = false;
1629 pg_locale_t locale;
1630
1632
1634
1635 /*
1636 * If possible, set ssup->comparator to a function which can be used to
1637 * directly compare two datums. If we can do this, we'll avoid the
1638 * overhead of a trip through the fmgr layer for every comparison, which
1639 * can be substantial.
1640 *
1641 * Most typically, we'll set the comparator to varlenafastcmp_locale,
1642 * which uses strcoll() to perform comparisons. We use that for the
1643 * BpChar case too, but type NAME uses namefastcmp_locale. However, if
1644 * LC_COLLATE = C, we can make things quite a bit faster with
1645 * varstrfastcmp_c, bpcharfastcmp_c, or namefastcmp_c, all of which use
1646 * memcmp() rather than strcoll().
1647 */
1648 if (locale->collate_is_c)
1649 {
1650 if (typid == BPCHAROID)
1652 else if (typid == NAMEOID)
1653 {
1654 ssup->comparator = namefastcmp_c;
1655 /* Not supporting abbreviation with type NAME, for now */
1656 abbreviate = false;
1657 }
1658 else
1660
1661 collate_c = true;
1662 }
1663 else
1664 {
1665 /*
1666 * We use varlenafastcmp_locale except for type NAME.
1667 */
1668 if (typid == NAMEOID)
1669 {
1671 /* Not supporting abbreviation with type NAME, for now */
1672 abbreviate = false;
1673 }
1674 else
1676
1677 /*
1678 * Unfortunately, it seems that abbreviation for non-C collations is
1679 * broken on many common platforms; see pg_strxfrm_enabled().
1680 *
1681 * Even apart from the risk of broken locales, it's possible that
1682 * there are platforms where the use of abbreviated keys should be
1683 * disabled at compile time. For example, macOS's strxfrm()
1684 * implementation is known to not effectively concentrate a
1685 * significant amount of entropy from the original string in earlier
1686 * transformed blobs. It's possible that other supported platforms
1687 * are similarly encumbered. So, if we ever get past disabling this
1688 * categorically, we may still want or need to disable it for
1689 * particular platforms.
1690 */
1691 if (!pg_strxfrm_enabled(locale))
1692 abbreviate = false;
1693 }
1694
1695 /*
1696 * If we're using abbreviated keys, or if we're using a locale-aware
1697 * comparison, we need to initialize a VarStringSortSupport object. Both
1698 * cases will make use of the temporary buffers we initialize here for
1699 * scratch space (and to detect requirement for BpChar semantics from
1700 * caller), and the abbreviation case requires additional state.
1701 */
1702 if (abbreviate || !collate_c)
1703 {
1705 sss->buf1 = palloc(TEXTBUFLEN);
1706 sss->buflen1 = TEXTBUFLEN;
1707 sss->buf2 = palloc(TEXTBUFLEN);
1708 sss->buflen2 = TEXTBUFLEN;
1709 /* Start with invalid values */
1710 sss->last_len1 = -1;
1711 sss->last_len2 = -1;
1712 /* Initialize */
1713 sss->last_returned = 0;
1714 if (collate_c)
1715 sss->locale = NULL;
1716 else
1717 sss->locale = locale;
1718
1719 /*
1720 * To avoid somehow confusing a strxfrm() blob and an original string,
1721 * constantly keep track of the variety of data that buf1 and buf2
1722 * currently contain.
1723 *
1724 * Comparisons may be interleaved with conversion calls. Frequently,
1725 * conversions and comparisons are batched into two distinct phases,
1726 * but the correctness of caching cannot hinge upon this. For
1727 * comparison caching, buffer state is only trusted if cache_blob is
1728 * found set to false, whereas strxfrm() caching only trusts the state
1729 * when cache_blob is found set to true.
1730 *
1731 * Arbitrarily initialize cache_blob to true.
1732 */
1733 sss->cache_blob = true;
1734 sss->collate_c = collate_c;
1735 sss->typid = typid;
1736 ssup->ssup_extra = sss;
1737
1738 /*
1739 * If possible, plan to use the abbreviated keys optimization. The
1740 * core code may switch back to authoritative comparator should
1741 * abbreviation be aborted.
1742 */
1743 if (abbreviate)
1744 {
1745 sss->prop_card = 0.20;
1746 initHyperLogLog(&sss->abbr_card, 10);
1747 initHyperLogLog(&sss->full_card, 10);
1748 ssup->abbrev_full_comparator = ssup->comparator;
1752 }
1753 }
1754}
#define palloc_object(type)
Definition fe_memutils.h:74
void initHyperLogLog(hyperLogLogState *cState, uint8 bwidth)
Definition hyperloglog.c:66
bool pg_strxfrm_enabled(pg_locale_t locale)
Definition pg_locale.c:1418
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Datum(* abbrev_converter)(Datum original, SortSupport ssup)
int(* abbrev_full_comparator)(Datum x, Datum y, SortSupport ssup)
bool(* abbrev_abort)(int memtupcount, SortSupport ssup)
int ssup_datum_unsigned_cmp(Datum x, Datum y, SortSupport ssup)
Definition tuplesort.c:3122
static bool varstr_abbrev_abort(int memtupcount, SortSupport ssup)
Definition varlena.c:2154
static int varlenafastcmp_locale(Datum x, Datum y, SortSupport ssup)
Definition varlena.c:1842
static int bpcharfastcmp_c(Datum x, Datum y, SortSupport ssup)
Definition varlena.c:1797
static int namefastcmp_c(Datum x, Datum y, SortSupport ssup)
Definition varlena.c:1830
static int namefastcmp_locale(Datum x, Datum y, SortSupport ssup)
Definition varlena.c:1873
static Datum varstr_abbrev_convert(Datum original, SortSupport ssup)
Definition varlena.c:1987
static int varstrfastcmp_c(Datum x, Datum y, SortSupport ssup)
Definition varlena.c:1760
#define TEXTBUFLEN
Definition varlena.c:117

References SortSupportData::abbrev_abort, SortSupportData::abbrev_converter, SortSupportData::abbrev_full_comparator, SortSupportData::abbreviate, bpcharfastcmp_c(), check_collation_set(), pg_locale_struct::collate_is_c, collid, SortSupportData::comparator, fb(), initHyperLogLog(), namefastcmp_c(), namefastcmp_locale(), palloc(), palloc_object, pg_newlocale_from_collation(), pg_strxfrm_enabled(), ssup_datum_unsigned_cmp(), SortSupportData::ssup_extra, TEXTBUFLEN, varlenafastcmp_locale(), varstr_abbrev_abort(), varstr_abbrev_convert(), and varstrfastcmp_c().

Referenced by bpchar_sortsupport(), btbpchar_pattern_sortsupport(), btnamesortsupport(), bttext_pattern_sortsupport(), and bttextsortsupport().

◆ varstrfastcmp_c()

static int varstrfastcmp_c ( Datum  x,
Datum  y,
SortSupport  ssup 
)
static

Definition at line 1760 of file varlena.c.

1761{
1764 char *a1p,
1765 *a2p;
1766 int len1,
1767 len2,
1768 result;
1769
1770 a1p = VARDATA_ANY(arg1);
1771 a2p = VARDATA_ANY(arg2);
1772
1773 len1 = VARSIZE_ANY_EXHDR(arg1);
1774 len2 = VARSIZE_ANY_EXHDR(arg2);
1775
1776 result = memcmp(a1p, a2p, Min(len1, len2));
1777 if ((result == 0) && (len1 != len2))
1778 result = (len1 < len2) ? -1 : 1;
1779
1780 /* We can't afford to leak memory here. */
1781 if (PointerGetDatum(arg1) != x)
1782 pfree(arg1);
1783 if (PointerGetDatum(arg2) != y)
1784 pfree(arg2);
1785
1786 return result;
1787}

References DatumGetVarStringPP, fb(), Min, pfree(), PointerGetDatum(), VARDATA_ANY(), VARSIZE_ANY_EXHDR(), x, and y.

Referenced by varstr_sortsupport().

◆ varstrfastcmp_locale()

static int varstrfastcmp_locale ( char a1p,
int  len1,
char a2p,
int  len2,
SortSupport  ssup 
)
static

Definition at line 1887 of file varlena.c.

1888{
1890 int result;
1891 bool arg1_match;
1892
1893 /* Fast pre-check for equality, as discussed in varstr_cmp() */
1894 if (len1 == len2 && memcmp(a1p, a2p, len1) == 0)
1895 {
1896 /*
1897 * No change in buf1 or buf2 contents, so avoid changing last_len1 or
1898 * last_len2. Existing contents of buffers might still be used by
1899 * next call.
1900 *
1901 * It's fine to allow the comparison of BpChar padding bytes here,
1902 * even though that implies that the memcmp() will usually be
1903 * performed for BpChar callers (though multibyte characters could
1904 * still prevent that from occurring). The memcmp() is still very
1905 * cheap, and BpChar's funny semantics have us remove trailing spaces
1906 * (not limited to padding), so we need make no distinction between
1907 * padding space characters and "real" space characters.
1908 */
1909 return 0;
1910 }
1911
1912 if (sss->typid == BPCHAROID)
1913 {
1914 /* Get true number of bytes, ignoring trailing spaces */
1915 len1 = bpchartruelen(a1p, len1);
1916 len2 = bpchartruelen(a2p, len2);
1917 }
1918
1919 if (len1 >= sss->buflen1)
1920 {
1921 sss->buflen1 = Max(len1 + 1, Min(sss->buflen1 * 2, MaxAllocSize));
1922 sss->buf1 = repalloc(sss->buf1, sss->buflen1);
1923 }
1924 if (len2 >= sss->buflen2)
1925 {
1926 sss->buflen2 = Max(len2 + 1, Min(sss->buflen2 * 2, MaxAllocSize));
1927 sss->buf2 = repalloc(sss->buf2, sss->buflen2);
1928 }
1929
1930 /*
1931 * We're likely to be asked to compare the same strings repeatedly, and
1932 * memcmp() is so much cheaper than strcoll() that it pays to try to cache
1933 * comparisons, even though in general there is no reason to think that
1934 * that will work out (every string datum may be unique). Caching does
1935 * not slow things down measurably when it doesn't work out, and can speed
1936 * things up by rather a lot when it does. In part, this is because the
1937 * memcmp() compares data from cachelines that are needed in L1 cache even
1938 * when the last comparison's result cannot be reused.
1939 */
1940 arg1_match = true;
1941 if (len1 != sss->last_len1 || memcmp(sss->buf1, a1p, len1) != 0)
1942 {
1943 arg1_match = false;
1944 memcpy(sss->buf1, a1p, len1);
1945 sss->buf1[len1] = '\0';
1946 sss->last_len1 = len1;
1947 }
1948
1949 /*
1950 * If we're comparing the same two strings as last time, we can return the
1951 * same answer without calling strcoll() again. This is more likely than
1952 * it seems (at least with moderate to low cardinality sets), because
1953 * quicksort compares the same pivot against many values.
1954 */
1955 if (len2 != sss->last_len2 || memcmp(sss->buf2, a2p, len2) != 0)
1956 {
1957 memcpy(sss->buf2, a2p, len2);
1958 sss->buf2[len2] = '\0';
1959 sss->last_len2 = len2;
1960 }
1961 else if (arg1_match && !sss->cache_blob)
1962 {
1963 /* Use result cached following last actual strcoll() call */
1964 return sss->last_returned;
1965 }
1966
1967 result = pg_strcoll(sss->buf1, sss->buf2, sss->locale);
1968
1969 /* Break tie if necessary. */
1970 if (result == 0 && sss->locale->deterministic)
1971 result = strcmp(sss->buf1, sss->buf2);
1972
1973 /* Cache result, perhaps saving an expensive strcoll() call next time */
1974 sss->cache_blob = false;
1975 sss->last_returned = result;
1976 return result;
1977}
int pg_strcoll(const char *arg1, const char *arg2, pg_locale_t locale)
Definition pg_locale.c:1384

References bpchartruelen(), fb(), Max, MaxAllocSize, Min, pg_strcoll(), repalloc(), and SortSupportData::ssup_extra.

Referenced by namefastcmp_locale(), and varlenafastcmp_locale().