title: Latest entry for append-only log in postgresql
body: <p>I want to store users' settings in a postgresql database.
  I would like to keep full history of their settings, and also be able to query the latest settings for a given user.</p>
  
  <p>I have tried storing settings in a table like this:</p>
  
  <pre><code>CREATE TABLE customer (
      customer_id INTEGER PRIMARY KEY,
      name VARCHAR NOT NULL  
  );
  
  CREATE TABLE customer_settings (
      customer_id INTEGER REFERENCES customer NOT NULL,
      sequence INTEGER NOT NULL, -- start at 1 and increase, set by the application
      settings JSONB NOT NULL,
      PRIMARY KEY(customer_id, sequence)
  );
  </code></pre>
  
  <p>So customer_settings is an append-only log, per customer.</p>
  
  <p>Then to query latest settings I use a long query that will do a subquery to SELECT the max <code>sequence</code> for the given <code>customer_id</code>, then will select the settings for that id.</p>
  
  <p>This is awkward! I wonder if there is a better way? May I use a view or a trigger to make a second table <code>latest_customer_settings</code>??</p>