Select and Update in Stored Functions Postgresql
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
add a comment |
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
2
Unrelated, but: using PL/pgSQL for a simply query is overkill. Using alanguage 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
add a comment |
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
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
sql sql-update plpgsql postgresql-9.5 stored-functions
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 alanguage 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
add a comment |
2
Unrelated, but: using PL/pgSQL for a simply query is overkill. Using alanguage 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
add a comment |
1 Answer
1
active
oldest
votes
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;
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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;
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
add a comment |
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;
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
add a comment |
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;
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;
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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