Re: Plpgsql - Custom fields Postgres 9.5

From: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
To: Patrick B <patrickbakerbr(at)gmail(dot)com>, pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Plpgsql - Custom fields Postgres 9.5
Date: 2016-12-14 21:23:02
Message-ID: d950d9a4-1dd3-5a39-9ff9-2e666d48444f@aklaver.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 12/14/2016 01:17 PM, Patrick B wrote:
> Hi,
>
> I've got this query, that I manually run it once a month:
>
> SELECT
> uuid,
> clientid),
> *
> FROM
> logging
> WHERE
> logtime
> BETWEEN
> '201611015'
> AND
> '201612015'
>
>
>
> As you can see, I select a date. So in December, the date will be:
> *BETWEEN '201612015' AND '201601015'*, for example.
>
> I always need to run this on the 15th of each month.
> I was thinking about creating a PLPGSQL function and a Cron task, so
> this task can be automated.
>
> Also, the file must be saved with the date+.csv. Example:
>
> CREATE or REPLACE FUNCTION logextract(date_start integer,
> date_end integer)
>
> RETURNS void AS $$
>
> begin
>
> execute '
>
> COPY
>
> (
>
> SELECT
>
> uuid,
>
> clientid),
>
> *
>
> FROM
>
> logging
>
> WHERE
>
> logtime
>
> BETWEEN
>
> ' || date_start || '
>
> AND
>
> ' || date_end || '
>
> )
>
> TO ''/var/lib/postgresql/'|| date_start ||'_logs.csv''';
>
> end
>
> $$ language 'plpgsql';
>
>
>
> *Questions:*
>
> 1. Why when I run the function manually I get this error?
>
> select logextract(201612015, 201612015);
>
> ERROR: operator does not exist: timestamp without time zone >=
> integer
>
> LINE 13: BETWEEN

The answer is above. Look at your original query at the top of the post.

>
>
> I presume this is wrong: _CREATE or REPLACE FUNCTION
> logextract(date_start integer, date_end integer) _- But what should I
> use instead?
>
>
> 2. To call the function, I have to login to postgres and then
> run: select logextract(201612015, 201612015);
> How can I do it on cron? because the dates will be different every time.
>
> Thanks
> Patrick

--
Adrian Klaver
adrian(dot)klaver(at)aklaver(dot)com

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Patrick B 2016-12-14 21:30:19 Re: Plpgsql - Custom fields Postgres 9.5
Previous Message Patrick B 2016-12-14 21:17:09 Plpgsql - Custom fields Postgres 9.5