title: Postgresql: unique value within the same high-level key(s) but different directly subkey
body: <p>I have a table like this:</p>
  
  <pre class="lang-sql prettyprint-override"><code>CREATE TABLE IF NOT EXISTS tt
  (key1     integer
  ,key2     integer
  ,data     varchar(8)
  
  ,CONSTRAINT cst_tt_key1_key2 UNIQUE (key1, key2)
  );
  
  CREATE INDEX idx_tt_data_trigram
      ON data
   USING gist(data gist_trgm_ops);
  </code></pre>
  
  <p>I want to have unique value of <code>data</code> under the same value of <code>key1</code>.</p>
  
  <p>In order words, the following records are possible.</p>
  
  <pre><code>1,  1,  "Postgres"
  2,  1,  "Postgres"
  </code></pre>
  
  <p>However, the following records are not permitted.</p>
  
  <pre><code>1,  1,  "Postgres"
  1,  2,  "Postgres"
  </code></pre>
  
  <p>Any idea how to do it using <code>CHECK</code> or <code>CONSTRAINT</code>?</p>