PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
varchar.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * varchar.c
4 * Functions for the built-in types char(n) and varchar(n).
5 *
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 *
10 * IDENTIFICATION
11 * src/backend/utils/adt/varchar.c
12 *
13 *-------------------------------------------------------------------------
14 */
15#include "postgres.h"
16
17#include "access/detoast.h"
18#include "access/htup_details.h"
20#include "catalog/pg_type.h"
21#include "common/hashfn.h"
22#include "libpq/pqformat.h"
23#include "mb/pg_wchar.h"
24#include "nodes/nodeFuncs.h"
25#include "nodes/supportnodes.h"
26#include "utils/array.h"
27#include "utils/builtins.h"
28#include "utils/pg_locale.h"
29#include "utils/varlena.h"
30
31/* common code for bpchartypmodin and varchartypmodin */
32static int32
33anychar_typmodin(ArrayType *ta, const char *typename)
34{
35 int32 typmod;
36 int32 *tl;
37 int n;
38
39 tl = ArrayGetIntegerTypmods(ta, &n);
40
41 /*
42 * we're not too tense about good error message here because grammar
43 * shouldn't allow wrong number of modifiers for CHAR
44 */
45 if (n != 1)
47 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
48 errmsg("invalid type modifier")));
49
50 if (*tl < 1)
52 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
53 errmsg("length for type %s must be at least 1", typename)));
54 if (*tl > MaxAttrSize)
56 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
57 errmsg("length for type %s cannot exceed %d",
58 typename, MaxAttrSize)));
59
60 /*
61 * For largely historical reasons, the typmod is VARHDRSZ plus the number
62 * of characters; there is enough client-side code that knows about that
63 * that we'd better not change it.
64 */
65 typmod = VARHDRSZ + *tl;
66
67 return typmod;
68}
69
70/* common code for bpchartypmodout and varchartypmodout */
71static char *
73{
74 char *res = (char *) palloc(64);
75
76 if (typmod > VARHDRSZ)
77 snprintf(res, 64, "(%d)", (int) (typmod - VARHDRSZ));
78 else
79 *res = '\0';
80
81 return res;
82}
83
84
85/*
86 * CHAR() and VARCHAR() types are part of the SQL standard. CHAR()
87 * is for blank-padded string whose length is specified in CREATE TABLE.
88 * VARCHAR is for storing string whose length is at most the length specified
89 * at CREATE TABLE time.
90 *
91 * It's hard to implement these types because we cannot figure out
92 * the length of the type from the type itself. I changed (hopefully all) the
93 * fmgr calls that invoke input functions of a data type to supply the
94 * length also. (eg. in INSERTs, we have the tupleDescriptor which contains
95 * the length of the attributes and hence the exact length of the char() or
96 * varchar(). We pass this to bpcharin() or varcharin().) In the case where
97 * we cannot determine the length, we pass in -1 instead and the input
98 * converter does not enforce any length check.
99 *
100 * We actually implement this as a varlena so that we don't have to pass in
101 * the length for the comparison functions. (The difference between these
102 * types and "text" is that we truncate and possibly blank-pad the string
103 * at insertion time.)
104 *
105 * - ay 6/95
106 */
107
108
109/*****************************************************************************
110 * bpchar - char() *
111 *****************************************************************************/
112
113/*
114 * bpchar_input -- common guts of bpcharin and bpcharrecv
115 *
116 * s is the input text of length len (may not be null-terminated)
117 * atttypmod is the typmod value to apply
118 *
119 * Note that atttypmod is measured in characters, which
120 * is not necessarily the same as the number of bytes.
121 *
122 * If the input string is too long, raise an error, unless the extra
123 * characters are spaces, in which case they're truncated. (per SQL)
124 *
125 * If escontext points to an ErrorSaveContext node, that is filled instead
126 * of throwing an error; the caller must check SOFT_ERROR_OCCURRED()
127 * to detect errors.
128 */
129static BpChar *
130bpchar_input(const char *s, size_t len, int32 atttypmod, Node *escontext)
131{
132 BpChar *result;
133 char *r;
134 size_t maxlen;
135
136 /* If typmod is -1 (or invalid), use the actual string length */
137 if (atttypmod < (int32) VARHDRSZ)
138 maxlen = len;
139 else
140 {
141 size_t charlen; /* number of CHARACTERS in the input */
142
143 maxlen = atttypmod - VARHDRSZ;
144 charlen = pg_mbstrlen_with_len(s, len);
145 if (charlen > maxlen)
146 {
147 /* Verify that extra characters are spaces, and clip them off */
148 size_t mbmaxlen = pg_mbcharcliplen(s, len, maxlen);
149 size_t j;
150
151 /*
152 * at this point, len is the actual BYTE length of the input
153 * string, maxlen is the max number of CHARACTERS allowed for this
154 * bpchar type, mbmaxlen is the length in BYTES of those chars.
155 */
156 for (j = mbmaxlen; j < len; j++)
157 {
158 if (s[j] != ' ')
159 ereturn(escontext, NULL,
160 (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
161 errmsg("value too long for type character(%d)",
162 (int) maxlen)));
163 }
164
165 /*
166 * Now we set maxlen to the necessary byte length, not the number
167 * of CHARACTERS!
168 */
169 maxlen = len = mbmaxlen;
170 }
171 else
172 {
173 /*
174 * Now we set maxlen to the necessary byte length, not the number
175 * of CHARACTERS!
176 */
177 maxlen = len + (maxlen - charlen);
178 }
179 }
180
181 result = (BpChar *) palloc(maxlen + VARHDRSZ);
182 SET_VARSIZE(result, maxlen + VARHDRSZ);
183 r = VARDATA(result);
184 memcpy(r, s, len);
185
186 /* blank pad the string if necessary */
187 if (maxlen > len)
188 memset(r + len, ' ', maxlen - len);
189
190 return result;
191}
192
193/*
194 * Convert a C string to CHARACTER internal representation. atttypmod
195 * is the declared length of the type plus VARHDRSZ.
196 */
197Datum
199{
200 char *s = PG_GETARG_CSTRING(0);
201#ifdef NOT_USED
202 Oid typelem = PG_GETARG_OID(1);
203#endif
204 int32 atttypmod = PG_GETARG_INT32(2);
205 BpChar *result;
206
207 result = bpchar_input(s, strlen(s), atttypmod, fcinfo->context);
208 PG_RETURN_BPCHAR_P(result);
209}
210
211
212/*
213 * Convert a CHARACTER value to a C string.
214 *
215 * Uses the text conversion functions, which is only appropriate if BpChar
216 * and text are equivalent types.
217 */
218Datum
220{
221 Datum txt = PG_GETARG_DATUM(0);
222
224}
225
226/*
227 * bpcharrecv - converts external binary format to bpchar
228 */
229Datum
231{
233#ifdef NOT_USED
234 Oid typelem = PG_GETARG_OID(1);
235#endif
236 int32 atttypmod = PG_GETARG_INT32(2);
237 BpChar *result;
238 char *str;
239 int nbytes;
240
241 str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
242 result = bpchar_input(str, nbytes, atttypmod, NULL);
243 pfree(str);
244 PG_RETURN_BPCHAR_P(result);
245}
246
247/*
248 * bpcharsend - converts bpchar to binary format
249 */
250Datum
252{
253 /* Exactly the same as textsend, so share code */
254 return textsend(fcinfo);
255}
256
257
258/*
259 * Converts a CHARACTER type to the specified size.
260 *
261 * maxlen is the typmod, ie, declared length plus VARHDRSZ bytes.
262 * isExplicit is true if this is for an explicit cast to char(N).
263 *
264 * Truncation rules: for an explicit cast, silently truncate to the given
265 * length; for an implicit cast, raise error unless extra characters are
266 * all spaces. (This is sort-of per SQL: the spec would actually have us
267 * raise a "completion condition" for the explicit cast case, but Postgres
268 * hasn't got such a concept.)
269 */
270Datum
272{
274 int32 maxlen = PG_GETARG_INT32(1);
275 bool isExplicit = PG_GETARG_BOOL(2);
276 BpChar *result;
277 int32 len;
278 char *r;
279 char *s;
280 int i;
281 int charlen; /* number of characters in the input string +
282 * VARHDRSZ */
283
284 /* No work if typmod is invalid */
285 if (maxlen < (int32) VARHDRSZ)
287
288 maxlen -= VARHDRSZ;
289
291 s = VARDATA_ANY(source);
292
293 charlen = pg_mbstrlen_with_len(s, len);
294
295 /* No work if supplied data matches typmod already */
296 if (charlen == maxlen)
298
299 if (charlen > maxlen)
300 {
301 /* Verify that extra characters are spaces, and clip them off */
302 size_t maxmblen;
303
304 maxmblen = pg_mbcharcliplen(s, len, maxlen);
305
306 if (!isExplicit)
307 {
308 for (i = maxmblen; i < len; i++)
309 if (s[i] != ' ')
311 (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
312 errmsg("value too long for type character(%d)",
313 maxlen)));
314 }
315
316 len = maxmblen;
317
318 /*
319 * At this point, maxlen is the necessary byte length, not the number
320 * of CHARACTERS!
321 */
322 maxlen = len;
323 }
324 else
325 {
326 /*
327 * At this point, maxlen is the necessary byte length, not the number
328 * of CHARACTERS!
329 */
330 maxlen = len + (maxlen - charlen);
331 }
332
333 Assert(maxlen >= len);
334
335 result = palloc(maxlen + VARHDRSZ);
336 SET_VARSIZE(result, maxlen + VARHDRSZ);
337 r = VARDATA(result);
338
339 memcpy(r, s, len);
340
341 /* blank pad the string if necessary */
342 if (maxlen > len)
343 memset(r + len, ' ', maxlen - len);
344
345 PG_RETURN_BPCHAR_P(result);
346}
347
348
349/* char_bpchar()
350 * Convert char to bpchar(1).
351 */
352Datum
354{
355 char c = PG_GETARG_CHAR(0);
356 BpChar *result;
357
358 result = (BpChar *) palloc(VARHDRSZ + 1);
359
360 SET_VARSIZE(result, VARHDRSZ + 1);
361 *(VARDATA(result)) = c;
362
363 PG_RETURN_BPCHAR_P(result);
364}
365
366
367/* bpchar_name()
368 * Converts a bpchar() type to a NameData type.
369 */
370Datum
372{
374 char *s_data;
375 Name result;
376 int len;
377
379 s_data = VARDATA_ANY(s);
380
381 /* Truncate oversize input */
382 if (len >= NAMEDATALEN)
383 len = pg_mbcliplen(s_data, len, NAMEDATALEN - 1);
384
385 /* Remove trailing blanks */
386 while (len > 0)
387 {
388 if (s_data[len - 1] != ' ')
389 break;
390 len--;
391 }
392
393 /* We use palloc0 here to ensure result is zero-padded */
394 result = (Name) palloc0(NAMEDATALEN);
395 memcpy(NameStr(*result), s_data, len);
396
397 PG_RETURN_NAME(result);
398}
399
400/* name_bpchar()
401 * Converts a NameData type to a bpchar type.
402 *
403 * Uses the text conversion functions, which is only appropriate if BpChar
404 * and text are equivalent types.
405 */
406Datum
408{
409 Name s = PG_GETARG_NAME(0);
410 BpChar *result;
411
412 result = (BpChar *) cstring_to_text(NameStr(*s));
413 PG_RETURN_BPCHAR_P(result);
414}
415
416Datum
418{
420
422}
423
424Datum
426{
427 int32 typmod = PG_GETARG_INT32(0);
428
430}
431
432
433/*****************************************************************************
434 * varchar - varchar(n)
435 *
436 * Note: varchar piggybacks on type text for most operations, and so has no
437 * C-coded functions except for I/O and typmod checking.
438 *****************************************************************************/
439
440/*
441 * varchar_input -- common guts of varcharin and varcharrecv
442 *
443 * s is the input text of length len (may not be null-terminated)
444 * atttypmod is the typmod value to apply
445 *
446 * Note that atttypmod is measured in characters, which
447 * is not necessarily the same as the number of bytes.
448 *
449 * If the input string is too long, raise an error, unless the extra
450 * characters are spaces, in which case they're truncated. (per SQL)
451 *
452 * If escontext points to an ErrorSaveContext node, that is filled instead
453 * of throwing an error; the caller must check SOFT_ERROR_OCCURRED()
454 * to detect errors.
455 */
456static VarChar *
457varchar_input(const char *s, size_t len, int32 atttypmod, Node *escontext)
458{
459 VarChar *result;
460 size_t maxlen;
461
462 maxlen = atttypmod - VARHDRSZ;
463
464 if (atttypmod >= (int32) VARHDRSZ && len > maxlen)
465 {
466 /* Verify that extra characters are spaces, and clip them off */
467 size_t mbmaxlen = pg_mbcharcliplen(s, len, maxlen);
468 size_t j;
469
470 for (j = mbmaxlen; j < len; j++)
471 {
472 if (s[j] != ' ')
473 ereturn(escontext, NULL,
474 (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
475 errmsg("value too long for type character varying(%d)",
476 (int) maxlen)));
477 }
478
479 len = mbmaxlen;
480 }
481
482 /*
483 * We can use cstring_to_text_with_len because VarChar and text are
484 * binary-compatible types.
485 */
486 result = (VarChar *) cstring_to_text_with_len(s, len);
487 return result;
488}
489
490/*
491 * Convert a C string to VARCHAR internal representation. atttypmod
492 * is the declared length of the type plus VARHDRSZ.
493 */
494Datum
496{
497 char *s = PG_GETARG_CSTRING(0);
498#ifdef NOT_USED
499 Oid typelem = PG_GETARG_OID(1);
500#endif
501 int32 atttypmod = PG_GETARG_INT32(2);
502 VarChar *result;
503
504 result = varchar_input(s, strlen(s), atttypmod, fcinfo->context);
505 PG_RETURN_VARCHAR_P(result);
506}
507
508
509/*
510 * Convert a VARCHAR value to a C string.
511 *
512 * Uses the text to C string conversion function, which is only appropriate
513 * if VarChar and text are equivalent types.
514 */
515Datum
517{
518 Datum txt = PG_GETARG_DATUM(0);
519
521}
522
523/*
524 * varcharrecv - converts external binary format to varchar
525 */
526Datum
528{
530#ifdef NOT_USED
531 Oid typelem = PG_GETARG_OID(1);
532#endif
533 int32 atttypmod = PG_GETARG_INT32(2);
534 VarChar *result;
535 char *str;
536 int nbytes;
537
538 str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
539 result = varchar_input(str, nbytes, atttypmod, NULL);
540 pfree(str);
541 PG_RETURN_VARCHAR_P(result);
542}
543
544/*
545 * varcharsend - converts varchar to binary format
546 */
547Datum
549{
550 /* Exactly the same as textsend, so share code */
551 return textsend(fcinfo);
552}
553
554
555/*
556 * varchar_support()
557 *
558 * Planner support function for the varchar() length coercion function.
559 *
560 * Currently, the only interesting thing we can do is flatten calls that set
561 * the new maximum length >= the previous maximum length. We can ignore the
562 * isExplicit argument, since that only affects truncation cases.
563 */
564Datum
566{
567 Node *rawreq = (Node *) PG_GETARG_POINTER(0);
568 Node *ret = NULL;
569
570 if (IsA(rawreq, SupportRequestSimplify))
571 {
573 FuncExpr *expr = req->fcall;
574 Node *typmod;
575
576 Assert(list_length(expr->args) >= 2);
577
578 typmod = (Node *) lsecond(expr->args);
579
580 if (IsA(typmod, Const) && !((Const *) typmod)->constisnull)
581 {
582 Node *source = (Node *) linitial(expr->args);
583 int32 old_typmod = exprTypmod(source);
584 int32 new_typmod = DatumGetInt32(((Const *) typmod)->constvalue);
585 int32 old_max = old_typmod - VARHDRSZ;
586 int32 new_max = new_typmod - VARHDRSZ;
587
588 if (new_typmod < 0 || (old_typmod >= 0 && old_max <= new_max))
589 ret = relabel_to_typmod(source, new_typmod);
590 }
591 }
592
594}
595
596/*
597 * Converts a VARCHAR type to the specified size.
598 *
599 * maxlen is the typmod, ie, declared length plus VARHDRSZ bytes.
600 * isExplicit is true if this is for an explicit cast to varchar(N).
601 *
602 * Truncation rules: for an explicit cast, silently truncate to the given
603 * length; for an implicit cast, raise error unless extra characters are
604 * all spaces. (This is sort-of per SQL: the spec would actually have us
605 * raise a "completion condition" for the explicit cast case, but Postgres
606 * hasn't got such a concept.)
607 */
608Datum
610{
612 int32 typmod = PG_GETARG_INT32(1);
613 bool isExplicit = PG_GETARG_BOOL(2);
614 int32 len,
615 maxlen;
616 size_t maxmblen;
617 int i;
618 char *s_data;
619
621 s_data = VARDATA_ANY(source);
622 maxlen = typmod - VARHDRSZ;
623
624 /* No work if typmod is invalid or supplied data fits it already */
625 if (maxlen < 0 || len <= maxlen)
627
628 /* only reach here if string is too long... */
629
630 /* truncate multibyte string preserving multibyte boundary */
631 maxmblen = pg_mbcharcliplen(s_data, len, maxlen);
632
633 if (!isExplicit)
634 {
635 for (i = maxmblen; i < len; i++)
636 if (s_data[i] != ' ')
638 (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
639 errmsg("value too long for type character varying(%d)",
640 maxlen)));
641 }
642
644 maxmblen));
645}
646
647Datum
649{
651
652 PG_RETURN_INT32(anychar_typmodin(ta, "varchar"));
653}
654
655Datum
657{
658 int32 typmod = PG_GETARG_INT32(0);
659
661}
662
663
664/*****************************************************************************
665 * Exported functions
666 *****************************************************************************/
667
668/* "True" length (not counting trailing blanks) of a BpChar */
669static inline int
671{
673}
674
675int
676bpchartruelen(char *s, int len)
677{
678 int i;
679
680 /*
681 * Note that we rely on the assumption that ' ' is a singleton unit on
682 * every supported multibyte server encoding.
683 */
684 for (i = len - 1; i >= 0; i--)
685 {
686 if (s[i] != ' ')
687 break;
688 }
689 return i + 1;
690}
691
692Datum
694{
696 int len;
697
698 /* get number of bytes, ignoring trailing spaces */
699 len = bcTruelen(arg);
700
701 /* in multibyte encoding, convert to number of characters */
704
706}
707
708Datum
710{
712
713 /* We need not detoast the input at all */
715}
716
717
718/*****************************************************************************
719 * Comparison Functions used for bpchar
720 *
721 * Note: btree indexes need these routines not to leak memory; therefore,
722 * be careful to free working copies of toasted datums. Most places don't
723 * need to be so careful.
724 *****************************************************************************/
725
726static void
728{
729 if (!OidIsValid(collid))
730 {
731 /*
732 * This typically means that the parser could not resolve a conflict
733 * of implicit collations, so report it that way.
734 */
736 (errcode(ERRCODE_INDETERMINATE_COLLATION),
737 errmsg("could not determine which collation to use for string comparison"),
738 errhint("Use the COLLATE clause to set the collation explicitly.")));
739 }
740}
741
742Datum
744{
745 BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
746 BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
747 int len1,
748 len2;
749 bool result;
751 pg_locale_t mylocale;
752
754
755 len1 = bcTruelen(arg1);
756 len2 = bcTruelen(arg2);
757
759
760 if (mylocale->deterministic)
761 {
762 /*
763 * Since we only care about equality or not-equality, we can avoid all
764 * the expense of strcoll() here, and just do bitwise comparison.
765 */
766 if (len1 != len2)
767 result = false;
768 else
769 result = (memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), len1) == 0);
770 }
771 else
772 {
773 result = (varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
774 collid) == 0);
775 }
776
777 PG_FREE_IF_COPY(arg1, 0);
778 PG_FREE_IF_COPY(arg2, 1);
779
780 PG_RETURN_BOOL(result);
781}
782
783Datum
785{
786 BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
787 BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
788 int len1,
789 len2;
790 bool result;
792 pg_locale_t mylocale;
793
795
796 len1 = bcTruelen(arg1);
797 len2 = bcTruelen(arg2);
798
800
801 if (mylocale->deterministic)
802 {
803 /*
804 * Since we only care about equality or not-equality, we can avoid all
805 * the expense of strcoll() here, and just do bitwise comparison.
806 */
807 if (len1 != len2)
808 result = true;
809 else
810 result = (memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), len1) != 0);
811 }
812 else
813 {
814 result = (varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
815 collid) != 0);
816 }
817
818 PG_FREE_IF_COPY(arg1, 0);
819 PG_FREE_IF_COPY(arg2, 1);
820
821 PG_RETURN_BOOL(result);
822}
823
824Datum
826{
827 BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
828 BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
829 int len1,
830 len2;
831 int cmp;
832
833 len1 = bcTruelen(arg1);
834 len2 = bcTruelen(arg2);
835
836 cmp = varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
838
839 PG_FREE_IF_COPY(arg1, 0);
840 PG_FREE_IF_COPY(arg2, 1);
841
842 PG_RETURN_BOOL(cmp < 0);
843}
844
845Datum
847{
848 BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
849 BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
850 int len1,
851 len2;
852 int cmp;
853
854 len1 = bcTruelen(arg1);
855 len2 = bcTruelen(arg2);
856
857 cmp = varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
859
860 PG_FREE_IF_COPY(arg1, 0);
861 PG_FREE_IF_COPY(arg2, 1);
862
863 PG_RETURN_BOOL(cmp <= 0);
864}
865
866Datum
868{
869 BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
870 BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
871 int len1,
872 len2;
873 int cmp;
874
875 len1 = bcTruelen(arg1);
876 len2 = bcTruelen(arg2);
877
878 cmp = varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
880
881 PG_FREE_IF_COPY(arg1, 0);
882 PG_FREE_IF_COPY(arg2, 1);
883
884 PG_RETURN_BOOL(cmp > 0);
885}
886
887Datum
889{
890 BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
891 BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
892 int len1,
893 len2;
894 int cmp;
895
896 len1 = bcTruelen(arg1);
897 len2 = bcTruelen(arg2);
898
899 cmp = varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
901
902 PG_FREE_IF_COPY(arg1, 0);
903 PG_FREE_IF_COPY(arg2, 1);
904
905 PG_RETURN_BOOL(cmp >= 0);
906}
907
908Datum
910{
911 BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
912 BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
913 int len1,
914 len2;
915 int cmp;
916
917 len1 = bcTruelen(arg1);
918 len2 = bcTruelen(arg2);
919
920 cmp = varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
922
923 PG_FREE_IF_COPY(arg1, 0);
924 PG_FREE_IF_COPY(arg2, 1);
925
927}
928
929Datum
931{
933 Oid collid = ssup->ssup_collation;
934 MemoryContext oldcontext;
935
936 oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
937
938 /* Use generic string SortSupport */
939 varstr_sortsupport(ssup, BPCHAROID, collid);
940
941 MemoryContextSwitchTo(oldcontext);
942
944}
945
946Datum
948{
949 BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
950 BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
951 int len1,
952 len2;
953 int cmp;
954
955 len1 = bcTruelen(arg1);
956 len2 = bcTruelen(arg2);
957
958 cmp = varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
960
961 PG_RETURN_BPCHAR_P((cmp >= 0) ? arg1 : arg2);
962}
963
964Datum
966{
967 BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
968 BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
969 int len1,
970 len2;
971 int cmp;
972
973 len1 = bcTruelen(arg1);
974 len2 = bcTruelen(arg2);
975
976 cmp = varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
978
979 PG_RETURN_BPCHAR_P((cmp <= 0) ? arg1 : arg2);
980}
981
982
983/*
984 * bpchar needs a specialized hash function because we want to ignore
985 * trailing blanks in comparisons.
986 */
987Datum
989{
992 char *keydata;
993 int keylen;
994 pg_locale_t mylocale;
995 Datum result;
996
997 if (!collid)
999 (errcode(ERRCODE_INDETERMINATE_COLLATION),
1000 errmsg("could not determine which collation to use for string hashing"),
1001 errhint("Use the COLLATE clause to set the collation explicitly.")));
1002
1003 keydata = VARDATA_ANY(key);
1004 keylen = bcTruelen(key);
1005
1007
1008 if (mylocale->deterministic)
1009 {
1010 result = hash_any((unsigned char *) keydata, keylen);
1011 }
1012 else
1013 {
1014 Size bsize,
1015 rsize;
1016 char *buf;
1017
1018 bsize = pg_strnxfrm(NULL, 0, keydata, keylen, mylocale);
1019 buf = palloc(bsize + 1);
1020
1021 rsize = pg_strnxfrm(buf, bsize + 1, keydata, keylen, mylocale);
1022
1023 /* the second call may return a smaller value than the first */
1024 if (rsize > bsize)
1025 elog(ERROR, "pg_strnxfrm() returned unexpected result");
1026
1027 /*
1028 * In principle, there's no reason to include the terminating NUL
1029 * character in the hash, but it was done before and the behavior must
1030 * be preserved.
1031 */
1032 result = hash_any((uint8_t *) buf, bsize + 1);
1033
1034 pfree(buf);
1035 }
1036
1037 /* Avoid leaking memory for toasted inputs */
1038 PG_FREE_IF_COPY(key, 0);
1039
1040 return result;
1041}
1042
1043Datum
1045{
1048 char *keydata;
1049 int keylen;
1050 pg_locale_t mylocale;
1051 Datum result;
1052
1053 if (!collid)
1054 ereport(ERROR,
1055 (errcode(ERRCODE_INDETERMINATE_COLLATION),
1056 errmsg("could not determine which collation to use for string hashing"),
1057 errhint("Use the COLLATE clause to set the collation explicitly.")));
1058
1059 keydata = VARDATA_ANY(key);
1060 keylen = bcTruelen(key);
1061
1063
1064 if (mylocale->deterministic)
1065 {
1066 result = hash_any_extended((unsigned char *) keydata, keylen,
1067 PG_GETARG_INT64(1));
1068 }
1069 else
1070 {
1071 Size bsize,
1072 rsize;
1073 char *buf;
1074
1075 bsize = pg_strnxfrm(NULL, 0, keydata, keylen, mylocale);
1076 buf = palloc(bsize + 1);
1077
1078 rsize = pg_strnxfrm(buf, bsize + 1, keydata, keylen, mylocale);
1079
1080 /* the second call may return a smaller value than the first */
1081 if (rsize > bsize)
1082 elog(ERROR, "pg_strnxfrm() returned unexpected result");
1083
1084 /*
1085 * In principle, there's no reason to include the terminating NUL
1086 * character in the hash, but it was done before and the behavior must
1087 * be preserved.
1088 */
1089 result = hash_any_extended((uint8_t *) buf, bsize + 1,
1090 PG_GETARG_INT64(1));
1091
1092 pfree(buf);
1093 }
1094
1095 PG_FREE_IF_COPY(key, 0);
1096
1097 return result;
1098}
1099
1100/*
1101 * The following operators support character-by-character comparison
1102 * of bpchar datums, to allow building indexes suitable for LIKE clauses.
1103 * Note that the regular bpchareq/bpcharne comparison operators, and
1104 * regular support functions 1 and 2 with "C" collation are assumed to be
1105 * compatible with these!
1106 */
1107
1108static int
1110{
1111 int result;
1112 int len1,
1113 len2;
1114
1115 len1 = bcTruelen(arg1);
1116 len2 = bcTruelen(arg2);
1117
1118 result = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
1119 if (result != 0)
1120 return result;
1121 else if (len1 < len2)
1122 return -1;
1123 else if (len1 > len2)
1124 return 1;
1125 else
1126 return 0;
1127}
1128
1129
1130Datum
1132{
1133 BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
1134 BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
1135 int result;
1136
1137 result = internal_bpchar_pattern_compare(arg1, arg2);
1138
1139 PG_FREE_IF_COPY(arg1, 0);
1140 PG_FREE_IF_COPY(arg2, 1);
1141
1142 PG_RETURN_BOOL(result < 0);
1143}
1144
1145
1146Datum
1148{
1149 BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
1150 BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
1151 int result;
1152
1153 result = internal_bpchar_pattern_compare(arg1, arg2);
1154
1155 PG_FREE_IF_COPY(arg1, 0);
1156 PG_FREE_IF_COPY(arg2, 1);
1157
1158 PG_RETURN_BOOL(result <= 0);
1159}
1160
1161
1162Datum
1164{
1165 BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
1166 BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
1167 int result;
1168
1169 result = internal_bpchar_pattern_compare(arg1, arg2);
1170
1171 PG_FREE_IF_COPY(arg1, 0);
1172 PG_FREE_IF_COPY(arg2, 1);
1173
1174 PG_RETURN_BOOL(result >= 0);
1175}
1176
1177
1178Datum
1180{
1181 BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
1182 BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
1183 int result;
1184
1185 result = internal_bpchar_pattern_compare(arg1, arg2);
1186
1187 PG_FREE_IF_COPY(arg1, 0);
1188 PG_FREE_IF_COPY(arg2, 1);
1189
1190 PG_RETURN_BOOL(result > 0);
1191}
1192
1193
1194Datum
1196{
1197 BpChar *arg1 = PG_GETARG_BPCHAR_PP(0);
1198 BpChar *arg2 = PG_GETARG_BPCHAR_PP(1);
1199 int result;
1200
1201 result = internal_bpchar_pattern_compare(arg1, arg2);
1202
1203 PG_FREE_IF_COPY(arg1, 0);
1204 PG_FREE_IF_COPY(arg2, 1);
1205
1206 PG_RETURN_INT32(result);
1207}
1208
1209
1210Datum
1212{
1214 MemoryContext oldcontext;
1215
1216 oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
1217
1218 /* Use generic string SortSupport, forcing "C" collation */
1219 varstr_sortsupport(ssup, BPCHAROID, C_COLLATION_OID);
1220
1221 MemoryContextSwitchTo(oldcontext);
1222
1224}
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:263
int32 * ArrayGetIntegerTypmods(ArrayType *arr, int *n)
Definition: arrayutils.c:233
#define TextDatumGetCString(d)
Definition: builtins.h:98
#define NameStr(name)
Definition: c.h:700
NameData * Name
Definition: c.h:698
#define Min(x, y)
Definition: c.h:958
#define VARHDRSZ
Definition: c.h:646
#define Assert(condition)
Definition: c.h:812
int32_t int32
Definition: c.h:481
#define OidIsValid(objectId)
Definition: c.h:729
size_t Size
Definition: c.h:559
Oid collid
Size toast_raw_datum_size(Datum value)
Definition: detoast.c:545
int errhint(const char *fmt,...)
Definition: elog.c:1317
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ereturn(context, dummy_value,...)
Definition: elog.h:277
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define ereport(elevel,...)
Definition: elog.h:149
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_GETARG_CHAR(n)
Definition: fmgr.h:273
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278
#define PG_GETARG_BPCHAR_PP(n)
Definition: fmgr.h:310
#define PG_GETARG_VARCHAR_PP(n)
Definition: fmgr.h:311
#define PG_RETURN_BPCHAR_P(x)
Definition: fmgr.h:373
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
#define PG_RETURN_NAME(x)
Definition: fmgr.h:363
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_RETURN_VARCHAR_P(x)
Definition: fmgr.h:374
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
static Datum hash_any_extended(const unsigned char *k, int keylen, uint64 seed)
Definition: hashfn.h:37
static Datum hash_any(const unsigned char *k, int keylen)
Definition: hashfn.h:31
const char * str
#define MaxAttrSize
Definition: htup_details.h:583
int j
Definition: isn.c:73
int i
Definition: isn.c:72
int pg_mbstrlen_with_len(const char *mbstr, int limit)
Definition: mbutils.c:1057
int pg_mbcharcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:1125
int pg_mbcliplen(const char *mbstr, int len, int limit)
Definition: mbutils.c:1083
int pg_database_encoding_max_length(void)
Definition: mbutils.c:1546
void pfree(void *pointer)
Definition: mcxt.c:1521
void * palloc0(Size size)
Definition: mcxt.c:1347
void * palloc(Size size)
Definition: mcxt.c:1317
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:298
Node * relabel_to_typmod(Node *expr, int32 typmod)
Definition: nodeFuncs.c:684
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
void * arg
#define NAMEDATALEN
const void size_t len
static int list_length(const List *l)
Definition: pg_list.h:152
#define linitial(l)
Definition: pg_list.h:178
#define lsecond(l)
Definition: pg_list.h:183
size_t pg_strnxfrm(char *dest, size_t destsize, const char *src, ssize_t srclen, pg_locale_t locale)
Definition: pg_locale.c:1680
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition: pg_locale.c:1341
static rewind_source * source
Definition: pg_rewind.c:89
static char * buf
Definition: pg_test_fsync.c:72
#define snprintf
Definition: port.h:238
uintptr_t Datum
Definition: postgres.h:64
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:202
unsigned int Oid
Definition: postgres_ext.h:31
char * pq_getmsgtext(StringInfo msg, int rawbytes, int *nbytes)
Definition: pqformat.c:546
char * c
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:743
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
StringInfoData * StringInfo
Definition: stringinfo.h:54
List * args
Definition: primnodes.h:768
Definition: nodes.h:129
MemoryContext ssup_cxt
Definition: sortsupport.h:66
Definition: c.h:695
bool deterministic
Definition: pg_locale.h:69
Definition: c.h:641
#define VARDATA(PTR)
Definition: varatt.h:278
#define VARDATA_ANY(PTR)
Definition: varatt.h:324
#define SET_VARSIZE(PTR, len)
Definition: varatt.h:305
#define VARSIZE_ANY_EXHDR(PTR)
Definition: varatt.h:317
Datum bpcharrecv(PG_FUNCTION_ARGS)
Definition: varchar.c:230
Datum bpcharne(PG_FUNCTION_ARGS)
Definition: varchar.c:784
Datum varchar_support(PG_FUNCTION_ARGS)
Definition: varchar.c:565
static int bcTruelen(BpChar *arg)
Definition: varchar.c:670
Datum bpchar_larger(PG_FUNCTION_ARGS)
Definition: varchar.c:947
Datum bpchar_pattern_lt(PG_FUNCTION_ARGS)
Definition: varchar.c:1131
static BpChar * bpchar_input(const char *s, size_t len, int32 atttypmod, Node *escontext)
Definition: varchar.c:130
Datum varcharrecv(PG_FUNCTION_ARGS)
Definition: varchar.c:527
Datum varcharout(PG_FUNCTION_ARGS)
Definition: varchar.c:516
int bpchartruelen(char *s, int len)
Definition: varchar.c:676
Datum bpchar_pattern_le(PG_FUNCTION_ARGS)
Definition: varchar.c:1147
Datum bpcharsend(PG_FUNCTION_ARGS)
Definition: varchar.c:251
Datum varcharsend(PG_FUNCTION_ARGS)
Definition: varchar.c:548
Datum bpcharlen(PG_FUNCTION_ARGS)
Definition: varchar.c:693
Datum bpchar(PG_FUNCTION_ARGS)
Definition: varchar.c:271
Datum name_bpchar(PG_FUNCTION_ARGS)
Definition: varchar.c:407
Datum bpchar_sortsupport(PG_FUNCTION_ARGS)
Definition: varchar.c:930
Datum bpchargt(PG_FUNCTION_ARGS)
Definition: varchar.c:867
Datum btbpchar_pattern_sortsupport(PG_FUNCTION_ARGS)
Definition: varchar.c:1211
static char * anychar_typmodout(int32 typmod)
Definition: varchar.c:72
Datum bpcharge(PG_FUNCTION_ARGS)
Definition: varchar.c:888
static int internal_bpchar_pattern_compare(BpChar *arg1, BpChar *arg2)
Definition: varchar.c:1109
Datum bpchar_pattern_ge(PG_FUNCTION_ARGS)
Definition: varchar.c:1163
static void check_collation_set(Oid collid)
Definition: varchar.c:727
Datum bpchartypmodout(PG_FUNCTION_ARGS)
Definition: varchar.c:425
Datum varcharin(PG_FUNCTION_ARGS)
Definition: varchar.c:495
Datum bpchartypmodin(PG_FUNCTION_ARGS)
Definition: varchar.c:417
static VarChar * varchar_input(const char *s, size_t len, int32 atttypmod, Node *escontext)
Definition: varchar.c:457
Datum varchar(PG_FUNCTION_ARGS)
Definition: varchar.c:609
Datum bpcharlt(PG_FUNCTION_ARGS)
Definition: varchar.c:825
Datum hashbpcharextended(PG_FUNCTION_ARGS)
Definition: varchar.c:1044
Datum bpchar_smaller(PG_FUNCTION_ARGS)
Definition: varchar.c:965
Datum bpcharout(PG_FUNCTION_ARGS)
Definition: varchar.c:219
static int32 anychar_typmodin(ArrayType *ta, const char *typename)
Definition: varchar.c:33
Datum bpcharcmp(PG_FUNCTION_ARGS)
Definition: varchar.c:909
Datum bpchareq(PG_FUNCTION_ARGS)
Definition: varchar.c:743
Datum btbpchar_pattern_cmp(PG_FUNCTION_ARGS)
Definition: varchar.c:1195
Datum bpchar_name(PG_FUNCTION_ARGS)
Definition: varchar.c:371
Datum bpcharin(PG_FUNCTION_ARGS)
Definition: varchar.c:198
Datum varchartypmodin(PG_FUNCTION_ARGS)
Definition: varchar.c:648
Datum varchartypmodout(PG_FUNCTION_ARGS)
Definition: varchar.c:656
Datum bpcharle(PG_FUNCTION_ARGS)
Definition: varchar.c:846
Datum bpcharoctetlen(PG_FUNCTION_ARGS)
Definition: varchar.c:709
Datum bpchar_pattern_gt(PG_FUNCTION_ARGS)
Definition: varchar.c:1179
Datum char_bpchar(PG_FUNCTION_ARGS)
Definition: varchar.c:353
Datum hashbpchar(PG_FUNCTION_ARGS)
Definition: varchar.c:988
Datum textsend(PG_FUNCTION_ARGS)
Definition: varlena.c:619
int varstr_cmp(const char *arg1, int len1, const char *arg2, int len2, Oid collid)
Definition: varlena.c:1538
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:196
void varstr_sortsupport(SortSupport ssup, Oid typid, Oid collid)
Definition: varlena.c:1856
text * cstring_to_text(const char *s)
Definition: varlena.c:184