Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6897903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:13:41+00:00 2026-05-27T07:13:41+00:00

I have a complex query in PostgreSQL and I want to use the result

  • 0

I have a complex query in PostgreSQL and I want to use the result of it in other operations like UPDATEs and DELETEs, something like:

<COMPLEX QUERY>;
UPDATE WHERE <COMPLEX QUERY RESULT> = ?;
DELETE WHERE <COMPLEX QUERY RESULT> = ?;
UPDATE WHERE <COMPLEX QUERY RESULT> = ?;

I don’t want to have to do the complex query one time for each operations. One way to avoid this is store the result in a table and use it for the WHERE and JOINS and after finishing, drop the temporary table.

I want to know if there is another way without storing the results to database, but already using the results in memory.

I already use loops for this, but I think doing only one operation for each thing will be faster than doing the operations per row.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:13:42+00:00Added an answer on May 27, 2026 at 7:13 am

    You can loop through the query results like @phatfingers demonstrates (probably with a generic record variable or scalar variables instead of a rowtype, if the result type of the query doesn’t match any existing rowtype). This is a good idea for few resulting rows or when sequential processing is necessary.

    For big result sets your original approach will perform faster by an order of magnitude. It is much cheaper to do a mass INSERT / UPDATE / DELETE with one SQL command
    than to write / delete incrementally, one row at a time.

    A temporary table is the right thing for reusing such results. It gets dropped automatically at the end of the session. You only have to delete explicitly if you want to get rid of it right away or at the end of a transaction. I quote the manual here:

    Temporary tables are automatically dropped at the end of a session, or
    optionally at the end of the current transaction.

    For big temporary tables it might be a good idea to run ANALYZE after they are populated.

    Writeable CTE

    Here is a demo for what Pavel added in his comment:

    CREATE TEMP TABLE t1(id serial, txt text);
    INSERT INTO t1(txt)
    VALUES ('foo'), ('bar'), ('baz'), ('bax');
    
    CREATE TEMP TABLE t2(id serial, txt text);
    INSERT INTO t2(txt)
    VALUES ('foo2'),('bar2'),('baz2');
    
    CREATE TEMP TABLE t3 (id serial, txt text);
    
    WITH x AS (
        UPDATE t1
        SET    txt = txt || '2'
        WHERE  txt ~~ 'ba%'
        RETURNING txt
        )
    , y AS (
        DELETE FROM t2
        USING  x
        WHERE  t2.txt = x.txt
        RETURNING *
        )
    INSERT INTO t3
    SELECT *
    FROM   y
    RETURNING *;
    

    Read more in the chapter Data-Modifying Statements in WITH in the manual.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a fairly complex query that looks something like this: create table Items(SomeOtherTableID
I have a complex query that gets executed like this: if ($stmt = $dbi->prepare($pt_query))
I have following complex query which I need to use. When I run it,
I have a somewhat complex query with multiple (nested) sub-queries, which I want to
In my symfony project, I have a complex query that looks like: $d =
The problem: we have a very complex search query. If its result yields too
I have a complex query that I need to use in a subsequent query
i have a really complex query (mysql + php) and i would like to
I have a complex query with group by and order by clause and I
I have a complex sql query that I have in a stored procedure and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.