Blog
Ghostrow Labs
A production-shaped copy without copying a row
What Twin measures in a database, what it refuses to keep, and how it builds a new dataset with the same shape. With a real run on a SQLite sample.
A copy of production is useful because of its shape, not its rows. Some customers have no orders and a few have dozens. Half the phone numbers are empty. One status covers most of the table. Tests written against hand-made fixtures miss all of that. ghostrow twin (Max plan) measures those properties on your machine and generates a new dataset that has them. This post explains what is measured, what is thrown away, and where the approach has limits. The numbers come from a database we made for this post.
The sample
We built a SQLite file with two tables and fake data: 400 customers, with a plan column, a nullable phone, and a unique email; and 641 orders with a status, an amount and a placed-at time. Order counts per customer are heavily skewed: 254 customers have none, and one has 40. Two values are rare on purpose: three customers are on the max plan, and four orders are chargeback.
ghostrow --seed 3 twin --from sqlite:prod.db --out twin-out
measuring prod.db (2 tables, read-only)
ghostrow twin · prod.db → twin-out · scale 1 · Max
customers 400 measured → 400 synthetic
orders 641 measured → 641 synthetic
wrote 2 CSV files to twin-out (+ twin-report.json)
1,041 rows, 0 violations · no source row copied; categories reused only when shared by ≥5 rows
What gets measured
Measurement happens in memory. For each table, Twin reads up to 50,000 rows and takes the total from a count. For each column it computes:
- A null rate. The share of rows where the value is missing.
- A quantile profile. Twenty-one points, from the minimum through every 5th percentile to the maximum, for numbers and for dates. For text columns the profile describes the lengths of the values, not the values.
- Categories, only for columns that qualify (below).
- Fan-out, for each foreign key: how many children each parent has, again as a quantile profile. Parents with zero children count, which is how a long tail of empty parents survives.
Primary keys and references are not profiled at all. A column whose meaning is personal or free-form, such as an email, a phone number, a name, an address, a token, a URL or a description, never gets category values. We still keep its null rate and length profile, because "30% of phones are empty" is a shape property.
The rule for categories
A status or plan column needs its values to reproduce its distribution, but a value that appears in a handful of rows can identify those rows. So categories follow strict conditions. A column gets categories only if it is not a personal field, has at most 40 distinct values, and every value is at most 60 characters long. A value is kept only if at least 5 rows share it. If the kept values do not cover at least 80% of the non-null rows, the column keeps no categories at all and falls back to the normal generator. The weights that remain are the kept values' shares, renormalised.
On our sample that means max (3 customers) and chargeback (4 orders) cannot survive. Here is the comparison, source on the left:
| Column | Source | Twin |
|---|---|---|
| customers.plan | free 250, plus 120, pro 27, max 3 | free 242, plus 131, pro 27 |
| orders.status | paid 542, refunded 60, pending 35, chargeback 4 | paid 544, refunded 57, pending 40 |
The rare values are gone and the others are close but not equal, because Twin samples from the weights instead of replaying counts. If your tests need a rare value, Haunt or an explicit rule is the right tool; a copy that kept every rare value would be a copy of the rows that hold them.
How a new dataset is built
Row counts are the measured counts times --scale (rounded, with at least one row for any table that had data). Then the same generator that powers seed runs, with measured shapes in place of its default value models:
- Numbers and dates are drawn by picking a random position along the quantile profile and interpolating between the two neighbouring points. The result follows the source distribution without being any source value.
- Categories are drawn by weight. Nulls are drawn at the measured rate. Unique columns skip the profile and use the normal generator, so uniqueness is not at risk.
- Personal and free text is generated from scratch. Free-text columns such as descriptions and titles are trimmed or extended to the measured length.
- Fan-out works by giving every parent a random weight drawn from the measured children-per-parent profile, then assigning each child to a parent in proportion to those weights.
- Keys are new. Ids are numbered fresh, and every reference points at a parent row that exists in the copy.
After that the usual rules apply: CHECKs, orderings, sums and products hold, and the validator re-checks every row before anything is written. With --to instead of --out, the rows go into a database that already has the schema, with ids continuing after what is there.
Did the shape survive?
We compared the source and the copy ourselves, then checked twin-report.json, which lists null rate and median for each column, original beside synthetic.
- Nulls.
phoneis empty in 120 of 400 rows in the source. In the copy it is empty in 120 of 400 rows. The report shows 0.3 for both. - Amounts. The median order amount is 34.41 in the source and 34.65 in the copy.
- Fan-out. The source has 254 customers with no orders, the copy has 262. The most orders for one customer is 40 in the source and 47 in the copy.
- Rows. 0 of the 400 synthetic emails appear in the source.
The fan-out line deserves a note. A 21-point summary cannot describe a long tail exactly, and the child counts per parent are sampled, so the tail can come out heavier or lighter than the source. The copy has the same character, many empty parents and a few busy ones, but not the same numbers. We would rather say that than round it away.
What Twin does not guarantee
The minimum and maximum are in the profile. The first and last quantile points are real values from the source, held in memory during the run. Generated values are interpolated, so they are not copies, but they can fall close to the extremes. They are not written to the output; the report contains only null rates and medians. If a single extreme value is sensitive in your data, treat it accordingly.
Text that is really a date stays text. In our SQLite sample the signed_up column was declared TEXT, so Twin treated it as text and produced words of a similar length rather than dates. A column declared as a date type gets date profiles. Declare the type, or expect text.
Shape is not meaning. Twin keeps each column's distribution and each table's fan-out. It does not model correlations between columns, such as high amounts among pro customers, beyond the rules Ghostrow already knows (a total is the sum of its lines, a ship date is not before the order date). If a test depends on a correlation, it is not in the copy.
It is statistics, not anonymisation. The promise we make is narrow: no source row is copied, personal fields are never profiled by value, and a category is reused only when at least five rows share it. We do not claim more.
Twin only reads from the source (--from says read-only access is enough), and measuring and generating both happen on your machine. Why we built it that way is in why Ghostrow runs on your machine.