我们正在使用postgres 9.2数据库。
设备ID列逻辑在我的组织中已更改。因此,我需要将所有旧设备ID值修改为包含设备ID列的表的新设备ID值。旧设备ID和新设备ID列映射在“old_new_deviceids”表中。
有20个表包含设备ID列。因此,我需要根据“old_new_deviceids”表修改所有这20个表中的设备ID。每个表都包含大约200万条记录。
我使用EXECUTE FORMAT创建了一个动态过程,它接受表名和列名作为输入参数,如下所示。
当我执行select pop_with_new_deviceid()时,它会一次更新20个表。它在5分钟内完成的一些环境以及在2小时25分钟内完成的一些环境。我在不同的环境中多次遇到过这个问题。但是所有环境的数据和配置设置都是相同的。在执行此脚本时,数据库中没有锁。
CREATE OR REPLACE FUNCTION pop_with_new_deviceid()
RETURNS void
AS $$
DECLARE
--tables text[] = ARRAY['tcconfig_endpointlist','medianode','calldetailrecord','calldetailrecord','statsciscotbgcallstreamsource','statsciscotbgcallchannelsaudio','statsciscotbgcallchannelsvideo','statsciscotbgperipheral','statsciscotbgperipheralhistory','statsciscotbgcall','statsciscotpcall','statsciscotpcallstreamsource','statsciscotpperipheral','statsciscotpperipheralhistory','statsciscotpcallstreamtype','statsciscophonecallstream','monthlyendpointnoshow','monthlyendpointutilization','mtg_src_nd_prtcpnts'];
--columns text[]= ARRAY['element','deviceid','deviceid','destdeviceid','deviceid','deviceid','deviceid','deviceid','deviceid','deviceid','deviceid','deviceid','deviceid','deviceid','deviceid','deviceid','deviceid','deviceid','source'];
--v_select varchar(5000);
-- rec record;
BEGIN
--This funciton updates the deviceid column for all tables which contains endpoint details using endpoint_deviceids_barediscovery table through pop_new_deviceid funciton
RAISE NOTICE 'Updation of deviceid column for dependent tables which contains endpoints related information has started';
PERFORM insert_log('INFO' ,'pop_with_new_deviceid' ,'Updation of deviceid column for dependent tables which contains endpoints related information has started');
PERFORM pop_new_deviceid_for_table(‘Table_Name1','deviceid');
PERFORM pop_new_deviceid_for_table(‘Table_Name2','deviceid');
PERFORM pop_new_deviceid_for_table(‘Table_Name3','deviceid');
PERFORM pop_new_deviceid_for_table(‘Table_Name4','deviceid');
PERFORM pop_new_deviceid_for_table(‘Table_Name5','deviceid');
PERFORM pop_new_deviceid_for_table(‘Table_Name6','deviceid');
PERFORM pop_new_deviceid_for_table(‘Table_Name7','deviceid');
PERFORM pop_new_deviceid_for_table(‘Table_Name8','deviceid');
PERFORM pop_new_deviceid_for_table(‘Table_Name9','deviceid');
PERFORM pop_new_deviceid_for_table(‘Table_Name10','deviceid');
PERFORM pop_new_deviceid_for_table(‘Table_Name11','deviceid');
PERFORM pop_new_deviceid_for_table(‘Table_Name12',deviceid');
PERFORM pop_new_deviceid_for_table(‘Table_Name13','deviceid');
PERFORM pop_new_deviceid_for_table(‘Table_Name14','deviceid');
PERFORM pop_new_deviceid_for_table(‘Table_Name15','deviceid');
PERFORM pop_new_deviceid_for_table(‘Table_Name16','deviceid');
PERFORM pop_new_deviceid_for_table(‘Table_Name17','deviceid');
PERFORM pop_new_deviceid_for_table(‘Table_Name18','deviceid');
PERFORM pop_new_deviceid_for_table(‘Table_Name19','deviceid');
PERFORM pop_new_deviceid_for_table(‘Table_Name20','deviceid');
RAISE NOTICE 'Updation of deviceid column for dependent tables which contains endpoints related information has completed successfully';
EXCEPTION WHEN OTHERS THEN
RAISE NOTICE 'Error occurred while executing pop_with_new_deviceid % %', SQLERRM, SQLSTATE;
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION pop_new_deviceid_for_table(p_table varchar,p_column varchar)
RETURNS void
AS $$
DECLARE
v_select varchar(5000);
id_error_count int:=0;
rec record;
BEGIN
--This funciton updates the deviceid column for spcified table using endpoint_deviceids_barediscovery table after rediscovery
v_select:='SELECT distinct t2.deviceid_old,t2.deviceid_new
FROM '|| p_table ||' t1,endpoint_deviceids_barediscovery t2
WHERE t1.'||p_column||'=t2.deviceid_old
AND t2.deviceid_new is not null';
RAISE NOTICE 'Updation of endpoints with newdeviceid for % started and query is %',p_table,v_select;
FOR rec IN EXECUTE v_select LOOP
BEGIN
EXECUTE FORMAT('UPDATE %I set %I = %s where %I=%s',p_table,p_column,rec.deviceid_new,p_column,rec.deviceid_old);
EXCEPTION
WHEN OTHERS THEN
id_error_count:=id_error_count+1;
RAISE NOTICE 'Error occurred while updating new deviceid column of % table for deviceid (%) % using pop_new_deviceid_for_table % %',p_table,p_column,rec.deviceid_old, SQLERRM, SQLSTATE;
END;
END LOOP;
EXCEPTION WHEN OTHERS THEN
RAISE NOTICE 'Error occurred while executing pop_new_deviceid_for_table for % table % %', p_table,SQLERRM, SQLSTATE;
END;
$$ LANGUAGE plpgsql;
请指导我
有时“select pop_with_new_deviceid()”仅需5分钟,有时超过2小时25分钟。如何缩小问题范围
如何在postgres中进行批量更新/插入/删除?我是否需要修改数据库中的任何配置参数?
如何确定postgres中每个函数所用的时间?