PostgreSQL 7.4beta4 Documentation
Prev | Fast Backward | Chapter 33. Extending SQL | Fast Forward | Next

# 33.11. User-Defined Types

As described above, there are two kinds of data types in PostgreSQL: base types and composite types. This section describes how to define new base types.

The examples in this section can be found in `complex.sql` and `complex.c` in the tutorial directory.

A user-defined type must always have input and output functions. These functions determine how the type appears in strings (for input by the user and output to the user) and how the type is organized in memory. The input function takes a null-terminated character string as its argument and returns the internal (in memory) representation of the type. The output function takes the internal representation of the type as argument and returns a null-terminated character string.

Suppose we want to define a type `complex` that represents complex numbers. A natural way to to represent a complex number in memory would be the following C structure:

    typedef struct Complex {
        double      x;
        double      y;
    } Complex;

As the external string representation of the type, we choose a string of the form (x,y).

The input and output functions are usually not hard to write, especially the output function. But when defining the external string representation of the type, remember that you must eventually write a complete and robust parser for that representation as your input function. For instance:

    Complex *
    complex_in(char *str)
    {
        double      x,
                    y;
        Complex    *result;

        if (sscanf(str, " ( %lf , %lf )", &x, &y) != 2)
            ereport(ERROR,
                    (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                     errmsg("invalid input syntax for complex: \"%s\"", str)));

        result = (Complex *) palloc(sizeof(Complex));
        result->x = x;
        result->y = y;
        return result;
    }

The output function can simply be:

    char *
    complex_out(Complex *complex)
    {
        char *result;

        if (complex == NULL)
            return(NULL);
        result = (char *) palloc(60);
        sprintf(result, "(%g,%g)", complex->x, complex->y);
        return result;
    }

You should try to make the input and output functions inverses of each other. If you do not, you will have severe problems when you need to dump your data into a file and then read it back in. This is a particularly common problem when floating-point numbers are involved.

To define the `complex` type, we need to create the two user-defined functions `complex_in` and `complex_out` before creating the type:

    CREATE FUNCTION complex_in(cstring)
        RETURNS complex
        AS 'filename'
        LANGUAGE C;

    CREATE FUNCTION complex_out(complex)
        RETURNS cstring
        AS 'filename'
        LANGUAGE C;

Notice that the declarations of the input and output functions must reference the not-yet-defined type. This is allowed, but will draw warning messages that may be ignored.

Finally, we can declare the data type:

    CREATE TYPE complex (
        internallength = 16,
        input = complex_in,
        output = complex_out
    );

When you define a new base type, PostgreSQL automatically provides support for arrays of that type. For historical reasons, the array type has the same name as the base type with the underscore character (_) prepended.

If the values of your data type might exceed a few hundred bytes in size (in internal form), you should mark them TOAST-able. To do this, the internal representation must follow the standard layout for variable-length data: the first four bytes must be an `int32` containing the total length in bytes of the datum (including itself). Also, when running the `CREATE TYPE` command, specify the internal length as variable and select the appropriate storage option.

For further details see the description of the CREATE TYPE command.

Prev | Home | Next
User-Defined Aggregates | Up | User-defined Operators