On Jun 11, 2004, at 1:02 PM, Christopher Kings-Lynne wrote:

3. Or even create a pg_get_sequence() function:
SELECT SETVAL(pg_get_sequence(schema.table, col), 17);

Actually, this is the best solution :)

John Hansen and I worked this up. It works, though it's not schema-aware, afaict.


create or replace function last_val(
    text     -- tablename
    , text   -- colname
    ) returns bigint
    language 'sql' as '
    select currval(
        (select
            split_part(adsrc,\'\'\'\',2) as seq
        from pg_class
        join pg_attribute on (pg_class.oid = pg_attribute.attrelid)
        join pg_attrdef
            on (pg_attrdef.adnum = pg_attribute.attnum
            and pg_attrdef.adrelid = pg_attribute.attrelid)
        where pg_class.relname = $1
            and pg_attribute.attname = $2)
    );
    ';

Might be a starting point.

Michael Glaesemann
grzm myrealbox com


---------------------------(end of broadcast)--------------------------- TIP 4: Don't 'kill -9' the postmaster

Reply via email to