PostgreSQL服务端开发学习 -- Datum

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
简介: 在使用C语言开发PostgreSQL后端、客户端应用时,Datum无处不在,所以必须要对Datum有很清楚的了解。

在使用C语言开发PostgreSQL后端(backend)、客户端(client)应用时,Datum无处不在,所以必须要对Datum有很清楚的了解。这里的backend应用是指诸如自户自定义函数、扩展等同PostgreSQL服务一起工作的应用;客户端则是通过调用libpq访问数据库的应用。Datum数据类型在postgres.h中定义,这个头文件通常是第一个要include的,对于客户端应用则是postgres_fe.h。

根据官方文档的解释,Datum包含一个按值传递类型(通常是比较简单的数据类型,比如整数)或者一个引用类型(通常是一个指针,指向一些比较复杂的类型,比如struct),因此有如下要求:

sizeof(Datum) ==sizeof(void*) ==4or8

Datum定义如下:

typedefuintptr_tDatum;

PostgreSQL提供了一些函数用于在Datum与适当的类型间进行转换。

NullableDatum

NullableDatum结构用于保存Datum数据或者数据为空值时指示数据为空。

typedefstructNullableDatum{
#define FIELDNO_NULLABLE_DATUM_DATUM 0Datumvalue;
#define FIELDNO_NULLABLE_DATUM_ISNULL 1boolisnull;
/* due to alignment padding this could be used for flags for free */} NullableDatum;

DatumGetBool

从datum中返回bool值,非0值会被转换成true。

staticinlineboolDatumGetBool(DatumX)
{
return (X!=0);
}

BoolGetDatum

将bool值转换成datum,非零值被转换成true。

staticinlineDatumBoolGetDatum(boolX)
{
return (Datum) (X?1 : 0);
}

DatumGetChar

从datum返回char值。

staticinlinecharDatumGetChar(DatumX)
{
return (char) X;
}

CharGetDatum

/** CharGetDatum*      Returns datum representation for a character.*/staticinlineDatumCharGetDatum(charX)
{
return (Datum) X;
}

Int8GetDatum

/** Int8GetDatum*      Returns datum representation for an 8-bit integer.*/staticinlineDatumInt8GetDatum(int8X)
{
return (Datum) X;
}

DatumGetUInt8

/** DatumGetUInt8*      Returns 8-bit unsigned integer value of a datum.*/staticinlineuint8DatumGetUInt8(DatumX)
{
return (uint8) X;
}

UInt8GetDatum

/** UInt8GetDatum*      Returns datum representation for an 8-bit unsigned integer.*/staticinlineDatumUInt8GetDatum(uint8X)
{
return (Datum) X;
}

DatumGetInt16

/** DatumGetInt16*      Returns 16-bit integer value of a datum.*/staticinlineint16DatumGetInt16(DatumX)
{
return (int16) X;
}

Int16GetDatum

/** Int16GetDatum*      Returns datum representation for a 16-bit integer.*/staticinlineDatumInt16GetDatum(int16X)
{
return (Datum) X;
}

DatumGetUInt16

/** DatumGetUInt16*      Returns 16-bit unsigned integer value of a datum.*/staticinlineuint16DatumGetUInt16(DatumX)
{
return (uint16) X;
}

UInt16GetDatum

/** UInt16GetDatum*      Returns datum representation for a 16-bit unsigned integer.*/staticinlineDatumUInt16GetDatum(uint16X)
{
return (Datum) X;
}

DatumGetInt32

/** DatumGetInt32*      Returns 32-bit integer value of a datum.*/staticinlineint32DatumGetInt32(DatumX)
{
return (int32) X;
}

Int32GetDatum

/** Int32GetDatum*      Returns datum representation for a 32-bit integer.*/staticinlineDatumInt32GetDatum(int32X)
{
return (Datum) X;
}

DatumGetUInt32

/** DatumGetUInt32*      Returns 32-bit unsigned integer value of a datum.*/staticinlineuint32DatumGetUInt32(DatumX)
{
return (uint32) X;
}

UInt32GetDatum

/** UInt32GetDatum*      Returns datum representation for a 32-bit unsigned integer.*/staticinlineDatumUInt32GetDatum(uint32X)
{
return (Datum) X;
}

DatumGetObjectId

/** DatumGetObjectId*      Returns object identifier value of a datum.*/staticinlineOidDatumGetObjectId(DatumX)
{
return (Oid) X;
}

ObjectIdGetDatum

/** ObjectIdGetDatum*      Returns datum representation for an object identifier.*/staticinlineDatumObjectIdGetDatum(OidX)
{
return (Datum) X;
}

DatumGetTransactionId

/** DatumGetTransactionId*      Returns transaction identifier value of a datum.*/staticinlineTransactionIdDatumGetTransactionId(DatumX)
{
return (TransactionId) X;
}

TransactionIdGetDatum

/** TransactionIdGetDatum*      Returns datum representation for a transaction identifier.*/staticinlineDatumTransactionIdGetDatum(TransactionIdX)
{
return (Datum) X;
}

MultiXactIdGetDatum

/** MultiXactIdGetDatum*      Returns datum representation for a multixact identifier.*/staticinlineDatumMultiXactIdGetDatum(MultiXactIdX)
{
return (Datum) X;
}

DatumGetCommandId

/** DatumGetCommandId*      Returns command identifier value of a datum.*/staticinlineCommandIdDatumGetCommandId(DatumX)
{
return (CommandId) X;
}

CommandIdGetDatum

/** CommandIdGetDatum*      Returns datum representation for a command identifier.*/staticinlineDatumCommandIdGetDatum(CommandIdX)
{
return (Datum) X;
}

DatumGetPointer

/** DatumGetPointer*      Returns pointer value of a datum.*/staticinlinePointerDatumGetPointer(DatumX)
{
return (Pointer) X;
}

PointerGetDatum

/** PointerGetDatum*      Returns datum representation for a pointer.*/staticinlineDatumPointerGetDatum(constvoid*X)
{
return (Datum) X;
}

DatumGetCString

/** DatumGetCString*      Returns C string (null-terminated string) value of a datum.** Note: C string is not a full-fledged Postgres type at present,* but type input functions use this conversion for their inputs.*/staticinlinechar*DatumGetCString(DatumX)
{
return (char*) DatumGetPointer(X);
}

CStringGetDatum

/** CStringGetDatum*      Returns datum representation for a C string (null-terminated string).** Note: C string is not a full-fledged Postgres type at present,* but type output functions use this conversion for their outputs.* Note: CString is pass-by-reference; caller must ensure the pointed-to* value has adequate lifetime.*/staticinlineDatumCStringGetDatum(constchar*X)
{
returnPointerGetDatum(X);
}

DatumGetName

/** DatumGetName*      Returns name value of a datum.*/staticinlineNameDatumGetName(DatumX)
{
return (Name) DatumGetPointer(X);
}

NameGetDatum

/** NameGetDatum*      Returns datum representation for a name.** Note: Name is pass-by-reference; caller must ensure the pointed-to* value has adequate lifetime.*/staticinlineDatumNameGetDatum(constNameData*X)
{
returnCStringGetDatum(NameStr(*X));
}

DatumGetInt64

/** DatumGetInt64*      Returns 64-bit integer value of a datum.** Note: this function hides whether int64 is pass by value or by reference.*/staticinlineint64DatumGetInt64(DatumX)
{
#ifdef USE_FLOAT8_BYVALreturn (int64) X;
#elsereturn*((int64*) DatumGetPointer(X));
#endif}

Int64GetDatum

/** Int64GetDatum*      Returns datum representation for a 64-bit integer.** Note: if int64 is pass by reference, this function returns a reference* to palloc'd space.*/#ifdef USE_FLOAT8_BYVALstaticinlineDatumInt64GetDatum(int64X)
{
return (Datum) X;
}
#elseexternDatumInt64GetDatum(int64X);
#endif

DatumGetUInt64

/** DatumGetUInt64*      Returns 64-bit unsigned integer value of a datum.** Note: this function hides whether int64 is pass by value or by reference.*/staticinlineuint64DatumGetUInt64(DatumX)
{
#ifdef USE_FLOAT8_BYVALreturn (uint64) X;
#elsereturn*((uint64*) DatumGetPointer(X));
#endif}

UInt64GetDatum

/** UInt64GetDatum*      Returns datum representation for a 64-bit unsigned integer.** Note: if int64 is pass by reference, this function returns a reference* to palloc'd space.*/staticinlineDatumUInt64GetDatum(uint64X)
{
#ifdef USE_FLOAT8_BYVALreturn (Datum) X;
#elsereturnInt64GetDatum((int64) X);
#endif}

DatumGetFloat4

/** Float <-> Datum conversions** These have to be implemented as inline functions rather than macros, when* passing by value, because many machines pass int and float function* parameters/results differently; so we need to play weird games with unions.*//** DatumGetFloat4*      Returns 4-byte floating point value of a datum.*/staticinlinefloat4DatumGetFloat4(DatumX)
{
union    {
int32value;
float4retval;
    } myunion;
myunion.value=DatumGetInt32(X);
returnmyunion.retval;
}

Float4GetDatum

/** Float4GetDatum*      Returns datum representation for a 4-byte floating point number.*/staticinlineDatumFloat4GetDatum(float4X)
{
union    {
float4value;
int32retval;
    } myunion;
myunion.value=X;
returnInt32GetDatum(myunion.retval);
}

DatumGetFloat8

/** DatumGetFloat8*      Returns 8-byte floating point value of a datum.** Note: this function hides whether float8 is pass by value or by reference.*/staticinlinefloat8DatumGetFloat8(DatumX)
{
#ifdef USE_FLOAT8_BYVALunion    {
int64value;
float8retval;
    } myunion;
myunion.value=DatumGetInt64(X);
returnmyunion.retval;
#elsereturn*((float8*) DatumGetPointer(X));
#endif}

Float8GetDatum

/** Float8GetDatum*      Returns datum representation for an 8-byte floating point number.** Note: if float8 is pass by reference, this function returns a reference* to palloc'd space.*/#ifdef USE_FLOAT8_BYVALstaticinlineDatumFloat8GetDatum(float8X)
{
union    {
float8value;
int64retval;
    } myunion;
myunion.value=X;
returnInt64GetDatum(myunion.retval);
}
#elseexternDatumFloat8GetDatum(float8X);
#endif





相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
3月前
|
Cloud Native 关系型数据库 分布式数据库
|
5月前
|
关系型数据库 C语言 PostgreSQL
PostgreSQL服务端开发学习 --- 常用结构及宏定义1
本篇主要讲解使用C语言开发PostgreSQL服务端应用(libpq、自定义函数、扩展)常用到的结构及宏定义。
96 0
|
18天前
|
Docker 容器 关系型数据库
【PolarDB-X从入门到精通】 第四讲:PolarDB分布式版安装部署(源码编译部署)
本期课程将于4月11日19:00开始直播,内容包括源码编译基础知识和实践操作,课程目标是使学员掌握源码编译部署技能,为未来发展奠定基础,期待大家在课程中取得丰富的学习成果!
【PolarDB-X从入门到精通】 第四讲:PolarDB分布式版安装部署(源码编译部署)
|
5月前
|
关系型数据库 C语言 PostgreSQL
PostgreSQL服务端开发学习 -- fmgr.h
fmgr按官方的解释就是Postgres函数管理器和函数调用接口,在使用C语言开发PostgreSQL后端应用时,所以与backend交互时必须遵循fmgr.h中定义的一些规范。
140 0
|
2月前
|
SQL 存储 关系型数据库
MySQL技能完整学习列表——1、数据库基础概念——1、关系型数据库(Relational Database)
MySQL技能完整学习列表——1、数据库基础概念——1、关系型数据库(Relational Database)
179 0
|
3月前
|
关系型数据库 分布式数据库 数据库
阿里云PolarDB开发者大会首度召开,让数据库开发像“搭积木”一样简单
阿里云PolarDB开发者大会首度召开,让数据库开发像“搭积木”一样简单
109 0
|
3月前
|
关系型数据库 分布式数据库 数据库
家人们谁懂啊?为了让你们免费体验PolarDB,我们开发了一个动手体验搭子!
抢鲜体验赢好礼,阿里云定制折叠伞和定制双肩包等你拿!
家人们谁懂啊?为了让你们免费体验PolarDB,我们开发了一个动手体验搭子!
|
4月前
|
SQL 监控 关系型数据库
postgresql|数据库|插件学习(二)---postgresql-12的外置插件pg_profile的启用和使用
postgresql|数据库|插件学习(二)---postgresql-12的外置插件pg_profile的启用和使用
71 0
|
4月前
|
SQL 监控 关系型数据库
postgresql|数据库|插件学习(一)---postgresql-12的内置插件pg_stat_statements的启用和使用
postgresql|数据库|插件学习(一)---postgresql-12的内置插件pg_stat_statements的启用和使用
85 0
|
30天前
|
关系型数据库 分布式数据库 数据库
成都晨云信息技术完成阿里云PolarDB数据库产品生态集成认证
近日,成都晨云信息技术有限责任公司(以下简称晨云信息)与阿里云PolarDB PostgreSQL版数据库产品展开产品集成认证。测试结果表明,晨云信息旗下晨云-站群管理系统(V1.0)与阿里云以下产品:开源云原生数据库PolarDB PostgreSQL版(V11),完全满足产品兼容认证要求,兼容性良好,系统运行稳定。