This documentation is available to agents as Markdown at https://docs.synthesized.io/tdk/latest/user_guide/030_core_concepts/how_subsetting_works.md. For the complete documentation index, see llms.txt.

How Subsetting Works

Subsetting writes a smaller copy of the input in which every foreign key still points to an existing row. It isn’t a separate mode: you make a table smaller with target_ratio or a filter, in KEEP or MASKING mode, so you can mask and subset in the same run.

Two ways to make a table smaller

Setting What it does

target_ratio

The share of rows to keep, between 0 and 1. Set it in default_config for every table, or per table. The default is 1, all rows.

filter

An SQL condition, as in a WHERE clause, that rows must meet. Set it per table. See Filter Source Rows.

Rows are chosen at random within what the ratio and the filter allow.

Parents first

Tables are processed parents first, and a row is kept only if the rows it references were kept. So a child table shrinks with its parent, even if you set no ratio on the child. There are two exceptions: a nullable foreign key to a parent table that ends up with no rows at all is set to null, and the row is kept; and references you list in cycle_breaker_references are ignored.

An example with three tables. customers has target_ratio 0.5, so 500 of its 1,000 rows are kept. orders references customers and has the default target_ratio of 1.0, but only the orders of kept customers can be kept, so about 2,500 of its 5,000 rows remain. order_items references orders, also with the default ratio, and keeps only the items of kept orders: about 10,000 of 20,000. Every foreign key in the output points to a kept row.

The same goes for a filter: filtering film to one film also limits inventory to that film’s copies, and rental to their rentals.

This rule keeps the output consistent, with no orphaned rows. It also explains the results that surprise people:

  • A table has fewer rows than its ratio. Its parents were reduced too, and the reductions add up down the chain.

  • A table is empty. None of the rows it depends on were kept. In Subset a Database, target_ratio: 0.5 on every table leaves film empty, and with it film_actor and rental, which depend on films. film references language, a small table, so when the rows films reference aren’t among the kept half of language, no film can be kept.

Controlling the result

  • Reduce only the large tables. Set target_ratio on the big transaction tables, and leave small lookup tables at the default of 1:

    default_config:
      mode: KEEP
    tables:
      - table_name_with_schema: "public.rental"
        target_ratio: 0.1
      - table_name_with_schema: "public.payment"
        target_ratio: 0.1
  • Start from the entities you need. Put the filter or the ratio on a parent, such as customers or accounts; their child rows follow.

  • Keep lookup tables whole. A ratio on a table that many others reference, like language above, empties everything below it when the wrong rows are dropped.

  • Mask at the same time. Use mode: MASKING with the same settings to get a smaller, masked copy in one run.

Cycles

When foreign keys form a loop, such as an employee table that references itself for each employee’s manager, there’s no table to start from. The cycle_resolution_strategy setting decides what happens:

Strategy What it does

FAIL

Stops with an error, unless you name the references to break in cycle_breaker_references.

DELETE_NOT_REQUIRED

Breaks the cycle at the last nullable reference, which is set to null in the output.

DEFER_FOREIGN_KEY

Disables the foreign keys in the cycle while writing, then turns them back on. Oracle, SQL Server, MySQL and Db2 only, in MASKING or KEEP mode, and only with a target_ratio of 1, so not for subsets.