Scrivio Interview library
ConceptFoundations7 min read · updated 12 November 2026

Choose column types for a payments table in PostgreSQL: amount, currency, created time, external reference, status. Explain why you avoid float for money and timestamp without time zone for events.

I would store amount as numeric(19,4) or as a bigint of minor units such as cents, currency as a three-letter ISO 4217 code with a CHECK or a reference table, created_at as timestamptz, external_ref as text with a UNIQUE constraint, and status as text with a CHECK or a lookup table. Float is avoided because it is binary and cannot represent 0.10 exactly, so sums drift, and timestamp without time zone is avoided because it drops the offset, so the same value means different instants in different regions and across daylight saving changes.

Why this question is asked

Column types feel like a detail when we create a table, and they become permanent the moment the table holds a few hundred million payments. A wrong type for money produces reconciliation differences of a few cents that finance chases for weeks, and a wrong type for time produces events that seem to happen an hour before they were caused. Interviewers ask this because it shows whether we understand how numbers and instants are represented, not only which keyword to type. In this answer, we will go through each column of the payments table, see exactly why float breaks for money and why timestamptz is the right choice for events, and then map the types to Java.

1The Table We Are Designing

Here is the complete table first, so we can then justify each choice. A payment has an amount, a currency, the moment it was created, the reference our payment provider gave it, and a status.

SQL
CREATE TABLE payments (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
amount numeric(19,4) NOT NULL CHECK (amount >= 0),
currency char(3) NOT NULL CHECK (currency ~ '^[A-Z]{3}$'),
created_at timestamptz NOT NULL DEFAULT now(),
external_ref text NOT NULL UNIQUE,
status text NOT NULL
CHECK (status IN ('PENDING', 'AUTHORIZED', 'CAPTURED', 'FAILED', 'REFUNDED'))
);
Column choices for payments
ColumnTypeAvoidReason
amountnumeric(19,4) or bigint minor unitsreal, double precisionexact decimal arithmetic
currencychar(3) + CHECK or FKfree textISO 4217 codes, validated
created_attimestamptztimestampstores an instant, not a wall-clock time
external_reftext + UNIQUEno constraintone row per provider transaction
statustext + CHECK or lookup tablenative enumeasy to extend later

Every column is NOT NULL because a payment without any of these facts makes no sense. Let us look at the reasons column by column.

2Why Float Breaks for Money

The types real and double precision, and Java's float and double, store numbers in binary, as a sum of powers of two. Many decimal fractions, including 0.10, have no exact binary form, just as 1/3 has no exact decimal form. The stored value is a very close approximation, and the error shows up as soon as we add values together.

SQL
SELECT 0.1::float8 + 0.2::float8; -- 0.30000000000000004
SELECT 0.1::numeric + 0.2::numeric; -- 0.3
SELECT sum(x) FROM (SELECT 0.10::float8 AS x FROM generate_series(1, 1000000)) t;
-- slightly more than 100000 (one million payments of 0.10; the exact digits vary)
SELECT sum(x) FROM (SELECT 0.10::numeric AS x FROM generate_series(1, 1000000)) t;
-- 100000.00

The error per payment is tiny, but it accumulates, and finance does not accept totals that are almost right. numeric stores decimal digits exactly, so the sum of a million payments of 0.10 is exactly 100,000.00. numeric(19,4) gives 15 digits before the point and 4 after, which covers very large amounts and currencies or prices that need more than two decimals. The alternative is a bigint of minor units, such as 1050 for 10.50 EUR; it is fast and exact, but the application must know how many decimals each currency has, since JPY has 0 and KWD has 3.

3Currency, External Reference and Status

A currency is an ISO 4217 code, three uppercase letters such as EUR or USD. char(3) or text with a CHECK keeps garbage out, and a currencies reference table with a foreign key goes further: it rejects codes that look valid but do not exist and can hold the number of minor units per currency. The amount is meaningless without its currency, so the two always travel together.

external_ref is the identifier the payment provider gave us. text is the natural type in PostgreSQL, where text and varchar have the same performance, and varchar(n) is fine if we want a length limit. The important part is the UNIQUE constraint: if the provider sends the same callback twice, the second insert fails instead of creating a duplicate payment, which makes the processing idempotent.

For status, a text column with a CHECK constraint lists the allowed values and is easy to change: we drop and recreate the constraint to add a new status. PostgreSQL native enums are compact and type-safe, but removing or renaming a value is awkward, and in MySQL changing an ENUM column definition can rebuild the table. A lookup table with a foreign key is the most flexible option when statuses have descriptions or are managed as data.

4Why timestamptz for Events

An event such as a payment happens at one instant in time, the same instant everywhere on Earth. timestamptz stores exactly that: PostgreSQL converts the input to UTC and stores it, and converts it back to the session's time zone only when displaying it. timestamp without time zone stores a wall-clock reading, like a photo of a clock with no label saying which city the clock was in.

SQL
SET TIME ZONE 'Europe/Berlin';
SELECT '2026-03-29 02:30'::timestamptz; -- 2026-03-29 03:30:00+02 (02:30 does not exist that night)
SELECT '2026-11-05 10:00+00'::timestamptz; -- 2026-11-05 11:00:00+01
-- Two services write "the same" local value
INSERT INTO events_bad (at) VALUES ('2026-11-05 10:00'); -- service in Berlin means 09:00 UTC
INSERT INTO events_bad (at) VALUES ('2026-11-05 10:00'); -- service in New York means 15:00 UTC
-- Stored values are identical; the six-hour difference is lost forever

With timestamp without time zone, the meaning of a value depends on whoever wrote it, which breaks when services run in different regions or when servers change their time zone. Daylight saving makes it worse: in autumn, the hour from 02:00 to 03:00 happens twice in Berlin, so two payments an hour apart can be stored with the same local value and sort in the wrong order. timestamptz avoids all of this because it never forgets the instant. Plain timestamp still has uses, for wall-clock concepts such as a store's opening time, but not for events.

5Mapping the Types to Java

The Java side should keep the same guarantees. numeric maps to BigDecimal, never to double. timestamptz maps best to OffsetDateTime or Instant; JDBC 4.2 drivers support OffsetDateTime directly, and Hibernate 6 maps Instant as well.

Java
@Entity
public class Payment {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) Long id;
@Column(precision = 19, scale = 4) BigDecimal amount; // exact
@Column(length = 3) String currency; // or a Currency with a converter
Instant createdAt; // an instant, like timestamptz
String externalRef;
@Enumerated(EnumType.STRING) PaymentStatus status; // stored as text, matches the CHECK
}
new BigDecimal("0.10").add(new BigDecimal("0.20")); // 0.30
0.10 + 0.20; // 0.30000000000000004

Notice that we construct BigDecimal from a string. new BigDecimal(0.1) takes the double's binary approximation and gives 0.1000000000000000055511151231257827..., so the precision is lost before we start. EnumType.STRING stores the enum name, which matches the text column; the default ORDINAL stores a position number that breaks when someone reorders the enum.

6Follow-up questions

What Java types map to numeric and timestamptz in JDBC and JPA?

numeric maps to java.math.BigDecimal, read with ResultSet.getBigDecimal and mapped automatically by JPA for BigDecimal fields with precision and scale. timestamptz maps to java.time.OffsetDateTime through ResultSet.getObject(col, OffsetDateTime.class); the PostgreSQL driver returns it in UTC. In JPA with Hibernate 6, Instant and OffsetDateTime fields both map to timestamp with time zone, and setting hibernate.jdbc.time_zone or the default UTC handling keeps conversions consistent. I would avoid java.util.Date and LocalDateTime for events, because LocalDateTime, like timestamp without time zone, has no offset.

Why not the PostgreSQL money type?

money stores a fixed number of decimals determined by the lc_monetary setting of the database, and its formatting and parsing depend on that setting too, so one value can mean different amounts across servers with different locales. It also holds no currency. numeric plus a currency column is clearer and portable.

7Conclusion

In this answer, we designed a payments table with numeric or minor units for amount, a validated ISO 4217 currency, timestamptz for created_at, a unique external_ref and a constrained text status. We saw that float cannot represent decimal fractions such as 0.10 exactly, so sums drift, and that timestamp without time zone forgets which instant it meant, which breaks across regions and daylight saving, and we mapped the types to BigDecimal and Instant or OffsetDateTime in Java.

What to say in the interviewAbout 80 seconds aloud

Open with

For amount I'd use numeric(19,4), or a bigint of minor units like cents if the team prefers integers, but never float or double. Those are binary floating point types and can't represent 0.10 exactly, so 0.1 plus 0.2 gives 0.30000000000000004 and sums of millions of payments drift by visible amounts. numeric stores decimal digits exactly.

Then, if they want more

Currency is a three-letter ISO 4217 code, char(3) or text with a CHECK, or better a foreign key to a currencies table that also stores minor units. created_at is timestamptz, which stores an instant in UTC and converts only on display. timestamp without time zone stores a wall-clock value with no offset, so the same value written by services in Berlin and New York means different instants, and during the autumn daylight saving change one local hour occurs twice. external_ref is text with a UNIQUE constraint, so a duplicate provider callback can't create a second payment.

Status is text with a CHECK constraint or a lookup table, because native enums are harder to change. In Java I'd map amount to BigDecimal built from strings, created_at to Instant or OffsetDateTime, and status with EnumType.STRING.

About this answer

Last updated
12 November 2026
Versions it names
Hibernate 6
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Choose column types for a payments table in PostgreSQL: amount, currency, created time...": https://scrivio-interview-library.netlify.app/databases/sql-fundamentals/59359f2950/