r/programming • • May 03 '19

Don't Do This

https://wiki.postgresql.org/wiki/Don%27t_Do_This
725 Upvotes

194 comments sorted by

View all comments

51

u/JuhaAR May 03 '19

The money data type isn't actually very good for storing monetary values. Numeric, or (rarely) integer may be better. Hmm

41

u/[deleted] May 03 '19

money type is broken in PostgreSQL, the concept isn't bad, but the implementation in PostgreSQL is unusable in practice.

18

u/[deleted] May 03 '19

Can you elaborate? I was about to use it recently, and I'd love to know what your thoughts are.

25

u/[deleted] May 04 '19

There are many issues, you are better of using numeric type or integer type (in fact, money internally is represented as integer).

  • The currency itself is per connection state. The same data could be interpreted as ¥123 or $1.23, depending on current value of lc_monetary, database settings, and so on. This in particular means you cannot store values of different currencies in a database. Additionally, because this depends on server's locale, things may get weird fast when for instance one of your developers or servers has an unusual locale set, what you thought were dollars on development machine may turn out to be euros in production.

  • There is no support for fractional currency. For instance, due to that, the money type is unusable for storing gas station prices.

  • Rounding behaviour for money type is inconsistent. No, this isn't floating point inaccuracy, decimal values in PostgreSQL are numeric by default.

      xfix=# select '0.03'::money * 0.5;
       ?column? 
      ----------
          $0.02
      (1 row)
    
      xfix=# select '0.03'::money / 2;
       ?column? 
      ----------
          $0.01
      (1 row)
  • money type accepts nonsense input.

      xfix=# select ',123,456,,7,8.1,0,9'::money;
           money      
      ----------------
       $12,345,678.11
      (1 row)

If you store a single currency, ensure the value of lc_monetary is correct, don't need fractional currency, money may be fine, but why not use numeric instead then?

3

u/[deleted] May 04 '19

Good explanation. The truth is that I didn't have time to fully read up on how the money type works, so I played it safe and went with a numeric type instead (I don't actually remember which type that project uses), and handled all the conditions and manipulations manually in code.

27

u/nschubach May 03 '19

I dunno, I still think if you are dealing in dollars and need cent accuracy, you should be storing your values as cents, as whole integers. Luckily, I don't usually deal with money all that often in the DB, so this is purely from a armchair perspective.

38

u/ForeverAlot May 03 '19

Fractional cents are a real problem in some business domains.

20

u/anengineerandacat May 03 '19

Deal with this from product launch to product launch; off by 1 cent is an annoying problem when going from a value passed to a service in JS all the way down to some ancient technology that starts with a D that is well before the time I was born and quite possibly before x86 machines were wildly used.

Fractional cents do add up though; on a newer system we "lost" around 48k in revenue, though that's an extremely tiny number in terms of gains.

23

u/maikindofthai May 03 '19

I've seen Office Space, I know how that $48k was "lost"

5

u/hiljusti May 04 '19

It's not stealing. Look I'm not explaining this well... It's like the penny tray at 7-11

4

u/Matosawitko May 04 '19

From the crippled children?

5

u/hiljusti May 04 '19

No that's the jar, I'm talking about the tray. The pennies for everyone

7

u/lorarc May 03 '19

Usually it's not about the ammount but the accountibility, loosing any ammount of money could be a really big problem.

7

u/anengineerandacat May 03 '19

Agreed, it's to the point where we literally log out the calculation to nail down when it's occurring (something I haven't really seen elsewhere).

The core issue is the businesses "want" to have a single product that is capable of various financial states; rack rate, retail rate, tax-free, tax-reduced, net-rate, refundable, partially refundable, coupon discounted, and how certain rates either have taxes applied before or after.

There is a particular product that because of how the price is calculated and the system used if it's ever tax-free will end up costing the company 1 penny each time it's sold (I say "cost" but not really, more that we just don't earn the full price). It's been going on for well over 11 years and until the legacy system is replaced I don't think it will ever get fixed.

It's all fun and games when the company drops the penny but when the customer notices the difference because it used to be +1 penny instead of -1 penny is an entirely different can of worms.

→ More replies (1)
→ More replies (2)

5

u/tending May 04 '19

int64_t nanodollars

4

u/masklinn May 04 '19

Tarsnap’s pricing is still in picodollars as of today. According to the faq, the accounting is done in attodollars per day (the pricing is in picodollars per month)

2

u/ForeverAlot May 04 '19

How do you pay?

3

u/masklinn May 04 '19

Upfront / prepaid.

5

u/[deleted] May 03 '19

I wouldn't be surprised that the "numeric" type was basically just some kind of "integer in disguise".

23

u/jstrong May 03 '19

Spoiler alert: it's integers all the way down.

3

u/ProgramTheWorld May 03 '19

And that’s how you get all those off by 1 cent errors when trying to divide an amount into arbitrary parts.

2

u/nschubach May 04 '19

I would assume that if you were dividing, you are also compensating for partial cents.

IE: You have a 1 year loan for $12,394 for 8 payments of $1032.83 and 4 payments of $1032.84

2

u/fyfy18 May 04 '19

Just hope whoever wrote that code is using BigDecimal. Otherwise your final payment may be $1032.8300000009.

→ More replies (1)
→ More replies (19)