Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id DEB19B5DC0E for ; Wed, 25 May 2011 19:29:52 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 76608-02 for ; Wed, 25 May 2011 22:29:45 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-fx0-f46.google.com (mail-fx0-f46.google.com [209.85.161.46]) by mail.postgresql.org (Postfix) with ESMTP id 5E94DB5DC0A for ; Wed, 25 May 2011 19:29:44 -0300 (ADT) Received: by fxm17 with SMTP id 17so203640fxm.19 for ; Wed, 25 May 2011 15:29:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=G0GhqL5fV7QYuwOaAK0G7B/GpiVgJMnmTFFaTOhE0/M=; b=RWhq2kkaJHwTxcxyDfGwEzH4eZ8QBlmwlzaJtPimapvNysH9ByZmk5eCuqI4JU2e3D x7lsF5gWPAS1yqQy3a3Cj9wKmCN+EAbEYS5UuGnxEbmFucn+noS1IeSkEXUX1rHGr23Z fhz8C0z7/ytrvh/90CTSJTwCF7+H9FlQgxZZY= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=WzLw6u+XRgXGvPNRTaNZalgJKMvewiug4pTnV+WZwC8oYLHIN91zR8jtNORJ+NzIHi DD1sj0VJvwSeZdjZD6pi+3Me3P7/2imKRPLe98pkDQAxeR64hAkwM6m4tROsgmGOohLX VyFlQVVN+YRQvdvYcYy0dsZNcARhsAWNUQ/78= MIME-Version: 1.0 Received: by 10.204.82.143 with SMTP id b15mr49258bkl.118.1306362206349; Wed, 25 May 2011 15:23:26 -0700 (PDT) Received: by 10.204.55.79 with HTTP; Wed, 25 May 2011 15:23:26 -0700 (PDT) In-Reply-To: <87ipt8tz5h.fsf@kolob.subpolar.dyndns.org> References: <87ipt8tz5h.fsf@kolob.subpolar.dyndns.org> Date: Wed, 25 May 2011 17:23:26 -0500 Message-ID: Subject: Re: enum data type vs table From: Peter Koczan To: Seb Cc: pgsql-sql@postgresql.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.898 tagged_above=-5 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001, RFC_ABUSE_POST=0.001 X-Spam-Level: X-Archive-Number: 201105/162 X-Sequence-Number: 35446 On Tue, May 17, 2011 at 11:23 PM, Seb wrote: > Are there any guidelines for deciding whether to 1) create an enum data > type or 2) create a table with the set of values and then have foreign > keys referencing this table? =A0Some fields in a database take a small > number of values, and I'm not sure which of these routes to take. =A0The > enum data type seems like a clean way to handle this without creating a > constellation of tables for all these values, but if one wants to add a > new label to the enum or make changes to it at some point, then the > tables using it have to be recreated, so it's quite rigid. =A0Have I got > this right? =A0Thanks. I think your choice depends on a few things: 1 - How do you want to interact with the tables? What I mean is, are you planning on querying, inserting, or updating data to those tables via text or will you need to join to your reference table? If you don't want to join, you'll either need to use enum types, use views (which can be a pain if you want to update a view), or duplicate/reference the text directly (which is slow and a bad idea for several reasons). 2 - How much can you tolerate downtime or a busy database? Changing types is a single transaction and requires an exclusive lock. On small tables this is negligible, but on big tables it can require downtime. 3 - How often do you really expect changes to the enum type? If adding a new value to an enum type is truly a rare event, it's . If it's frequent or regular, you should probably have a table. I've used both of these approaches and I've found enum types to be well worth any trouble to drop/recreate types. The changes I've made have been rare, and I've been able to schedule downtime pretty easily, so it made the most sense for me. Also, Postgres 9.1 allows adding values to enum types, so you could always use that when it is finally released. Hope this helps, Cheers, Peter