CStringGetTextDatum and CStringGetDatum

A datum in PostgreSQL is a generic data type that

either a value of a pass-by-value type or a pointer to a value of a pass-by-reference type.

Therefore, we require:

sizeof(Datum) == sizeof(void *) == 8 (on 64-bit systems.) 

CStringGetDatum

CStringGetDatum and its inversity DatumGetCString are plain pointer conversion functions.

static inline Datum
CStringGetDatum(const char *X)
{
	return PointerGetDatum(X);
}

static inline char *
DatumGetCString(Datum X)
{
	return (char *) DatumGetPointer(X);
}

CStringGetTextDatum

CStringGetTextDatum and its inversity TextDatumGetCString are functions that convert between C string and PostgreSQL text type.

CStringGetTextDatum
\__ cstring_to_text
	\__ cstring_to_text_with_len(const char *s, int len)
		\__ palloc
		\__ memcpy

Note that CStringGetTextDatum generate a malloced object to return.

The structure of text type generated by the code path above is as follows:

/* Variable-length datatypes all share the 'struct varlena' header. */
struct varlena
{
	char		vl_len_[4];		/* Do not touch this field directly! */
	char		vl_dat[FLEXIBLE_ARRAY_MEMBER];	/* Data content is here */
};

typedef struct varlena text;

/*
 * vl_len_ = len << 2
 * vl_dat  = s
 */

Note that vl_len_ = len << 2, which means that we can NOT generate a quite long text (greater than 2^30 bytes) in this way. (What are other code paths to generate a long or compressed text?)