Notes on pair deduplication
authorPeter Geoghegan <[email protected]>
Sun, 16 Mar 2014 07:55:24 +0000 (00:55 -0700)
committerPeter Geoghegan <[email protected]>
Sun, 16 Mar 2014 07:55:24 +0000 (00:55 -0700)
src/backend/utils/adt/jsonb_util.c
src/include/utils/jsonb.h

index 584f1b0271be6406034560f43420a38fc9e80ed4..ea6bea0b872577767a1a9321583b925edd25875f 100644 (file)
@@ -983,8 +983,9 @@ deepContains(JsonbIterator ** val, JsonbIterator ** mContained)
 }
 
 /*
- * 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)
@@ -995,6 +996,8 @@ 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,
@@ -1010,11 +1013,11 @@ arrayToJsonbSortedArray(ArrayType *a)
     * 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;
@@ -1664,7 +1667,7 @@ lengthCompareJsonbStringValue(const void *a, const void *b, void *binequal)
  * 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.
  */
index 0c360db65f9b03ead84ec6f7353ced96192019a0..04a74a2e510433cf07abf0aa592824774d417a4b 100644 (file)
@@ -195,7 +195,14 @@ struct JsonbValue
    };
 };
 
-/* 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 */