Skip to content
Snippets Groups Projects
  1. Jun 10, 2016
  2. Apr 06, 2016
    • Teodor Sigaev's avatar
      Add jsonb_insert · 0b62fd03
      Teodor Sigaev authored
      It inserts a new value into an jsonb array at arbitrary position or
      a new key to jsonb object.
      
      Author: Dmitry Dolgov
      Reviewers: Petr Jelinek, Vitaly Burovoy, Andrew Dunstan
      0b62fd03
  3. Mar 23, 2016
    • Tom Lane's avatar
      Code review for error reports in jsonb_set(). · ea4b8bd6
      Tom Lane authored
      User-facing (even tested by regression tests) error conditions were thrown
      with elog(), hence had wrong SQLSTATE and were untranslatable.  And the
      error message texts weren't up to project style, either.
      ea4b8bd6
    • Tom Lane's avatar
      Fix unsafe use of strtol() on a non-null-terminated Text datum. · 384dfbde
      Tom Lane authored
      jsonb_set() could produce wrong answers or incorrect error reports, or in
      the worst case even crash, when trying to convert a path-array element into
      an integer for use as an array subscript.  Per report from Vitaly Burovoy.
      Back-patch to 9.5 where the faulty code was introduced (in commit
      c6947010).
      
      Michael Paquier
      384dfbde
  4. Mar 03, 2016
    • Tom Lane's avatar
      Fix json_to_record() bug with nested objects. · a9d199f6
      Tom Lane authored
      A thinko concerning nesting depth caused json_to_record() to produce bogus
      output if a field of its input object contained a sub-object with a field
      name matching one of the requested output column names.  Per bug #13996
      from Johann Visagie.
      
      I added a regression test case based on his example, plus parallel tests
      for json_to_recordset, jsonb_to_record, jsonb_to_recordset.  The latter
      three do not exhibit the same bug (which suggests that we may be missing
      some opportunities to share code...) but testing seems like a good idea
      in any case.
      
      Back-patch to 9.4 where these functions were introduced.
      a9d199f6
  5. Jan 02, 2016
  6. Dec 11, 2015
  7. Oct 12, 2015
  8. Oct 05, 2015
    • Noah Misch's avatar
      Prevent stack overflow in json-related functions. · 08fa47c4
      Noah Misch authored
      Sufficiently-deep recursion heretofore elicited a SIGSEGV.  If an
      application constructs PostgreSQL json or jsonb values from arbitrary
      user input, application users could have exploited this to terminate all
      active database connections.  That applies to 9.3, where the json parser
      adopted recursive descent, and later versions.  Only row_to_json() and
      array_to_json() were at risk in 9.2, both in a non-security capacity.
      Back-patch to 9.2, where the json type was introduced.
      
      Oskari Saarenmaa, reviewed by Michael Paquier.
      
      Security: CVE-2015-5289
      08fa47c4
  9. Oct 04, 2015
    • Andrew Dunstan's avatar
      Disallow invalid path elements in jsonb_set · 1edd4ec8
      Andrew Dunstan authored
      Null path elements and, where the object is an array, invalid integer
      elements now cause an error.
      
      Incorrect behaviour noted by Thom Brown, patch from Dmitry Dolgov.
      
      Backpatch to 9.5 where jsonb_set was introduced
      1edd4ec8
  10. Sep 13, 2015
    • Andrew Dunstan's avatar
      Fix the fastpath rule for jsonb_concat with an empty operand. · e7e3ac2d
      Andrew Dunstan authored
      To prevent perverse results, we now only return the other operand if
      it's not scalar, and if both operands are of the same kind (array or
      object).
      
      Original bug complaint and patch from Oskari Saarenmaa, extended by me
      to cover the cases of different kinds of jsonb.
      
      Backpatch to 9.5 where jsonb_concat was introduced.
      e7e3ac2d
  11. Jul 28, 2015
  12. Jul 19, 2015
  13. Jul 18, 2015
    • Andrew Dunstan's avatar
      Support JSON negative array subscripts everywhere · e02d44b8
      Andrew Dunstan authored
      Previously, there was an inconsistency across json/jsonb operators that
      operate on datums containing JSON arrays -- only some operators
      supported negative array count-from-the-end subscripting.  Specifically,
      only a new-to-9.5 jsonb deletion operator had support (the new "jsonb -
      integer" operator).  This inconsistency seemed likely to be
      counter-intuitive to users.  To fix, allow all places where the user can
      supply an integer subscript to accept a negative subscript value,
      including path-orientated operators and functions, as well as other
      extraction operators.  This will need to be called out as an
      incompatibility in the 9.5 release notes, since it's possible that users
      are relying on certain established extraction operators changed here
      yielding NULL in the event of a negative subscript.
      
      For the json type, this requires adding a way of cheaply getting the
      total JSON array element count ahead of time when parsing arrays with a
      negative subscript involved, necessitating an ad-hoc lex and parse.
      This is followed by a "conversion" from a negative subscript to its
      equivalent positive-wise value using the count.  From there on, it's as
      if a positive-wise value was originally provided.
      
      Note that there is still a minor inconsistency here across jsonb
      deletion operators.  Unlike the aforementioned new "-" deletion operator
      that accepts an integer on its right hand side, the new "#-" path
      orientated deletion variant does not throw an error when it appears like
      an array subscript (input that could be recognized by as an integer
      literal) is being used on an object, which is wrong-headed.  The reason
      for not being stricter is that it could be the case that an object pair
      happens to have a key value that looks like an integer; in general,
      these two possibilities are impossible to differentiate with rhs path
      text[] argument elements.  However, we still don't allow the "#-"
      path-orientated deletion operator to perform array-style subscripting.
      Rather, we just return the original left operand value in the event of a
      negative subscript (which seems analogous to how the established
      "jsonb/json #> text[]" path-orientated operator may yield NULL in the
      event of an invalid subscript).
      
      In passing, make SetArrayPath() stricter about not accepting cases where
      there is trailing non-numeric garbage bytes rather than a clean NUL
      byte.  This means, for example, that strings like "10e10" are now not
      accepted as an array subscript of 10 by some new-to-9.5 path-orientated
      jsonb operators (e.g. the new #- operator).  Finally, remove dead code
      for jsonb subscript deletion; arguably, this should have been done in
      commit b81c7b40.
      
      Peter Geoghegan and Andrew Dunstan
      e02d44b8
  14. Jun 13, 2015
    • Andrew Dunstan's avatar
      Fix "path" infrastructure bug affecting jsonb_set() · 2271d002
      Andrew Dunstan authored
      jsonb_set() and other clients of the setPathArray() utility function
      could get spurious results when an array integer subscript is provided
      that is not within the range of int.
      
      To fix, ensure that the value returned by strtol() within setPathArray()
      is within the range of int;  when it isn't, assume an invalid input in
      line with existing, similar cases.  The path-orientated operators that
      appeared in PostgreSQL 9.3 and 9.4 do not call setPathArray(), and
      already independently take this precaution, so no change there.
      
      Peter Geoghegan
      2271d002
  15. Jun 08, 2015
    • Andrew Dunstan's avatar
      Desupport jsonb subscript deletion on objects · b81c7b40
      Andrew Dunstan authored
      Supporting deletion of JSON pairs within jsonb objects using an
      array-style integer subscript allowed for surprising outcomes.  This was
      mostly due to the implementation-defined ordering of pairs within
      objects for jsonb.
      
      It also seems desirable to make jsonb integer subscript deletion
      consistent with the 9.4 era general purpose integer subscripting
      operator for jsonb (although that operator returns NULL when an object
      is encountered, while we prefer here to throw an error).
      
      Peter Geoghegan, following discussion on -hackers.
      b81c7b40
  16. Jun 01, 2015
  17. May 27, 2015
  18. May 26, 2015
  19. May 25, 2015
  20. May 24, 2015
  21. May 17, 2015
  22. May 13, 2015
    • Andrew Dunstan's avatar
      Fix jsonb replace and delete on scalars and empty structures · 3f2cec79
      Andrew Dunstan authored
      These operations now error out if attempted on scalars, and simply
      return the input if attempted on empty arrays or objects. Along the way
      we remove the unnecessary cloning of the input when it's known to be
      unchanged. Regression tests covering these cases are added.
      3f2cec79
  23. May 12, 2015
    • Andrew Dunstan's avatar
      Additional functions and operators for jsonb · c6947010
      Andrew Dunstan authored
      jsonb_pretty(jsonb) produces nicely indented json output.
      jsonb || jsonb concatenates two jsonb values.
      jsonb - text removes a key and its associated value from the json
      jsonb - int removes the designated array element
      jsonb - text[] removes a key and associated value or array element at
      the designated path
      jsonb_replace(jsonb,text[],jsonb) replaces the array element designated
      by the path or the value associated with the key designated by the path
      with the given value.
      
      Original work by Dmitry Dolgov, adapted and reworked for PostgreSQL core
      by Andrew Dunstan, reviewed and tidied up by Petr Jelinek.
      c6947010
  24. May 04, 2015
    • Andrew Dunstan's avatar
      Fix two small bugs in json's populate_record_worker · 3c000fd9
      Andrew Dunstan authored
      The first bug is not releasing a tupdesc when doing an early return out
      of the function. The second bug is a logic error in choosing when to do
      an early return if given an empty jsonb object.
      
      Bug reports from Pavel Stehule and Tom Lane respectively.
      
      Backpatch to 9.4 where these were introduced.
      3c000fd9
  25. Mar 31, 2015
  26. Feb 20, 2015
  27. Jan 06, 2015
  28. Dec 16, 2014
    • Andrew Dunstan's avatar
      Fix some jsonb issues found by Coverity in recent commits. · c8315930
      Andrew Dunstan authored
      Mostly these issues concern the non-use of function results. These
      have been changed to use (void) pushJsonbValue(...) instead of assigning
      the result to a variable that gets overwritten before it is used.
      
      There is a larger issue that we should possibly examine the API for
      pushJsonbValue(), so that instead of returning a value it modifies a
      state argument. The current idiom is rather clumsy. However, changing
      that requires quite a bit more work, so this change should do for the
      moment.
      c8315930
  29. Dec 12, 2014
    • Andrew Dunstan's avatar
      Add json_strip_nulls and jsonb_strip_nulls functions. · 237a8824
      Andrew Dunstan authored
      The functions remove object fields, including in nested objects, that
      have null as a value. In certain cases this can lead to considerably
      smaller datums, with no loss of semantic information.
      
      Andrew Dunstan, reviewed by Pavel Stehule.
      237a8824
  30. Aug 22, 2014
    • Tom Lane's avatar
      Fix corner-case behaviors in JSON/JSONB field extraction operators. · 41dd50e8
      Tom Lane authored
      Cause the path extraction operators to return their lefthand input,
      not NULL, if the path array has no elements.  This seems more consistent
      since the case ought to correspond to applying the simple extraction
      operator (->) zero times.
      
      Cause other corner cases in field/element/path extraction to return NULL
      rather than failing.  This behavior is arguably more useful than throwing
      an error, since it allows an expression index using these operators to be
      built even when not all values in the column are suitable for the
      extraction being indexed.  Moreover, we already had multiple
      inconsistencies between the path extraction operators and the simple
      extraction operators, as well as inconsistencies between the JSON and
      JSONB code paths.  Adopt a uniform rule of returning NULL rather than
      throwing an error when the JSON input does not have a structure that
      permits the request to be satisfied.
      
      Back-patch to 9.4.  Update the release notes to list this as a behavior
      change since 9.3.
      41dd50e8
  31. Aug 20, 2014
    • Tom Lane's avatar
      Fix core dump in jsonb #> operator, and add regression test cases. · 9bac6602
      Tom Lane authored
      jsonb's #> operator segfaulted (dereferencing a null pointer) if the RHS
      was a zero-length array, as reported in bug #11207 from Justin Van Winkle.
      json's #> operator returns NULL in such cases, so for the moment let's
      make jsonb act likewise.
      
      Also add a bunch of regression test queries memorializing the -> and #>
      operators' behavior for this and other corner cases.
      
      There is a good argument for changing some of these behaviors, as they
      are not very consistent with each other, and throwing an error isn't
      necessarily a desirable behavior for operators that are likely to be
      used in indexes.  However, everybody can agree that a core dump is the
      Wrong Thing, and we need test cases even if we decide to change their
      expected output later.
      9bac6602
  32. Jun 29, 2014
    • Tom Lane's avatar
      Remove use_json_as_text options from json_to_record/json_populate_record. · a749a23d
      Tom Lane authored
      The "false" case was really quite useless since all it did was to throw
      an error; a definition not helped in the least by making it the default.
      Instead let's just have the "true" case, which emits nested objects and
      arrays in JSON syntax.  We might later want to provide the ability to
      emit sub-objects in Postgres record or array syntax, but we'd be best off
      to drive that off a check of the target field datatype, not a separate
      argument.
      
      For the functions newly added in 9.4, we can just remove the flag arguments
      outright.  We can't do that for json_populate_record[set], which already
      existed in 9.3, but we can ignore the argument and always behave as if it
      were "true".  It helps that the flag arguments were optional and not
      documented in any useful fashion anyway.
      a749a23d
  33. Jun 26, 2014
    • Tom Lane's avatar
      Rationalize error messages within jsonfuncs.c. · 798e2357
      Tom Lane authored
      I noticed that the functions in jsonfuncs.c sometimes printed error
      messages that claimed I'd called some other function.  Investigation showed
      that this was from repurposing code into "worker" functions without taking
      much care as to whether it would mention the right SQL-level function if it
      threw an error.  Moreover, there was a weird mismash of messages that
      contained a fixed function name, messages that used %s for a function name,
      and messages that constructed a function name out of spare parts, like
      "json%s_populate_record" (which, quite aside from being ugly as sin, wasn't
      even sufficient to cover all the cases).  This would put an undue burden on
      our long-suffering translators.  Standardize on inserting the SQL function
      name with %s so as to reduce the number of translatable strings, and pass
      function names around as needed to make sure we can report the right one.
      Fix up some gratuitous variations in wording, too.
      798e2357
  34. Jun 25, 2014
    • Tom Lane's avatar
      Cosmetic improvements in jsonfuncs.c. · 8d2d7ad5
      Tom Lane authored
      Re-pgindent, remove a lot of random vertical whitespace, remove useless
      (if not counterproductive) inline markings, get rid of unnecessary
      zero-padding of strings for hashtable searches.  No functional changes.
      8d2d7ad5
    • Tom Lane's avatar
      Fix handling of nested JSON objects in json_populate_recordset and friends. · 57d8c127
      Tom Lane authored
      populate_recordset_object_start() improperly created a new hash table
      (overwriting the link to the existing one) if called at nest levels
      greater than one.  This resulted in previous fields not appearing in
      the final output, as reported by Matti Hameister in bug #10728.
      In 9.4 the problem also affects json_to_recordset.
      
      This perhaps missed detection earlier because the default behavior is to
      throw an error for nested objects: you have to pass use_json_as_text = true
      to see the problem.
      
      In addition, fix query-lifespan leakage of the hashtable created by
      json_populate_record().  This is pretty much the same problem recently
      fixed in dblink: creating an intended-to-be-temporary context underneath
      the executor's per-tuple context isn't enough to make it go away at the
      end of the tuple cycle, because MemoryContextReset is not
      MemoryContextResetAndDeleteChildren.
      
      Michael Paquier and Tom Lane
      57d8c127
  35. Jun 02, 2014
    • Andrew Dunstan's avatar
      Improve the efficiency of certain jsonb get operations. · 1a4174a4
      Andrew Dunstan authored
      Instead of iterating over jsonb structures, use the inbuilt functions
      findJsonbValueFromContainerLen() and getIthJsonbValueFromContainer() to
      extract values directly. These functions use algorithms that are O(n log
      n) and O(1) respectively, whereas iterating is O(n), so we should see
      considerable speedup here.
      
      Teodor Sigaev.
      1a4174a4
  36. May 07, 2014
    • Heikki Linnakangas's avatar
      Clean up jsonb code. · 364ddc3e
      Heikki Linnakangas authored
      The main target of this cleanup is the convertJsonb() function, but I also
      touched a lot of other things that I spotted into in the process.
      
      The new convertToJsonb() function uses an output buffer that's resized on
      demand, so the code to estimate of the size of JsonbValue is removed.
      
      The on-disk format was not changed, even though I refactored the structs
      used to handle it. The term "superheader" is replaced with "container".
      
      The jsonb_exists_any and jsonb_exists_all functions no longer sort the input
      array. That was a premature optimization, the idea being that if there are
      duplicates in the input array, you only need to check them once. Also,
      sorting the array saves some effort in the binary search used to find a key
      within an object. But there were drawbacks too: the sorting and
      deduplicating obviously isn't free, and in the typical case there are no
      duplicates to remove, and the gain in the binary search was minimal. Remove
      all that, which makes the code simpler too.
      
      This includes a bug-fix; the total length of the elements in a jsonb array
      or object mustn't exceed 2^28. That is now checked.
      364ddc3e
Loading