129 points · by narfz · 12y ago · 67 comments
[1]: http://www.postgresql.org/docs/9.3/static/sql-createtype.htm...
Want to store an order id create a OrderId domain. Want to store weight create a gram domain. Want to store quantity create a quantity domain.
That way it becomes much more easy to spot logical errors and maintain data consistency.
Also one can use it to create something like an auto ORM.
[A domain Is like a user defined child type of the system types that can have some extra restrictions.]
And integer is a hole number. Nothing else can be inferred from knowing that something is a integer.
"If you have to ask, you can't afford it."
Prosgres == Commercial & Open Source [1] TRUE
http://www.postgresql.org/docs/9.3/static/datatype-geometric...
Currently I'm working with numeric range types to model order quantities and stock quantities, and forecasting which order is going to consume what stock by joining on rows with overlapping ranges, then taking the intersections of those overlaps. Again, Postgres supplies functions and operators for range overlaps, intersections, etc.
In the absence of those datatypes, there'd be a lot more work required to achieve either of these.
(https://github.com/RhodiumToad/ip4r-historical/blob/master/R...)
I really couldn't say how much time and money he saved me, but it was a lot.
[1] http://pgfoundry.org/frs/download.php/3383/ip4r-extension-2....
I suspect that for a lot of developers, the only database features they use are the ones their ORM of choice supports. The popular ORMs out there tend to be database-agnostic, which means they'll only natively support the lowest-common-denominator database features.
Also, I tried the uuid extension once... It is not well supported, had to make a small change to a c header file to get it to compile on ubuntu, for dev on os x I think I gave up.
I've used UUID-as-pkey and had it work well.
UUID has no guaranteed properties you can rely on in high volumes.
The only desirable property it has is that it's an easy way out in tutorials.
The better solution is simply longer to explain (have a unique name for each server, an incremental generation number every time the server is restarted, and an incremental number on top of that for the runtime of a given generation - way Erlang does it).
"only after generating 1 billion UUIDs every second for the next 100 years, the probability of creating just one duplicate would be about 50%."
http://en.wikipedia.org/wiki/Universally_unique_identifier#R...
Now, if you want to mark your product SKU with an UUID you have an outstanding chance it'll be unique amongst other UUID SKUs in the scope of every shop selling your SKUs.
But people have been captured by the idea UUID is truly "universally unique", so you can use it for everything, at any volume.
Not at all. Let's say that we're implementing a distributed Actor system (or a similar message passing architecture) at the scale of Google. We'll be using UUID to tag every message to guarantee its identity and various messaging properties (like deliver just once etc.), because UUID is unique. While actor systems can reach millions of messages per server per second, here we'll use a humble 100k messages per second.
- They have over 2 million servers (2,000,000)
- Each of which generates 100k messages per second.
You reach a point of 50% collision rate (for every new UUID) in 3 years:
log2 (2,000,000 * 100,000 * 3600 * 24 * 365 * 3)= ~64
2^64 is roughly the number of 128-bit UUIDs you need to reach that collision rate due to the Birthday paradox.Now you might say, I have a lot of variables pegged to worst case scenario. Sure I have. But this is about 50 damn percent chance of collision on every new UUID generated at that point.
Things will become bad long before 50% collision chance if you use UUID.
Timestamp with Timezone
There’s seldom a case you shouldn’t be using these.
For recording most event times that's true but:
Birthdates (which can include times) shouldn't use them.
Calendaring should think very carefully too, should the 9am meeting move to 10am due to DST? or should it "stay put" but potentially move for other timezones?
I don't understand why you don't want to use timezone with birthdates(with time). Can you explain this, please?
Just for instance, older Iraqis (if I remember correctly) tend to be part of a culture where their individual birthdates were never recorded, and each person was considered 1 year older on some September date (maybe even talking the Islamic calendar there too). Should you store that date in your database, pretending that it means the same thing as an American who knows they were born at 7:08pm EST on July 17th, 1977?
And that's just one example of many.
I think I learned about this one because intelligence officials were freaking out when they eventually noticed all these people sharing a "birthdate", and it made the news.
Data about human beings is highly un-normalizable, as evidenced by everyone trying to stuff anthroponyms into the American "first name, middle name, last name" paradigm.
The postgresql manpage says it loud: don't fucking use timestamp with timezone. It is bounded to political decision that makes it unusable compared to an UTC TS. Plus good practices says: store UTC present in local
Does any one Read The Fantastics Manual these days?
That's very different than timestamp with time zone, which you should be using unless you have a very good reason to not.
I've used something else from my experiments in PostgreSQL - the TXID - this way I was able to track down changes in the database (by keeping previous TXID and some other various bits, and then by polling again (or making the server call me)) - polling again and only instructing to get the rows that have changed since my last TXID.
http://slashdot.org/story/06/11/09/1534204/slashdot-posting-...
Slashdot got over 16,777,216 comments overflowing a MySQL mediumint they were using for an index.
http://www.postgresql.org/docs/9.3/static/routine-vacuuming....
Fortunately the PostgreSQL has autovacuum so you probably don't need to worry about this.
So if your only dealing with US currency, why not love the money type?
http://www.postgresql.org/message-id/b42b73150805270629h309f...
I use it a lot, and yes, it is missing some casts but you can cast to decimal and go from there:
select '42.00'::money::decimal;
I've also used stripe, and maybe it's a javascript thing, but they represent all their money in cents. Personally, I'd rather use the money type, it is easy for me to reason about.
For a "datatype" as commonly needed as "money", it has always struck me as strange that a more standardized and widely agreed upon approach to dealing with them, have not emerged.
As the currency of money is not known it is pretty obvious that one needs to store that in another column.