title: How to make a foreign key with a constraint on the referenced table in PostgreSQL
body: <p>Suppose I have the following tables</p>
  
  <pre><code>CREATE TABLE plugins (
  id int primary key,
  type text);
  
  insert into plugins values (1,'matrix');
  insert into plugins values (2,'matrix');
  insert into plugins values (3,'function');
  insert into plugins values (4,'function');
  
  CREATE TABLE matrix_params (
  id int primary key,
  pluginid int references plugins (id)
  );
  </code></pre>
  
  <p>This all works as expected but I would like to add an additional constraint that a matrix_param can only refer to the pluginid that has type 'matrix'. So</p>
  
  <pre><code>insert into matrix_params values (1,1);
  </code></pre>
  
  <p>Should succeed but</p>
  
  <pre><code>insert into matrix_params values (2,3);
  </code></pre>
  
  <p>Should fail.</p>
  
  <p>A simple constraint for matrix_params does not work as it has no way of knowing what the corresponding type is in the plugins table.</p>