Common data types

更新时间:
复制 MD 格式

PolarDB for PostgreSQL (Compatible with Oracle) supports a broad set of data types spanning numeric, string, date/time, JSON, XML, geometric, network, and full-text search categories. Many Oracle type names are accepted directly, so existing DDL often requires little or no change.

Common data types

Different type names can share the same storage structure and processing logic. PolarDB groups these under a Display type — the canonical name used internally. Understanding this grouping helps you recognize which Oracle type names map to which PostgreSQL-native types.

Type nameDisplay typeDescription
boolean, boolbooleanLogical Boolean (true/false)
serial2smallintAuto-incrementing 2-byte integer
serial, serial4integerAuto-incrementing 4-byte integer
bigserialbigintAuto-incrementing 8-byte integer
smallint, int2int2Signed 2-byte integer; range −32,768 to 32,767
integer, binary_integer, int, int4integerSigned 4-byte integer; range −2,147,483,648 to 2,147,483,647
bigint, int8int8Signed 8-byte integer; range −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
binary_float, float4, realrealSingle-precision floating-point; 4 bytes; ~6 decimal digits of precision
binary_double, float8, double precisiondouble precisionDouble-precision floating-point; 8 bytes; ~15 decimal digits of precision
decimal [ (p,s) ], number [ (p,s) ], numeric [ (p,s) ]numeric [ (p,s) ]Exact numeric with user-specified precision and scale
raw, blob, byteabyteaBinary data (byte array)
bit [ (n) ]bit [ (n) ]Fixed-length bit string
bit varying [ (n) ], varbit [ (n) ]bit varying [ (n) ]Variable-length bit string
clob, string, long, nclob, texttextVariable-length string with no length limit
character [ (n) ], nchar [ (n) ], char [ (n) ]character [ (n) ]Fixed-length string padded with spaces to length n
character varying [ (n) ], varchar2 [ (n) ], nvarchar2 [ (n) ], varchar [ (n) ]character varying [ (n) ]Variable-length string; stores only the actual content
datedateDate and time (year, month, day, hour, minute, second)
time [ (p) ] [ without time zone ]time [ (p) ] [ without time zone ]Time of day without time zone
time [ (p) ] with time zonetime [ (p) ] with time zoneTime of day with time zone
timestamp [ (p) ] [ without time zone ]timestamp [ (p) ] [ without time zone ]Date and time without time zone
timestamp [ (p) ] with time zone, timestamp [ (p) ] with local time zonetimestamp [ (p) ] with time zone, timestamp [ (p) ] with local time zoneDate and time with time zone
interval [ fields ] [ (p) ]interval [ fields ] [ (p) ]Time span
jsonjsonTextual JSON data
jsonbjsonbBinary JSON data (pre-parsed); supports indexing
jsonpathjsonpathPath expression for querying JSON data
json_element_tjson_element_tJSON base class compatible with Oracle syntax
json_array_tjson_array_tJSON array compatible with Oracle syntax
json_object_tjson_object_tJSON object compatible with Oracle syntax
json_key_listjson_key_listVariable-length array of type VARCHAR2 for storing JSON object key names
json_nkey_listjson_nkey_listVariable-length array of type NVARCHAR2 for storing JSON object key names
boxboxRectangle on a plane
cidrcidrIPv4 or IPv6 network address
circlecircleCircle on a plane
rowidrowidRow identifier type
urowidurowidUniversal row identifier type
xmlxmlXML data
xmltypexmltypeXML data compatible with Oracle syntax
xmlsequencetypexmlsequencetypeVariable-length array of xmltype; return value of the xmlsequence function
inetinetIPv4 or IPv6 host address
linelineInfinite line on a plane
lseglsegLine segment on a plane
macaddrmacaddrMAC (Media Access Control) address
macaddr8macaddr8MAC address in EUI-64 format
moneymoneyMonetary amount
pathpathGeometric path on a plane
pg_lsnpg_lsnPostgreSQL log sequence number
pg_snapshotpg_snapshotTransaction ID snapshot
txid_snapshottxid_snapshotTransaction ID snapshot — deprecated; use pg_snapshot instead
pointpointGeometric point on a plane
polygonpolygonClosed geometric path on a plane
tsquerytsqueryFull-text search query type
tsvectortsvectorFull-text search document type
uuiduuidUniversally Unique Identifier

Oracle-to-PolarDB type mapping

Oracle type names that appear in the Type name column above are accepted directly. The following DDL example shows how common Oracle column types map when used in PolarDB.

-- Oracle DDL (source)
CREATE TABLE sales (
  id            NUMBER(10),
  amount        BINARY_DOUBLE,
  payload       BLOB,
  description   CLOB,
  created_at    DATE,
  label         NVARCHAR2(100),
  raw_data      RAW(200),
  doc           XMLTYPE
);

-- Equivalent PolarDB DDL
-- Oracle aliases resolve to the display types shown in the table above
CREATE TABLE sales (
  id            numeric(10),             -- NUMBER    -> numeric
  amount        double precision,        -- BINARY_DOUBLE -> double precision
  payload       bytea,                   -- BLOB      -> bytea
  description   text,                    -- CLOB      -> text
  created_at    date,                    -- DATE      -> date (includes time component)
  label         character varying(100),  -- NVARCHAR2 -> character varying
  raw_data      bytea,                   -- RAW       -> bytea
  doc           xmltype                  -- XMLTYPE   -> xmltype (Oracle-compatible)
);

The Oracle DATE type stores date and time down to the second. In PolarDB this maps to the date display type, which retains the same behavior. If you need sub-second precision, use timestamp.

Selection guide

json vs jsonb

Use jsonb for most workloads. It stores data in an optimized binary format, supports indexing, and delivers far better query performance than json. The json type stores data as plain text, which allows for fast insertions, making it suitable for audit logging or cases where exact text fidelity matters.

Numeric precision

When using numeric or decimal, always specify precision and scale to control storage size and accuracy. Example: numeric(10, 2).

String types

character(n) (and its alias char(n)) pads values with spaces to length n, which can cause unexpected comparison behavior. Prefer varchar or text unless you have a specific fixed-length requirement.

Integer types

Choose the smallest type that fits your data range:

  • smallint — −32,768 to 32,767; use for compact lookup tables or flags

  • integer — −2,147,483,648 to 2,147,483,647; the default choice for most integer columns

  • bigint — up to ~9.2 × 10¹⁸; use for counters, user IDs, or any column that may exceed two billion rows

For auto-incrementing primary keys, use serial (integer-sized) or bigserial (bigint-sized).

FAQ

What is the difference between varchar(n) and text, and which should I use?

There is no performance difference between varchar(n) and text in PolarDB for PostgreSQL (Compatible with Oracle). The only difference is that varchar(n) rejects values longer than n characters at insert time, while text has no length limit. If your schema does not need a hard length constraint, use text — it simplifies schema evolution and avoids silent truncation surprises.

How do I choose the right integer type?

Start with integer. It covers the vast majority of use cases (up to about ±2.1 billion). Switch to bigint when you expect values to exceed that range, such as global counters or row IDs in high-volume tables. Use smallint only when storage compactness is critical and the range −32,768 to 32,767 is sufficient.

For auto-incrementing IDs, use serial (integer range) or bigserial (bigint range).