0

It appears that as soon as the INSERT clause is run, my sequence values increment, and are not rolled back to the previous number. Is there any way to avoid incrementing the sequence number?

I am trying to populate a table from a CSV, inserting rows where they haven't yet been added and doing nothing except returning the ID if they have already been added (there may well be a better way to do this):

cur.execute("INSERT INTO development_status (dst_name) VALUES (%s) ON CONFLICT (dst_name) DO UPDATE SET dst_name = %s RETURNING dst_id;", (status,status,))
        id_of_status = cur.fetchone()[0]
 if len(development_site) > 0:
        cur.execute("INSERT INTO development_site (ds_name,ds_developer,ds_zone,ds_status) VALUES (%s, %s, %s, %s) ON CONFLICT (ds_name) DO UPDATE SET ds_name = %s RETURNING ds_id;", (development_site,id_of_developer,zone,id_of_status,development_site,))
        id_of_site = cur.fetchone()[0]
hvrauhal
  • 432
  • 3
  • 13
Stev_k
  • 2,118
  • 3
  • 22
  • 36
  • Yes: create a temp table , insert the csv into thuis temp, and do a `insert into target(...) select ... from temp where not exists (...);` – wildplasser Mar 02 '16 at 13:22
  • This seems to be expected behaviour: http://www.postgresql.org/message-id/CAM3SWZQMfR6Zfe3A0Nr4ddko8xZrijAuQQ=EcGjGeJSs2piAXA@mail.gmail.com – Stev_k Mar 02 '16 at 17:43
  • Yes of course it is. A gapless sequence would cause a locking bottleneck / nightmare. – wildplasser Mar 02 '16 at 17:52

0 Answers0