website template: the seed button fails intermittently with a deadlock, because the seed clears collections in parallel

Aperta Adatta ai principianti
#20,019 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@zacyungblut ci sta già lavorando.

Dal 28/11/2026.

  • #20036 di @zacyungblut — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
72/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
nextjs, postgresql, typescript
Ambito
backend, database

Direzione di ricerca

Il problema si trova in src/endpoints/seed/index.ts del template del sito web, dove il passaggio di pulizia esegue Promise.all sulla lista collections. Inizia riproducendo il problema con il repository collegato payload-website-seed-race: premi ripetutamente il pulsante seed su Postgres e controlla la presenza di errori 40P01 e 23502. È completato quando il seed svuota le collections in un ordine sicuro, con form-submissions prima di forms, e le esecuzioni ripetute non falliscono più.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

area: templates Bug db: postgres Payload OSS
Describe the Bug

The website template's seed (src/endpoints/seed/index.ts, behind POST /next/seed) clears its collections with Promise.all(collections.map((collection) => payload.db.deleteMany({ collection, req, where: {} }))). There is no transaction, so each delete runs on its own pool connection, all at once. On Postgres this fails in two ways:

  1. Deadlock (40P01), even with only the seed's own data. delete from "media" sets the references in pages_blocks_media_block and posts to null (ON DELETE SET NULL), while delete from "pages" / delete from "posts" deletes the same rows in a different order. The Postgres log shows deadlock detected while deleting tuples in pages_blocks_media_block and posts.
  2. Not-null violation (23502) once the contact form has a submission. When forms is cleared before form-submissions, it hits #11177.

Measured on a new app, production build (pnpm build && pnpm start), Postgres 18:

Scenario Failed runs
Seed button pressed again, seed data only 4 of 30 (40P01)
One contact form submission before each run 4 of 8 (three 23502, one 40P01)

Each delete commits on its own, so a failed run leaves the collections partly cleared. After one failed run, pages still had 2 rows, the contact page among them, while forms, posts and media were empty; the contact page's form block then points at nothing, and the next pnpm build fails prerendering /contact with TypeError: Cannot destructure property 'id' of '{}' as it is null.

Suggested fix (measured: 0 failures in the same 38 runs). Clear the collections one at a time, with form-submissions before forms. That is the PostgreSQL manual's own advice on deadlocks: take locks in a consistent order (https://www.postgresql.org/docs/18/explicit-locking.html#LOCKING-DEADLOCKS).

@@ -16,6 +16,6 @@ const collections: CollectionSlug[] = [
   'pages',
   'posts',
-  'forms',
   'form-submissions',
+  'forms',
   'search',
 ]
@@ -60,7 +60,8 @@ export const seed = async ({
   )
 
-  await Promise.all(
-    collections.map((collection) => payload.db.deleteMany({ collection, req, where: {} })),
-  )
+  // One at a time: parallel deletes of related tables deadlock, and a form can only go after its submissions.
+  for (const collection of collections) {
+    await payload.db.deleteMany({ collection, req, where: {} })
+  }

The same code is on main today (https://github.com/payloadcms/payload/blob/1b1a9ca2040202def9004f379207bd17dd230443/templates/website/src/endpoints/seed/index.ts#L63-L65). I can open a PR with this change if that helps.

Link to the code that reproduces this issue

https://github.com/brunoccalmeida/payload-website-seed-race (untouched app from [email protected] -t website --db postgres plus the generated migration on main; the suggested fix is on the fix/seed-sequential branch)

Reproduction Steps
  1. npx [email protected] -t website --db postgres, pointing at an empty Postgres 18 database.
  2. pnpm payload migrate:create && pnpm payload migrate, then pnpm build && pnpm start.
  3. Create the first admin user, then click "Seed your database" on the dashboard.
  4. Click it again a few times. Some clicks fail with Error seeding data. and the log shows deadlock detected.
  5. Optionally, submit the form on /contact before clicking: about half the clicks then fail, mostly with 23502.
Which area(s) are affected?

area: templates, db: postgres

Environment Info
Binaries:
  Node: 26.13.1
  npm: N/A
  Yarn: N/A
  pnpm: 11.28.9
Relevant Packages:
  payload: 3.94.2
  next: 16.4.6
  @payloadcms/db-postgres: 3.94.2
  @payloadcms/drizzle: 3.94.2
  @payloadcms/graphql: 3.94.2
  @payloadcms/live-preview: 3.94.2
  @payloadcms/live-preview-react: 3.94.2
  @payloadcms/next/utilities: 3.94.2
  @payloadcms/plugin-form-builder: 3.94.2
  @payloadcms/plugin-nested-docs: 3.94.2
  @payloadcms/plugin-redirects: 3.94.2
  @payloadcms/plugin-search: 3.94.2
  @payloadcms/plugin-seo: 3.94.2
  @payloadcms/richtext-lexical: 3.94.2
  @payloadcms/translations: 3.94.2
  @payloadcms/ui/shared: 3.94.2
  react: 19.2.9
  react-dom: 19.2.9
Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 11 Pro
  Available memory (MB): 32689
  Available CPU cores: 20
PostgreSQL: 18 (Docker)
Lingua principale
TypeScript
Stelle
46.1k
Fork
4.5k
Merge medio
2g 15h
PR unite (30g)
238

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di payloadcms/payload

Tutte le issue di payloadcms/payload

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.