3

I intend to log user ids by policy change in a temporary table. The following should return the logged ids, but it does return empty rows. Somehow the ids are lost :S The row count is okay, the ids are in the table, I don't know what I am doing wrong...

BEGIN;
SELECT * FROM policy_change_update(ARRAY[1,2,3]);
SELECT * FROM user_read_all_by_policy_change();
COMMIT;

The functions:

policy_change_provide_store:

CREATE FUNCTION policy_change_provide_store ()
  RETURNS VOID
AS
  $BODY$
  BEGIN
    CREATE TEMPORARY TABLE policy_change (
      user_id INT4
    );
    EXCEPTION WHEN duplicate_table THEN
    RETURN;
  END;
  $BODY$
LANGUAGE plpgsql VOLATILE;

policy_change_update

CREATE FUNCTION policy_change_update (
  IN user_ids INT4 []
)
  RETURNS VOID
AS
  $BODY$
  BEGIN
    PERFORM policy_change_provide_store();
    INSERT INTO policy_change
    SELECT inserted_user_id FROM unnest(user_ids) AS inserted_user_id
    WHERE NOT EXISTS(SELECT 1 FROM policy_change WHERE user_id = inserted_user_id);
  END;
  $BODY$
LANGUAGE plpgsql VOLATILE;

user_read_all_by_policy_change

CREATE FUNCTION user_read_all_by_policy_change ()
  RETURNS TABLE (
  user_id INT4
  )
AS
  $BODY$
  BEGIN
    PERFORM policy_change_provide_store();
    RETURN QUERY
    SELECT user_id FROM policy_change ORDER BY user_id DESC;
    IF NOT FOUND THEN
      RAISE SQLSTATE 'UE404' USING MESSAGE = 'not found';
    END IF;
  END;
  $BODY$
LANGUAGE plpgsql VOLATILE;

Any idea?

2

1 Answer 1

2

The solution:

SELECT policy_change.user_id FROM policy_change ORDER BY policy_change.user_id DESC;

instead of

SELECT user_id FROM policy_change ORDER BY user_id DESC;
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.