Postgres doc为CREATE TYPE重新调整INPUT函数似乎不正确

时间:2016-11-03 16:26:21

标签: postgresql

我为数据类型创建了一个输入函数,如下所示:

mytimestamp(X)

Postgres文档指出,在这种情况下,当调用输入函数时,类型修饰符将作为第3个参数传递给函数。但是,在我的情况下,类型修饰符始终为-1,这没有任何意义。以下命令:

select atttypid,atttypmod 
from pq_attribute 
where attname='mytimestamp';

表明创建了类型,并且atttypmod值是我所期望的。但是,输入函数仍将类型修饰符显示为-1。我还尝试使用getBaseTypeAndTypmod函数,但它也返回-1的类型修饰符。我完全不知道接下来要做什么来解决这个问题。

这与我如何定义类型有关吗?

create type mytimestamp
( 
   INPUT = mytimestampin, 
   OUTPUT = mytimestampout,
   RECEIVE = mytimestamprecv,
   SEND = mytimestampsend,
   TYPEMOD_IN = mytimestamptypmodin,
--       TYPEMOD_OUT = mytimestamptypmodout,
   STORAGE = extended, 
   CATEGORY = 'd', 
   PREFERRED = true 
);

并且函数定义为:

CREATE OR REPLACE FUNCTION mytimestampin(cstring, oid, int4) RETURNS mytimestamp
            AS '$libdir/libmy_pgmod', 'mytimestampin'
            LANGUAGE C IMMUTABLE STRICT;
CREATE OR REPLACE FUNCTION mytimestampout(mytimestamp) RETURNS cstring
            AS '$libdir/libmy_pgmod', 'mytimestampout'
            LANGUAGE C IMMUTABLE STRICT;
CREATE OR REPLACE FUNCTION mytimestamprecv(internal, oid, int4) RETURNS mytimestamp
            AS '$libdir/libmy_pgmod', 'mytimestamprecv'
            LANGUAGE C STABLE STRICT;
CREATE OR REPLACE FUNCTION mytimestampsend(mytimestamp) RETURNS bytea
            AS '$libdir/libmy_pgmod', 'mytimestampsend'
            LANGUAGE C STABLE STRICT;
CREATE OR REPLACE FUNCTION mytimestamptypmodin(cstring[]) RETURNS int4
            AS '$libdir/libmy_pgmod', 'mytimestamptypmodin'
            LANGUAGE C IMMUTABLE STRICT;
--CREATE OR REPLACE FUNCTION mytimestamptypmodout(int4) RETURNS cstring
--                AS '$libdir/libmy_pgmod', 'mytimestamptypmodout'
--                LANGUAGE C IMMUTABLE STRICT;

我不能在这里放置完整的代码,但是,这是开始:

PG_FUNCTION_INFO_V1(mytimestampin);
...
Datum mytimestampin( PG_FUNCTION_ARGS ) {
    const char     *pszTS      = PG_GETARG_CSTRING( 0 );
    uint32_t        atttypmod  = PG_GETARG_INT32( 2 );
    struct varlena *result     = NULL;
    PAPrSqBs_t      pTimestamp = NULL;
    PAPrSqCl_t      pCol       = NULL;
    int64_t         width      = 0;
    int             length     = 0;
    unsigned char  *pOut       = NULL;
    int32_t         precision  = -1;

    PAPrSqRcInit( );

    pTimestamp = PAPrSqBs_new( timestamp );
    pCol       = PAPrSqCl_new( timestamp );

    /**
     * Get the precision.
     */
    if ( (uint32_t)-1 == atttypmod ) {
            precision = PAPrSqCl_getPrecision( pCol, max );
    } else {
            precision = atttypmod;
    }
    ...

    /**
     * Destroy the column and timestamp types.
     */
    PAPrSqCl_delete( pCol );
    PAPrSqBs_delete( pTimestamp );

    PAPrSqRcClean( );

    PG_RETURN_POINTER( result );
}

查询请求的结果如下:

 attname |  atttypid   | atttypmod
---------+-------------+-----------
 TS      | mytimestamp |         6
 CHAR10  | mychar      |    656506
 VCHAR20 | myvarchar   |   1311866
 FB31    | integer     |        -1
 FB15    | smallint    |        -1
 DT      | mydate      |        -1
 TM      | mytime      |        -1
(7 rows)

0 个答案:

没有答案