PostgreSQL Data Types
In this chapter, we discuss PostgreSQL data types, which are set for each field when creating a table.
Benefits of setting data types:
PostgreSQL provides a rich set of data types. Users can use the CREATE TYPE command to create new data types in the database. There are many kinds of PostgreSQL data types, which we describe in detail below.
Numeric Types
Numeric types consist of 2-byte, 4-byte, or 8-byte integers, as well as 4-byte or 8-byte floating-point numbers and decimal numbers with optional precision.
The following table lists the available numeric types.
| Name | Storage Length | Description | Range |
|---|---|---|---|
| smallint | 2 bytes | Small-range integer | -32768 to +32767 |
| integer | 4 bytes | Commonly used integer | -2147483648 to +2147483647 |
| bigint | 8 bytes | Large-range integer | -9223372036854775808 to +9223372036854775807 |
| decimal | Variable length | User-specified precision, exact | 131072 digits before the decimal point; 16383 digits after the decimal point |
| numeric | Variable length | User-specified precision, exact | 131072 digits before the decimal point; 16383 digits after the decimal point |
| real | 4 bytes | Variable precision, inexact | 6 decimal digit precision |
| double precision | 8 bytes | Variable precision, inexact | 15 decimal digit precision |
| smallserial | 2 bytes | Auto-incrementing small-range integer | 1 to 32767 |
| serial | 4 bytes | Auto-incrementing integer | 1 to 2147483647 |
| bigserial | 8 bytes | Auto-incrementing large-range integer | 1 to 9223372036854775807 |
Currency Types
The money type stores monetary amounts with fixed decimal precision.
Values of types numeric, int, and bigint can be converted to money; it is not recommended to use floating-point numbers to handle currency types because of the possibility of rounding errors.
| Name | Storage Capacity | Description | Range |
|---|---|---|---|
| money | 8 bytes | Monetary amount | -92233720368547758.08 to +92233720368547758.07 |
Character type
The following table lists the character types supported by PostgreSQL:
| No. | Name & Description |
|---|---|
| 1 |
character varying(n), varchar(n) Variable length, with length limit |
| 2 |
character(n), char(n) f fixed length, padded with spaces when shorter |
| 3 |
text Variable length, no length limit |
Date/Time Types
The following table lists the date and time types supported by PostgreSQL.
| Name | Storage space | Description | Minimum Value | Maximum Value | Resolution |
|---|---|---|---|---|---|
| timestamp [ (p) ] [ without time zone ] | 8 bytes | Date and time (without time zone) | 4713 BC | 294276 AD | 1 millisecond / 14 digits |
| timestamp [ (p) ] with time zone | 8 bytes | Date and time, with time zone | 4713 BC | 294276 AD | 1 millisecond / 14 digits |
| date | 4 bytes | Date only | 4713 BC | 5874897 AD | 1 day |
| time [ (p) ] [ without time zone ] | 8 bytes | Time of day only | 00:00:00 | 24:00:00 | 1 millisecond / 14 digits |
| time [ (p) ] with time zone | 12 bytes | Time of day only, with time zone | 00:00:00+1459 | 24:00:00-1459 | 1 millisecond / 14 digits |
| interval [ fields ] [ (p) ] | 12 bytes | Time interval | -178000000 years | 178000000 years | 1 millisecond / 14 digits |
Boolean type
PostgreSQL supports the standard boolean data type.
boolean has two states, "true" (true) or "false" (false), and a third "unknown" (unknown) state, represented by NULL.
| Name | Storage Format | Description |
|---|---|---|
| boolean | 1 byte | true/false |
Enumeration Type
An enum type is a data type consisting of an ordered set of static values.
Enum types in PostgreSQL are similar to enum types in the C language.
Unlike other types, enum types need to be created using the CREATE TYPE command.
CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
Create the days of a week as follows:
CREATE TYPE week AS ENUM ('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun');
Like other types, once created, enum types can be used in table and function definitions.
CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
CREATE TABLE person (
name text,
current_mood mood
);
INSERT INTO person VALUES ('Moe', 'happy');
SELECT * FROM person WHERE current_mood = 'happy';
name | current_mood
------+--------------
Moe | happy
(1 row)
Geometric Types
Geometric data types represent two-dimensional planar objects.
The following table lists the geometric types supported by PostgreSQL.
The most basic type: point. It is the foundation of other types.
| Name | Storage space | Description | Representation |
|---|---|---|---|
| point | 16 bytes | A point in a plane | (x,y) |
| line | 32 bytes | (Infinite) line (not fully implemented) | ((x1,y1),(x2,y2)) |
| lseg | 32 bytes | (Finite) line segment | ((x1,y1),(x2,y2)) |
| box | 32 bytes | Rectangle | ((x1,y1),(x2,y2)) |
| path | 16+16n bytes | Closed path (similar to polygon) | ((x1,y1),...) |
| path | 16+16n bytes | Open path | [(x1,y1),...] |
| polygon | 40+16n bytes | Polygon (similar to closed path) | ((x1,y1),...) |
| circle | 24 bytes | Circle | <(x,y),r> (center and radius) |
Network Address Types
PostgreSQL provides data types for storing IPv4, IPv6, and MAC addresses.
Using these data types to store network addresses is better than using plain text types, because these types provide input error checking and special operations and functions.
| Name | Storage space | Description |
|---|---|---|
| cidr | 7 or 19 bytes | IPv4 or IPv6 network |
| inet | 7 or 19 bytes | IPv4 or IPv6 host and network |
| macaddr | 6 bytes | MAC address |
When sorting the inet or cidr data types, IPv4 addresses always come before IPv6 addresses, including IPv4 addresses encapsulated or mapped within IPv6 addresses, such as ::10.2.3.4 or ::ffff:10.4.3.2.
Bit String Types
A bit string is a string of 1s and 0s. They can be used to store and visualize bit masks. We have two SQL bit types: bit(n) and bit varying(n), where n is a positive integer.
Data of type bit must match the length n exactly; attempting to store data that is shorter or longer is an error. Data of type bit varying is a variable-length type with a maximum of n; longer strings are rejected. Writing bit without a length is equivalent to bit(1), while bit varying without a length means there is no length limit.
Text Search Types
Full text search is the retrieval that finds those matching a query through a collection of natural-language documents.
PostgreSQL provides two data types to support full text search:
| No. | Name & Description |
|---|---|
| 1 |
tsvector A tsvector value is a sorted list of distinct lexemes, which are the normalized forms of the different variants of the same word. |
| 2 |
tsquery A tsquery stores the words to be searched and uses the Boolean operators &(AND), |(OR), and !(NOT) to combine them; parentheses are used to emphasize the grouping of operators. |
UUID Type
The uuid data type is used to store Universally Unique Identifiers (UUID) as defined by RFC 4122, ISO/IEF 9834-8:2005, and related standards. (Some systems consider this data type a Globally Unique Identifier, or GUID.) This identifier is a 128-bit identifier generated by an algorithm, making it impossible for it to be identical to identifiers produced in other ways among modules known to use the same algorithm. Therefore, for distributed systems, such identifiers provide a better guarantee of uniqueness than sequences, which can only guarantee uniqueness within a single database.
A UUID is written as a sequence of lowercase hexadecimal digits, divided into groups by separator characters, specifically one group of 8 digits + 3 groups of 4 digits + one group of 12 digits, for a total of 32 digits representing 128 bits. An example of such a standard UUID is as follows:
a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11
XML Type
The xml data type can be used to store XML data. The advantage of storing XML data in the text type is that it can check input values for well-formedness, and it also supports functions to perform type-safe checks on them. To use this data type, you must compile with configure --with-libxml.
xml can store well-formed "documents" as defined by the XML standard, as well as "content" fragments defined by the XMLDecl? content XML standard. Roughly, this means a content fragment can have multiple top-level elements or character nodes. The xmlvalue IS DOCUMENT expression can be used to determine whether a particular xml value is a complete document or a content fragment.
Creating XML Values
Use the function xmlparse: to produce a value of type xml from character data:
XMLPARSE (DOCUMENT 'Manual ... ')
XMLPARSE (CONTENT 'abcbar foo ')
JSON Type
The json data type can be used to store JSON (JavaScript Object Notation) data. Such data can also be stored as text, but the json data type is better at checking that each stored value is a valid JSON value.
In addition, there are related functions for processing json data:
| Example | Example Result |
|---|---|
| array_to_json('{{1,5},{99,100}}'::int[]) | [[1,5],[99,100]] |
| row_to_json(row(1,'foo')) | {"f1":1,"f2":"foo"} |
Array Types
PostgreSQL allows a column to be defined as a variable-length multidimensional array.
An array type can be any base type or user-defined type, enum type, or composite type.
Declaring Arrays
When creating a table, we can declare arrays as follows:
CREATE TABLE sal_emp (
name text,
pay_by_quarter integer[],
schedule text[][]
);
pay_by_quarter is a one-dimensional integer array, and schedule is a two-dimensional text array.
We can also use the "ARRAY" keyword, as shown below:
CREATE TABLE sal_emp (
name text,
pay_by_quarter integer ARRAY[4],
schedule text[][]
);
Inserting Values
Values are inserted using curly braces {}, with elements separated by commas inside {}:
INSERT INTO sal_emp
VALUES ('Bill',
'{10000, 10000, 10000, 10000}',
'{{"meeting", "lunch"}, {"training", "presentation"}}');
INSERT INTO sal_emp
VALUES ('Carol',
'{20000, 25000, 25000, 25000}',
'{{"breakfast", "consulting"}, {"meeting", "lunch"}}');
Accessing Arrays
Now we can run some queries on this table.
First, we demonstrate how to access one element of an array. This query retrieves the names of employees whose salary changed in the second quarter:
SELECT name FROM sal_emp WHERE pay_by_quarter[1] <> pay_by_quarter[2];
name
-------
Carol
(1 row)
The subscript number of an array is written inside square brackets.
Modifying Arrays
We can modify the values of an array:
UPDATE sal_emp SET pay_by_quarter = '{25000,25000,27000,27000}'
WHERE name = 'Carol';
Or use the ARRAY constructor syntax:
UPDATE sal_emp SET pay_by_quarter = ARRAY[25000,25000,27000,27000]
WHERE name = 'Carol';
Searching in Arrays
To search for a value in an array, you must check every value of the array.
For example:
SELECT * FROM sal_emp WHERE pay_by_quarter[1] = 10000 OR
pay_by_quarter[2] = 10000 OR
pay_by_quarter[3] = 10000 OR
pay_by_quarter[4] = 10000;
Alternatively, you can use the following statement to find rows where all element values in the array equal 10000:
SELECT * FROM sal_emp WHERE 10000 = ALL (pay_by_quarter);
Alternatively, you can use the generate_subscripts function. For example:
SELECT * FROM
(SELECT pay_by_quarter,
generate_subscripts(pay_by_quarter, 1) AS s
FROM sal_emp) AS foo
WHERE pay_by_quarter[s] = 10000;
Composite types
A composite type represents the structure of a row or record; it is essentially just a list of field names and their data types. PostgreSQL allows composite types to be used like simple data types. For example, a column of a table can be declared as a composite type.
Declaring Composite Types
Here are two simple examples of defining composite types:
CREATE TYPE complex AS (
r double precision,
i double precision
);
CREATE TYPE inventory_item AS (
name text,
supplier_id integer,
price numeric
);
The syntax is similar to CREATE TABLE, except that here only field names and types can be declared.
Once the type is defined, we can use it to create a table:
CREATE TABLE on_hand (
item inventory_item,
count integer
);
INSERT INTO on_hand VALUES (ROW('fuzzy dice', 42, 1.99), 1000);
Composite Type Value Input
To write a composite type value as a text constant, enclose the field values in parentheses and separate them with commas. You can put double quotes around any field value, and if the value itself contains a comma or parentheses, you must enclose it in double quotes.
The general format of a composite type constant is as follows:
'( val1 , val2 , ... )'
An example is:
'("fuzzy dice",42,1.99)'
Accessing Composite Types
To access a field of a composite type, we write a dot followed by the field name, much like selecting a field from a table name. In fact, because it is so similar to selecting a field from a table name, we often need to use parentheses to avoid confusing the parser. For example, you might need to select some subfields from the on_hand example table, like this:
SELECT item.name FROM on_hand WHERE item.price > 9.99;
This will not work, because according to SQL syntax, item is taken as a table name, not a field name. You must write it like this:
SELECT (item).name FROM on_hand WHERE (item).price > 9.99;
Or if you also need to use the table name (for example, in a multi-table query), write it like this:
SELECT (on_hand.item).name FROM on_hand WHERE (on_hand.item).price > 9.99;
Now the parenthesized object is correctly parsed as a reference to the item field, and subfields can be selected from it.
Range Types
A range data type represents values of a certain element type over a range.
For example, a timestamp range might be used to represent the time range during which a meeting room is booked.
The built-in range types in PostgreSQL are:
-
int4range — range of integer
-
int8range — range of bigint
-
numrange — range of numeric
-
tsrange — range of timestamp without time zone
-
tstzrange — range of timestamp with time zone
-
daterange — range of date
In addition, you can define your own range types.
CREATE TABLE reservation (room int, during tsrange);
INSERT INTO reservation VALUES
(1108, '[2010-01-01 14:30, 2010-01-01 15:30)');
-- 包含
SELECT int4range(10, 20) @> 3;
-- 重叠
SELECT numrange(11.1, 22.2) && numrange(20.0, 30.0);
-- Extract upper boundary
SELECT upper(int8range(15, 25));
-- Calculate intersection
SELECT int4range(10, 20) * int4range(15, 25);
-- Is the range empty
SELECT isempty(numrange(1, 5));
The input of a range value must follow the format below:
(下边界,上边界)
(下边界,上边界]
[下边界,上边界)
[下边界,上边界]
空
Parentheses or brackets indicate whether the lower and upper bounds are exclusive or inclusive. Note that the last form is empty, representing an empty range (a range containing no values).
-- 包括3,不包括7,并且包括二者之间的所有点
SELECT '[3,7)'::int4range;
-- excluding 3 and 7, but including all points between them
SELECT '(3,7)'::int4range;
-- only include single value 4
SELECT '[4,4]'::int4range;
-- Does not include dots (standardized to 'empty')
SELECT '[4,4)'::int4range;
Object Identifier Types
PostgreSQL uses Object Identifiers (OID) internally as the primary keys of various system tables.
At the same time, the system does not add an OID system column to user-created tables (unless WITH OIDS is declared at table creation time or the configuration parameter default_with_oids is set to on). The oid type represents an object identifier. In addition, oid has several aliases: regproc, regprocedure, regoper, regoperator, regclass, regtype, regconfig, and regdictionary.
| Name | reference | Description | Numeric Example |
|---|---|---|---|
| oid | Any | Numeric object identifier | 564182 |
| regproc | pg_proc | Function name | sum |
| regprocedure | pg_proc | Function with argument types | sum(int4) |
| regoper | pg_operator | Operator name | + |
| regoperator | pg_operator | Operator with argument types | *(integer,integer) or -(NONE,integer) |
| regclass | pg_class | Relation name | pg_type |
| regtype | pg_type | Data type name | integer |
| regconfig | pg_ts_config | Text search configuration | english |
| regdictionary | pg_ts_dict | Text search dictionary | simple |
Pseudo-type
The PostgreSQL type system contains a number of special-purpose entries, which are categorized as pseudo-types. A pseudo-type cannot be used as a column data type, but it can be used to declare the argument or result type of a function. Pseudo-types are useful when a function does not simply accept and return some SQL data type.
The following table lists all the pseudo-types:
| Name | Description |
|---|---|
| any | Indicates that a function accepts any input data type. |
| anyelement | Indicates that a function accepts any data type. |
| anyarray | Indicates that a function accepts any array data type. |
| anynonarray | Indicates that a function accepts any non-array data type. |
| anyenum | Indicates that a function accepts any enum data type. |
| anyrange | Indicates that a function accepts any range data type. |
| cstring | Indicates that a function accepts or returns a null-terminated C string. |
| internal | Indicates that a function accepts or returns a server-internal data type. |
| language_handler | A procedural language call handler is declared to return language_handler. |
| fdw_handler | A foreign data wrapper is declared to return fdw_handler. |
| record | Identifies a function that returns an undeclared row type. |
| trigger | A trigger function is declared to return trigger. |
| void | Indicates that a function returns no value. |
| opaque | An obsolete type that was formerly used for all of the above purposes. |
For more information, refer to:PostgreSQL Data Types