topleft
topright

Topics

ServoyCamp Login



New Users

                 

Online Users

No users online!
Easy (semi) automatic update of your PostgreSQL database sequences
Tips
Written by Robert Ivens | Saturday, 18 June 2011 20:56   

 

When using dbidenty as sequence type in Servoy for your PostgreSQL table you are in fact using a sequence object inside the PostgreSQL database.

Servoy queries this sequence object when it creates a new record for you and stores it in the PK column.

Now sometimes this sequence might be out of sync because you imported data (with PK values) that have higher values than the sequence or perhaps you want to reset the sequence after a big purge of your data.

PostgreSQL has a simple command to update the sequence using SQL:

 

SELECT setval('sequenceName', value); 

 

So you could use SQL to get the correct value for your  sequence:

 

SELECT setval('sequenceName', (SELECT MAX(pkColumn) FROM tableName)+1);

 

Of course if you need to update ALL your sequences in your database this will be a daunting task, especially when you still modeling your tables.

Sure it's easy to fetch all sequence names but then you still need to parse it all into usable SQL that you can fire off at your database.

Turns out that this is an old problem. Michael Fuhr, a PostgreSQL community member, already solved this in 2004 in a post to the pgsql-general mailing list.

 

Please login or register to see the full article
 

Comments  

 
0 #1 Adelo Herrero Pérez 2011-06-19 08:23
Good tip:

Us we could also write a stored procedure that runs each result of "select" without write file in disk, and can be called with a single command from psql, pgadmin or Servoy.

Best regards.