Search code examples
databasepostgresqljdbclimit

Postgresql "update query returning *" with limit


Can I run an update query with return clause and also limit the rows returned? For example I run an udate query and it updates a million records, but I dont want to get the updated million rows back to result set.. just a sample say 1000 records. Is this possible?

My Query :

UPDATE table1 SET col1 = value1 RETURNING *

I want to get the number of columns updated and a sample 1000 rows after update.


Solution

  • with updated as (
      update the_table_with_many_rows
         set some_column = 42
      where ...
      returning *
    )
    select u.*, 
           count(*) over () as total_update_count
    from updated as u
    limit 1000;