title: Postgres UPSERT - don't update if all data the same?
body: <p>I am using Postgres 9.6. I want to update a record if <em>anything</em> about it is different, but keep it the same if not. </p>
  
  <p>This is my SQL, slightly simplified because my table actually has many more fields (run via psycopg2):</p>
  
  <pre><code>query = "INSERT INTO cap(scheme, year, code) "
  query += "VALUES(%s, %s, %s) "
  query += "ON CONFLICT DO NOTHING"
  cursor.execute(query, ('abc', 2015, 'twu'))
  </code></pre>
  
  <p>However, if I run it twice with exactly the same values, I get two records in the database. </p>
  
  <p>How can I amend this query so that providing the exact same values does not create a new record?</p>
  
  <p>I've tried to read the <a href="https://www.postgresql.org/docs/9.5/static/sql-insert.html#SQL-ON-CONFLICT" rel="nofollow noreferrer">ON CONFLICT UPDATE documentation</a> but don't know how to adapt it for my case. In particular, it looks to me like <code>DO NOTHING</code> should not insert a row (the docs say: <em>"ON CONFLICT DO NOTHING simply avoids inserting a row as its alternative action"</em>), but it does insert a new row.  </p>