I posted a tip on ServoyCamp about how to easily update all your database sequences in your PostgreSQL database in a (semi) automated fashion .
http://www.servoycamp.com/topics/tips/1 ... ences.html
Enjoy!
select update_seq();
databaseManager.getDataSetByQuery("serverName","select update_seq()");
-- Function: update_seq()
-- DROP FUNCTION update_seq();
CREATE OR REPLACE FUNCTION update_seq()
RETURNS void AS
$BODY$-- Update sequences in database.
DECLARE
sql_result information_schema.tables%rowtype;
query text;
BEGIN
query:="SELECT 'SELECT setval('''||_schema||'.'||_seq||''', (SELECT MAX('||_column||') FROM '||_schema||'.'||_table||')+1); ' AS updatestring
FROM (SELECT n.nspname AS _schema,
c.relname AS _table,
a.attname AS _column,
SUBSTRING(d.adsrc FROM E'nextval\\(''([^'')]+)''') AS _seq
FROM pg_attrdef AS d
JOIN pg_attribute AS a ON a.attrelid = d.adrelid AND a.attnum = d.adnum
JOIN pg_class AS c ON c.oid = d.adrelid
JOIN pg_namespace AS n ON n.oid = c.relnamespace
WHERE adsrc LIKE 'nextval(''%') t;";
FOR sql_result in EXECUTE(query) LOOP
EXECUTE(sql_result);
END LOOP;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION update_seq() OWNER TO postgres;
COMMENT ON FUNCTION update_seq() IS 'Update sequences in database.';
SELECT COALESCE(MAX(id), 0)+1 AS maxall FROM (
SELECT MAX(tableOneColumn) AS id FROM tableOne
UNION
SELECT MAX(tableTwoColumn) AS id FROM tableTwo
) AS s;
SELECT setval('sequencename',SELECT COALESCE(MAX(id), 0)+1 AS maxall FROM (SELECT MAX(tableOneColumn) AS id FROM tableOne UNION SELECT MAX(tableTwoColumn) AS id FROM tableTwo) AS s);
var _sServer = "myConnectionName",
_sQuery = "SELECT n.nspname as _schema, \
c.relname as _table, \
a.attname as _column, \
SUBSTRING(d.adsrc FROM E'nextval\\\\(''([^'')]+)''') AS _seq \
FROM pg_attrdef AS d \
JOIN pg_attribute AS a ON a.attrelid = d.adrelid AND a.attnum = d.adnum \
JOIN pg_class AS c ON c.oid = d.adrelid \
JOIN pg_namespace AS n ON n.oid = c.relnamespace \
WHERE adsrc LIKE 'nextval(''%' \
ORDER BY _seq",
_ds = databaseManager.getDataSetByQuery(_sServer, _sQuery, null, -1),
_aGetMax = new Array(),
_sUpdateSQL,
_oException;
if (_ds.getException()) {
application.output("Error:" + _ds.getException().getMessage(), LOGGINGLEVEL.ERROR);
} else {
for (var i = 1; i <= _ds.getMaxRowIndex(); i++) {
_aGetMax.push("SELECT MAX(" + _ds.getValue(i, 3) + ") AS maxid FROM " + _ds.getValue(i, 1) + "." + _ds.getValue(i, 2));
if (i == _ds.getMaxRowIndex() || _ds.getValue(i + 1, 4) != _ds.getValue(i, 4)) {
_sUpdateSQL = "SELECT setval('" + _ds.getValue(i, 4) + "', (SELECT COALESCE(MAX(maxid), 0)+1 AS maxall FROM (" + _aGetMax.join(" UNION ") + ") AS t ));";
_oException = databaseManager.getDataSetByQuery(_sServer, _sUpdateSQL, null, 1).getException();
if (_oException) {
application.output("Error:" + _oException.getMessage(), LOGGINGLEVEL.ERROR);
application.output(_sUpdateSQL, LOGGINGLEVEL.ERROR);
}
_aGetMax = new Array();
}
}
}
Adelo Herrero wrote:We can automate it with an stored procedure in plpgsql
Users browsing this forum: No registered users and 1 guest