Home / Databricks on Azure

Databricks on AzureTroubleshootingAdvanced

Lakebase synced tables failing or stale on Azure Databricks - null bytes, duplicate keys, index builds and slow syncs

Diagnose Unity Catalog to Lakebase Postgres synced tables that fail, lag or lose rows. Covers CDF, keys, null bytes, locks, connection use and full refresh capacity.

Deepak Bhardwaj7 min read

Applies toAzure Databricks, Lakebase Postgres Autoscaling projects, Unity Catalog synced tables

An application reads a Unity Catalog table through a Lakebase synced table. The Postgres copy is stale, the sync fails, or rows are missing. Find which of three layers is at fault: the source Delta table, the sync pipeline, or Postgres.

Quick map

Symptom Likely cause Fix
invalid byte sequence for encoding "UTF8": 0x00 Null bytes in a string Clean in the source or use BINARY
\u0000 cannot be converted to text Null bytes in a nested string (JSONB) Clean inside the struct, array or map
Pipeline fails on duplicate keys Primary key isn't unique Fix the source or recreate with a timeseries key
Fewer rows in Postgres, no error Null primary keys are skipped, or dedupe Compare counts (below)
Triggered or Continuous won't start No change data feed Enable CDF
Sync fails during index work Index build blocked writes too long Build when no sync is running
Data overwritten after an app update Application wrote to the synced table Stop writes; use a separate table
INITIALIZING for hours Large initial load Often expected; check throughput
Cannot connect to Postgres Sync connections use the limit Stagger syncs, resize, pool clients

Step 1: read the sync status

databricks api get /api/2.0/postgres/synced_tables/prod.sales.orders_pg

Or with the Python SDK:

from databricks.sdk import WorkspaceClient

w = WorkspaceClient()
t = w.postgres.get_synced_table("synced_tables/prod.sales.orders_pg")
print(t.status.detailed_state, t.status.last_sync_time)
print(t.status.message)

Then open the pipeline from the table's Overview tab and read the first error, not the last one. A Snapshot or Triggered table runs only when triggered. If its last sync time is old and there is no error, check whether the Lakeflow job with the "Database Table Sync pipeline" task ran.

Step 2: check the source

Triggered and Continuous need a change feed. For Delta:

SHOW TBLPROPERTIES prod.sales.orders ('delta.enableChangeDataFeed');

ALTER TABLE prod.sales.orders
SET TBLPROPERTIES (delta.enableChangeDataFeed = true);

Test the key before blaming the pipeline:

-- null keys: these rows are skipped
SELECT count(*) AS null_keys FROM prod.sales.orders WHERE order_id IS NULL;

-- duplicates: these fail the sync unless a timeseries key is set
SELECT order_id, count(*) AS n
FROM prod.sales.orders
GROUP BY order_id HAVING count(*) > 1
ORDER BY n DESC LIMIT 20;

With a timeseries key, Postgres keeps only the row with the latest value per primary key. Lower row counts are then correct, so compare against distinct non-null keys, not against count(*).

Warning

The primary key and timeseries key can't be changed after creation. Changing either means deleting and recreating the synced table, which runs a full initial load and drops the Postgres table meanwhile. Plan for the application outage.

Step 3: find and remove null bytes

Delta strings can contain 0x00. Postgres TEXT and JSONB can't.

SELECT order_id
FROM prod.sales.orders
WHERE instr(note, CAST(CHAR(0) AS STRING)) > 0
LIMIT 100;

Clean it in the transformation that writes the source, so the fix survives the next load:

SELECT order_id,
       REPLACE(note, CAST(CHAR(0) AS STRING), '') AS note
FROM prod.bronze.orders;

For a one-time repair of rows already in the source:

UPDATE prod.sales.orders
SET note = REPLACE(note, CAST(CHAR(0) AS STRING), '')
WHERE instr(note, CAST(CHAR(0) AS STRING)) > 0;

For nested values, rebuild the field with transform, named_struct or transform_values. If the column really holds bytes, make it BINARY, which maps to BYTEA.

Step 4: check schema changes

Only additive changes, such as a new column, flow through Triggered and Continuous syncs. A dropped column, rename or incompatible type change needs a delete and recreate. No interface edits a synced table in place, including Terraform and bundles, so review any plan that shows a replacement.

Vector overrides fail if the Postgres extension isn't installed first. Creating the synced table doesn't install it:

CREATE EXTENSION IF NOT EXISTS vector;

Step 5: look for Postgres contention

Connect with psql and list what is waiting:

SELECT pid, usename, state, wait_event_type, wait_event,
       now() - query_start AS running_for,
       pg_blocking_pids(pid) AS blocked_by,
       left(query, 100) AS query
FROM pg_stat_activity
WHERE state <> 'idle'
ORDER BY running_for DESC;

A long application transaction or a plain CREATE INDEX can block the sync writer. CREATE INDEX CONCURRENTLY isn't supported on synced tables because they are partitioned. If a build holds up a waiting sync for more than 20 minutes, the sync fails and isn't retried.

Index runbook:

  1. Pause the sync schedule, and for Continuous plan a window.
  2. Confirm no sync is running.
  3. Run the build from psql, not a browser editor.
  4. Resume, then check last_sync_time.

Every extra index costs twice: it slows incremental writes and is rebuilt on each full load. Remove unused ones:

SELECT relname, indexrelname, idx_scan
FROM pg_stat_user_indexes
WHERE schemaname = 'sales'
ORDER BY idx_scan, indexrelname;

Allow for replicas and statistics resets before dropping anything.

Step 6: size throughput and connections

Documented planning figures are about 150 rows per second per Capacity Unit for Triggered and Continuous, and up to 2,000 for Snapshot. Check the current figures, then calculate:

triggered seconds ≈ changed_rows / (150 × CU)
snapshot  seconds ≈ total_rows   / (2000 × CU)

Example: 40M rows, 1M changed, 4 CU
  triggered ≈ 1,000,000 / 600   ≈ 28 min
  snapshot  ≈ 40,000,000 / 8000 ≈ 83 min

Snapshot becomes attractive when roughly more than 10% of rows change per cycle. A job that rewrites the whole source with an overwrite produces a change feed as large as the table, which makes Triggered slower than Snapshot. Measure the real feed:

SELECT _change_type, count(*)
FROM table_changes('prod.sales.orders', 1520, 1521)
GROUP BY _change_type;

Convert overwrites to a MERGE with a change condition, so unchanged rows aren't rewritten as updates.

Each synced table can use up to 16 database connections, which count toward the project limit. Twenty tables on the same schedule can ask for 320. Stagger them and put the application behind a pool:

SELECT usename, count(*) FROM pg_stat_activity GROUP BY usename ORDER BY 2 DESC;
SHOW max_connections;

Full refresh needs double the space

A full refresh keeps the old copy until the new one is complete, and both count toward the branch's storage quota. Check free quota before starting one. Databricks recommends not exceeding 1 TB for tables that need refreshes.

Step 7: permissions and unexpected writes

Synced tables belong to an internal databricks_writer_<dbid> role. In Postgres you can read, manage indexes and drop the table; other DDL is denied. Application changes to synced rows are overwritten on the next refresh. Put application state in its own table and join to the synced one.

To let another role manage indexes, a superuser runs:

CREATE EXTENSION IF NOT EXISTS databricks_auth;
SELECT databricks_synced_table_add_manager(
  '"sales"."orders_pg"'::regclass, 'app_dba');

Caution

Deleting the synced table in Unity Catalog also drops the Postgres table. Save index definitions from pg_indexes and grants first, and recreate them after the new initial load.

Prevent it next time

  • Add null-key, duplicate-key and null-byte checks to the job that writes the source.
  • Alert on last_sync_time age against the freshness the application needs.
  • Keep index DDL and grants in version control so a recreate is scripted.
  • Review the CDF change volume after every source job change.
  • Record the primary key, timeseries key and mode per synced table, and treat them as immutable.