PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
hstore_io.c
Go to the documentation of this file.
1 /*
2  * contrib/hstore/hstore_io.c
3  */
4 #include "postgres.h"
5 
6 #include <ctype.h>
7 
8 #include "access/htup_details.h"
9 #include "catalog/pg_type.h"
10 #include "funcapi.h"
11 #include "lib/stringinfo.h"
12 #include "libpq/pqformat.h"
13 #include "utils/builtins.h"
14 #include "utils/json.h"
15 #include "utils/jsonapi.h"
16 #include "utils/jsonb.h"
17 #include "utils/lsyscache.h"
18 #include "utils/memutils.h"
19 #include "utils/typcache.h"
20 
21 #include "hstore.h"
22 
24 
25 /* old names for C functions */
27 
28 
29 typedef struct
30 {
31  char *begin;
32  char *ptr;
33  char *cur;
34  char *word;
35  int wordlen;
36 
38  int pcur;
39  int plen;
40 } HSParser;
41 
42 #define RESIZEPRSBUF \
43 do { \
44  if ( state->cur - state->word + 1 >= state->wordlen ) \
45  { \
46  int32 clen = state->cur - state->word; \
47  state->wordlen *= 2; \
48  state->word = (char*)repalloc( (void*)state->word, state->wordlen ); \
49  state->cur = state->word + clen; \
50  } \
51 } while (0)
52 
53 
54 #define GV_WAITVAL 0
55 #define GV_INVAL 1
56 #define GV_INESCVAL 2
57 #define GV_WAITESCIN 3
58 #define GV_WAITESCESCIN 4
59 
60 static bool
61 get_val(HSParser *state, bool ignoreeq, bool *escaped)
62 {
63  int st = GV_WAITVAL;
64 
65  state->wordlen = 32;
66  state->cur = state->word = palloc(state->wordlen);
67  *escaped = false;
68 
69  while (1)
70  {
71  if (st == GV_WAITVAL)
72  {
73  if (*(state->ptr) == '"')
74  {
75  *escaped = true;
76  st = GV_INESCVAL;
77  }
78  else if (*(state->ptr) == '\0')
79  {
80  return false;
81  }
82  else if (*(state->ptr) == '=' && !ignoreeq)
83  {
84  elog(ERROR, "Syntax error near '%c' at position %d", *(state->ptr), (int32) (state->ptr - state->begin));
85  }
86  else if (*(state->ptr) == '\\')
87  {
88  st = GV_WAITESCIN;
89  }
90  else if (!isspace((unsigned char) *(state->ptr)))
91  {
92  *(state->cur) = *(state->ptr);
93  state->cur++;
94  st = GV_INVAL;
95  }
96  }
97  else if (st == GV_INVAL)
98  {
99  if (*(state->ptr) == '\\')
100  {
101  st = GV_WAITESCIN;
102  }
103  else if (*(state->ptr) == '=' && !ignoreeq)
104  {
105  state->ptr--;
106  return true;
107  }
108  else if (*(state->ptr) == ',' && ignoreeq)
109  {
110  state->ptr--;
111  return true;
112  }
113  else if (isspace((unsigned char) *(state->ptr)))
114  {
115  return true;
116  }
117  else if (*(state->ptr) == '\0')
118  {
119  state->ptr--;
120  return true;
121  }
122  else
123  {
124  RESIZEPRSBUF;
125  *(state->cur) = *(state->ptr);
126  state->cur++;
127  }
128  }
129  else if (st == GV_INESCVAL)
130  {
131  if (*(state->ptr) == '\\')
132  {
133  st = GV_WAITESCESCIN;
134  }
135  else if (*(state->ptr) == '"')
136  {
137  return true;
138  }
139  else if (*(state->ptr) == '\0')
140  {
141  elog(ERROR, "Unexpected end of string");
142  }
143  else
144  {
145  RESIZEPRSBUF;
146  *(state->cur) = *(state->ptr);
147  state->cur++;
148  }
149  }
150  else if (st == GV_WAITESCIN)
151  {
152  if (*(state->ptr) == '\0')
153  elog(ERROR, "Unexpected end of string");
154  RESIZEPRSBUF;
155  *(state->cur) = *(state->ptr);
156  state->cur++;
157  st = GV_INVAL;
158  }
159  else if (st == GV_WAITESCESCIN)
160  {
161  if (*(state->ptr) == '\0')
162  elog(ERROR, "Unexpected end of string");
163  RESIZEPRSBUF;
164  *(state->cur) = *(state->ptr);
165  state->cur++;
166  st = GV_INESCVAL;
167  }
168  else
169  elog(ERROR, "Unknown state %d at position line %d in file '%s'", st, __LINE__, __FILE__);
170 
171  state->ptr++;
172  }
173 }
174 
175 #define WKEY 0
176 #define WVAL 1
177 #define WEQ 2
178 #define WGT 3
179 #define WDEL 4
180 
181 
182 static void
184 {
185  int st = WKEY;
186  bool escaped = false;
187 
188  state->plen = 16;
189  state->pairs = (Pairs *) palloc(sizeof(Pairs) * state->plen);
190  state->pcur = 0;
191  state->ptr = state->begin;
192  state->word = NULL;
193 
194  while (1)
195  {
196  if (st == WKEY)
197  {
198  if (!get_val(state, false, &escaped))
199  return;
200  if (state->pcur >= state->plen)
201  {
202  state->plen *= 2;
203  state->pairs = (Pairs *) repalloc(state->pairs, sizeof(Pairs) * state->plen);
204  }
205  state->pairs[state->pcur].key = state->word;
206  state->pairs[state->pcur].keylen = hstoreCheckKeyLen(state->cur - state->word);
207  state->pairs[state->pcur].val = NULL;
208  state->word = NULL;
209  st = WEQ;
210  }
211  else if (st == WEQ)
212  {
213  if (*(state->ptr) == '=')
214  {
215  st = WGT;
216  }
217  else if (*(state->ptr) == '\0')
218  {
219  elog(ERROR, "Unexpected end of string");
220  }
221  else if (!isspace((unsigned char) *(state->ptr)))
222  {
223  elog(ERROR, "Syntax error near '%c' at position %d", *(state->ptr), (int32) (state->ptr - state->begin));
224  }
225  }
226  else if (st == WGT)
227  {
228  if (*(state->ptr) == '>')
229  {
230  st = WVAL;
231  }
232  else if (*(state->ptr) == '\0')
233  {
234  elog(ERROR, "Unexpected end of string");
235  }
236  else
237  {
238  elog(ERROR, "Syntax error near '%c' at position %d", *(state->ptr), (int32) (state->ptr - state->begin));
239  }
240  }
241  else if (st == WVAL)
242  {
243  if (!get_val(state, true, &escaped))
244  elog(ERROR, "Unexpected end of string");
245  state->pairs[state->pcur].val = state->word;
246  state->pairs[state->pcur].vallen = hstoreCheckValLen(state->cur - state->word);
247  state->pairs[state->pcur].isnull = false;
248  state->pairs[state->pcur].needfree = true;
249  if (state->cur - state->word == 4 && !escaped)
250  {
251  state->word[4] = '\0';
252  if (0 == pg_strcasecmp(state->word, "null"))
253  state->pairs[state->pcur].isnull = true;
254  }
255  state->word = NULL;
256  state->pcur++;
257  st = WDEL;
258  }
259  else if (st == WDEL)
260  {
261  if (*(state->ptr) == ',')
262  {
263  st = WKEY;
264  }
265  else if (*(state->ptr) == '\0')
266  {
267  return;
268  }
269  else if (!isspace((unsigned char) *(state->ptr)))
270  {
271  elog(ERROR, "Syntax error near '%c' at position %d", *(state->ptr), (int32) (state->ptr - state->begin));
272  }
273  }
274  else
275  elog(ERROR, "Unknown state %d at line %d in file '%s'", st, __LINE__, __FILE__);
276 
277  state->ptr++;
278  }
279 }
280 
281 static int
282 comparePairs(const void *a, const void *b)
283 {
284  const Pairs *pa = a;
285  const Pairs *pb = b;
286 
287  if (pa->keylen == pb->keylen)
288  {
289  int res = memcmp(pa->key, pb->key, pa->keylen);
290 
291  if (res)
292  return res;
293 
294  /* guarantee that needfree will be later */
295  if (pb->needfree == pa->needfree)
296  return 0;
297  else if (pa->needfree)
298  return 1;
299  else
300  return -1;
301  }
302  return (pa->keylen > pb->keylen) ? 1 : -1;
303 }
304 
305 /*
306  * this code still respects pairs.needfree, even though in general
307  * it should never be called in a context where anything needs freeing.
308  * we keep it because (a) those calls are in a rare code path anyway,
309  * and (b) who knows whether they might be needed by some caller.
310  */
311 int
313 {
314  Pairs *ptr,
315  *res;
316 
317  *buflen = 0;
318  if (l < 2)
319  {
320  if (l == 1)
321  *buflen = a->keylen + ((a->isnull) ? 0 : a->vallen);
322  return l;
323  }
324 
325  qsort((void *) a, l, sizeof(Pairs), comparePairs);
326  ptr = a + 1;
327  res = a;
328  while (ptr - a < l)
329  {
330  if (ptr->keylen == res->keylen &&
331  memcmp(ptr->key, res->key, res->keylen) == 0)
332  {
333  if (ptr->needfree)
334  {
335  pfree(ptr->key);
336  pfree(ptr->val);
337  }
338  }
339  else
340  {
341  *buflen += res->keylen + ((res->isnull) ? 0 : res->vallen);
342  res++;
343  memcpy(res, ptr, sizeof(Pairs));
344  }
345 
346  ptr++;
347  }
348 
349  *buflen += res->keylen + ((res->isnull) ? 0 : res->vallen);
350  return res + 1 - a;
351 }
352 
353 size_t
354 hstoreCheckKeyLen(size_t len)
355 {
356  if (len > HSTORE_MAX_KEY_LEN)
357  ereport(ERROR,
358  (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
359  errmsg("string too long for hstore key")));
360  return len;
361 }
362 
363 size_t
364 hstoreCheckValLen(size_t len)
365 {
366  if (len > HSTORE_MAX_VALUE_LEN)
367  ereport(ERROR,
368  (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
369  errmsg("string too long for hstore value")));
370  return len;
371 }
372 
373 
374 HStore *
375 hstorePairs(Pairs *pairs, int32 pcount, int32 buflen)
376 {
377  HStore *out;
378  HEntry *entry;
379  char *ptr;
380  char *buf;
381  int32 len;
382  int32 i;
383 
384  len = CALCDATASIZE(pcount, buflen);
385  out = palloc(len);
386  SET_VARSIZE(out, len);
387  HS_SETCOUNT(out, pcount);
388 
389  if (pcount == 0)
390  return out;
391 
392  entry = ARRPTR(out);
393  buf = ptr = STRPTR(out);
394 
395  for (i = 0; i < pcount; i++)
396  HS_ADDITEM(entry, buf, ptr, pairs[i]);
397 
398  HS_FINALIZE(out, pcount, buf, ptr);
399 
400  return out;
401 }
402 
403 
405 Datum
407 {
408  HSParser state;
409  int32 buflen;
410  HStore *out;
411 
412  state.begin = PG_GETARG_CSTRING(0);
413 
414  parse_hstore(&state);
415 
416  state.pcur = hstoreUniquePairs(state.pairs, state.pcur, &buflen);
417 
418  out = hstorePairs(state.pairs, state.pcur, buflen);
419 
420  PG_RETURN_POINTER(out);
421 }
422 
423 
425 Datum
427 {
428  int32 buflen;
429  HStore *out;
430  Pairs *pairs;
431  int32 i;
432  int32 pcount;
434 
435  pcount = pq_getmsgint(buf, 4);
436 
437  if (pcount == 0)
438  {
439  out = hstorePairs(NULL, 0, 0);
440  PG_RETURN_POINTER(out);
441  }
442 
443  if (pcount < 0 || pcount > MaxAllocSize / sizeof(Pairs))
444  ereport(ERROR,
445  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
446  errmsg("number of pairs (%d) exceeds the maximum allowed (%d)",
447  pcount, (int) (MaxAllocSize / sizeof(Pairs)))));
448  pairs = palloc(pcount * sizeof(Pairs));
449 
450  for (i = 0; i < pcount; ++i)
451  {
452  int rawlen = pq_getmsgint(buf, 4);
453  int len;
454 
455  if (rawlen < 0)
456  ereport(ERROR,
457  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
458  errmsg("null value not allowed for hstore key")));
459 
460  pairs[i].key = pq_getmsgtext(buf, rawlen, &len);
461  pairs[i].keylen = hstoreCheckKeyLen(len);
462  pairs[i].needfree = true;
463 
464  rawlen = pq_getmsgint(buf, 4);
465  if (rawlen < 0)
466  {
467  pairs[i].val = NULL;
468  pairs[i].vallen = 0;
469  pairs[i].isnull = true;
470  }
471  else
472  {
473  pairs[i].val = pq_getmsgtext(buf, rawlen, &len);
474  pairs[i].vallen = hstoreCheckValLen(len);
475  pairs[i].isnull = false;
476  }
477  }
478 
479  pcount = hstoreUniquePairs(pairs, pcount, &buflen);
480 
481  out = hstorePairs(pairs, pcount, buflen);
482 
483  PG_RETURN_POINTER(out);
484 }
485 
486 
488 Datum
490 {
491  text *key;
492  text *val = NULL;
493  Pairs p;
494  HStore *out;
495 
496  if (PG_ARGISNULL(0))
497  PG_RETURN_NULL();
498 
499  p.needfree = false;
500  key = PG_GETARG_TEXT_PP(0);
501  p.key = VARDATA_ANY(key);
503 
504  if (PG_ARGISNULL(1))
505  {
506  p.vallen = 0;
507  p.isnull = true;
508  }
509  else
510  {
511  val = PG_GETARG_TEXT_PP(1);
512  p.val = VARDATA_ANY(val);
514  p.isnull = false;
515  }
516 
517  out = hstorePairs(&p, 1, p.keylen + p.vallen);
518 
519  PG_RETURN_POINTER(out);
520 }
521 
522 
524 Datum
526 {
527  int32 buflen;
528  HStore *out;
529  Pairs *pairs;
530  Datum *key_datums;
531  bool *key_nulls;
532  int key_count;
533  Datum *value_datums;
534  bool *value_nulls;
535  int value_count;
536  ArrayType *key_array;
537  ArrayType *value_array;
538  int i;
539 
540  if (PG_ARGISNULL(0))
541  PG_RETURN_NULL();
542 
543  key_array = PG_GETARG_ARRAYTYPE_P(0);
544 
545  Assert(ARR_ELEMTYPE(key_array) == TEXTOID);
546 
547  /*
548  * must check >1 rather than != 1 because empty arrays have 0 dimensions,
549  * not 1
550  */
551 
552  if (ARR_NDIM(key_array) > 1)
553  ereport(ERROR,
554  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
555  errmsg("wrong number of array subscripts")));
556 
557  deconstruct_array(key_array,
558  TEXTOID, -1, false, 'i',
559  &key_datums, &key_nulls, &key_count);
560 
561  /* see discussion in hstoreArrayToPairs() */
562  if (key_count > MaxAllocSize / sizeof(Pairs))
563  ereport(ERROR,
564  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
565  errmsg("number of pairs (%d) exceeds the maximum allowed (%d)",
566  key_count, (int) (MaxAllocSize / sizeof(Pairs)))));
567 
568  /* value_array might be NULL */
569 
570  if (PG_ARGISNULL(1))
571  {
572  value_array = NULL;
573  value_count = key_count;
574  value_datums = NULL;
575  value_nulls = NULL;
576  }
577  else
578  {
579  value_array = PG_GETARG_ARRAYTYPE_P(1);
580 
581  Assert(ARR_ELEMTYPE(value_array) == TEXTOID);
582 
583  if (ARR_NDIM(value_array) > 1)
584  ereport(ERROR,
585  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
586  errmsg("wrong number of array subscripts")));
587 
588  if ((ARR_NDIM(key_array) > 0 || ARR_NDIM(value_array) > 0) &&
589  (ARR_NDIM(key_array) != ARR_NDIM(value_array) ||
590  ARR_DIMS(key_array)[0] != ARR_DIMS(value_array)[0] ||
591  ARR_LBOUND(key_array)[0] != ARR_LBOUND(value_array)[0]))
592  ereport(ERROR,
593  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
594  errmsg("arrays must have same bounds")));
595 
596  deconstruct_array(value_array,
597  TEXTOID, -1, false, 'i',
598  &value_datums, &value_nulls, &value_count);
599 
600  Assert(key_count == value_count);
601  }
602 
603  pairs = palloc(key_count * sizeof(Pairs));
604 
605  for (i = 0; i < key_count; ++i)
606  {
607  if (key_nulls[i])
608  ereport(ERROR,
609  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
610  errmsg("null value not allowed for hstore key")));
611 
612  if (!value_nulls || value_nulls[i])
613  {
614  pairs[i].key = VARDATA(key_datums[i]);
615  pairs[i].val = NULL;
616  pairs[i].keylen =
617  hstoreCheckKeyLen(VARSIZE(key_datums[i]) - VARHDRSZ);
618  pairs[i].vallen = 4;
619  pairs[i].isnull = true;
620  pairs[i].needfree = false;
621  }
622  else
623  {
624  pairs[i].key = VARDATA(key_datums[i]);
625  pairs[i].val = VARDATA(value_datums[i]);
626  pairs[i].keylen =
627  hstoreCheckKeyLen(VARSIZE(key_datums[i]) - VARHDRSZ);
628  pairs[i].vallen =
629  hstoreCheckValLen(VARSIZE(value_datums[i]) - VARHDRSZ);
630  pairs[i].isnull = false;
631  pairs[i].needfree = false;
632  }
633  }
634 
635  key_count = hstoreUniquePairs(pairs, key_count, &buflen);
636 
637  out = hstorePairs(pairs, key_count, buflen);
638 
639  PG_RETURN_POINTER(out);
640 }
641 
642 
644 Datum
646 {
647  ArrayType *in_array = PG_GETARG_ARRAYTYPE_P(0);
648  int ndims = ARR_NDIM(in_array);
649  int count;
650  int32 buflen;
651  HStore *out;
652  Pairs *pairs;
653  Datum *in_datums;
654  bool *in_nulls;
655  int in_count;
656  int i;
657 
658  Assert(ARR_ELEMTYPE(in_array) == TEXTOID);
659 
660  switch (ndims)
661  {
662  case 0:
663  out = hstorePairs(NULL, 0, 0);
664  PG_RETURN_POINTER(out);
665 
666  case 1:
667  if ((ARR_DIMS(in_array)[0]) % 2)
668  ereport(ERROR,
669  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
670  errmsg("array must have even number of elements")));
671  break;
672 
673  case 2:
674  if ((ARR_DIMS(in_array)[1]) != 2)
675  ereport(ERROR,
676  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
677  errmsg("array must have two columns")));
678  break;
679 
680  default:
681  ereport(ERROR,
682  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
683  errmsg("wrong number of array subscripts")));
684  }
685 
686  deconstruct_array(in_array,
687  TEXTOID, -1, false, 'i',
688  &in_datums, &in_nulls, &in_count);
689 
690  count = in_count / 2;
691 
692  /* see discussion in hstoreArrayToPairs() */
693  if (count > MaxAllocSize / sizeof(Pairs))
694  ereport(ERROR,
695  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
696  errmsg("number of pairs (%d) exceeds the maximum allowed (%d)",
697  count, (int) (MaxAllocSize / sizeof(Pairs)))));
698 
699  pairs = palloc(count * sizeof(Pairs));
700 
701  for (i = 0; i < count; ++i)
702  {
703  if (in_nulls[i * 2])
704  ereport(ERROR,
705  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
706  errmsg("null value not allowed for hstore key")));
707 
708  if (in_nulls[i * 2 + 1])
709  {
710  pairs[i].key = VARDATA(in_datums[i * 2]);
711  pairs[i].val = NULL;
712  pairs[i].keylen =
713  hstoreCheckKeyLen(VARSIZE(in_datums[i * 2]) - VARHDRSZ);
714  pairs[i].vallen = 4;
715  pairs[i].isnull = true;
716  pairs[i].needfree = false;
717  }
718  else
719  {
720  pairs[i].key = VARDATA(in_datums[i * 2]);
721  pairs[i].val = VARDATA(in_datums[i * 2 + 1]);
722  pairs[i].keylen =
723  hstoreCheckKeyLen(VARSIZE(in_datums[i * 2]) - VARHDRSZ);
724  pairs[i].vallen =
725  hstoreCheckValLen(VARSIZE(in_datums[i * 2 + 1]) - VARHDRSZ);
726  pairs[i].isnull = false;
727  pairs[i].needfree = false;
728  }
729  }
730 
731  count = hstoreUniquePairs(pairs, count, &buflen);
732 
733  out = hstorePairs(pairs, count, buflen);
734 
735  PG_RETURN_POINTER(out);
736 }
737 
738 /* most of hstore_from_record is shamelessly swiped from record_out */
739 
740 /*
741  * structure to cache metadata needed for record I/O
742  */
743 typedef struct ColumnIOData
744 {
749 } ColumnIOData;
750 
751 typedef struct RecordIOData
752 {
755  int ncolumns;
756  ColumnIOData columns[FLEXIBLE_ARRAY_MEMBER];
757 } RecordIOData;
758 
760 Datum
762 {
763  HeapTupleHeader rec;
764  int32 buflen;
765  HStore *out;
766  Pairs *pairs;
767  Oid tupType;
768  int32 tupTypmod;
769  TupleDesc tupdesc;
770  HeapTupleData tuple;
771  RecordIOData *my_extra;
772  int ncolumns;
773  int i,
774  j;
775  Datum *values;
776  bool *nulls;
777 
778  if (PG_ARGISNULL(0))
779  {
780  Oid argtype = get_fn_expr_argtype(fcinfo->flinfo, 0);
781 
782  /*
783  * have no tuple to look at, so the only source of type info is the
784  * argtype. The lookup_rowtype_tupdesc call below will error out if we
785  * don't have a known composite type oid here.
786  */
787  tupType = argtype;
788  tupTypmod = -1;
789 
790  rec = NULL;
791  }
792  else
793  {
794  rec = PG_GETARG_HEAPTUPLEHEADER(0);
795 
796  /* Extract type info from the tuple itself */
797  tupType = HeapTupleHeaderGetTypeId(rec);
798  tupTypmod = HeapTupleHeaderGetTypMod(rec);
799  }
800 
801  tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
802  ncolumns = tupdesc->natts;
803 
804  /*
805  * We arrange to look up the needed I/O info just once per series of
806  * calls, assuming the record type doesn't change underneath us.
807  */
808  my_extra = (RecordIOData *) fcinfo->flinfo->fn_extra;
809  if (my_extra == NULL ||
810  my_extra->ncolumns != ncolumns)
811  {
812  fcinfo->flinfo->fn_extra =
813  MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
814  offsetof(RecordIOData, columns) +
815  ncolumns * sizeof(ColumnIOData));
816  my_extra = (RecordIOData *) fcinfo->flinfo->fn_extra;
817  my_extra->record_type = InvalidOid;
818  my_extra->record_typmod = 0;
819  }
820 
821  if (my_extra->record_type != tupType ||
822  my_extra->record_typmod != tupTypmod)
823  {
824  MemSet(my_extra, 0,
825  offsetof(RecordIOData, columns) +
826  ncolumns * sizeof(ColumnIOData));
827  my_extra->record_type = tupType;
828  my_extra->record_typmod = tupTypmod;
829  my_extra->ncolumns = ncolumns;
830  }
831 
832  Assert(ncolumns <= MaxTupleAttributeNumber); /* thus, no overflow */
833  pairs = palloc(ncolumns * sizeof(Pairs));
834 
835  if (rec)
836  {
837  /* Build a temporary HeapTuple control structure */
839  ItemPointerSetInvalid(&(tuple.t_self));
840  tuple.t_tableOid = InvalidOid;
841  tuple.t_data = rec;
842 
843  values = (Datum *) palloc(ncolumns * sizeof(Datum));
844  nulls = (bool *) palloc(ncolumns * sizeof(bool));
845 
846  /* Break down the tuple into fields */
847  heap_deform_tuple(&tuple, tupdesc, values, nulls);
848  }
849  else
850  {
851  values = NULL;
852  nulls = NULL;
853  }
854 
855  for (i = 0, j = 0; i < ncolumns; ++i)
856  {
857  ColumnIOData *column_info = &my_extra->columns[i];
858  Form_pg_attribute att = TupleDescAttr(tupdesc, i);
859  Oid column_type = att->atttypid;
860  char *value;
861 
862  /* Ignore dropped columns in datatype */
863  if (att->attisdropped)
864  continue;
865 
866  pairs[j].key = NameStr(att->attname);
867  pairs[j].keylen = hstoreCheckKeyLen(strlen(NameStr(att->attname)));
868 
869  if (!nulls || nulls[i])
870  {
871  pairs[j].val = NULL;
872  pairs[j].vallen = 4;
873  pairs[j].isnull = true;
874  pairs[j].needfree = false;
875  ++j;
876  continue;
877  }
878 
879  /*
880  * Convert the column value to text
881  */
882  if (column_info->column_type != column_type)
883  {
884  bool typIsVarlena;
885 
886  getTypeOutputInfo(column_type,
887  &column_info->typiofunc,
888  &typIsVarlena);
889  fmgr_info_cxt(column_info->typiofunc, &column_info->proc,
890  fcinfo->flinfo->fn_mcxt);
891  column_info->column_type = column_type;
892  }
893 
894  value = OutputFunctionCall(&column_info->proc, values[i]);
895 
896  pairs[j].val = value;
897  pairs[j].vallen = hstoreCheckValLen(strlen(value));
898  pairs[j].isnull = false;
899  pairs[j].needfree = false;
900  ++j;
901  }
902 
903  ncolumns = hstoreUniquePairs(pairs, j, &buflen);
904 
905  out = hstorePairs(pairs, ncolumns, buflen);
906 
907  ReleaseTupleDesc(tupdesc);
908 
909  PG_RETURN_POINTER(out);
910 }
911 
912 
914 Datum
916 {
917  Oid argtype = get_fn_expr_argtype(fcinfo->flinfo, 0);
918  HStore *hs;
919  HEntry *entries;
920  char *ptr;
921  HeapTupleHeader rec;
922  Oid tupType;
923  int32 tupTypmod;
924  TupleDesc tupdesc;
925  HeapTupleData tuple;
926  HeapTuple rettuple;
927  RecordIOData *my_extra;
928  int ncolumns;
929  int i;
930  Datum *values;
931  bool *nulls;
932 
933  if (!type_is_rowtype(argtype))
934  ereport(ERROR,
935  (errcode(ERRCODE_DATATYPE_MISMATCH),
936  errmsg("first argument must be a rowtype")));
937 
938  if (PG_ARGISNULL(0))
939  {
940  if (PG_ARGISNULL(1))
941  PG_RETURN_NULL();
942 
943  rec = NULL;
944 
945  /*
946  * have no tuple to look at, so the only source of type info is the
947  * argtype. The lookup_rowtype_tupdesc call below will error out if we
948  * don't have a known composite type oid here.
949  */
950  tupType = argtype;
951  tupTypmod = -1;
952  }
953  else
954  {
955  rec = PG_GETARG_HEAPTUPLEHEADER(0);
956 
957  if (PG_ARGISNULL(1))
958  PG_RETURN_POINTER(rec);
959 
960  /* Extract type info from the tuple itself */
961  tupType = HeapTupleHeaderGetTypeId(rec);
962  tupTypmod = HeapTupleHeaderGetTypMod(rec);
963  }
964 
965  hs = PG_GETARG_HSTORE_P(1);
966  entries = ARRPTR(hs);
967  ptr = STRPTR(hs);
968 
969  /*
970  * if the input hstore is empty, we can only skip the rest if we were
971  * passed in a non-null record, since otherwise there may be issues with
972  * domain nulls.
973  */
974 
975  if (HS_COUNT(hs) == 0 && rec)
976  PG_RETURN_POINTER(rec);
977 
978  tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
979  ncolumns = tupdesc->natts;
980 
981  if (rec)
982  {
983  /* Build a temporary HeapTuple control structure */
985  ItemPointerSetInvalid(&(tuple.t_self));
986  tuple.t_tableOid = InvalidOid;
987  tuple.t_data = rec;
988  }
989 
990  /*
991  * We arrange to look up the needed I/O info just once per series of
992  * calls, assuming the record type doesn't change underneath us.
993  */
994  my_extra = (RecordIOData *) fcinfo->flinfo->fn_extra;
995  if (my_extra == NULL ||
996  my_extra->ncolumns != ncolumns)
997  {
998  fcinfo->flinfo->fn_extra =
999  MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
1000  offsetof(RecordIOData, columns) +
1001  ncolumns * sizeof(ColumnIOData));
1002  my_extra = (RecordIOData *) fcinfo->flinfo->fn_extra;
1003  my_extra->record_type = InvalidOid;
1004  my_extra->record_typmod = 0;
1005  }
1006 
1007  if (my_extra->record_type != tupType ||
1008  my_extra->record_typmod != tupTypmod)
1009  {
1010  MemSet(my_extra, 0,
1011  offsetof(RecordIOData, columns) +
1012  ncolumns * sizeof(ColumnIOData));
1013  my_extra->record_type = tupType;
1014  my_extra->record_typmod = tupTypmod;
1015  my_extra->ncolumns = ncolumns;
1016  }
1017 
1018  values = (Datum *) palloc(ncolumns * sizeof(Datum));
1019  nulls = (bool *) palloc(ncolumns * sizeof(bool));
1020 
1021  if (rec)
1022  {
1023  /* Break down the tuple into fields */
1024  heap_deform_tuple(&tuple, tupdesc, values, nulls);
1025  }
1026  else
1027  {
1028  for (i = 0; i < ncolumns; ++i)
1029  {
1030  values[i] = (Datum) 0;
1031  nulls[i] = true;
1032  }
1033  }
1034 
1035  for (i = 0; i < ncolumns; ++i)
1036  {
1037  ColumnIOData *column_info = &my_extra->columns[i];
1038  Form_pg_attribute att = TupleDescAttr(tupdesc, i);
1039  Oid column_type = att->atttypid;
1040  char *value;
1041  int idx;
1042  int vallen;
1043 
1044  /* Ignore dropped columns in datatype */
1045  if (att->attisdropped)
1046  {
1047  nulls[i] = true;
1048  continue;
1049  }
1050 
1051  idx = hstoreFindKey(hs, 0,
1052  NameStr(att->attname),
1053  strlen(NameStr(att->attname)));
1054 
1055  /*
1056  * we can't just skip here if the key wasn't found since we might have
1057  * a domain to deal with. If we were passed in a non-null record
1058  * datum, we assume that the existing values are valid (if they're
1059  * not, then it's not our fault), but if we were passed in a null,
1060  * then every field which we don't populate needs to be run through
1061  * the input function just in case it's a domain type.
1062  */
1063  if (idx < 0 && rec)
1064  continue;
1065 
1066  /*
1067  * Prepare to convert the column value from text
1068  */
1069  if (column_info->column_type != column_type)
1070  {
1071  getTypeInputInfo(column_type,
1072  &column_info->typiofunc,
1073  &column_info->typioparam);
1074  fmgr_info_cxt(column_info->typiofunc, &column_info->proc,
1075  fcinfo->flinfo->fn_mcxt);
1076  column_info->column_type = column_type;
1077  }
1078 
1079  if (idx < 0 || HSTORE_VALISNULL(entries, idx))
1080  {
1081  /*
1082  * need InputFunctionCall to happen even for nulls, so that domain
1083  * checks are done
1084  */
1085  values[i] = InputFunctionCall(&column_info->proc, NULL,
1086  column_info->typioparam,
1087  att->atttypmod);
1088  nulls[i] = true;
1089  }
1090  else
1091  {
1092  vallen = HSTORE_VALLEN(entries, idx);
1093  value = palloc(1 + vallen);
1094  memcpy(value, HSTORE_VAL(entries, ptr, idx), vallen);
1095  value[vallen] = 0;
1096 
1097  values[i] = InputFunctionCall(&column_info->proc, value,
1098  column_info->typioparam,
1099  att->atttypmod);
1100  nulls[i] = false;
1101  }
1102  }
1103 
1104  rettuple = heap_form_tuple(tupdesc, values, nulls);
1105 
1106  ReleaseTupleDesc(tupdesc);
1107 
1109 }
1110 
1111 
1112 static char *
1113 cpw(char *dst, char *src, int len)
1114 {
1115  char *ptr = src;
1116 
1117  while (ptr - src < len)
1118  {
1119  if (*ptr == '"' || *ptr == '\\')
1120  *dst++ = '\\';
1121  *dst++ = *ptr++;
1122  }
1123  return dst;
1124 }
1125 
1127 Datum
1129 {
1130  HStore *in = PG_GETARG_HSTORE_P(0);
1131  int buflen,
1132  i;
1133  int count = HS_COUNT(in);
1134  char *out,
1135  *ptr;
1136  char *base = STRPTR(in);
1137  HEntry *entries = ARRPTR(in);
1138 
1139  if (count == 0)
1141 
1142  buflen = 0;
1143 
1144  /*
1145  * this loop overestimates due to pessimistic assumptions about escaping,
1146  * so very large hstore values can't be output. this could be fixed, but
1147  * many other data types probably have the same issue. This replaced code
1148  * that used the original varlena size for calculations, which was wrong
1149  * in some subtle ways.
1150  */
1151 
1152  for (i = 0; i < count; i++)
1153  {
1154  /* include "" and => and comma-space */
1155  buflen += 6 + 2 * HSTORE_KEYLEN(entries, i);
1156  /* include "" only if nonnull */
1157  buflen += 2 + (HSTORE_VALISNULL(entries, i)
1158  ? 2
1159  : 2 * HSTORE_VALLEN(entries, i));
1160  }
1161 
1162  out = ptr = palloc(buflen);
1163 
1164  for (i = 0; i < count; i++)
1165  {
1166  *ptr++ = '"';
1167  ptr = cpw(ptr, HSTORE_KEY(entries, base, i), HSTORE_KEYLEN(entries, i));
1168  *ptr++ = '"';
1169  *ptr++ = '=';
1170  *ptr++ = '>';
1171  if (HSTORE_VALISNULL(entries, i))
1172  {
1173  *ptr++ = 'N';
1174  *ptr++ = 'U';
1175  *ptr++ = 'L';
1176  *ptr++ = 'L';
1177  }
1178  else
1179  {
1180  *ptr++ = '"';
1181  ptr = cpw(ptr, HSTORE_VAL(entries, base, i), HSTORE_VALLEN(entries, i));
1182  *ptr++ = '"';
1183  }
1184 
1185  if (i + 1 != count)
1186  {
1187  *ptr++ = ',';
1188  *ptr++ = ' ';
1189  }
1190  }
1191  *ptr = '\0';
1192 
1193  PG_RETURN_CSTRING(out);
1194 }
1195 
1196 
1198 Datum
1200 {
1201  HStore *in = PG_GETARG_HSTORE_P(0);
1202  int i;
1203  int count = HS_COUNT(in);
1204  char *base = STRPTR(in);
1205  HEntry *entries = ARRPTR(in);
1207 
1208  pq_begintypsend(&buf);
1209 
1210  pq_sendint32(&buf, count);
1211 
1212  for (i = 0; i < count; i++)
1213  {
1214  int32 keylen = HSTORE_KEYLEN(entries, i);
1215 
1216  pq_sendint32(&buf, keylen);
1217  pq_sendtext(&buf, HSTORE_KEY(entries, base, i), keylen);
1218  if (HSTORE_VALISNULL(entries, i))
1219  {
1220  pq_sendint32(&buf, -1);
1221  }
1222  else
1223  {
1224  int32 vallen = HSTORE_VALLEN(entries, i);
1225 
1226  pq_sendint32(&buf, vallen);
1227  pq_sendtext(&buf, HSTORE_VAL(entries, base, i), vallen);
1228  }
1229  }
1230 
1232 }
1233 
1234 
1235 /*
1236  * hstore_to_json_loose
1237  *
1238  * This is a heuristic conversion to json which treats
1239  * 't' and 'f' as booleans and strings that look like numbers as numbers,
1240  * as long as they don't start with a leading zero followed by another digit
1241  * (think zip codes or phone numbers starting with 0).
1242  */
1244 Datum
1246 {
1247  HStore *in = PG_GETARG_HSTORE_P(0);
1248  int i;
1249  int count = HS_COUNT(in);
1250  char *base = STRPTR(in);
1251  HEntry *entries = ARRPTR(in);
1252  StringInfoData tmp,
1253  dst;
1254 
1255  if (count == 0)
1257 
1258  initStringInfo(&tmp);
1259  initStringInfo(&dst);
1260 
1261  appendStringInfoChar(&dst, '{');
1262 
1263  for (i = 0; i < count; i++)
1264  {
1265  resetStringInfo(&tmp);
1266  appendBinaryStringInfo(&tmp, HSTORE_KEY(entries, base, i),
1267  HSTORE_KEYLEN(entries, i));
1268  escape_json(&dst, tmp.data);
1269  appendStringInfoString(&dst, ": ");
1270  if (HSTORE_VALISNULL(entries, i))
1271  appendStringInfoString(&dst, "null");
1272  /* guess that values of 't' or 'f' are booleans */
1273  else if (HSTORE_VALLEN(entries, i) == 1 &&
1274  *(HSTORE_VAL(entries, base, i)) == 't')
1275  appendStringInfoString(&dst, "true");
1276  else if (HSTORE_VALLEN(entries, i) == 1 &&
1277  *(HSTORE_VAL(entries, base, i)) == 'f')
1278  appendStringInfoString(&dst, "false");
1279  else
1280  {
1281  resetStringInfo(&tmp);
1282  appendBinaryStringInfo(&tmp, HSTORE_VAL(entries, base, i),
1283  HSTORE_VALLEN(entries, i));
1284  if (IsValidJsonNumber(tmp.data, tmp.len))
1285  appendBinaryStringInfo(&dst, tmp.data, tmp.len);
1286  else
1287  escape_json(&dst, tmp.data);
1288  }
1289 
1290  if (i + 1 != count)
1291  appendStringInfoString(&dst, ", ");
1292  }
1293  appendStringInfoChar(&dst, '}');
1294 
1296 }
1297 
1299 Datum
1301 {
1302  HStore *in = PG_GETARG_HSTORE_P(0);
1303  int i;
1304  int count = HS_COUNT(in);
1305  char *base = STRPTR(in);
1306  HEntry *entries = ARRPTR(in);
1307  StringInfoData tmp,
1308  dst;
1309 
1310  if (count == 0)
1312 
1313  initStringInfo(&tmp);
1314  initStringInfo(&dst);
1315 
1316  appendStringInfoChar(&dst, '{');
1317 
1318  for (i = 0; i < count; i++)
1319  {
1320  resetStringInfo(&tmp);
1321  appendBinaryStringInfo(&tmp, HSTORE_KEY(entries, base, i),
1322  HSTORE_KEYLEN(entries, i));
1323  escape_json(&dst, tmp.data);
1324  appendStringInfoString(&dst, ": ");
1325  if (HSTORE_VALISNULL(entries, i))
1326  appendStringInfoString(&dst, "null");
1327  else
1328  {
1329  resetStringInfo(&tmp);
1330  appendBinaryStringInfo(&tmp, HSTORE_VAL(entries, base, i),
1331  HSTORE_VALLEN(entries, i));
1332  escape_json(&dst, tmp.data);
1333  }
1334 
1335  if (i + 1 != count)
1336  appendStringInfoString(&dst, ", ");
1337  }
1338  appendStringInfoChar(&dst, '}');
1339 
1341 }
1342 
1344 Datum
1346 {
1347  HStore *in = PG_GETARG_HSTORE_P(0);
1348  int i;
1349  int count = HS_COUNT(in);
1350  char *base = STRPTR(in);
1351  HEntry *entries = ARRPTR(in);
1352  JsonbParseState *state = NULL;
1353  JsonbValue *res;
1354 
1355  (void) pushJsonbValue(&state, WJB_BEGIN_OBJECT, NULL);
1356 
1357  for (i = 0; i < count; i++)
1358  {
1359  JsonbValue key,
1360  val;
1361 
1362  key.type = jbvString;
1363  key.val.string.len = HSTORE_KEYLEN(entries, i);
1364  key.val.string.val = HSTORE_KEY(entries, base, i);
1365 
1366  (void) pushJsonbValue(&state, WJB_KEY, &key);
1367 
1368  if (HSTORE_VALISNULL(entries, i))
1369  {
1370  val.type = jbvNull;
1371  }
1372  else
1373  {
1374  val.type = jbvString;
1375  val.val.string.len = HSTORE_VALLEN(entries, i);
1376  val.val.string.val = HSTORE_VAL(entries, base, i);
1377  }
1378  (void) pushJsonbValue(&state, WJB_VALUE, &val);
1379  }
1380 
1381  res = pushJsonbValue(&state, WJB_END_OBJECT, NULL);
1382 
1384 }
1385 
1387 Datum
1389 {
1390  HStore *in = PG_GETARG_HSTORE_P(0);
1391  int i;
1392  int count = HS_COUNT(in);
1393  char *base = STRPTR(in);
1394  HEntry *entries = ARRPTR(in);
1395  JsonbParseState *state = NULL;
1396  JsonbValue *res;
1397  StringInfoData tmp;
1398 
1399  initStringInfo(&tmp);
1400 
1401  (void) pushJsonbValue(&state, WJB_BEGIN_OBJECT, NULL);
1402 
1403  for (i = 0; i < count; i++)
1404  {
1405  JsonbValue key,
1406  val;
1407 
1408  key.type = jbvString;
1409  key.val.string.len = HSTORE_KEYLEN(entries, i);
1410  key.val.string.val = HSTORE_KEY(entries, base, i);
1411 
1412  (void) pushJsonbValue(&state, WJB_KEY, &key);
1413 
1414  if (HSTORE_VALISNULL(entries, i))
1415  {
1416  val.type = jbvNull;
1417  }
1418  /* guess that values of 't' or 'f' are booleans */
1419  else if (HSTORE_VALLEN(entries, i) == 1 &&
1420  *(HSTORE_VAL(entries, base, i)) == 't')
1421  {
1422  val.type = jbvBool;
1423  val.val.boolean = true;
1424  }
1425  else if (HSTORE_VALLEN(entries, i) == 1 &&
1426  *(HSTORE_VAL(entries, base, i)) == 'f')
1427  {
1428  val.type = jbvBool;
1429  val.val.boolean = false;
1430  }
1431  else
1432  {
1433  resetStringInfo(&tmp);
1434  appendBinaryStringInfo(&tmp, HSTORE_VAL(entries, base, i),
1435  HSTORE_VALLEN(entries, i));
1436  if (IsValidJsonNumber(tmp.data, tmp.len))
1437  {
1438  val.type = jbvNumeric;
1439  val.val.numeric = DatumGetNumeric(
1441  CStringGetDatum(tmp.data), 0, -1));
1442  }
1443  else
1444  {
1445  val.type = jbvString;
1446  val.val.string.len = HSTORE_VALLEN(entries, i);
1447  val.val.string.val = HSTORE_VAL(entries, base, i);
1448  }
1449  }
1450  (void) pushJsonbValue(&state, WJB_VALUE, &val);
1451  }
1452 
1453  res = pushJsonbValue(&state, WJB_END_OBJECT, NULL);
1454 
1456 }
Datum hstore_in(PG_FUNCTION_ARGS)
Definition: hstore_io.c:406
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
#define GV_WAITVAL
Definition: hstore_io.c:54
Definition: fmgr.h:56
Definition: hstore.h:44
FmgrInfo proc
Definition: hstore_io.c:748
Datum hstore_to_jsonb_loose(PG_FUNCTION_ARGS)
Definition: hstore_io.c:1388
struct ColumnIOData ColumnIOData
Datum hstore_out(PG_FUNCTION_ARGS)
Definition: hstore_io.c:1128
Datum hstore_to_jsonb(PG_FUNCTION_ARGS)
Definition: hstore_io.c:1345
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2632
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define VARDATA(PTR)
Definition: postgres.h:303
Jsonb * JsonbValueToJsonb(JsonbValue *val)
Definition: jsonb_util.c:79
Datum hstore_to_json(PG_FUNCTION_ARGS)
Definition: hstore_io.c:1300
MemoryContext fn_mcxt
Definition: fmgr.h:65
void escape_json(StringInfo buf, const char *str)
Definition: json.c:2433
#define PG_GETARG_HSTORE_P(x)
Definition: hstore.h:154
TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
Definition: typcache.c:1486
#define MaxTupleAttributeNumber
Definition: htup_details.h:33
char * word
Definition: hstore_io.c:34
Oid record_type
Definition: hstore_io.c:753
#define TEXTOID
Definition: pg_type.h:324
#define VARSIZE(PTR)
Definition: postgres.h:304
static void pq_sendint32(StringInfo buf, int32 i)
Definition: pqformat.h:148
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:84
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:328
#define VARHDRSZ
Definition: c.h:439
char * pstrdup(const char *in)
Definition: mcxt.c:1076
char * val
Definition: jsonb.h:259
#define HSTORE_KEYLEN(arr_, i_)
Definition: hstore.h:81
Definition: jsonb.h:234
StringInfoData * StringInfo
Definition: stringinfo.h:43
size_t hstoreCheckValLen(size_t len)
Definition: hstore_io.c:364
#define WGT
Definition: hstore_io.c:178
int errcode(int sqlerrcode)
Definition: elog.c:575
#define MemSet(start, val, len)
Definition: c.h:863
void pq_sendtext(StringInfo buf, const char *str, int slen)
Definition: pqformat.c:174
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:264
#define HSTORE_VALLEN(arr_, i_)
Definition: hstore.h:82
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
Definition: jsonb.h:231
Datum hstore_from_arrays(PG_FUNCTION_ARGS)
Definition: hstore_io.c:525
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:695
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:330
size_t vallen
Definition: hstore.h:166
#define PG_GETARG_HEAPTUPLEHEADER(n)
Definition: fmgr.h:276
unsigned int Oid
Definition: postgres_ext.h:31
Datum hstore_from_text(PG_FUNCTION_ARGS)
Definition: hstore_io.c:489
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:348
Datum hstore_to_json_loose(PG_FUNCTION_ARGS)
Definition: hstore_io.c:1245
int natts
Definition: tupdesc.h:73
bool needfree
Definition: hstore.h:168
int32 record_typmod
Definition: hstore_io.c:754
signed int int32
Definition: c.h:246
char * val
Definition: hstore.h:164
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1662
#define ARR_LBOUND(a)
Definition: array.h:281
HeapTupleHeader t_data
Definition: htup.h:67
#define HeapTupleHeaderGetTypMod(tup)
Definition: htup_details.h:455
char * key
Definition: hstore.h:163
static char * cpw(char *dst, char *src, int len)
Definition: hstore_io.c:1113
JsonbValue * pushJsonbValue(JsonbParseState **pstate, JsonbIteratorToken seq, JsonbValue *jbval)
Definition: jsonb_util.c:517
#define PG_GETARG_ARRAYTYPE_P(n)
Definition: array.h:248
#define HSTORE_VALISNULL(arr_, i_)
Definition: hstore.h:83
void pfree(void *pointer)
Definition: mcxt.c:949
static void parse_hstore(HSParser *state)
Definition: hstore_io.c:183
#define ERROR
Definition: elog.h:43
int hstoreFindKey(HStore *hs, int *lowbound, char *key, int keylen)
Definition: hstore_op.c:37
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
Definition: fmgr.c:1904
bool IsValidJsonNumber(const char *str, int len)
Definition: json.c:192
#define ARR_DIMS(a)
Definition: array.h:279
static struct @121 value
ItemPointerData t_self
Definition: htup.h:65
HStore * hstorePairs(Pairs *pairs, int32 pcount, int32 buflen)
Definition: hstore_io.c:375
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:157
uint32 t_len
Definition: htup.h:64
static char * buf
Definition: pg_test_fsync.c:67
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:161
#define HS_COUNT(hsp_)
Definition: hstore.h:61
static int comparePairs(const void *a, const void *b)
Definition: hstore_io.c:282
char * cur
Definition: hstore_io.c:33
#define CStringGetDatum(X)
Definition: postgres.h:584
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:62
Oid t_tableOid
Definition: htup.h:66
#define HS_ADDITEM(dent_, dbuf_, dptr_, pair_)
Definition: hstore.h:112
Datum numeric_in(PG_FUNCTION_ARGS)
Definition: numeric.c:569
Definition: jsonb.h:23
#define GV_INVAL
Definition: hstore_io.c:55
bool type_is_rowtype(Oid typid)
Definition: lsyscache.c:2404
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:132
#define HSTORE_KEY(arr_, str_, i_)
Definition: hstore.h:79
#define WDEL
Definition: hstore_io.c:179
HSTORE_POLLUTE(hstore_from_text, tconvert)
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2599
#define ereport(elevel, rest)
Definition: elog.h:122
#define HS_FINALIZE(hsp_, count_, buf_, ptr_)
Definition: hstore.h:129
struct RecordIOData RecordIOData
#define DirectFunctionCall3(func, arg1, arg2, arg3)
Definition: fmgr.h:589
char * pq_getmsgtext(StringInfo msg, int rawbytes, int *nbytes)
Definition: pqformat.c:548
#define GV_INESCVAL
Definition: hstore_io.c:56
#define MaxAllocSize
Definition: memutils.h:40
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:169
void initStringInfo(StringInfo str)
Definition: stringinfo.c:46
#define CALCDATASIZE(x, lenstr)
Definition: hstore.h:72
ColumnIOData columns[FLEXIBLE_ARRAY_MEMBER]
Definition: hstore_io.c:756
int pcur
Definition: hstore_io.c:38
uintptr_t Datum
Definition: postgres.h:372
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
#define HeapTupleHeaderGetTypeId(tup)
Definition: htup_details.h:445
#define RESIZEPRSBUF
Definition: hstore_io.c:42
int hstoreUniquePairs(Pairs *a, int32 l, int32 *buflen)
Definition: hstore_io.c:312
Datum InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1618
Datum hstore_send(PG_FUNCTION_ARGS)
Definition: hstore_io.c:1199
#define WVAL
Definition: hstore_io.c:176
#define InvalidOid
Definition: postgres_ext.h:36
size_t keylen
Definition: hstore.h:165
Datum hstore_populate_record(PG_FUNCTION_ARGS)
Definition: hstore_io.c:915
#define HSTORE_MAX_KEY_LEN
Definition: hstore.h:41
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:331
#define GV_WAITESCESCIN
Definition: hstore_io.c:58
text * cstring_to_text(const char *s)
Definition: varlena.c:149
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
Datum hstore_from_array(PG_FUNCTION_ARGS)
Definition: hstore_io.c:645
#define DatumGetNumeric(X)
Definition: numeric.h:49
#define Assert(condition)
Definition: c.h:681
Definition: regguts.h:298
bool isnull
Definition: hstore.h:167
Datum hstore_from_record(PG_FUNCTION_ARGS)
Definition: hstore_io.c:761
PG_MODULE_MAGIC
Definition: hstore_io.c:23
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:322
PG_FUNCTION_INFO_V1(hstore_in)
Definition: hstore.h:18
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:222
Oid typioparam
Definition: hstore_io.c:747
#define ARR_NDIM(a)
Definition: array.h:275
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:962
Pairs * pairs
Definition: hstore_io.c:37
enum jbvType type
Definition: jsonb.h:250
char * begin
Definition: hstore_io.c:31
#define HSTORE_VAL(arr_, str_, i_)
Definition: hstore.h:80
void heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition: heaptuple.c:936
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3449
static Datum values[MAXATTR]
Definition: bootstrap.c:164
size_t hstoreCheckKeyLen(size_t len)
Definition: hstore_io.c:354
#define ItemPointerSetInvalid(pointer)
Definition: itemptr.h:150
char * ptr
Definition: hstore_io.c:32
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
void * palloc(Size size)
Definition: mcxt.c:848
int errmsg(const char *fmt,...)
Definition: elog.c:797
int wordlen
Definition: hstore_io.c:35
#define HSTORE_MAX_VALUE_LEN
Definition: hstore.h:42
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:706
#define STRPTR(x)
Definition: hstore.h:76
Datum hstore_recv(PG_FUNCTION_ARGS)
Definition: hstore_io.c:426
#define WEQ
Definition: hstore_io.c:177
int i
#define GV_WAITESCIN
Definition: hstore_io.c:57
#define NameStr(name)
Definition: c.h:493
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:242
#define WKEY
Definition: hstore_io.c:175
Definition: c.h:433
#define PG_FUNCTION_ARGS
Definition: fmgr.h:158
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition: pqformat.c:417
int plen
Definition: hstore_io.c:39
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
#define ARRPTR(x)
Definition: cube.c:26
#define elog
Definition: elog.h:219
#define HS_SETCOUNT(hsp_, c_)
Definition: hstore.h:62
#define qsort(a, b, c, d)
Definition: port.h:447
#define ReleaseTupleDesc(tupdesc)
Definition: tupdesc.h:115
static bool get_val(HSParser *state, bool ignoreeq, bool *escaped)
Definition: hstore_io.c:61
Definition: hstore.h:161
#define ARR_ELEMTYPE(a)
Definition: array.h:277
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:208
long val
Definition: informix.c:689
#define PG_RETURN_NULL()
Definition: fmgr.h:305
#define offsetof(type, field)
Definition: c.h:549
Oid column_type
Definition: hstore_io.c:745
#define HeapTupleHeaderGetDatumLength(tup)
Definition: htup_details.h:439