Select and Update in Stored Functions Postgresql












0














I created a stored function for fetching data:



CREATE OR REPLACE FUNCTION sold_quantity()  
RETURNS TABLE(
invoiceid BIGINT,
itemid BIGINT,
sum_sold_quantity NUMERIC)
AS $$
BEGIN
RETURN QUERY SELECT
invoice_id as invoiceid, item_id as itemid, sum(sold_quantity) as
sum_sold_quantity
FROM
invoice_item
WHERE
status='sold'
GROUP BY
invoice_id, item_id;
END; $$

LANGUAGE 'plpgsql';


How to store this data and using it to update the table below?



UPDATE invoice_item 
SET sold_quantity = sum_sold_quantity
where invoice_id=invoiceid
and item_id = itemid;


How can we club these two queries (Select and update) in one stored function and execute to fetch all 500k records with the above three columns and update batch wise (like 10 000 records)










share|improve this question




















  • 2




    Unrelated, but: using PL/pgSQL for a simply query is overkill. Using a language sql function would be more efficient
    – a_horse_with_no_name
    Nov 19 '18 at 13:31










  • please, don't do it - don't write procedures just for wrapping SELECT command. It is significant performance antipattern - use views instread.
    – Pavel Stehule
    Nov 20 '18 at 5:00
















0














I created a stored function for fetching data:



CREATE OR REPLACE FUNCTION sold_quantity()  
RETURNS TABLE(
invoiceid BIGINT,
itemid BIGINT,
sum_sold_quantity NUMERIC)
AS $$
BEGIN
RETURN QUERY SELECT
invoice_id as invoiceid, item_id as itemid, sum(sold_quantity) as
sum_sold_quantity
FROM
invoice_item
WHERE
status='sold'
GROUP BY
invoice_id, item_id;
END; $$

LANGUAGE 'plpgsql';


How to store this data and using it to update the table below?



UPDATE invoice_item 
SET sold_quantity = sum_sold_quantity
where invoice_id=invoiceid
and item_id = itemid;


How can we club these two queries (Select and update) in one stored function and execute to fetch all 500k records with the above three columns and update batch wise (like 10 000 records)










share|improve this question




















  • 2




    Unrelated, but: using PL/pgSQL for a simply query is overkill. Using a language sql function would be more efficient
    – a_horse_with_no_name
    Nov 19 '18 at 13:31










  • please, don't do it - don't write procedures just for wrapping SELECT command. It is significant performance antipattern - use views instread.
    – Pavel Stehule
    Nov 20 '18 at 5:00














0












0








0







I created a stored function for fetching data:



CREATE OR REPLACE FUNCTION sold_quantity()  
RETURNS TABLE(
invoiceid BIGINT,
itemid BIGINT,
sum_sold_quantity NUMERIC)
AS $$
BEGIN
RETURN QUERY SELECT
invoice_id as invoiceid, item_id as itemid, sum(sold_quantity) as
sum_sold_quantity
FROM
invoice_item
WHERE
status='sold'
GROUP BY
invoice_id, item_id;
END; $$

LANGUAGE 'plpgsql';


How to store this data and using it to update the table below?



UPDATE invoice_item 
SET sold_quantity = sum_sold_quantity
where invoice_id=invoiceid
and item_id = itemid;


How can we club these two queries (Select and update) in one stored function and execute to fetch all 500k records with the above three columns and update batch wise (like 10 000 records)










share|improve this question















I created a stored function for fetching data:



CREATE OR REPLACE FUNCTION sold_quantity()  
RETURNS TABLE(
invoiceid BIGINT,
itemid BIGINT,
sum_sold_quantity NUMERIC)
AS $$
BEGIN
RETURN QUERY SELECT
invoice_id as invoiceid, item_id as itemid, sum(sold_quantity) as
sum_sold_quantity
FROM
invoice_item
WHERE
status='sold'
GROUP BY
invoice_id, item_id;
END; $$

LANGUAGE 'plpgsql';


How to store this data and using it to update the table below?



UPDATE invoice_item 
SET sold_quantity = sum_sold_quantity
where invoice_id=invoiceid
and item_id = itemid;


How can we club these two queries (Select and update) in one stored function and execute to fetch all 500k records with the above three columns and update batch wise (like 10 000 records)







sql sql-update plpgsql postgresql-9.5 stored-functions






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 14:50









James Z

11.1k71735




11.1k71735










asked Nov 19 '18 at 12:49









Gagan Mesala

227




227








  • 2




    Unrelated, but: using PL/pgSQL for a simply query is overkill. Using a language sql function would be more efficient
    – a_horse_with_no_name
    Nov 19 '18 at 13:31










  • please, don't do it - don't write procedures just for wrapping SELECT command. It is significant performance antipattern - use views instread.
    – Pavel Stehule
    Nov 20 '18 at 5:00














  • 2




    Unrelated, but: using PL/pgSQL for a simply query is overkill. Using a language sql function would be more efficient
    – a_horse_with_no_name
    Nov 19 '18 at 13:31










  • please, don't do it - don't write procedures just for wrapping SELECT command. It is significant performance antipattern - use views instread.
    – Pavel Stehule
    Nov 20 '18 at 5:00








2




2




Unrelated, but: using PL/pgSQL for a simply query is overkill. Using a language sql function would be more efficient
– a_horse_with_no_name
Nov 19 '18 at 13:31




Unrelated, but: using PL/pgSQL for a simply query is overkill. Using a language sql function would be more efficient
– a_horse_with_no_name
Nov 19 '18 at 13:31












please, don't do it - don't write procedures just for wrapping SELECT command. It is significant performance antipattern - use views instread.
– Pavel Stehule
Nov 20 '18 at 5:00




please, don't do it - don't write procedures just for wrapping SELECT command. It is significant performance antipattern - use views instread.
– Pavel Stehule
Nov 20 '18 at 5:00












1 Answer
1






active

oldest

votes


















1














You can use the function directly in the UPDATE statement



UPDATE invoice_item ii
SET sold_quantity = sq.sum_sold_quantity
from sold_quantity() as sq
where ii.invoice_id = sq.invoiceid
and ii.item_id = sq.itemid;




For just 500.000 rows, I wouldn't bother doing any batching at all. Just update all rows in a single statement.



In fact you don't really need a function to begin with:



UPDATE invoice_item ii
SET sold_quantity = sq.sum_sold_quantity
from (
select invoice_id as invoiceid, item_id as itemid, sum(sold_quantity) as sum_sold_quantity
FROM invoice_item
WHERE status='sold'
GROUP BY invoice_id, item_id;
) as sq
where ii.invoice_id = sq.invoiceid
and ii.item_id = sq.itemid;





share|improve this answer





















  • The above query is worked for me. thanks
    – Gagan Mesala
    Nov 20 '18 at 6:01












  • can you please look into this query. stackoverflow.com/questions/53387137/…
    – Gagan Mesala
    Nov 20 '18 at 6:07













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375017%2fselect-and-update-in-stored-functions-postgresql%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You can use the function directly in the UPDATE statement



UPDATE invoice_item ii
SET sold_quantity = sq.sum_sold_quantity
from sold_quantity() as sq
where ii.invoice_id = sq.invoiceid
and ii.item_id = sq.itemid;




For just 500.000 rows, I wouldn't bother doing any batching at all. Just update all rows in a single statement.



In fact you don't really need a function to begin with:



UPDATE invoice_item ii
SET sold_quantity = sq.sum_sold_quantity
from (
select invoice_id as invoiceid, item_id as itemid, sum(sold_quantity) as sum_sold_quantity
FROM invoice_item
WHERE status='sold'
GROUP BY invoice_id, item_id;
) as sq
where ii.invoice_id = sq.invoiceid
and ii.item_id = sq.itemid;





share|improve this answer





















  • The above query is worked for me. thanks
    – Gagan Mesala
    Nov 20 '18 at 6:01












  • can you please look into this query. stackoverflow.com/questions/53387137/…
    – Gagan Mesala
    Nov 20 '18 at 6:07


















1














You can use the function directly in the UPDATE statement



UPDATE invoice_item ii
SET sold_quantity = sq.sum_sold_quantity
from sold_quantity() as sq
where ii.invoice_id = sq.invoiceid
and ii.item_id = sq.itemid;




For just 500.000 rows, I wouldn't bother doing any batching at all. Just update all rows in a single statement.



In fact you don't really need a function to begin with:



UPDATE invoice_item ii
SET sold_quantity = sq.sum_sold_quantity
from (
select invoice_id as invoiceid, item_id as itemid, sum(sold_quantity) as sum_sold_quantity
FROM invoice_item
WHERE status='sold'
GROUP BY invoice_id, item_id;
) as sq
where ii.invoice_id = sq.invoiceid
and ii.item_id = sq.itemid;





share|improve this answer





















  • The above query is worked for me. thanks
    – Gagan Mesala
    Nov 20 '18 at 6:01












  • can you please look into this query. stackoverflow.com/questions/53387137/…
    – Gagan Mesala
    Nov 20 '18 at 6:07
















1












1








1






You can use the function directly in the UPDATE statement



UPDATE invoice_item ii
SET sold_quantity = sq.sum_sold_quantity
from sold_quantity() as sq
where ii.invoice_id = sq.invoiceid
and ii.item_id = sq.itemid;




For just 500.000 rows, I wouldn't bother doing any batching at all. Just update all rows in a single statement.



In fact you don't really need a function to begin with:



UPDATE invoice_item ii
SET sold_quantity = sq.sum_sold_quantity
from (
select invoice_id as invoiceid, item_id as itemid, sum(sold_quantity) as sum_sold_quantity
FROM invoice_item
WHERE status='sold'
GROUP BY invoice_id, item_id;
) as sq
where ii.invoice_id = sq.invoiceid
and ii.item_id = sq.itemid;





share|improve this answer












You can use the function directly in the UPDATE statement



UPDATE invoice_item ii
SET sold_quantity = sq.sum_sold_quantity
from sold_quantity() as sq
where ii.invoice_id = sq.invoiceid
and ii.item_id = sq.itemid;




For just 500.000 rows, I wouldn't bother doing any batching at all. Just update all rows in a single statement.



In fact you don't really need a function to begin with:



UPDATE invoice_item ii
SET sold_quantity = sq.sum_sold_quantity
from (
select invoice_id as invoiceid, item_id as itemid, sum(sold_quantity) as sum_sold_quantity
FROM invoice_item
WHERE status='sold'
GROUP BY invoice_id, item_id;
) as sq
where ii.invoice_id = sq.invoiceid
and ii.item_id = sq.itemid;






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 '18 at 13:35









a_horse_with_no_name

291k46445539




291k46445539












  • The above query is worked for me. thanks
    – Gagan Mesala
    Nov 20 '18 at 6:01












  • can you please look into this query. stackoverflow.com/questions/53387137/…
    – Gagan Mesala
    Nov 20 '18 at 6:07




















  • The above query is worked for me. thanks
    – Gagan Mesala
    Nov 20 '18 at 6:01












  • can you please look into this query. stackoverflow.com/questions/53387137/…
    – Gagan Mesala
    Nov 20 '18 at 6:07


















The above query is worked for me. thanks
– Gagan Mesala
Nov 20 '18 at 6:01






The above query is worked for me. thanks
– Gagan Mesala
Nov 20 '18 at 6:01














can you please look into this query. stackoverflow.com/questions/53387137/…
– Gagan Mesala
Nov 20 '18 at 6:07






can you please look into this query. stackoverflow.com/questions/53387137/…
– Gagan Mesala
Nov 20 '18 at 6:07




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375017%2fselect-and-update-in-stored-functions-postgresql%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

'app-layout' is not a known element: how to share Component with different Modules