Skip to content

Allow partial pipelining within a transaction #951

Closed

Description

@astralarya

Allow pipelining the some requests in a transaction without requiring that all the requests are pipelined. This would allow selecting to get the id of a row, then issuing multiple statements based on the id of the row in a pipelined fashion.

For example:

await sql.begin(async sql => {
  const [movie] = await sql`
    SELECT * FROM Movie
    WHERE name = 'Star Wars'
  `;
  return [
    sql`
      INSERT INTO MovieActor
      -- some statement using ${movie.id}
    `,
    sql`
      INSERT INTO MovieReleases
      -- some statement using ${movie.id}
    `,
     // etc
  ]
})

Activity

  1. astralarya commented on Oct 3, 2024

    @astralarya
    Author

    Alternatively, maybe there could be a specific sql.pipeline() function, which would accept an array of sql statements to pipeline. This would allow opening a transaction and running some non-pipelined statements before and after a block of pipelined statements.

  2. porsager commented on Oct 3, 2024

    @porsager
    Owner

    I think your example is great, and ought to work :) Also for doing it in the middle of other queries this should be supported:

    await sql.begin(async sql => {
      const [movie] = await sql`
        SELECT * FROM Movie
        WHERE name = 'Star Wars'
      `
      
      await Promise.all([
        sql`
          INSERT INTO MovieActor
          -- some statement using ${movie.id}
        `,
        sql`
          INSERT INTO MovieReleases
          -- some statement using ${movie.id}
        `,
         // etc
      ])
      
      await sql` ... something else `
    })

    I might have some time to toy with this soon, but if you want to take a stab at it, feel free :)

  3. porsager commented on Oct 3, 2024

    @porsager
    Owner

    Until then, are you sure your use case can't work with CTE's ?

  4. astralarya commented on Oct 3, 2024

    @astralarya
    Author

    I think Promise.all doesn't guarantee any sequencing, while pipelining with libpq does. This doesn't matter in many cases, but it can occasionally. For example, you could do an INSERT, then another INSERT that uses some aggregate that includes the first INSERT, etc.

    CTEs can work in most situations, but it would be nice to have the option to pipeline statements.

  5. astralarya commented on Dec 11, 2024

    @astralarya
    Author

    Another place where partial pipelining would be great is when using temporary tables. For example:

    await sql.pipeline(sql => [
      sql`
        CREATE TEMP TABLE ActorsInYear
        ON COMMIT DROP AS
        SELECT Actor.*
        FROM Actor
        JOIN MovieActor USING (ActorId)
        JOIN Movie USING (MovieId)
        WHERE Movie.Year = ${year}
      `,
      sql`
        CREATE INDEX ON ActorsInYear(ActorId)
      `,
      sql`
        ANALYZE ActorsInYear
      `
    ]);
    // Do something with ActorsInYear
  6. oscar6echo commented on Mar 28, 2025

    @oscar6echo

    I think Promise.all doesn't guarantee any sequencing

    There should be a way to keep the sequence order: #1030 (comment)

  7. karlhorky commented on Aug 20, 2026

    @karlhorky
    Contributor
  8. porsager commented on Aug 20, 2026

    @porsager
    Owner

    It's been there since day one 😉 I think this issue is about handling it explicitly

  9. karlhorky commented on Aug 20, 2026

    @karlhorky
    Contributor

    Ah nice! I was wondering if that pipelining feature was this bit from the readme:

    It's also possible to pipeline the requests in a transaction if needed by returning an array with queries from the callback function like this:

    const result = await sql.begin(sql => [
      sql`update ...`,
      sql`update ...`,
      sql`insert ...`
    ])
  10. porsager commented on Oct 29, 2026

    @porsager
    Owner

    v4 documents and tests this: queries in a Promise.all between awaited ones are sent together and run in the order they were made.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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