Thread: [GENERAL] Writing a C function to return the log file name

[GENERAL] Writing a C function to return the log file name

From
Kouber Saparev
Date:
Hello,

I am trying to write a function in C to return the log file name by given timestamp. I will use that later to make dynamic creation of a foreign table (file_fdw) to read the csv logs themselves. The thing is I do now want to hardcode neither the format, nor the directory in my extension.

I already looked into the adminpack extension, but the format is hardcoded to the default one there, so it does not serve my needs.

Here is what I currently have:

The thing is the function is returning random results, obtained by pg_strftime().

kouber=# select now()::timestamp, sqlog.log_path(now()::timestamp);
NOTICE:  Log directory = "pg_log"
NOTICE:  Log filename = "postgresql-%F.log"
NOTICE:  Length = "7"
NOTICE:  Filename = "pg_log/postgresql-17422165-04-30.log"
           now             |               log_path                
----------------------------+--------------------------------------
2017-06-02 14:17:47.832446 | pg_log/postgresql-17422165-04-30.csv
(1 row)

kouber=# select now()::timestamp, sqlog.log_path(now()::timestamp);
NOTICE:  Log directory = "pg_log"
NOTICE:  Log filename = "postgresql-%F.log"
NOTICE:  Length = "7"
NOTICE:  Filename = "pg_log/postgresql-17422166-02-08.log"
           now             |               log_path                
----------------------------+--------------------------------------
2017-06-02 14:18:12.390558 | pg_log/postgresql-17422166-02-08.csv
(1 row)


Any idea what am I doing wrong?

I copied logfile_getname() from syslogger.c, and simply added some debug messages in there.

Regards,
--
Kouber Saparev

Re: [GENERAL] Writing a C function to return the log file name

From
Albe Laurenz
Date:
Kouber Saparev wrote:
> I am trying to write a function in C to return the log file name by given timestamp. I
> will use that later to make dynamic creation of a foreign table (file_fdw) to read the csv
> logs themselves. The thing is I do now want to hardcode neither the format, nor the
> directory in my extension.
> 
> I already looked into the adminpack extension, but the format is hardcoded to the default
> one there, so it does not serve my needs.
> 
> Here is what I currently have:
> https://gist.github.com/kouber/89b6e5b647452a672a446b12413e20cf
> 
> 
> The thing is the function is returning random results, obtained by pg_strftime().
> 
> kouber=# select now()::timestamp, sqlog.log_path(now()::timestamp);
> NOTICE:  Log directory = "pg_log"
> NOTICE:  Log filename = "postgresql-%F.log"
> NOTICE:  Length = "7"
> NOTICE:  Filename = "pg_log/postgresql-17422165-04-30.log"
>            now             |               log_path
> ----------------------------+--------------------------------------
> 2017-06-02 14:17:47.832446 | pg_log/postgresql-17422165-04-30.csv
> (1 row)
> 
> kouber=# select now()::timestamp, sqlog.log_path(now()::timestamp);
> NOTICE:  Log directory = "pg_log"
> NOTICE:  Log filename = "postgresql-%F.log"
> NOTICE:  Length = "7"
> NOTICE:  Filename = "pg_log/postgresql-17422166-02-08.log"
>            now             |               log_path
> ----------------------------+--------------------------------------
> 2017-06-02 14:18:12.390558 | pg_log/postgresql-17422166-02-08.csv
> (1 row)
> 
> 
> 
> 
> 
> Any idea what am I doing wrong?
> 
> 
> I copied logfile_getname() from syslogger.c, and simply added some debug messages in
> there.

You are mixing up "Timestamp" and "pg_time_t".

Both are int64, but the former contains the number of microseconds since
2000-01-01 00:00:00, while the latter represents "the number of seconds
elapsed since 00:00:00 on January 1, 1970, Coordinated Universal Time (UTC)"
(quote from "man localtime").

Yours,
Laurenz Albe

Re: [GENERAL] Writing a C function to return the log file name

From
Kouber Saparev
Date:
Thank you Laurenz,

I replaced "pg_time_t" with "Timestamp", yet the result looks the same - each call returns random result.

2017-06-06 18:56 GMT+03:00 Albe Laurenz <laurenz.albe@wien.gv.at>:
Kouber Saparev wrote:
> I am trying to write a function in C to return the log file name by given timestamp. I
> will use that later to make dynamic creation of a foreign table (file_fdw) to read the csv
> logs themselves. The thing is I do now want to hardcode neither the format, nor the
> directory in my extension.
>
> I already looked into the adminpack extension, but the format is hardcoded to the default
> one there, so it does not serve my needs.
>
> Here is what I currently have:
> https://gist.github.com/kouber/89b6e5b647452a672a446b12413e20cf
>
>
> The thing is the function is returning random results, obtained by pg_strftime().
>
> kouber=# select now()::timestamp, sqlog.log_path(now()::timestamp);
> NOTICE:  Log directory = "pg_log"
> NOTICE:  Log filename = "postgresql-%F.log"
> NOTICE:  Length = "7"
> NOTICE:  Filename = "pg_log/postgresql-17422165-04-30.log"
>            now             |               log_path
> ----------------------------+--------------------------------------
> 2017-06-02 14:17:47.832446 | pg_log/postgresql-17422165-04-30.csv
> (1 row)
>
> kouber=# select now()::timestamp, sqlog.log_path(now()::timestamp);
> NOTICE:  Log directory = "pg_log"
> NOTICE:  Log filename = "postgresql-%F.log"
> NOTICE:  Length = "7"
> NOTICE:  Filename = "pg_log/postgresql-17422166-02-08.log"
>            now             |               log_path
> ----------------------------+--------------------------------------
> 2017-06-02 14:18:12.390558 | pg_log/postgresql-17422166-02-08.csv
> (1 row)
>
>
>
>
>
> Any idea what am I doing wrong?
>
>
> I copied logfile_getname() from syslogger.c, and simply added some debug messages in
> there.

You are mixing up "Timestamp" and "pg_time_t".

Both are int64, but the former contains the number of microseconds since
2000-01-01 00:00:00, while the latter represents "the number of seconds
elapsed since 00:00:00 on January 1, 1970, Coordinated Universal Time (UTC)"
(quote from "man localtime").

Yours,
Laurenz Albe