赞
踩
DO $$ DECLARE v_state TEXT; v_msg TEXT; v_detail TEXT; v_hint TEXT; v_context TEXT; lv_table_logical_name varchar:='t_tmp'; lv_table_shard_seq_begin bigint:=0; lv_table_shard_seq_end bigint:=4095; lv_table_shard_name_rec record; lv_begin_time varchar:='2023-08-08 10:30:00'; lv_end_time varchar:='2023-08-08 11:00:00'; lv_sql text; lv_result_1 varchar; lv_result_2 bigint; BEGIN for lv_table_shard_name_rec in ( select t0.* from ( select lv_table_logical_name||'_'||id as table_shard_name, id from generate_series(lv_table_shard_seq_begin, lv_table_shard_seq_end) as id order by id ) t0, ( select * from pg_tables pt where 1=1 and pt.schemaname not in ( 'information_schema', 'pg_catalog', 'pg_temp_1', 'pg_toast', 'pg_toast_temp_1' ) ) t1 where 1=1 and t0.table_shard_name = t1.tablename ) loop lv_sql= ' select '''||lv_table_shard_name_rec.table_shard_name||''' as tablename,count(1) from '||lv_table_shard_name_rec.table_shard_name||' where create_time BETWEEN '''||lv_begin_time||''' and '''||lv_end_time||''''; EXECUTE lv_sql into lv_result_1,lv_result_2; if lv_result_2 > 0 then raise notice 'lv_sql=%',lv_sql; raise notice 'tablename= % ,count= %', lv_result_1,lv_result_2; end if; end loop; EXCEPTION when OTHERS then get stacked diagnostics v_state = returned_sqlstate, v_msg = message_text, v_detail = pg_exception_detail, v_hint = pg_exception_hint, v_context = pg_exception_context; raise notice E'exception: state : % message: % detail : % hint : % context: %', v_state, v_msg, v_detail, v_hint, v_context; raise notice E'exception:SQLSTATE: % SQLERRM: %', SQLSTATE, SQLERRM; END; $$ LANGUAGE plpgsql;
DO $$ DECLARE v_state TEXT; v_msg TEXT; v_detail TEXT; v_hint TEXT; v_context TEXT; lv_table_logical_name varchar:='t_tmp'; lv_table_shard_seq_begin bigint:=0; lv_table_shard_seq_end bigint:=4095; lv_table_shard_name_rec record; lv_list_rec record; lv_begin_time varchar:='2023-08-08 10:30:00'; lv_end_time varchar:='2023-08-08 11:00:00'; lv_sql text; BEGIN for lv_table_shard_name_rec in ( select t0.* from ( select lv_table_logical_name||'_'||id as table_shard_name, id from generate_series(lv_table_shard_seq_begin, lv_table_shard_seq_end) as id order by id ) t0, ( select * from pg_tables pt where 1=1 and pt.schemaname not in ( 'information_schema', 'pg_catalog', 'pg_temp_1', 'pg_toast', 'pg_toast_temp_1' ) ) t1 where 1=1 and t0.table_shard_name = t1.tablename ) loop lv_sql= ' select '''||lv_table_shard_name_rec.table_shard_name||''' as tablename,id,name from '||lv_table_shard_name_rec.table_shard_name||' where create_time BETWEEN '''||lv_begin_time||''' and '''||lv_end_time||''''; for lv_list_rec in EXECUTE lv_sql loop raise notice 'lv_sql=%',lv_sql; raise notice 'tablename= % ,id= %, name= %', lv_list_rec.tablename,lv_list_rec.id,lv_list_rec.name; end loop; end loop; EXCEPTION when OTHERS then get stacked diagnostics v_state = returned_sqlstate, v_msg = message_text, v_detail = pg_exception_detail, v_hint = pg_exception_hint, v_context = pg_exception_context; raise notice E'exception: state : % message: % detail : % hint : % context: %', v_state, v_msg, v_detail, v_hint, v_context; raise notice E'exception:SQLSTATE: % SQLERRM: %', SQLSTATE, SQLERRM; END; $$ LANGUAGE plpgsql;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。