Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL: month := interval '30 days';

Trying to delete records older than 1 month from 2 tables, where 1 references the "id" column in another:

create or replace function quincytrack_clean()
        returns void as $BODY$
        begin
                month := interval '30 days';

                delete from hide_id
                where id in
                (select id from quincytrack
                where age(QDATETIME) > month);

                delete from quincytrack
                where age(QDATETIME) > month;
        end;
$BODY$ language plpgsql;

but this fails with:

ERROR:  syntax error at or near "month"
LINE 1: month := interval '30 days'
        ^
QUERY:  month := interval '30 days'
CONTEXT:  SQL statement in PL/PgSQL function "quincytrack_clean" near line 2

I'm reading the doc, but don't understand what's wrong with my declaration...

like image 806
Alexander Farber Avatar asked Apr 13 '26 06:04

Alexander Farber


1 Answers

You need to declare the variable 'month', viz.:

declare
    month interval;
begin
    month := interval '30 days';
end;

Also, you might want to re-examine your "where" criteria. If QDATETIME is an indexed column, I don't think it will use the index, whereas QDATETIME < (now() - month) would.

like image 150
araqnid Avatar answered Apr 15 '26 20:04

araqnid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!