WARNING

This text was automatically converted from troff me macros to HTML. Information may have been lost, added, or changed in the process. Lars Aronsson and Lysator do not guarantee the correctness of this HTML document.

NAME

define type *- define a new base data type

SYNOPSIS

define type typename (externallength = (number | variable), [ externallength = (number | variable), ] input = input_function, output = output_function [, element = typename] [, delimiter = <character>] [, default = "string" ] [, send = procedure ] [, receive = procedure ] [, passedbyvalue])

DESCRIPTION

"Define type" allows the user to register a new user data type with POSTGRES for use in the current data base. The user who defines a type becomes its owner. Typename is the name of the new type and must be unique within the types defined for this database.

"Define type" requires the registration of two functions (using "define function" ) before defining the type. The representation of a new base type is determined by the function input , which converts the type's external representation to an internal representation usable by the operators and functions defined for the type. Naturally, "output" performs the reverse transformation.

New base data types can be fixed length, in which case "internal length" is a positive integer, or variable length, in which case POSTGRES assumes that the new type has the same format as the POSTGRES-supplied data type, "text". To indicate that a type is variable length, set "internal length" to "-1"


Moreover, the external representation is similarly specified using
"external length."

To indicate that a type is an array and to indicate that a type has array elements, indicate the type of the array element using the "element" attribute. For example, to define an array of 4 byte integers (int4), set the "element" attribute equal to "int4".

To indicate the delimiter to be used on arrays of this type, the "delimiter" attribute can be set to a specific character. The default delimiter is the comma (``,'') character.

A "default" value is optionally available in case a user wants some specific bit pattern to mean data not present.

The optional functions "send" and "receive" are used when the application program requesting POSTGRES services resides on a different machine. In this case, the machine on which POSTGRES runs may use a different format for the data type than used on the remote machine. In this case it is appropriate to convert data items to a standard form on output "send" and convert from the standard format to the machine specific format on input "receive." If these functions are not specified, then it is assumed that the internal format of the type is acceptable on all relevant machine architectures (for example, single characters do not have to be converted if passed from a Sun 3 to a DECstation).

The optional "passedbyvalue" flag indicates that operators and functions which use this data type should be passed an argument by value rather than by reference. Note that only types whose internal representation is smaller than sizeof(char *), which is typically four bytes, may be passed by value.

For new base types, a user can define operators, functions and aggregates using the appropriate facilities described in this section.

ARRAY TYPES

Two generalized builtin functions, array_in and array_out, exist for quick creation of variable length array types. These functions operate on any existing POSTGRES type.

LARGE OBJECT TYPES

A "regular" POSTGRES type can only be 8K bytes in length. If you need a larger type, then you will want to create a Large Object type. The interface for these types is discussed at length in Section 7, the Large Object Backend Interface. The length of all large object types is always variable, meaning the internallength for large objects is always -1.

EXAMPLES

/*
 * This command creates the box data type and then uses the
 * type in a class definition
 */

define type box (internallength = 8,
  input = my_procedure_1, output = my_procedure_2)

create MYBOXES (id = int4, description = box)

/*
 * This command creates a variable length array type with
 * integer elements.
 */
 
define type int4array
   (input = array_in, output = array_out,
    internallength = variable, element = int4)

   create MYARRAYS (id = int4, numbers = int4array)

/*
 * This command creates a large object type and uses it in
 * a class definition.
 */

define type bigobj
   (input = lo_filein, output = lo_fileout,
    internallength = variable)

   create BIG_OBJS (id = int4, obj = bigobj)

SEE ALSO

define function(commands), define operator(commands), remove type(commands), Large Object Backend Interface.