Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Rules by default <a href="http://www.postgresql.org/docs/current/static/sql-createrule.html" rel="noreferrer">add things to the current action</a>:</p> <blockquote> <p>Roughly speaking, a rule causes additional commands to be executed when a given command on a given table is executed.</p> </blockquote> <p>But an INSTEAD rule allows you to replace the action:</p> <blockquote> <p>Alternatively, an INSTEAD rule can replace a given command by another, or cause a command not to be executed at all.</p> </blockquote> <p>So, I think you want <a href="http://www.postgresql.org/docs/current/static/rules-update.html" rel="noreferrer">to specify INSTEAD</a>:</p> <pre><code>CREATE OR REPLACE RULE ignore_duplicate_inserts AS ON INSERT TO mytable WHERE (EXISTS ( SELECT mytable.id FROM mytable WHERE mytable.id = new.id)) DO INSTEAD NOTHING; </code></pre> <p>Without the INSTEAD, your rule is essentially saying "do the INSERT and then do nothing" when you want to say "instead of the INSERT, do nothing" and, AFAIK, the <code>DO INSTEAD NOTHING</code> will do that.</p> <p>I'm not an expert on PostgreSQL rules but I think adding the "INSTEAD" should work.</p> <p><strong>UPDATE</strong>: Thanks to araqnid <a href="http://www.postgresql.org/docs/9.0/interactive/sql-copy.html#AEN58860" rel="noreferrer">we know that</a>:</p> <blockquote> <p>COPY FROM will invoke any triggers and check constraints on the destination table. However, it will not invoke rules</p> </blockquote> <p>So a rule isn't going to work in this situation. However, triggers are fired during COPY FROM so you could write a BEFORE INSERT <a href="http://www.postgresql.org/docs/9.0/interactive/trigger-definition.html" rel="noreferrer">trigger that would return NULL</a> when it detected duplicate rows:</p> <blockquote> <p>It can return NULL to skip the operation for the current row. This instructs the executor to not perform the row-level operation that invoked the trigger (the insertion or modification of a particular table row).</p> </blockquote> <p>That said, I think you'd be better off with araqnid's "load it all into a temporary table, clean it up, and copy it to the final destination" would be a more sensible solution for a bulk loading operation like you have.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload