We have a monitor on our databases to check for ids approaching max-int or max-bigint. We just moved from MySQL, and I'm struggling to get a similar check working on PostgreSQL. I'm hoping someone can help.
Here's the query in MySQL
SELECT table_name, auto_increment FROM information_schema.tables WHERE table_schema = DATABASE();
I'm trying to get the same results from PostgreSQL. We found a way to do this with a bunch of calls to the database, checking each table individually.
I'd like to make just 1 call to the database. Here's what I have so far:
CREATE OR REPLACE FUNCTION getAllSeqId() RETURNS SETOF record AS$body$DECLARE sequence_name varchar(255);BEGIN FOR sequence_name in SELECT relname FROM pg_class WHERE (relkind = 'S') LOOP RETURN QUERY EXECUTE 'SELECT last_value FROM ' || sequence_name; END LOOP; RETURN;END$body$LANGUAGE 'plpgsql';SELECT last_value from getAllSeqId() as(last_value bigint);
However, I need to somehow add the sequence_name to each record so that I get output in records of [table_name, last_value] or [sequence_name, last_value].
So I'd like to call my function something like this:
SELECT sequence_name, last_value from getAllSeqId() as(sequence_name varchar(255), last_value bigint);
How can I do this?
EDIT: In ruby, this creates the output we're looking for. As you can see, we're doing 1 call to get all the indexes, then 1 call per index to get the last value. Gotta be a better way.
def perform find_auto_inc_tables.each do |auto_inc_table| check_limit(auto_inc_table, find_curr_auto_inc_id(auto_inc_table)) end end def find_curr_auto_inc_id(table_name) ActiveRecord::Base.connection.execute("SELECT last_value FROM #{table_name}").first["last_value"].to_iend def find_auto_inc_tables ActiveRecord::Base.connection.execute("SELECT c.relname "+"FROM pg_class c "+"WHERE c.relkind = 'S'").map { |i| i["relname"] }end