T
T
Taras Labiak2017-02-05 16:25:30
PostgreSQL
Taras Labiak, 2017-02-05 16:25:30

In which number format to work with Bitcoin in PostgreSQL?

I'm going to store bitcoin payments in NUMERIC(10, 5), i.e. with an accuracy of 0.00001 ("bitcent" ~ USD cent), the maximum amount is 100,000 bitcoins. I think this is enough, considering that the minimum accrual can be 1% of 0.02, and the minimum withdrawal amount from the system is 0.01.
I just read that operations with NUMERIC are much slower than with INTEGER (32 bits) and thought about how to store in bitcents in INTEGER. If this is indeed the case, then I will have a significant performance gain, given that all business logic is implemented in the form of views and procedures (functions), for example, the user's balance is calculated in the view based on all user operations (transactions).
But there are also disadvantages, related to the fact that you need to convert to bitcoins, i.e. multiply by 100,000 and various errors may occur (due to the negligence of the frontend). In general, how justified is it to use INTEGER, given that all operations are carried out in PostgreSQL, and the task of the Node.js backend is only to call the corresponding PostgreSQL function.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
alex maslakoff, 2017-02-05
@teke_teke

I think that it is justified in integer since there will be no rounding error. and js itself will work better with integer for the same reason.

P
Puma Thailand, 2017-02-05
@opium

And how many trillions of operations per minute will you do if you think about it?

R
rPman, 2017-02-27
@rPman

It all depends on where you will work with the database, i.e. in what language and what libraries will you use.
Universal case - do not trust non-integer numbers, at some point you can get very unpleasant rounding!
Use int64 integers (and independently divide by 10^8 when outputting and consider if you will carry out multiplications by the same integer values, for example, when multiplying by the cost in another currency, which will be stored in the same way)
If you use php, use the string representation of the number and BCMath methods - bcadd, bcdiv, bcdiv,..) thus even on 32-bit systems (you may come across such virtual machines) you will not get the conversion of a number to a float

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question