0

In the following PgSql stored function:

CREATE OR REPLACE FUNCTION get_offer_from_id(
        offer_id     bigint)
RETURNS json AS $$
DECLARE
    res "entity_data_result";
    r   "get_offer_data";
BEGIN
    FOR r IN SELECT * FROM "get_offer_data"
              WHERE "id" = offer_id
              LIMIT 1
              FOR SHARE
        LOOP
            res.found := true;
            res.data := row_to_json(r);
        END LOOP;
    RETURN row_to_json(res);
END; $$
LANGUAGE plpgsql;

is there a real benefit to using FOR SHARE or can one remove it from the query?

1 Answer 1

1

That depends on what else happens in the transaction.

With the for share there, nobody can update the selected get_offer_data row until the transaction commits or rolls back.

Without the context, it's really impossible to say what the original intended purpose of adding the lock there was.

2
  • Let's assume nothing else happens in the transaction. It is just a simple read. I believe I can safely get rid of the FOR SHARE. Am I right? Commented Sep 13, 2014 at 16:54
  • @JVerstry Yes, in that case it seems to serve no purpose at all. Commented Sep 13, 2014 at 17:48

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.