Sql postgres:查找包含当前最大值的所有整数列

Sql postgres:查找包含当前最大值的所有整数列,sql,postgresql,information-schema,Sql,Postgresql,Information Schema,如何从Postgres实例中所有数据库的所有表中查找当前最大值为整数类型的所有主键列 我想从所有表中找到所有int类型的主键列,这些表的最大值2147483647即将溢出 CREATE OR REPLACE FUNCTION intpkmax() RETURNS TABLE(schema_name name, table_name name, column_name name, max_value integer) LANGUAGE plpgsql STABLE AS $$BEGIN

如何从Postgres实例中所有数据库的所有表中查找当前最大值为整数类型的所有主键列

我想从所有表中找到所有int类型的主键列,这些表的最大值2147483647即将溢出

CREATE OR REPLACE FUNCTION intpkmax() RETURNS
   TABLE(schema_name name, table_name name, column_name name, max_value integer)
   LANGUAGE plpgsql STABLE AS
$$BEGIN
   /* loop through tables with a simgle integer column as primary key */
   FOR schema_name, table_name, column_name IN
      SELECT sch.nspname, tab.relname, col.attname
         FROM pg_class tab
            JOIN pg_constraint con ON con.conrelid = tab.oid
            JOIN pg_attribute col ON col.attrelid = tab.oid
            JOIN pg_namespace sch ON sch.oid = tab.relnamespace
         WHERE con.contype = 'p'
            AND array_length(con.conkey, 1) = 1
            AND col.atttypid = 'integer'::regtype
            AND NOT col.attisdropped
   LOOP
      /* get the maximum value of the primary key column */
      EXECUTE 'SELECT max(' || quote_ident(column_name) ||
              ') FROM ' || quote_ident(schema_name) ||
              '.' || quote_ident(table_name) || ''
         INTO max_value;
      /* return the next result */
      RETURN NEXT;
   END LOOP;
END;$$;
然后你就可以用

SELECT * FROM intpkmax();