I can address part of your question to simplify the overall code. You are doing way to much work processing dates. Look at just one of your date 'conversions':
mnxtdate:= (SELECT to_date(to_char(now()::date + INTERVAL '1 DAY','yyyy-mm-dd'),'yyyy-mm-dd'));
First off a SELECT statement is not at all necessary, you can have direct assignment:
instead of
SELECT to_date(to_char(now()::date + INTERVAL '1 DAY','yyyy-mm-dd'),'yyyy-mm-dd') into mnxtdate;
just do:
mnxtdate:= expression; (more later)
Now a look at what the interpreter has to do resolving the statement.
- call the function now() ==> get the internal representation of the
current timestamp.
- (...)::date ==> convert internal representation of timestamp from #1
to internal representation a date.
- + interval '1 DAY' ==> convert the internal date from #2 to a internal timestamp and add 1 day.
- to_char(..., 'yyyy-mm-dd') ==> convert the internal timestamp from
#3 to string with with format 'yyyy-mm_dd'.
- to_date(..., 'yyyy-mm-dd') ==> convert the string from #4 into an
internal representation of a date.
- Store result of #5 in variable local variable mnxtdate.
A much simpler way eliminates the select altogether, is easier to write, easier to understand and eliminates most a lot on internal processing:
mnxtdate = (now() + interval '1 day')::date;
- call the function now() ==> get the internal representation of the current timestamp.
- + interval '1 day' ==> add 1 day to result of #1
- (...)::date ==> convert result of #2 in internal representation of date.
- Store result of #3 in variable local variable mnxtdate.
Note: You could also cast the timestamp before converting to date then just add 1. See example here. You might want to spend some time reviewing Postgres Date Processing.
The following shows your reworking date assignments - nothing else.
create table or replace function cnwd_details()
returns void as
$body$
declare
counter record;
sdcounter record;
cnt int;
currentdate date;
nextdate date;
mnxtdate date;
flag boolean;
begin
flag:=false;
currentdate:= now()::date;
for counter in (dblink('host=172.16.2.32 user=postgres password=postgres dbname=taphc port=5432','select holidaydate from holiday_t where date_part(''year'', create_modify) = date_part(''year'', current_date) and display=''y'' and holidaydate >= ''now()''::date order by holidaydate asc ')as subquery(holidaydate date)
loop
flag:=true;
if currentdate<=counter.holidaydate then
raise notice 'holiday_t table holidaydate column: %', currentdate<=counter.holidaydate;
nextdate:= (counter.holidaydate+interval '1 day')::date;
perform dblink('host=172.16.2.32 user=postgres password=postgres dbname=periphery port=5432',format('update ts_courtslip_cnwd set cnwd ='''||nextdate||'''')) as subquery ;
end if;
end loop;
if flag=false then
mnxtdate:= (now()+interval '1 day')::date ;
perform dblink('host=172.16.2.32 user=postgres password=postgres dbname=periphery port=5432',format('update ts_courtslip_cnwd set cnwd ='''||mnxtdate||'''')) as subquery ;
end if;
end;
$body$
language plpgsql volatile
cost 100;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…