目標: RRULE 文字列 (つまりFREQ=WEEKLY;INTERVAL=2;COUNT=8;WKST=SU;BYDAY=TU,TH
) と開始タイムスタンプから、それぞれがイベントの発生を表すタイムスタンプのリストを生成すること。これらの時間はユーザーの気まぐれで変更されるため、postgresql は、(1) 厳密なビジネス ロジックの品質と (2) トリガー (値が変更されたときに行を自動更新する) のために望まれます。
代替ソリューション:
結局、plpythonu (postgresql の Python 言語) を使用しました。dateutilライブラリには優れた rrule パーサーがあります。
mydatabase=# CREATE FUNCTION parse_occurrences(rule text, start timestamp) RETURNS timestamp[] AS
mydatabase-# 'from dateutil.rrule import *
mydatabase'# from dateutil.parser import *
mydatabase'# import datetime
mydatabase'# dates = list(rrulestr(rule, dtstart=parse(start)))
mydatabase'# return [str(a)[0:10] for a in dates]'
mydatabase-# LANGUAGE plpythonu;
CREATE FUNCTION
mydatabase=# SELECT parse_occurrences('FREQ=WEEKLY;INTERVAL=2;COUNT=8;WKST=SU;BYDAY=TU,TH'::text, now()::timestamp);
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
{"2013-02-14 00:00:00","2013-02-26 00:00:00","2013-02-28 00:00:00","2013-03-12 00:00:00","2013-03-14 00:00:00","2013-03-26 00:00:00","2013-03-28 00:00:00","2013-04-09 00:00:00"}
(元の) 努力: C ライブラリ libicalをPostgresql C-Extensionsに接続すること。これには、次のことを行う特殊な C プログラムが必要です。(1) postgresql データ型を C データ型に変換し、(2) 必要なすべての C ライブラリ関数を実行し、(3) ヘッダー ファイルを使用して postgresql 形式でデータを返します。 postgres.h".
コネクタ ファイル: ical_recur.c
#include "postgres.h"
#include "icalrecur.h"
#include <time.h> /* for time() */
#include "fmgr.h"
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
PG_FUNCTION_INFO_V1(get_occurrences);
Datum
get_occurrences(PG_FUNCTION_ARGS)
{
//char* rule; /* rule string */
time_t start; /* start time */
int count;
char *rrule;
// *rule = PG_GETARG_CHAR(0);
start = (time_t) PG_GETARG_INT32(1);
count = (int) PG_GETARG_INT32(2);
*rrule = PG_GETARG_CHAR(0);
time_t *result[count]; /* output array */
icalrecur_expand_recurrence(rrule, start, count, *result);
PG_RETURN_INT32(*result);
}
コネクタ ファイルの準備:
ステップ 1: オブジェクト ファイルにコンパイルし、共有オブジェクトを作成し、postgresql が C 拡張を検索する場所にコピーします。
sudo gcc -I/usr/local/libical/lib -lical -I/usr/include/postgresql/9.2/server -fpic -c ical_recur.c
sudo gcc -shared -L/usr/local/libical/lib -lical -o ical_recur.so ical_recur.o
sudo cp ical_recur.so /usr/lib/postgresql/9.2/lib/
ステップ 2: C で見つけられる libical lib フォルダーを追加し、構成をリロードします。
sudo echo "/usr/local/libical/lib" >> /etc/ld.so.conf.d/libc.conf
sudo ldconfig
コネクタ ファイルのテスト:
ステップ 1: psql をロードして FUNCTION を作成する
psql
mydatabase=# CREATE FUNCTION get_occurrences(text, integer, integer) RETURNS int[]
mydatabase=- AS '$libdir/ical_recur', 'get_occurrences'
mydatabase=- LANGUAGE C STRICT;
CREATE FUNCTION
mydatabase=#
現在の障害:
C 関数が postgresql サーバーをクラッシュさせました。
psql (9.2.3)
Type "help" for help.
mydatabase=# SELECT get_occurrences('FREQ=WEEKLY;INTERVAL=2;COUNT=8;WKST=SU;BYDAY=TU,TH', now()::timestamp, 5);
The connection to the server was lost. Attempting reset: Failed.
!>
ログ...
2013-02-11 22:03:33 UTC LOG: server process (PID 22733) was terminated by signal 11: Segmentation fault
2013-02-11 22:03:33 UTC DETAIL: Failed process was running: SELECT get_occurrences('FREQ=WEEKLY;INTERVAL=2;COUNT=8;WKST=SU;BYDAY=TU,TH', now()::timestamp, 5);
2013-02-11 22:03:33 UTC LOG: terminating any other active server processes
2013-02-11 22:03:33 UTC WARNING: terminating connection because of crash of another server process
2013-02-11 22:03:33 UTC DETAIL: The postmaster has commanded this server process to roll back the current transaction and exit, because another server process exited abnormally and possibly corrupted shared memory.
2013-02-11 22:03:33 UTC HINT: In a moment you should be able to reconnect to the database and repeat your command.
2013-02-11 22:03:33 UTC LOG: all server processes terminated; reinitializing
2013-02-11 22:03:33 UTC FATAL: the database system is in recovery mode
2013-02-11 22:03:33 UTC LOG: database system was interrupted; last known up at 2013-02-11 21:47:26 UTC
2013-02-11 22:03:33 UTC LOG: database system was not properly shut down; automatic recovery in progress
2013-02-11 22:03:33 UTC LOG: redo starts at 0/1903A0C
2013-02-11 22:03:33 UTC LOG: record with zero length at 0/190E1E0
2013-02-11 22:03:33 UTC LOG: redo done at 0/190E1B8
2013-02-11 22:03:33 UTC LOG: last completed transaction was at log time 2013-02-11 22:03:29.641161+00
2013-02-11 22:03:33 UTC LOG: database system is ready to accept connections
2013-02-11 22:03:33 UTC LOG: autovacuum launcher started
更新しました:
いくつかの提案に対処するためにメソッドを更新しました。
#include "postgres.h"
#include "icalrecur.h"
#include <time.h> /* for time() */
#include "fmgr.h"
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
PG_FUNCTION_INFO_V1(get_occurrences);
Datum
get_occurrences(PG_FUNCTION_ARGS)
{
time_t start = (time_t) PG_GETARG_INT32(1); /* convert int to time_t */
int count = PG_GETARG_INT32(2);
char rrule = PG_GETARG_CHAR(0);
char *_rrule = &rrule; /* icalrecur wants a pointer to the rrule */
time_t result[count]; /* instantiate the output array */
int success = icalrecur_expand_recurrence(_rrule, start, count, result);
/* convert time_t values to int */
int *output = malloc(sizeof(result));
int i;
for(i = 0; i < (int) (sizeof(result) / sizeof(result[0]) - 1); i++){
output[i] = (int) result[i];
}
if(success != 1){
PG_RETURN_INT32(0);
} else {
PG_RETURN_INT32(output);
}
}
建物の建物..
sudo gcc -Wall -Wextra -l/usr/local/libical/lib/ical -I/usr/local/libical/include/libical -I/usr/include/postgresql/9.2/server -fpic -c ical_recur.c
sudo gcc -Wall -shared -static -L/usr/local/libical/lib -lical -o ical_recur.so ical_recur.o
sudo cp libical.so /usr/lib/postgresql/9.2/lib/
データベースに入り、..で実行
mydb=# CREATE FUNCTION get_occurrences(text, integer, integer) RETURNS int[]
AS '$libdir/ical_recur', 'get_occurrences'
LANGUAGE C STRICT;
CREATE FUNCTION
mydb=# SELECT get_occurrences('FREQ=WEEKLY;INTERVAL=2;COUNT=8;WKST=SU;BYDAY=TU,TH', 1360690024, 5);
The connection to the server was lost. Attempting reset: Failed.
!> \q
同じログ出力。ドキュメントには、パラメーターが記載されています。
int icalrecur_expand_recurrence(char * rule, time_t start, int count, time_t * array)
質問: Postgres をクラッシュさせずにこれを機能させるにはどうすればよいですか?