}
/*
- * Convert a Postgres text array to a JsonbSortedArray, with de-duplicated key
- * elements.
+ * Convert a Postgres text array to a Jsonb array, sorted and with
+ * de-duplicated key elements. This is used for searching for items in the
+ * array.
*/
JsonbValue *
arrayToJsonbSortedArray(ArrayType *a)
JsonbValue *result;
int i,
j;
+ Size maxn = Min(MaxAllocSize / sizeof(JsonbPair),
+ JENTRY_POSMASK / sizeof(JsonbPair));
/* Extract data for sorting */
deconstruct_array(a, TEXTOID, -1, false, 'i', &key_datums, &key_nulls,
* assumption. Therefore, use an explicit check rather than relying on
* palloc() to complain.
*/
- if (key_count > MaxAllocSize / sizeof(JsonbPair))
+ if (key_count > maxn)
ereport(ERROR,
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
errmsg("number of pairs (%d) exceeds the maximum allowed (%zu)",
- key_count, MaxAllocSize / sizeof(JsonbPair))));
+ key_count, maxn)));
result = palloc(sizeof(JsonbValue));
result->type = jbvArray;
* same "arg setting" hack will be applied here in respect of the pair's key
* values.
*
- * N.B: String comparions here are "length-wise"
+ * N.B: String comparisons here are "length-wise"
*
* Pairs with equals keys are ordered such that the order field is respected.
*/
};
};
-/* Pair within an Object */
+/*
+ * Pair within an Object.
+ *
+ * Pairs with duplicate keys are de-duplicated. We store the order for the
+ * benefit of doing so in a well-defined way with respect to the original
+ * observed order (which is "last observed wins"). This is only stored when
+ * originally constructing a Jsonb.
+ */
struct JsonbPair
{
JsonbValue key; /* Must be a jbvString */