TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Representing Enums in PostgreSQL

112 pointsby wojcikstefanalmost 2 years ago

14 comments

cryptonectoralmost 2 years ago
TFA is all about how to make changes where you drop elements of an enum, and how hard that is. The obvious thing though is not covered: don&#x27;t do that! Instead you should:<p><pre><code> a. Add CHECK() constraints on columns of the relevant ENUM type checking that the value is one of the &quot;live&quot; values. b. RENAME &quot;dead&quot; ENUM values to indicate that they&#x27;re dead. c. UPDATE ... SET column_of_that_enum_type = ... WHERE column_of_that_enum_type IN (dead_values_here) ... </code></pre> (c) can be done slowly, say, with a LIMIT clause to keep load and disruptions down. Yeah, you might have &quot;broken&quot; data for a while, but you can make your queries report better values for columns of that ENUM type in the same way that the UPDATE would fix them.
评论 #36405919 未加载
评论 #36406725 未加载
评论 #36406172 未加载
评论 #36407321 未加载
rpcope1almost 2 years ago
After having suffered through the consequences of &quot;type&quot; enums on MySQL, and see some things go through a long life that used &quot;enums&quot; in the database (in multiple different databases, include Postgres), I&#x27;m not convinced that either of these are the right choice for representing enumerations. The string with check constraint seems dumb if for no other reason than if the table that uses it winds up having many rows, you&#x27;re basically burning up lots of extra space for arguably no reason, and if you ever have to alter the name of an enum (or something similar), that update is going to be really expensive.<p>I think the &quot;right&quot; choice for enums probably looks a little more like:<p><pre><code> CREATE TABLE myEnum( enumID SERIAL NOT NULL PRIMARY KEY, enumName TEXT UNIQUE NOT NULL, -- enum description or other metadata columns here. ); CREATE TABLE foo( ... associatedEnumID INTEGER NOT NULL REFERENCES myEnum(enumID), ... ); </code></pre> I think this has the benefit of being space efficient like the native typed enum, while being relatively flexible (easy to change names, add new enum values, add data about the enum itself, etc.)
评论 #36405362 未加载
评论 #36405777 未加载
评论 #36410091 未加载
评论 #36404662 未加载
评论 #36405543 未加载
jpgvmalmost 2 years ago
PostgreSQL enums feel like a bit of a hack in general. I end up using an &quot;enum table&quot; approach in many cases as joining against a very small table has negligible performance impact in all but the most performance sensitive databases and foreign key constraints are a well understood quantity.
评论 #36410109 未加载
评论 #36404775 未加载
akerstenalmost 2 years ago
Is there a reason ALTER TYPE name ADD VALUE new_enum_value wouldn&#x27;t Just Work for the first example for 99% of use cases? Seems like the only drawback highlighted for native enums is that you have to lock the whole table if you completely swap out the type of one of the columns, which... Yes, that&#x27;s true, but also very rare? I guess TFA mentions removing a value from an enum, but you shouldn&#x27;t do that imo - leave the data in place and ignore it at the app layer, like how you&#x27;d treat a deleted flag or similar. Or migrate it and leave the old value hanging around in the type.
评论 #36407661 未加载
jpalomakialmost 2 years ago
Native enums can be used in queries like strings, but with type checking:<p><pre><code> select * from cust where type = ’company’ -- ok select * from cust where type = ’c0mpany’ -- error </code></pre> As mentioned, they take less space. Important if you use these columns in index and have millions of rows.
评论 #36415864 未加载
评论 #36428147 未加载
Icathianalmost 2 years ago
Little known bug, but enums also choke dump&#x2F;restore if you use them as a hash partition key.<p>That said, they can be really nice for all the same reasons static types are nice. Good article!
评论 #36405096 未加载
评论 #36405282 未加载
fabian2kalmost 2 years ago
Native enums in Postgres just seem to have many more drawbacks than advantages. I never really used them, it didn&#x27;t seem worth it.<p>I usually use C# Enums translated by EF Core now, which works perfectly fine on the C# side. The only missing part would be to give the DB the information about the enum names, so it could show those to me instead of just the raw numbers. But I assume there is no way to do that. Adding entire tables just doesn&#x27;t seem worth it for this use case alone.
评论 #36405360 未加载
评论 #36405111 未加载
评论 #36404903 未加载
评论 #36412309 未加载
brycelarkinalmost 2 years ago
The article misses the most common way to represent enums: foreign keys…
评论 #36430622 未加载
rollulusalmost 2 years ago
When would one use this approach versus a domain type [1]? How does it differ?<p>[1]: <a href="https:&#x2F;&#x2F;www.postgresql.org&#x2F;docs&#x2F;current&#x2F;domains.html" rel="nofollow noreferrer">https:&#x2F;&#x2F;www.postgresql.org&#x2F;docs&#x2F;current&#x2F;domains.html</a>
评论 #36407600 未加载
somatalmost 2 years ago
I am probably missing something obvious(space&#x2F;time optimization for very large&#x2F;busy tables?) but isn&#x27;t this a perfect example of why foreign keys exist. In fact both case studies presented by the article(enums and fixed string check constraints) are clunky enough that I would say both are wrong and they should just go with a foreign key.
Canadaalmost 2 years ago
I agree with this, I don&#x27;t use enums they are always more trouble than they are worth.<p>They break FDW unless they are pre-created on the importing side. Super inconvenient.
评论 #36404871 未加载
评论 #36410426 未加载
cmcconomyalmost 2 years ago
I&#x27;m happy to have a FK to a reftable with int, shortname, description
jensenboxalmost 2 years ago
What is the difference in size on disk between the two options?
评论 #36407712 未加载
out_of_protocolalmost 2 years ago
smallint looks like a good alternative, with dictionary in the app or separate table. So far i&#x27;ve only seen storing dictionary in app source code approach
评论 #36404857 未加载
评论 #36407747 未加载