GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.postgresql.org — Extending SQL: Types November 19, 2026: PostgreSQL 19 Released!
Development Versions:
devel
This documentation is for an unsupported version of PostgreSQL.
You may want to view the same page for the current version, or one of the other supported versions listed above instead. Chapter 13. Extending SQL: TypesAs previously mentioned, there are two kinds of types in PostgreSQL: base types (defined in a programming language) and composite types. This chapter 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. Composite examples are in funcs.sql. 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 input and returns the internal (in memory) representation of the type. The output function takes the internal representation of the type and returns a null-terminated character string. Suppose we want to define a complex type which represents complex numbers. Naturally, we would choose to represent a complex in memory as the following C structure: typedef struct Complex {
double x;
double y;
} Complex;
and a string of the form (x,y) as the external string representation. The functions are usually not hard to write, especially the output function. However, there are a number of points to remember:
To define the complex type, we need to
create the two user-defined functions CREATE FUNCTION complex_in(opaque)
RETURNS complex
AS 'PGROOT/tutorial/complex'
LANGUAGE C;
CREATE FUNCTION complex_out(opaque)
RETURNS opaque
AS 'PGROOT/tutorial/complex'
LANGUAGE C;
Finally, we can declare the data type: CREATE TYPE complex (
internallength = 16,
input = complex_in,
output = complex_out
);
As discussed earlier, PostgreSQL fully supports arrays of base types. Additionally, PostgreSQL supports arrays of user-defined types as well. When you define a type, PostgreSQL automatically provides support for arrays of that type. For historical reasons, the array type has the same name as the user-defined type with the underscore character _ prepended. Composite types do not need any function defined on them, since the system already understands what they look like inside. If the values of your
datatype might exceed a few hundred bytes in size (in internal
form), you should be careful to 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). Then, all your functions that accept
values of the type must be careful to call |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |