PostgreSQL/開発

概要

参考(utils/timeout.h) enum TimeoutId - https://git.postgresql.org/gitweb/?p=postgresql.git;a=shortlog;h=refs/heads/REL_10_STABLE

timeout-overview.png

定義

変数,エイリアス

マクロ

列挙型

TimeoutId

タイムアウトの種別を示す、またタイムアウトを管理する配列のインデックスでもある。

定数名説明
STARTUP_PACKET_TIMEOUTPostgreSQLの予約タイムアウト定義
DEADLOCK_TIMEOUT
LOCK_TIMEOUT
STATEMENT_TIMEOUT
STANDBY_DEADLOCK_TIMEOUT
STANDBY_TIMEOUT
STANDBY_LOCK_TIMEOUT
IDLE_IN_TRANSACTION_SESSION_TIMEOUT
USER_TIMEOUTユーザーが独自に定義できるタイムアウトを示す。プラグインなどでRegisterTimeoutするときは、この定数を引数に指定する。
MAX_TIMEOUTSタイムアウトの上限を示す定数。16である。

TimeoutType

定数名説明
TMPARAM_AFTERタイムアウト設定で、xx秒後を示すタイプ識別子
TMPARAM_ATタイムアウト設定で、xx時間を示すタイプ識別子

これらの定数は、複数のタイムアウト設定を有効にするenable_timeouts関数で用いる。
enable_timeoutsには、EnableTimeoutParams構造体のオブジェクトを指定する。

構造体/共用体

EnableTimeoutParams

番号データ型フィールド説明
1TimeoutIdidタイムアウトID
2TimeoutTypetypeタイムアウトがxx秒後またはxx時間かを示すタイプ識別子
3intdelay_mstypeがTMPARAM_AFTERの時の時間指定(msec)
4TimestampTzfin_timetypeがTMPARAM_ATの時の時間指定(msec)

enable_timeouts関数で指定する構造体。

DisableTimeoutParams

番号データ型フィールド説明
1TimeoutIdidタイムアウトID
2boolkeep_indicatorindicator(タイマが発火したか否かを示すフラグ)をリセットするか否か

disable_timeouts関数で指定する構造体。

関数

InitializeTimeouts

// 引数1:
extern ForeignServer *GetForeignServer(Oid serverid);

RegisterTimeout

/*-------------------------------------------------------------------------
 *
 * timeout_test.c
 *		Timeout test code
 *
 * IDENTIFICATION
 *	  contrib/timeout_test/pg_retire.c
 *
 *-------------------------------------------------------------------------
 */

#include "postgres.h"

#include "fmgr.h"
#include "utils/timeout.h"


PG_MODULE_MAGIC;


static TimeoutId MyTimeoutId = USER_TIMEOUT;


static void my_timeout_handler(void);


static void my_timeout_handler(void)
{
	ereport(DEBUG3,
			(errmsg("MyTimer fired!")));
}

PG_FUNCTION_INFO_V1(my_register_timeout);
Datum
my_register_timeout(PG_FUNCTION_ARGS)
{
	if (MyTimeoutId != USER_TIMEOUT)
	{
		ereport(WARNING,
				 (errmsg("timeout is already set.")));
		PG_RETURN_INT32(-1);
	}

	MyTimeoutId = RegisterTimeout(USER_TIMEOUT, my_timeout_handler);
	PG_RETURN_INT32(MyTimeoutId);
}

PG_FUNCTION_INFO_V1(my_enable_timeout_after);
Datum
my_enable_timeout_after(PG_FUNCTION_ARGS)
{
	int timeout_after = PG_GETARG_INT32(0);

	enable_timeout_after(MyTimeoutId, timeout_after);

	PG_RETURN_BOOL(true);
}


PG_FUNCTION_INFO_V1(my_enable_timeout_at);
Datum
my_enable_timeout_at(PG_FUNCTION_ARGS)
{
	int timeout_at = PG_GETARG_INT32(0);

	enable_timeout_at(MyTimeoutId, timeout_at);

	PG_RETURN_BOOL(true);
}

PG_FUNCTION_INFO_V1(my_disable_timeout);
Datum
my_disable_timeout(PG_FUNCTION_ARGS)
{
	disable_timeout(MyTimeoutId, true);

	PG_RETURN_BOOL(true);
}


PG_FUNCTION_INFO_V1(my_get_timeout_indicator);
Datum
my_get_timeout_indicator(PG_FUNCTION_ARGS)
{
	bool indicator;

	indicator = get_timeout_indicator(MyTimeoutId, false);

	PG_RETURN_BOOL(indicator);
}

PG_FUNCTION_INFO_V1(my_get_timeout_start_time);
Datum
my_get_timeout_start_time(PG_FUNCTION_ARGS)
{
	int64 start_time;

	start_time = get_timeout_start_time(MyTimeoutId);

	PG_RETURN_INT64(start_time);
}

PG_FUNCTION_INFO_V1(my_get_timeout_finish_time);
Datum
my_get_timeout_finish_time(PG_FUNCTION_ARGS)
{
	int64 finish_time;

	finish_time = get_timeout_finish_time(MyTimeoutId);

	PG_RETURN_INT64(finish_time);
}

サンプル

static TimeoutId MyTimeoutId = MAX_TIMEOUTS;
MyTimeoutId = RegisterTimeout(USER_TIMEOUT, my_alarm_handler);

reschedule_timeouts

extern void reschedule_timeouts(void);

enable_timeout_after

// 引数1:タイムアウトID
// 引数2:タイムアウト設定(xxミリ秒後)
extern void enable_timeout_after(TimeoutId id, int delay_ms);

enable_timeout_at

// 引数1:タイムアウトID
// 引数2:タイムアウト設定(xxミリ秒後)
extern void enable_timeout_at(TimeoutId id, TimestampTz fin_time);

enable_timeouts

// 引数1:タイムアウト設定を保持するEnableTimeoutParams構造体配列のポインタ
// 引数2:配列の要素数
extern void enable_timeouts(const EnableTimeoutParams *timeouts, int count);

disable_timeout

// 引数1:タイムアウトID
// 引数2:indicatorフラグをリセットするか否か
extern void disable_timeout(TimeoutId id, bool keep_indicator);

disable_timeouts

// 引数1:タイムアウト設定を保持するDisableTimeoutParams構造体配列のポインタ
// 引数2:配列の要素数
extern void disable_timeouts(const DisableTimeoutParams *timeouts, int count);

disable_all_timeouts

// 引数1:indicatorフラグをリセットするか否か
extern void disable_all_timeouts(bool keep_indicators);

get_timeout_indicator

// 引数1:タイムアウトID
// 引数2:indicatorフラグをリセットするか否か
// 戻り値:indicatorの値
extern bool get_timeout_indicator(TimeoutId id, bool reset_indicator);

get_timeout_start_time

// 引数1:タイムアウトID
// 戻り値:タイマ開始時間
extern TimestampTz get_timeout_start_time(TimeoutId id);

get_timeout_finish_time

// 引数1:タイムアウトID
// 戻り値:タイムアウト時間
extern TimestampTz get_timeout_finish_time(TimeoutId id);

サンプルプログラム

以下は、timeout.hの各種関数を拡張機能から呼び出すサンプルである。
詳細は、添付のfilemy_timeout.zipを参照。

/*-------------------------------------------------------------------------
 *
 * timeout_test.c
 *		Timeout test code
 *
 * IDENTIFICATION
 *	  contrib/timeout_test/pg_retire.c
 *
 *-------------------------------------------------------------------------
 */

#include "postgres.h"

#include "fmgr.h"
#include "utils/timeout.h"


PG_MODULE_MAGIC;


static TimeoutId MyTimeoutId = USER_TIMEOUT;


static void my_timeout_handler(void);


static void my_timeout_handler(void)
{
	ereport(DEBUG3,
			(errmsg("MyTimer fired!")));
}

PG_FUNCTION_INFO_V1(my_register_timeout);
Datum
my_register_timeout(PG_FUNCTION_ARGS)
{
	if (MyTimeoutId != USER_TIMEOUT)
	{
		ereport(WARNING,
				 (errmsg("timeout is already set.")));
		PG_RETURN_INT32(-1);
	}

	MyTimeoutId = RegisterTimeout(USER_TIMEOUT, my_timeout_handler);
	PG_RETURN_INT32(MyTimeoutId);
}

PG_FUNCTION_INFO_V1(my_enable_timeout_after);
Datum
my_enable_timeout_after(PG_FUNCTION_ARGS)
{
	int timeout_after = PG_GETARG_INT32(0);

	enable_timeout_after(MyTimeoutId, timeout_after);

	PG_RETURN_BOOL(true);
}


PG_FUNCTION_INFO_V1(my_enable_timeout_at);
Datum
my_enable_timeout_at(PG_FUNCTION_ARGS)
{
	int timeout_at = PG_GETARG_INT32(0);

	enable_timeout_at(MyTimeoutId, timeout_at);

	PG_RETURN_BOOL(true);
}

PG_FUNCTION_INFO_V1(my_disable_timeout);
Datum
my_disable_timeout(PG_FUNCTION_ARGS)
{
	disable_timeout(MyTimeoutId, true);

	PG_RETURN_BOOL(true);
}


PG_FUNCTION_INFO_V1(my_get_timeout_indicator);
Datum
my_get_timeout_indicator(PG_FUNCTION_ARGS)
{
	bool indicator;

	indicator = get_timeout_indicator(MyTimeoutId, false);

	PG_RETURN_BOOL(indicator);
}

PG_FUNCTION_INFO_V1(my_get_timeout_start_time);
Datum
my_get_timeout_start_time(PG_FUNCTION_ARGS)
{
	int64 start_time;

	start_time = get_timeout_start_time(MyTimeoutId);

	PG_RETURN_INT64(start_time);
}

PG_FUNCTION_INFO_V1(my_get_timeout_finish_time);
Datum
my_get_timeout_finish_time(PG_FUNCTION_ARGS)
{
	int64 finish_time;

	finish_time = get_timeout_finish_time(MyTimeoutId);

	PG_RETURN_INT64(finish_time);
}

実行結果は、以下のようになる。

postgres=# CREATE EXTENSION my_timeout;
CREATE EXTENSION
postgres=# SELECT my_register_timeout(); -- Register my timer.
 my_register_timeout 
---------------------
                   8
(1 ROW)

postgres=# SELECT my_enable_timeout_after(5000); -- A timer will be fired after 5 seconds.
 my_enable_timeout_after 
-------------------------
 t
(1 ROW)

postgres=# SELECT my_get_timeout_indicator(); -- Check whether timer fired.
 my_get_timeout_indicator 
--------------------------
 t
(1 ROW)

postgres=# SELECT my_get_timeout_start_time(); -- Show start-time of the timer.
   my_get_timeout_start_time   
-------------------------------
 2018-04-16 00:17:37.234418+09
(1 ROW)

postgres=# SELECT my_get_timeout_finish_time(); -- Show finish-time of the timer.
  my_get_timeout_finish_time   
-------------------------------
 2018-04-16 00:17:42.234418+09
(1 ROW)

postgres=# DROP EXTENSION my_timeout;
DEBUG:  DROP auto-cascades TO FUNCTION my_register_timeout()
DEBUG:  DROP auto-cascades TO FUNCTION my_enable_timeout_after(INTEGER)
DEBUG:  DROP auto-cascades TO FUNCTION my_enable_timeout_at(INTEGER)
DEBUG:  DROP auto-cascades TO FUNCTION my_disable_timeout()
DEBUG:  DROP auto-cascades TO FUNCTION my_get_timeout_indicator()
DEBUG:  DROP auto-cascades TO FUNCTION my_get_timeout_start_time()
DEBUG:  DROP auto-cascades TO FUNCTION my_get_timeout_finish_time()
DROP EXTENSION

参考・関連

関連


トップ   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
目次
ダブルクリックで閉じるTOP | 閉じる
GO TO TOP