How I Learned to Stop Worrying and Love the Triggers

Some people are afraid of triggers. Reasons for this are not really understandable for me, but I guess it stems from the fact that these are usually application developers, and not database admins. Or they encountered some kind of problem with triggers, and now they tend to think that triggers are inherently evil. But they … Continue reading “How I Learned to Stop Worrying and Love the Triggers”

Getting top-N rows per group

Yesterday on irc someone asked: Hi, how do I get top 5 values from a column group by another column?? From further discussion, I learned that: total rows in table is 2 million. It’ll have unique words of less than 1 million.. (approx count) I didn’t have time yesterday, but decided to write a solution, … Continue reading “Getting top-N rows per group”

What index to create?

Some time ago I wrote a blogpost about why index might not be used. While this post seemed to be well received (top link from depesz.com on reddit), it doesn’t answer another question – what index to create for given situation. I’ll try to cover this question now. IMPORTANT UPDATE: As of PostgreSQL 10 hash … Continue reading “What index to create?”

Should you use HASH index?

Today, Mattias|farm on IRC asked how to create primary key using HASH index. After some talk, he said that in some books it said that for “=” (equality) hash indexes are better. So, I digged a bit deeper. IMPORTANT UPDATE: As of PostgreSQL 10 hash indexes are WAL logged. As such, main point against them … Continue reading “Should you use HASH index?”

CHAR(x) vs. VARCHAR(x) vs. VARCHAR vs. TEXT – UPDATED 2010-03-03

UPDATE Please read also about this change in Pg 9.1, and this change in Pg 9.2 posts, as they explain that since Pg 9.1 some of the limitations listed in this post are no longer there. END OF UPDATE Fight! But more seriously – people tend to use various data types, and there have been … Continue reading “CHAR(x) vs. VARCHAR(x) vs. VARCHAR vs. TEXT – UPDATED 2010-03-03”