Skip to content
Snippets Groups Projects
  1. Jul 15, 2016
  2. Jun 23, 2016
    • Tom Lane's avatar
      Fix small memory leak in partial-aggregate deserialization functions. · bd1693e8
      Tom Lane authored
      A deserialize function's result is short-lived data during partial
      aggregation, since we're just going to pass it to the combine function
      and then it's of no use anymore.  However, the built-in deserialize
      functions allocated their results in the aggregate state context,
      resulting in a query-lifespan memory leak.  It's probably not possible for
      this to amount to anything much at present, since the number of leaked
      results would only be the number of worker processes.  But it might become
      a problem in future.  To fix, don't use the same convenience subroutine for
      setting up results that the aggregate transition functions use.
      
      David Rowley
      
      Report: <10050.1466637736@sss.pgh.pa.us>
      bd1693e8
  3. Jun 22, 2016
    • Tom Lane's avatar
      Fix type-safety problem with parallel aggregate serial/deserialization. · f8ace547
      Tom Lane authored
      The original specification for this called for the deserialization function
      to have signature "deserialize(serialtype) returns transtype", which is a
      security violation if transtype is INTERNAL (which it always would be in
      practice) and serialtype is not (which ditto).  The patch blithely overrode
      the opr_sanity check for that, which was sloppy-enough work in itself,
      but the indisputable reason this cannot be allowed to stand is that CREATE
      FUNCTION will reject such a signature and thus it'd be impossible for
      extensions to create parallelizable aggregates.
      
      The minimum fix to make the signature type-safe is to add a second, dummy
      argument of type INTERNAL.  But to lock it down a bit more and make misuse
      of INTERNAL-accepting functions less likely, let's get rid of the ability
      to specify a "serialtype" for an aggregate and just say that the only
      useful serialtype is BYTEA --- which, in practice, is the only interesting
      value anyway, due to the usefulness of the send/recv infrastructure for
      this purpose.  That means we only have to allow "serialize(internal)
      returns bytea" and "deserialize(bytea, internal) returns internal" as
      the signatures for these support functions.
      
      In passing fix bogus signature of int4_avg_combine, which I found thanks
      to adding an opr_sanity check on combinefunc signatures.
      
      catversion bump due to removing pg_aggregate.aggserialtype and adjusting
      signatures of assorted built-in functions.
      
      David Rowley and Tom Lane
      
      Discussion: <27247.1466185504@sss.pgh.pa.us>
      f8ace547
  4. Jun 10, 2016
  5. May 05, 2016
    • Dean Rasheed's avatar
      Fix corner-case loss of precision in numeric pow() calculation · 18a02ad2
      Dean Rasheed authored
      Commit 7d9a4737 greatly improved the
      accuracy of the numeric transcendental functions, however it failed to
      consider the case where the result from pow() is close to the overflow
      threshold, for example 0.12 ^ -2345.6. For such inputs, where the
      result has more than 2000 digits before the decimal point, the decimal
      result weight estimate was being clamped to 2000, leading to a loss of
      precision in the final calculation.
      
      Fix this by replacing the clamping code with an overflow test that
      aborts the calculation early if the final result is sure to overflow,
      based on the overflow limit in exp_var(). This provides the same
      protection against integer overflow in the subsequent result scale
      computation as the original clamping code, but it also ensures that
      precision is never lost and saves compute cycles in cases that are
      sure to overflow.
      
      The new early overflow test works with the initial low-precision
      result (expected to be accurate to around 8 significant digits) and
      includes a small fuzz factor to ensure that it doesn't kick in for
      values that would not overflow exp_var(), so the overall overflow
      threshold of pow() is unchanged and consistent for all inputs with
      non-integer exponents.
      
      Author: Dean Rasheed
      Reviewed-by: Tom Lane
      Discussion: http://www.postgresql.org/message-id/CAEZATCUj3U-cQj0jjoia=qgs0SjE3auroxh8swvNKvZWUqegrg@mail.gmail.com
      See-also: http://www.postgresql.org/message-id/CAEZATCV7w+8iB=07dJ8Q0zihXQT1semcQuTeK+4_rogC_zq5Hw@mail.gmail.com
      18a02ad2
  6. Apr 05, 2016
    • Robert Haas's avatar
      Add parallel query support functions for assorted aggregates. · 11c8669c
      Robert Haas authored
      This lets us use parallel aggregate for a variety of useful cases
      that didn't work before, like sum(int8), sum(numeric), several
      versions of avg(), and various other functions.
      
      Add some regression tests, as well, testing the general sanity of
      these and future catalog entries.
      
      David Rowley, reviewed by Tomas Vondra, with a few further changes
      by me.
      11c8669c
  7. Jan 05, 2016
  8. Jan 02, 2016
  9. Nov 25, 2015
    • Tom Lane's avatar
      Improve div_var_fast(), mostly by making comments better. · 46166197
      Tom Lane authored
      The integer overflow situation in div_var_fast() is a great deal more
      complicated than the pre-existing comments would suggest.  Moreover, the
      comments were also flat out incorrect as to the precise statement of the
      maxdiv loop invariant.  Upon clarifying that, it becomes apparent that the
      way in which we updated maxdiv after a carry propagation pass was overly
      slow, complex, and conservative: we can just reset it to one, which is much
      easier and also reduces the number of times carry propagation occurs.
      Fix that and improve the relevant comments.
      
      Since this is mostly a comment fix, with only a rather marginal performance
      boost, no need for back-patch.
      
      Tom Lane and Dean Rasheed
      46166197
  10. Nov 17, 2015
    • Tom Lane's avatar
      Fix possible internal overflow in numeric division. · 5f10b7a6
      Tom Lane authored
      div_var_fast() postpones propagating carries in the same way as mul_var(),
      so it has the same corner-case overflow risk we fixed in 246693e5,
      namely that the size of the carries has to be accounted for when setting
      the threshold for executing a carry propagation step.  We've not devised
      a test case illustrating the brokenness, but the required fix seems clear
      enough.  Like the previous fix, back-patch to all active branches.
      
      Dean Rasheed
      5f10b7a6
  11. Nov 14, 2015
    • Tom Lane's avatar
      Improve type numeric's calculations for ln(), log(), exp(), pow(). · 7d9a4737
      Tom Lane authored
      Set the "rscales" for intermediate-result calculations to ensure that
      suitable numbers of significant digits are maintained throughout.  The
      previous coding hadn't thought this through in any detail, and as a result
      could deliver results with many inaccurate digits, or in the worst cases
      even fail with divide-by-zero errors as a result of losing all nonzero
      digits of intermediate results.
      
      In exp_var(), get rid entirely of the logic that separated the calculation
      into integer and fractional parts: that was neither accurate nor
      particularly fast.  The existing range-reduction method of dividing by 2^n
      can be applied across the full input range instead of only 0..1, as long as
      we are careful to set an appropriate rscale for each step.
      
      Also fix the logic in mul_var() for shortening the calculation when the
      caller asks for fewer output digits than an exact calculation would
      require.  This bug doesn't affect simple multiplications since that code
      path asks for an exact result, but it does contribute to accuracy issues
      in the transcendental math functions.
      
      In passing, improve performance of mul_var() a bit by forcing the shorter
      input to be on the left, thus reducing the number of iterations of the
      outer loop and probably also reducing the number of carry-propagation
      steps needed.
      
      This is arguably a bug fix, but in view of the lack of field complaints,
      it does not seem worth the risk of back-patching.
      
      Dean Rasheed
      7d9a4737
  12. Sep 21, 2015
    • Tom Lane's avatar
      Fix possible internal overflow in numeric multiplication. · 246693e5
      Tom Lane authored
      mul_var() postpones propagating carries until it risks overflow in its
      internal digit array.  However, the logic failed to account for the
      possibility of overflow in the carry propagation step, allowing wrong
      results to be generated in corner cases.  We must slightly reduce the
      when-to-propagate-carries threshold to avoid that.
      
      Discovered and fixed by Dean Rasheed, with small adjustments by me.
      
      This has been wrong since commit d72f6c75,
      so back-patch to all supported branches.
      246693e5
  13. Aug 02, 2015
    • Tom Lane's avatar
      Avoid calling memcpy() with a NULL source pointer and count == 0. · 13bba022
      Tom Lane authored
      As in commit 0a52d378, avoid doing something that has undefined
      results according to the C standard, even though in practice there does
      not seem to be any problem with it.
      
      This fixes two places in numeric.c that demonstrably could call memcpy()
      with such arguments.  I looked through that file and didn't see any other
      places with similar hazards; this is not to claim that there are not such
      places in other files.
      
      Per report from Piotr Stefaniak.  Back-patch to 9.5 which is where the
      previous commit was added.  We're more or less setting a precedent that
      we will not worry about this type of issue in pre-9.5 branches unless
      someone demonstrates a problem in the field.
      13bba022
  14. May 24, 2015
  15. Apr 04, 2015
    • Robert Haas's avatar
      Fix numeric abbreviation for --disable-float8-byval. · 368b7c60
      Robert Haas authored
      When committing abd94bca, I tried to make
      it decide what kind of abbreviation to use based only on SIZEOF_DATUM,
      without regard to USE_FLOAT8_BYVAL.  That attempt was a few bricks short
      of a load, so try to fix it, and add a comment explaining what we're
      about.
      
      Patch by me; review (but not a full endorsement) by Andrew Gierth.
      368b7c60
  16. Apr 02, 2015
  17. Mar 22, 2015
  18. Mar 20, 2015
    • Andres Freund's avatar
      Use 128-bit math to accelerate some aggregation functions. · 959277a4
      Andres Freund authored
      On platforms where we support 128bit integers, use them to implement
      faster transition functions for sum(int8), avg(int8),
      var_*(int2/int4),stdev_*(int2/int4). Where not supported continue to use
      numeric as a transition type.
      
      In some synthetic benchmarks this has been shown to provide significant
      speedups.
      
      Bumps catversion.
      
      Discussion: 544BB5F1.50709@proxel.se
      Author: Andreas Karlsson
      Reviewed-By: Peter Geoghegan, Petr Jelinek, Andres Freund,
          Oskari Saarenmaa, David Rowley
      959277a4
  19. Feb 21, 2015
  20. Jan 27, 2015
    • Tom Lane's avatar
      Fix NUMERIC field access macros to treat NaNs consistently. · 1a2b2034
      Tom Lane authored
      Commit 14534353 arranged to store numeric
      NaN values as short-header numerics, but the field access macros did not
      get the memo: they thought only "SHORT" numerics have short headers.
      
      Most of the time this makes no difference because we don't access the
      weight or dscale of a NaN; but numeric_send does that.  As pointed out
      by Andrew Gierth, this led to fetching uninitialized bytes.
      
      AFAICS this could not have any worse consequences than that; in particular,
      an unaligned stored numeric would have been detoasted by PG_GETARG_NUMERIC,
      so that there's no risk of a fetch off the end of memory.  Still, the code
      is wrong on its own terms, and it's not hard to foresee future changes that
      might expose us to real risks.  So back-patch to all affected branches.
      1a2b2034
  21. Jan 06, 2015
  22. Dec 18, 2014
    • Fujii Masao's avatar
      Ensure variables live across calls in generate_series(numeric, numeric). · 19e065c0
      Fujii Masao authored
      In generate_series_step_numeric(), the variables "start_num"
      and "stop_num" may be potentially freed until the next call.
      So they should be put in the location which can survive across calls.
      But previously they were not, and which could cause incorrect
      behavior of generate_series(numeric, numeric). This commit fixes
      this problem by copying them on multi_call_memory_ctx.
      
      Andrew Gierth
      19e065c0
  23. Dec 01, 2014
    • Tom Lane's avatar
      Guard against bad "dscale" values in numeric_recv(). · 0927bf80
      Tom Lane authored
      We were not checking to see if the supplied dscale was valid for the given
      digit array when receiving binary-format numeric values.  While dscale can
      validly be more than the number of nonzero fractional digits, it shouldn't
      be less; that case causes fractional digits to be hidden on display even
      though they're there and participate in arithmetic.
      
      Bug #12053 from Tommaso Sala indicates that there's at least one broken
      client library out there that sometimes supplies an incorrect dscale value,
      leading to strange behavior.  This suggests that simply throwing an error
      might not be the best response; it would lead to failures in applications
      that might seem to be working fine today.  What seems the least risky fix
      is to truncate away any digits that would be hidden by dscale.  This
      preserves the existing behavior in terms of what will be printed for the
      transmitted value, while preventing subsequent arithmetic from producing
      results inconsistent with that.
      
      In passing, throw a specific error for the case of dscale being outside
      the range that will fit into a numeric's header.  Before you got "value
      overflows numeric format", which is a bit misleading.
      
      Back-patch to all supported branches.
      0927bf80
  24. Nov 11, 2014
  25. Nov 06, 2014
    • Tom Lane's avatar
      Fix normalization of numeric values in JSONB GIN indexes. · 48759319
      Tom Lane authored
      The default JSONB GIN opclass (jsonb_ops) converts numeric data values
      to strings for storage in the index.  It must ensure that numeric values
      that would compare equal (such as 12 and 12.00) produce identical strings,
      else index searches would have behavior different from regular JSONB
      comparisons.  Unfortunately the function charged with doing this was
      completely wrong: it could reduce distinct numeric values to the same
      string, or reduce equivalent numeric values to different strings.  The
      former type of error would only lead to search inefficiency, but the
      latter type of error would cause index entries that should be found by
      a search to not be found.
      
      Repairing this bug therefore means that it will be necessary for 9.4 beta
      testers to reindex GIN jsonb_ops indexes, if they care about getting
      correct results from index searches involving numeric data values within
      the comparison JSONB object.
      
      Per report from Thomas Fanghaenel.
      48759319
  26. Sep 12, 2014
    • Tom Lane's avatar
      Fix power_var_int() for large integer exponents. · 1d352325
      Tom Lane authored
      The code for raising a NUMERIC value to an integer power wasn't very
      careful about large powers.  It got an outright wrong answer for an
      exponent of INT_MIN, due to failure to consider overflow of the Abs(exp)
      operation; which is fixable by using an unsigned rather than signed
      exponent value after that point.  Also, even though the number of
      iterations of the power-computation loop is pretty limited, it's easy for
      the repeated squarings to result in ridiculously enormous intermediate
      values, which can take unreasonable amounts of time/memory to process,
      or even overflow the internal "weight" field and so produce a wrong answer.
      We can forestall misbehaviors of that sort by bailing out as soon as the
      weight value exceeds what will fit in int16, since then the final answer
      must overflow (if exp > 0) or underflow (if exp < 0) the packed numeric
      format.
      
      Per off-list report from Pavel Stehule.  Back-patch to all supported
      branches.
      1d352325
  27. May 06, 2014
    • Bruce Momjian's avatar
      pgindent run for 9.4 · 0a783200
      Bruce Momjian authored
      This includes removing tabs after periods in C comments, which was
      applied to back branches, so this change should not effect backpatching.
      0a783200
  28. Apr 13, 2014
    • Tom Lane's avatar
      Provide moving-aggregate support for a bunch of numerical aggregates. · 9d229f39
      Tom Lane authored
      First installment of the promised moving-aggregate support in built-in
      aggregates: count(), sum(), avg(), stddev() and variance() for
      assorted datatypes, though not for float4/float8.
      
      In passing, remove a 2001-vintage kluge in interval_accum(): interval
      array elements have been properly aligned since around 2003, but
      nobody remembered to take out this workaround.  Also, fix a thinko
      in the opr_sanity tests for moving-aggregate catalog entries.
      
      David Rowley and Florian Pflug, reviewed by Dean Rasheed
      9d229f39
  29. Mar 23, 2014
    • Andrew Dunstan's avatar
      Introduce jsonb, a structured format for storing json. · d9134d0a
      Andrew Dunstan authored
      The new format accepts exactly the same data as the json type. However, it is
      stored in a format that does not require reparsing the orgiginal text in order
      to process it, making it much more suitable for indexing and other operations.
      Insignificant whitespace is discarded, and the order of object keys is not
      preserved. Neither are duplicate object keys kept - the later value for a given
      key is the only one stored.
      
      The new type has all the functions and operators that the json type has,
      with the exception of the json generation functions (to_json, json_agg etc.)
      and with identical semantics. In addition, there are operator classes for
      hash and btree indexing, and two classes for GIN indexing, that have no
      equivalent in the json type.
      
      This feature grew out of previous work by Oleg Bartunov and Teodor Sigaev, which
      was intended to provide similar facilities to a nested hstore type, but which
      in the end proved to have some significant compatibility issues.
      
      Authors: Oleg Bartunov,  Teodor Sigaev, Peter Geoghegan and Andrew Dunstan.
      Review: Andres Freund
      d9134d0a
  30. Jan 07, 2014
  31. Nov 17, 2013
    • Tom Lane's avatar
      Improve performance of numeric sum(), avg(), stddev(), variance(), etc. · 69c8fbac
      Tom Lane authored
      This patch improves performance of most built-in aggregates that formerly
      used a NUMERIC or NUMERIC array as their transition type; this includes
      not only aggregates on numeric inputs, but some aggregates on integer
      inputs where overflow of an int8 value is a possibility.  The code now
      uses a special-purpose data structure to avoid array construction and
      deconstruction overhead, as well as packing and unpacking overhead for
      numeric values.
      
      These aggregates' transition type is now declared as INTERNAL, since
      it doesn't correspond to any SQL data type.  To keep the planner from
      thinking that that means a lot of storage will be used, we make use
      of the just-added pg_aggregate.aggtransspace feature.  The space estimate
      is set to 128 bytes, which is at least in the right ballpark.
      
      Hadi Moshayedi, reviewed by Pavel Stehule and Tomas Vondra
      69c8fbac
  32. May 29, 2013
  33. Apr 20, 2013
    • Peter Eisentraut's avatar
      Clean up references to SQL92 · cc26ea9f
      Peter Eisentraut authored
      In most cases, these were just references to the SQL standard in
      general.  In a few cases, a contrast was made between SQL92 and later
      standards -- those have been kept unchanged.
      cc26ea9f
  34. Jan 01, 2013
  35. Nov 21, 2012
    • Heikki Linnakangas's avatar
      Speed up operations on numeric, mostly by avoiding palloc() overhead. · 5cb0e335
      Heikki Linnakangas authored
      In many functions, a NumericVar was initialized from an input Numeric, to be
      passed as input to a calculation function. When the NumericVar is not
      modified, the digits array of the NumericVar can point directly to the digits
      array in the original Numeric, and we can avoid a palloc() and memcpy(). Add
      init_var_from_num() function to initialize a var like that.
      
      Remove dscale argument from get_str_from_var(), as all the callers just
      passed the dscale of the variable. That means that the rounding it used to
      do was not actually necessary, and get_str_from_var() no longer scribbles on
      its input. That makes it safer in general, and allows us to use the new
      init_var_from_num() function in e.g numeric_out().
      
      Also modified numericvar_to_int8() to no scribble on its input either. It
      creates a temporary copy to avoid that. To compensate, the callers no longer
      need to create a temporary copy, so the net # of pallocs is the same, but this
      is nicer.
      
      In the passing, use a constant for the number 10 in get_str_from_var_sci(),
      when calculating 10^exponent. Saves a palloc() and some cycles to convert
      integer 10 to numeric.
      
      Original patch by Kyotaro HORIGUCHI, with further changes by me. Reviewed
      by Pavel Stehule.
      5cb0e335
  36. Jun 10, 2012
  37. May 02, 2012
  38. Mar 23, 2012
    • Tom Lane's avatar
      Code review for protransform patches. · 0339047b
      Tom Lane authored
      Fix loss of previous expression-simplification work when a transform
      function fires: we must not simply revert to untransformed input tree.
      Instead build a dummy FuncExpr node to pass to the transform function.
      This has the additional advantage of providing a simpler, more uniform
      API for transform functions.
      
      Move documentation to a somewhat less buried spot, relocate some
      poorly-placed code, be more wary of null constants and invalid typmod
      values, add an opr_sanity check on protransform function signatures,
      and some other minor cosmetic adjustments.
      
      Note: although this patch touches pg_proc.h, no need for catversion
      bump, because the changes are cosmetic and don't actually change the
      intended catalog contents.
      0339047b
  39. Feb 24, 2012
Loading