Nine rows in the customers table and 2,217 real guests
The customers table had nine rows in it. The payment ledger had 2,720. Both numbers were correct, which is the whole problem.
I am building a multi-tenant management app for Autosterea's local-business clients, and tenant number one is a Seattle head spa that runs mostly on walk-ins. A walk-in taps a card at a Square Terminal and leaves. No name, no email, no phone, no account. Square does create a customer record for most of those payments, but the records are hollow: of 100 I sampled, zero had a phone number and four had an email. So the owner could tell you exactly how many people paid last month and could not tell you whether a single one of them had ever been in before.
The shape
The app is plain Node http with pg underneath, server rendered HTML, no build step, sitting behind nginx on one VPS with a Postgres database and a systemd timer that pulls the Square ledger. This week I added a pseudonymous guest key to the payments table, backfilled every payment since June 2024, and rebuilt the client and reporting screens on top of it. The result: 2,713 of 2,720 payment rows now carry a guest identity, resolving to 2,217 distinct guests. Of those, 2,041 came exactly once. 176 came back. Returning guests are 7.9 percent of the cards and 22 percent of the visits, and the median gap between one visit and the next is 28 days.
old: a customers table with nine rows, and no way to tell a returning guest from a first-timer
new: 2,217 pseudonymous guests keyed off the card, 176 of them returning, median return gap 28 daysThe one identity signal available, and the part you must not keep
Square returns a stable per-card fingerprint. Same card, same fingerprint, forever. That is the only thing in the entire payload that persists across two anonymous counter visits, and it is also exactly the kind of value you should not be storing in your own database.
So I do not store it. What lands in Postgres is HMAC-SHA256(GUEST_ID_SECRET, fingerprint) truncated to 32 hex characters. That gives me the one property I actually need, which is that the same card produces the same key every time, and denies me everything else. The output is meaningless outside this system because the secret never leaves the server's env file, and it is not reversible into a card number or a payment account reference.
The ordering turned out to be the real control. The sync already had a redactRaw() step that strips fingerprint and payment_account_reference out of the payload before the raw JSON is inserted. The guest hash has to be computed from the card object before that redaction runs. One line earlier and there is nothing left to hash. One line later and I have quietly written the raw fingerprint into the stored payload, which is the thing the redaction exists to prevent. It is two statements in a fixed order with a comment above them saying so.
Two backfills that wrote nothing at all
The first backfill ran clean and set every hash to NULL. So did the second.
The cause was a helper doing exactly what it promised. parseEnvFile() reads the app's env file and returns a plain object, and it deliberately does not populate process.env. The secret lookup checked process.env.GUEST_ID_SECRET first, found nothing, and guestHashOf() returns null on a missing secret rather than throwing. So the job ran to completion, reported success, and wrote nulls across the board. It now logs a loud error when the secret is missing, because a retention feature that silently produces no identities is worse than one that refuses to start.
The second attempt failed differently. The Square sync upserts on a payment's updated_at, which correctly makes a re-run a no-op for rows Square has not changed. That guard is right for normal operation and exactly wrong for a backfill: I had just added a column, nothing upstream had changed, so every existing row was skipped. The upsert needed a self-heal clause so that a row missing the new value gets it filled in even when Square says nothing has changed. 2,713 rows on the third run.
The columns I added and then refused to read
Migration 004 also added guest_visit_number and days_since_previous_visit to the payments table, on the reasonable theory that visit ordering is expensive to compute and cheap to store.
Before building the screens on them I checked how many rows actually carried them. 2,617 out of 2,720, against 2,713 rows that carry a guest hash. Then I read both sync scripts in full and found that neither one ever writes those two columns. They had been populated once, by hand, outside anything in the repo, and every payment synced since had left them NULL. A Clients screen built on that column would have quietly undercounted the most recent visit of every guest who came in after the backfill, and it would have looked completely fine.
So nothing reads them. Every visit count, first and last visit, and return gap is computed live with GROUP BY and window functions over the guest hash, in one shared module that all six screens go through. It is more SQL and it is correct today and stays correct as the sync keeps running without ever touching those columns again. The most actionable list in the business, the 41 guests whose last visit was 25 to 45 days ago with nothing since, brackets that 28 day median and is defined in exactly one place instead of being reimplemented slightly differently on every screen that wants it.
What I learned
A pseudonym you cannot reverse is still a complete identity. I do not know who guest a3f9... is, and the system does not need to. It needs to know that this card was here 28 days ago, which is enough to power a returning-guest view, a retention cohort and a rebooking list. Hashing the fingerprint with a server-held secret bought the entire feature and cost me nothing I actually wanted, which is a trade worth looking for before you go asking every walk-in for their email.
A derived column is a cache, and a cache nothing writes is a lie. The two ordering columns were 96 percent populated, which is the worst possible number: high enough that every spot check passes, low enough that the newest and most interesting rows are exactly the missing ones. Checking who writes a column, rather than how full it looks, is a thirty second grep that saved a screen full of wrong numbers.
A job that reports success while writing nulls is a bug in the job, not in the data. Two backfills said "done" and changed nothing, once because a missing secret degraded to null instead of failing, and once because a correctness guard for the steady state was wrong for a migration. Both would have been caught immediately by refusing to run without the secret and by counting the rows actually modified rather than the rows examined.
If you are sitting on a payment ledger that knows more about your customers than your customer database does, that is the kind of thing Autosterea builds.
First published on ravikantdewangan.com.

