Sub topic 3
What type is a literal?
This chapter assumes you can run SELECT queries in psql, know the basic types (integer, numeric, text, boolean, date), and have read the previous chapter on CAST, :: and typed literals. It covers how PostgreSQL assigns a type to a value you write directly in a query. That decision affects which operator runs, whether an error occurs, and whether an index is usable.
Literals are typed by their shape
A literal (or constant) is a value written directly in SQL text. The parser decides its initial type from how it is written, before it looks at the rest of the expression. Use pg_typeof to see the result:
SELECT pg_typeof(42) AS int_lit,
pg_typeof(3000000000) AS big_lit,
pg_typeof(12345678901234567890) AS huge_lit,
pg_typeof(1.5) AS dec_lit,
pg_typeof(1e3) AS exp_lit,
pg_typeof(TRUE) AS bool_lit;The result is integer, bigint, numeric, numeric, numeric, boolean.
| Literal | Initial type | Rule |
|---|---|---|
| `42` | integer | Whole number that fits in 32 bits |
| `3000000000` | bigint | Too large for integer, fits in 64 bits |
| `12345678901234567890` | numeric | Too large for bigint |
| `1.5`, `1e3` | numeric | Has a decimal point or exponent |
| `TRUE`, `FALSE` | boolean | Keywords |
| `'abc'` | unknown | Quoted string with no type attached |
| `NULL` | unknown | No value, no type until context supplies one |
| `DATE '2026-11-27'` | date | Typed literal: the type name comes first |
A decimal literal such as 1.5 is numeric, not double precision. Exact decimal arithmetic is the default. You only get floating point if you ask for it, for example with 1.5::float8.
The unknown type
A quoted string like '42' is not text. It is a literal of the pseudo-type unknown. This means "the type is not decided yet, and the surrounding expression will decide it." The parser delays the choice so one literal can become an integer, a date, a text value or a JSON document depending on where it sits.
SELECT 1 + '2' AS ok;
SELECT 1 + '2'::text AS fails;In the first query, 1 is an integer and '2' is unknown. The parser looks for an operator + where one side is integer. It assumes the unknown side has the same type as the known side, picks integer + integer, and passes '2' to the integer input function. The result is 3.
In the second query, ::text gives the literal a definite type. Now the operands are integer and text, and no + operator accepts that pair. PostgreSQL reports operator does not exist: integer + text. Adding a cast made the query fail because it removed the flexibility of unknown.
How the type gets decided
Common situations where context supplies the type:
- Operators. With
int_col = '5', the literal becomes an integer. Withdate_col > '2026-11-27', it becomes a date. - INSERT and UPDATE targets. In
INSERT INTO t(d) VALUES ('2026-11-27'), the target column's type is used. - Function arguments. If only one function signature fits, the literal is converted to its parameter type.
- Prepared statement parameters. In
PREPARE p AS SELECT $1 + 1;, the parameter$1is inferred as integer because of the+ 1.EXECUTE p('41')then returns42.
When nothing in the context fixes the type, PostgreSQL falls back. In a SELECT output list, an unknown literal is resolved as text (PostgreSQL 10 and later). Elsewhere it may stay ambiguous:
SELECT '1' + '2';This fails with operator is not unique: unknown + unknown. Both sides are open and many operators fit, such as integer, numeric and date-plus-interval variants. The parser has nothing to choose with. Add a cast to one side, such as '1'::int + '2', and the query works.
Typed literals and where they help
You can attach a type to a literal in three ways, all covered in the previous chapter: CAST('2026-11-27' AS date), '2026-11-27'::date, and DATE '2026-11-27'. The typed-literal form is useful for types whose names are a single word, because it reads like a constant. Typed literals work with a quoted string only, so DATE some_column is a syntax error.
Note one related trap in the other direction. Giving a literal a type too early can cost you an implicit conversion later:
SELECT pg_typeof(1 + 1.5) AS int_plus_numeric,
pg_typeof(1 + 1.5e0) AS also_numeric;Both are numeric. When integer meets numeric, PostgreSQL promotes the integer, so the result type follows the "wider" operand, not the literal you meant as an integer.
Numeric literals and columns
Because numeric literals have a definite type, a comparison with a column can involve a conversion on the column side. Suppose id is an integer column:
CREATE TABLE lit_demo (id integer PRIMARY KEY, label text);
EXPLAIN SELECT * FROM lit_demo WHERE id = 5;
EXPLAIN SELECT * FROM lit_demo WHERE id = 5.0;The first comparison uses integer = integer. The second has a numeric literal, so id must be converted to numeric before comparing, and the planner cannot use the primary key index for that condition in the usual way. The same applies to a text-typed expression compared with an integer column. Leave literals untyped ('5') or write them in the column's type (5), and the index stays usable. The chapter on casts and index usage returns to this.
Quoted strings that are not plain
A few string-literal forms also affect typing and parsing:
E'line1\nline2'is an escape-string literal. Backslash escapes are interpreted. It is stillunknownuntil context resolves it.$$some text$$is dollar-quoted. It follows the same typing rules as a normal quoted string.B'101'andX'1F'are bit-string literals and have typebit, notunknown.'{1,2,3}'is an unknown literal that becomes an array only if context calls for one, such as'{1,2,3}'::int[].
Checking what the parser decided
Two habits make literal typing visible:
- Wrap an expression in
pg_typeof(...). For a value coming from a column, a function, or a cast, it shows the resolved type. - Read the error message.
operator does not exist: integer + textpoints to a definite type conflict.operator is not unique: unknown + unknownpoints to missing context.
For code that goes through a driver with bound parameters, remember that the server infers parameter types in the same way as for literals. If the driver sends text parameters with no declared type, they behave like unknown literals.
Exercise
Predict the result of each statement and explain why: (a) SELECT 10 + '5'; (b) SELECT 10 + '5'::varchar; (c) SELECT '10' + '5'; (d) SELECT pg_typeof(2147483648);
Show answer
(a) Returns 15. The quoted '5' is unknown, so the parser assumes integer to match the left operand. (b) Fails with "operator does not exist: integer + character varying", because the cast gave the literal a definite type that has no matching operator. (c) Fails with "operator is not unique: unknown + unknown", because neither side gives the parser a type to choose with. (d) Returns bigint, because 2147483648 exceeds the 32-bit integer range but fits in 64 bits.
Exercise
A table has an integer column id with an index. Why might WHERE id = 5.0 be slower than WHERE id = 5, and how do you fix it?
Show answer
The literal 5.0 is numeric, so the integer column must be converted to numeric for the comparison, and a plain index on id cannot be used for that condition. Write the literal as 5 (an integer), or leave it as the quoted string '5', so the comparison stays integer = integer.