Skip to content

PostgreSQL error 54023 (cannot pass more than 100 arguments to a function) on wide nested relations #2524

@Natansal

Description

@Natansal

[Bug]: PostgreSQL error 54023 (cannot pass more than 100 arguments to a function) on wide nested relations

Description

When using ZenStack v3 with the PostgreSQL, querying a model that includes a nested relation with more than 50 columns causes a database crash.

ZenStack generates a jsonb_build_object function call to map the relation's columns into a JSON object. Because jsonb_build_object takes key-value pairs as individual arguments, a table with 55 columns results in 110 arguments. This exceeds PostgreSQL's hardcoded FUNC_MAX_ARGS limit of 100, throwing error 54023.

Environment

  • ZenStack Version: v3.x (specifically v3.4.5)
  • Database: PostgreSQL
  • Framework: NestJS

Steps to Reproduce

  1. Define a Prisma/ZenStack schema with a base model and a related model that contains 51+ columns.
    model Association {
      id String @id @default(uuid())
      opportunities Opportunity[]
      // ... other fields
    }
    
    model Opportunity {
      id String @id @default(uuid())
      associationRequestedId String
      association Association @relation(fields: [associationRequestedId], references: [id])
      // ... define 50+ additional columns here
    }
    
  2. Execute a find query that includes the wide relation:
await db.association.findMany({
  where: { /* conditions */ },
  include: { opportunities: true }
});

Expected Behavior

The query should execute successfully and return the nested relations. The query generator should ideally handle wide tables by using to_jsonb("TableName".*) or by concatenating multiple jsonb_build_object calls (e.g., jsonb_build_object(...) || jsonb_build_object(...)) to bypass the 100-argument limit.

Actual Behavior

The application crashes with the following error:

ORMError: Failed to execute query: Error: Failed to execute query: error: cannot pass more than 100 arguments to a function
  dbErrorCode: '54023',
  routine: 'ParseFuncOrColumn'

Generated SQL snippet causing the issue:

left join lateral (
  select coalesce(jsonb_agg(
    jsonb_build_object(
      'id', "$$t4"."id", 
      'imageId', "$$t4"."imageId", 
      -- ... >50 key/value pairs resulting in >100 arguments ...
      'updatedAt', "$$t4"."updatedAt"
    )
  ), '[]'::jsonb) as "$data"
  -- ...
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions