Verdict: Request changes. list.go does not compile as pasted, and the cursor query loses its keyset benefit once Postgres switches to a generic plan. Both need fixing before merge, and the migration needs a check against your migration runner.
Findings
1. [High] internal/bookings/list.go imports: fmt is used but not imported, so the build fails
List calls fmt.Errorf three times, but the import block in the diff has no fmt. The package will not compile and nothing ships. You say CI passed, which suggests the diff you pasted is not what CI ran. Confirm that before trusting the test results.
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"time"
...
Needs check: the hunk for internal/api/bookings.go doesn't show its imports. It now needs errors and strconv too.
2. [Medium] List SQL: the $2::boolean = false OR ... predicate stops the index from seeking to the cursor
- pgx's default exec mode uses cached prepared statements.
- After 5 executions, Postgres may switch to a generic plan.
- In a generic plan,
$2 = false OR (starts_at, id) > ($3, $4) cannot be folded away. It therefore can't be an index condition and becomes a filter. - Each page then walks the operator's index entries from the oldest booking and fetches heap rows until it passes the cursor.
- Late pages for Flimbra read ~41k rows, which is the slowdown this change is meant to remove, and it grows every season.
Fix: use two static queries so the row comparison is always a plain index condition.
const listSQL = `SELECT id, operator_id, vessel_id, starts_at, ends_at, party_size, status
FROM bookings WHERE operator_id = $1 %s ORDER BY starts_at, id LIMIT $2`
var (
firstPageSQL = fmt.Sprintf(listSQL, "")
nextPageSQL = fmt.Sprintf(listSQL, "AND (starts_at, id) > ($3::timestamptz, $4::bigint)")
)
var rows pgx.Rows
if hasCursor {
rows, err = db.Query(ctx, nextPageSQL, operatorID, limit+1, c.StartsAt, c.ID)
} else {
rows, err = db.Query(ctx, firstPageSQL, operatorID, limit+1)
}
Verify with EXPLAIN (ANALYZE) EXECUTE after 6+ runs, or with SET plan_cache_mode = force_generic_plan. You should see Index Cond: (operator_id = ...) AND (ROW(starts_at, id) > ...).
3. [Medium] migrations/0019: CREATE INDEX CONCURRENTLY fails inside a transaction, and a failed build is silently kept (Needs check)
- Many runners wrap each migration in a transaction. goose does this unless the file has
-- +goose NO TRANSACTION; some runners always do. In that case this fails with CREATE INDEX CONCURRENTLY cannot run inside a transaction block and the deploy stops. - A concurrent build can also fail partway, for example on a deadlock or a timeout. That leaves an
INVALID index behind. On retry, IF NOT EXISTS sees the name and skips, so you end up with an index that is never used and no error.
Fix:
- Mark the file as non-transactional for your runner.
- Drop the
IF NOT EXISTS, or precede it with DROP INDEX CONCURRENTLY IF EXISTS bookings_operator_starts_id_idx;. - After deploy, check that
pg_index.indisvalid is true. - Add a down migration if your runner expects one.
4. [Medium] List ordering and page size versus what the dashboard needs (Needs check)
- The first page is now the 50 oldest bookings, from three seasons ago, in ascending
starts_at order. Reaching current charters takes about 800 "next" requests. - Any dashboard feature that worked on the full array is now silently wrong. Examples: search, status filters, counts, an "upcoming" view, client-side sort.
Check the bookings page. If it opens on upcoming or recent bookings, either:
- add a
from (start-time lower bound) parameter, or - support descending order with
(starts_at, id) < (...) and ORDER BY starts_at DESC, id DESC.
The index serves both directions.
5. [Medium] ListBookings response shape: dashboard tabs left open during the deploy break
"Ships in the same deploy" covers newly loaded clients only. A browser tab already running the old bundle gets {"items":..., "next_cursor":...} where it expected an array, so its bookings page errors until the user reloads.
Fix: accept this and force a reload, for example with a version check or by bumping a build hash the client polls. Or, for one release, have the old client path tolerate both shapes.
6. [Medium] List scan and cursor: nullable starts_at breaks pages (Needs check)
If bookings.starts_at is nullable (for example, draft or unscheduled bookings):
- Scanning NULL into
time.Time makes pgx return an error, so the page fails with a 500. (starts_at, id) > (...) evaluates to NULL for those rows, so they are never returned after page 1.
Confirm the column is NOT NULL. If it isn't, either add AND starts_at IS NOT NULL and handle those rows separately, or make the constraint NOT NULL.
7. [Low] List: bookings rescheduled between page loads are skipped or shown twice
Keyset paging on a mutable column has this property. Suppose a user is on page 3 and a booking is moved from a later date to a date before the cursor. They never see it. A booking moved later is shown twice.
This is acceptable for a dashboard. Document it in the List doc comment so nobody later relies on the paged list for exports or reconciliation.
8. [Low] encodeCursor: the error from json.Marshal is discarded
This breaks your "never swallowed" rule. time.Time.MarshalJSON fails for years outside 0–9999. If that happens, the client gets an empty next_cursor, which reads as "last page", and stops paging silently.
func encodeCursor(c cursor) (string, error) {
b, err := json.Marshal(c)
if err != nil {
return "", fmt.Errorf("encode cursor: %w", err)
}
return base64.RawURLEncoding.EncodeToString(b), nil
}
9. [Low] ListBookings: requests cancelled by the client are logged as server errors
If the user navigates away mid-query, db.Query returns context canceled. That is logged at Error level and written as a 500 to a closed connection, which adds noise to logs and alerting.
if errors.Is(err, context.Canceled) {
return
}
Put this before the generic error branch.
10. [Low] Tests: the handler and limit handling are untested
Your rule is a test for every exported function, and ListBookings has none. Add httptest cases for:
limit=abc → 400- a bad
after → 400 - an empty result →
{"items":[],"next_cursor":""} - the default of 50 and the clamp at 200
List also has no test for limit <= 0 or limit > 200.
11. [Low] bookings.ListAll removed: other callers would break the build (Needs check)
Grep for ListAll in places like CSV export, reports or admin code. Any caller left behind fails to compile. Any caller switched to List now gets only 50 rows.
12. [Nit] ListBookings: negative and oversized limits are accepted silently
limit=-5 returns 50 rows, and limit=500 returns 200, with no signal to the caller. This is consistent with "max 200", but a 400 for limit < 1 would match "no 500s for client mistakes" more strictly and makes client bugs visible.
13. [Nit] ListBookings: error bodies are text/plain while success responses are JSON
http.Error sends text. If the dashboard parses every response as JSON, it shows a parse error instead of "invalid cursor". Consider a small writeError helper that uses writeJSON.
14. [Nit] ListBookings: map[string]any for the response
Use a named struct, for example type listResponse struct { Items []bookings.Booking json:"items"; NextCursor string json:"next_cursor" }. It gives a fixed field order, compile-time field names, and an easier type to decode in tests.
Count: Critical 0, High 1, Medium 5, Low 5, Nit 3.
Open questions
- Which migration runner applies
0019, and does it wrap files in a transaction? - Is
bookings.starts_at declared NOT NULL? - Does the bookings page open on current or upcoming charters, and does it search, filter or count client-side?
- Is there an existing index on
bookings(operator_id, starts_at) that becomes redundant and can be dropped? - Was CI run on exactly this diff, given the missing
fmt import?