postgresql_16:6.1.10.2 Base Types in C-Language Functions
PostgreSQL 16 > Server Programming > Extending SQL > C-Language Functions > Base Types in C-Language Functions
To know how to write C-language functions, you need to know how PostgreSQL internally represents base data types and how they can be passed to and from functions. Internally, PostgreSQL regards a base type as a “blob of memory”. The user-defined functions that you define over a type in turn define the way that PostgreSQL can operate on it. That is, PostgreSQL will only store and retrieve the data from disk and use your user-defined functions to input, process, and output the data.
Base types can have one of three internal formats: pass by value, fixed-length pass by reference, fixed-length pass by reference, variable-length
By-value types can only be 1, 2, or 4 bytes in length (also 8 bytes, if sizeof(Datum) is 8 on your machine). You should be careful to define your types such that they will be the same size (in bytes) on all architectures. For example, the long type is dangerous because it is 4 bytes on some machines and 8 bytes on others, whereas int type is 4 bytes on most Unix machines. A reasonable implementation of the int4 type on Unix machines might be: /* 4-byte integer, passed by value */ typedef int int4; (The actual PostgreSQL C code calls this type int32, because it is a convention in C that int*XX* means *XX* *bits*. Note therefore also that the C type int8 is 1 byte in size. The SQL type int8 is called int64 in C. See also (see xfunc-c-type-table).)
On the other hand, fixed-length types of any size can be passed by-reference. For example, here is a sample implementation of a PostgreSQL type: /* 16-byte structure, passed by reference */ typedef struct { double x, y; } Point; Only pointers to such types can be used when passing them in and out of PostgreSQL functions. To return a value of such a type, allocate the right amount of memory with palloc, fill in the allocated memory, and return a pointer to it. (Also, if you just want to return the same value as one of your input arguments that's of the same data type, you can skip the extra palloc and just return the pointer to the input value.)
Finally, all variable-length types must also be passed by reference. All variable-length types must begin with an opaque length field of exactly 4 bytes, which will be set by SET_VARSIZE; never set this field directly! All data to be stored within that type must be located in the memory immediately following that length field. The length field contains the total length of the structure, that is, it includes the size of the length field itself.
Another important point is to avoid leaving any uninitialized bits within data type values; for example, take care to zero out any alignment padding bytes that might be present in structs. Without this, logically-equivalent constants of your data type might be seen as unequal by the planner, leading to inefficient (though not incorrect) plans.
Warning
*Never* modify the contents of a pass-by-reference input value. If you do so you are likely to corrupt on-disk data, since the pointer you are given might point directly into a disk buffer. The sole exception to this rule is explained in (see postgresql_16:6.1.12).
As an example, we can define the type text as follows: typedef struct { int32 length; char data[FLEXIBLE_ARRAY_MEMBER]; } text; The [FLEXIBLE_ARRAY_MEMBER] notation means that the actual length of the data part is not specified by this declaration.
When manipulating variable-length types, we must be careful to allocate the correct amount of memory and set the length field correctly. For example, if we wanted to store 40 bytes in a text structure, we might use a code fragment like this: #include "postgres.h" ... char buffer[40]; /* our source data */ ... text *destination = (text *) palloc(VARHDRSZ + 40); SET_VARSIZE(destination, VARHDRSZ + 40); memcpy(destination->data, buffer, 40); ... VARHDRSZ is the same as sizeof(int32), but it's considered good style to use the macro VARHDRSZ to refer to the size of the overhead for a variable-length type. Also, the length field *must* be set using the SET_VARSIZE macro, not by simple assignment.
(see xfunc-c-type-table) shows the C types corresponding to many of the built-in SQL data types of PostgreSQL. The “Defined In” column gives the header file that needs to be included to get the type definition. (The actual definition might be in a different file that is included by the listed file. It is recommended that users stick to the defined interface.) Note that you should always include postgres.h first in any source file of server code, because it declares a number of things that you will need anyway, and because including other headers first can cause portability issues.
| SQL Type | C Type | Defined In | | --- | --- | --- | | boolean | bool | postgres.h (maybe compiler built-in) | | box | BOX* | utils/geo_decls.h | | bytea | bytea* | postgres.h | | "char" | char | (compiler built-in) | | character | BpChar* | postgres.h | | cid | CommandId | postgres.h | | date | DateADT | utils/date.h | | float4 (real) | float4 | postgres.h | | float8 (double precision) | float8 | postgres.h | | int2 (smallint) | int16 | postgres.h | | int4 (integer) | int32 | postgres.h | | int8 (bigint) | int64 | postgres.h | | interval | Interval* | datatype/timestamp.h | | lseg | LSEG* | utils/geo_decls.h | | name | Name | postgres.h | | numeric | Numeric | utils/numeric.h | | oid | Oid | postgres.h | | oidvector | oidvector* | postgres.h | | path | PATH* | utils/geo_decls.h | | point | POINT* | utils/geo_decls.h | | regproc | RegProcedure | postgres.h | | text | text* | postgres.h | | tid | ItemPointer | storage/itemptr.h | | time | TimeADT | utils/date.h | | time with time zone | TimeTzADT | utils/date.h | | timestamp | Timestamp | datatype/timestamp.h | | timestamp with time zone | TimestampTz | datatype/timestamp.h | | varchar | VarChar* | postgres.h | | xid | TransactionId | postgres.h |
Now that we've gone over all of the possible structures for base types, we can show some examples of real functions.