title: Postgres constraint ensuring one column of many is present?
body: <p>What are good ways to add a constraint to PostgreSQL to check that exactly one column (from a set of columns) contains a non-null value?</p>
  
  <p><em>Update</em>: It is likely that I want to use a <code>check</code> expression as detailed in <a href="http://www.postgresql.org/docs/9.2/static/sql-altertable.html">Create Table</a> and <a href="http://www.postgresql.org/docs/9.2/static/sql-createtable.html">Alter Table</a>.</p>
  
  <p><em>Update</em>: I'm looking through the available <a href="http://www.postgresql.org/docs/9.2/static/functions.html">functions</a>.</p>
  
  <p><em>Update</em>: Just for background, here is the Rails validation logic I'm currently using:</p>
  
  <pre><code>validate :multi_column_validation
  def multi_column_validation
    n = 0
    n += 1 if column_1
    n += 1 if column_2
    n += 1 if column_3
    unless 1 == n
      errors.add(:base, "Exactly one column from " +
        "column_1, column_2, column_3 must be present")
    end
  end
  </code></pre>
  
  <p>To be clear, I'm looking for PSQL, not Ruby, here. I just wanted to show the logic I'm using since it is more compact than enumerating all "truth table" possibilities.</p>