0

So,I was hoping to do this:

 let statement = "update players set walk_count = unnest($1), x = unnest($2), y = unnest($3)  where player_id = unnest($4)";

But the error I get is "message: "set-returning functions are not allowed in WHERE",

The only other way I can solve this is by doing individual updates, but I see the loop is taking a lot of time.

1
  • I found transactions, which allows you to open a transaction to send bulk operations to the database, and defers execution until you call commit. That definitely helped. But I read that using unnest would have been faster. Commented Oct 18, 2022 at 2:56

1 Answer 1

1

Assuming that each parameter ($1, $2, ...) is an array containing one item for each row you want to update, you should use a single unnest() call for all 4 arrays:

update players 
  set walk_count = v.wc, 
      x = v.x, 
      y = v.y
from (
  select *
  from unnest($1, $2, $3, $4)
) as v (wc, x, y, id)
where v.id = players.player_id  
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.