Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

121
Vistas
Is it possible to reuse scalar result from a single subquery in insert query in Postgres?

I would like to insert several rows of data in Postgres DB table in the same query, but value for one of the columns needs to be calculated using a scalar result from a sub-query and a passed bound parameter. The calculation is a concatenation of two Postgres arrays.

I was able to do it with a query like this:

INSERT INTO my_table (col1, col2, computed_col)
VALUES 
  (
    :col1Val1,
    :col2val1,
    (SELECT some_col FROM some_table WHERE id = :id) || ARRAY[:computed_col1]::bigint[]
  ),
  (
    :col1Val2,
    :col2val2,
    (SELECT some_col FROM some_table WHERE id = :id) || ARRAY[:computed_col2]::bigint[]
  );

CTE works as well, but it looks unnecessary due to the fact that we still need SELECT subquery from CTE "table" for each set of values.

As you can see SELECT subquery is the same for each set of data to be inserted. So is it possible somehow to specify single subquery and reuse the result without repeating SELECT sub-queries, or maybe there is some other way(s) to optimize the query above?

And what issues the query can cause from a performance point of view?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can do use insert . . . select, basically moving the VALUES() into the FROM clause:

INSERT INTO my_table (col1, col2, computed_col)
    SELECT v.col1, v.col2, x.some_col  || v.computed
    FROM (SELECT some_col FROM some_table WHERE id = :id
         ) x CROSS JOIN
         (VALUES (:col1Val1, :col2val1, ARRAY[:computed_col1]::bigint[]),
                 (:col1Val2, :col2val2, ARRAY[:computed_col2]::bigint[])
         ) v(col1, col2, computed);
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda