22. large object の auto-delete を考える - PostgreSQL のソースコードを読む

PostgreSQL のサーバーサイドで組み込みの型を増やす方法を探る。

pg_type.h

pg_type.h を見ると

postgres.h contains the system type definitions

とあるので postgres.h を見るべきか。


でも pg_type.h には pg_type カタログにデフォルトで存在する列が定義されている。
例えば char 型であれば。

DATA(insert OID = 18 (	char	   PGNSP PGUID	1 t b t \054 0	 0 1002 charin charout \
                      charrecv charsend - - - c p f 0 -1 0 _null_ _null_ ));
DESCR("single character");
#define CHAROID			18

DATA マクロは(おそらく)、type 構造体の初期値を設定している。type のバイト数や、type を扱う関数ポインタ(charinとか)など。
これらのマクロはまだ深追いしなくても良いだろう。

postgres.h

コメントを読むと postgres.h は backend の中では必要だが、外では必要ないものを定義すると書いてある。
postgres.h では外に見せたい type を定義してはだめなのか。
共有されるなら postgres_fe.h に書くっぽい。
ああ。いや違う。間違いだ。ここで言われている type は C 言語上の型の事か。(bool とか int16_t とか)

実験してみよう

今回の目的では

  • 組み込み型が定義できる事
  • OID 型と同じように扱える(けど別の型)

というのが実現できれば良い。

hige=# select typname from pg_type where typname like 'hige%';
 typname 
---------
 higeid
(1 row)

hige=# CREATE TABLE person (social_no higeid);

hige=# insert into person values(3);
ERROR:  column "social_no" is of type higeid but expression is of type integer

おっと。おしいな。続きは明日。

次の1手

  • 組み込み type の定義方法
  • craete table 時に trigger を作る方法