For any real production database hash indexes should be used with extreme caution if used at all.
For any real production database hash indexes should be used with extreme caution if used at all.
> PostgreSQL supports Hash Index from a long time, but they are not much used in production mainly because they are not durable.
And there's a good reason for bringing them up anyway in the second sentence:
> Now, with the next version of PostgreSQL, they will be durable.
# CREATE INDEX ON t USING hash (id);
WARNING: hash indexes are not WAL-logged and their use is discouraged
CREATE INDEXI think I'm missing the important point.
In the case of a primary key, a row is inserted or deleted but the power goes out before the hash index is updated. When the power comes back, the hash index still points to rows were deleted and is missing rows that were inserted.
PostgreSQL 10 which will be released this autumn will include durable hash index, do not use hash indexes until then unless you really know what you are doing.
Hash tables are good, for small (relative to available space) static sets that don't change. Dynamic production environments will introduce an escalating probablity for performance degradation, and this can detrimental for critical systems.
For something like this, the cost of reindexing is trivial, and the full data is usually in a .sql file somewhere, so even if there's data loss it takes all of about 10 seconds to restore it.
Why would you do this instead of hardcoding this in code, storing it in a local hashtable, or putting it in Redis/Memcached? Bunch of reasons. These tables can be shared across all clients of the database, so if you have multiple apps (in different languages!) that require common validation, they can all make a SQL query instead of recoding the logic. If you don't otherwise need Redis/Memcached, you can avoid adding that dependency to your stack. You can use foreign key constraints to ensure that the values in real data tables match the valid values in the validation table. You can throw an admin interface on top of the tables and let customers edit the set of available options, rather than forcing all changes to go through the developers.