Data Types
TacNode is a PostgreSQL-compatible distributed database that supports a rich set of data types, including basic data types, composite types, and special types, suitable for large-scale data storage and high-concurrency query scenarios.
This document introduces the basic data types and composite data types supported by TacNode, including value range specifications for each type.
Basic Data Types
Numeric Types
Type | Description | Range | Example |
---|---|---|---|
SMALLINT | 2-byte signed integer | -32,768 to 32,767 | SMALLINT '32767' |
INT / INTEGER | 4-byte signed integer | -2,147,483,648 to 2,147,483,647 | INT '2147483647' |
BIGINT | 8-byte signed integer | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | BIGINT '9223372036854775807' |
DECIMAL(p,s) | Exact decimal, p is precision, s is scale | Max precision 1000, variable storage | DECIMAL(10,2) '1234.56' |
NUMERIC | Same as DECIMAL | Same as DECIMAL | NUMERIC(5,3) '12.345' |
REAL | 4-byte single precision floating point | Approx ±1.18e-38 to ±3.40e38, 6 decimal precision | REAL '3.4028235e38' |
FLOAT / DOUBLE | 8-byte double precision floating point | Approx ±2.23e-308 to ±1.79e308, 15 decimal precision | FLOAT '1.7976931348623157e308' |
String Types
Type | Description | Range | Example |
---|---|---|---|
CHAR(n) | Fixed-length string, padded with spaces | Max length 1,000,000 characters | CHAR(10) 'hello' |
VARCHAR(n) | Variable-length string, max length n | Max length 1,000,000 characters | VARCHAR(255) 'world' |
TEXT | Unlimited text | Theoretically unlimited (limited by storage) | TEXT 'This is a long text...' |
Date and Time Types
Type | Description | Range | Example |
---|---|---|---|
DATE | Date (year, month, day) | 4713 BC to 294276 AD | DATE '2023-10-01' |
TIME | Time (hour, minute, second) | 00:00:00 to 24:00:00 | TIME '14:30:00' |
TIMESTAMP | Date and time (without timezone) | 4713 BC to 294276 AD, microsecond precision | TIMESTAMP '2023-10-01 14:30:00' |
TIMESTAMPTZ | Timestamp with timezone | Same as TIMESTAMP, but stores timezone info | TIMESTAMPTZ '2023-10-01 14:30:00+08' |
INTERVAL | Time interval | -178,000,000 years to 178,000,000 years | INTERVAL '1 day 2 hours' |
Boolean Type
Type | Description | Range | Example |
---|---|---|---|
BOOLEAN | True/false value | true/false or 1/0 | BOOLEAN 'true' |
Binary Type
Type | Description | Range | Example |
---|---|---|---|
BYTEA | Binary data | Max 1 GB (limited by storage) | BYTEA '\xDEADBEEF' |
Composite Data Types
Array Types
Supports storing multiple values of the same type, maximum 6 dimensions, single element size limited by TOAST storage (typically 1 GB).
Example:
JSON / JSONB
Type | Description | Range |
---|---|---|
JSON | Text format, preserves whitespace and order | Same as TEXT (theoretically unlimited) |
JSONB | Binary format, supports indexing and efficient queries | Same as TEXT (limited by storage) |
Example:
Special Data Types
UUID
Stores 128-bit globally unique identifier, format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
.
Example:
Enum Type (ENUM)
Defines fixed-value string enumerations, supports up to 1,000 enumeration values.
Example:
Type Conversion
TacNode supports data type conversion during queries and data operations, including both implicit and explicit conversion methods.
Implicit Type Conversion
In certain operations, TacNode automatically performs type conversion to ensure correct execution. For example, when comparing different numeric types, the system automatically converts them to the same type.
Example:
Explicit Type Conversion
Users can explicitly perform type conversion using the CAST
function or ::
operator.
Using CAST Function
Using :: Operator
Type Conversion Rules
- Numeric type conversion: Smaller range numeric types can be implicitly converted to larger range numeric types (e.g., INT to BIGINT), but the reverse may require explicit conversion and may lose precision.
- String and numeric conversion: Strings can be converted to numeric types, provided the string content conforms to numeric format.
- Date/time conversion: Strings can be converted to date/time types, must conform to standard date/time formats.
- JSON type conversion: JSON/JSONB types can be converted to and from string types.
- Timezone conversion:
TIMESTAMP
andTIMESTAMPTZ
can be converted between each other, but timezone information handling must be noted.
Important Notes
- Type conversion may result in data precision loss or conversion failure
- When performing type conversion, ensure source data format is compatible with target type
- Explicit type conversion is safer than implicit conversion; explicit conversion is recommended in critical business logic
- When converting timezones, pay attention to timezone information correctness to avoid time deviation due to improper timezone handling
Handling Conversion Failures
When performing type conversion, if the source data format is incompatible with the target type, conversion will fail and throw an error. Here are common conversion failure scenarios and handling methods:
Common Conversion Failure Scenarios
- Invalid numeric format conversion
- Out-of-range numeric conversion
- Invalid date/time format conversion
- JSON format error conversion
- Invalid timezone conversion
Error Handling Recommendations
-
Use conditional expressions for pre-checking: Before performing type conversion, first use conditional expressions to verify if data format meets requirements.
-
Use TRY_CAST function: The
TRY_CAST
function returnsNULL
instead of throwing an error when conversion fails.
- Use regex for format validation: For string to numeric or date conversions, you can first use regex to validate format.
- Exception handling: Handle type conversion exceptions at the application level, providing friendly error messages and alternative solutions.