203

I have a postgresql function

CREATE OR REPLACE FUNCTION fixMissingFiles() RETURNS VOID AS $$
DECLARE
    deletedContactId integer;
    BEGIN
            SELECT INTO deletedContactId contact_id FROM myContacts WHERE id=206351;

            -- print the value of deletedContactId variable to the console

    END;
$$ LANGUAGE plpgsql;

How can I print the value of the deletedContactId to the console?

0

2 Answers 2

431

You can raise a notice in Postgres as follows:

RAISE NOTICE 'Value: %', deletedContactId;

Read here for more details.

Sign up to request clarification or add additional context in comments.

4 Comments

You can also raise exception 'Value: %', deletedContactId;, which will raise an actual exception (obviously). This was useful for me because my web app was not logging notice-level messages to my STDOUT. Raising an exception was the quickest way for me to debug something.
You can also raise notice 'Value: % %', deletedContactId, E'\n'; Which also gives you newline after this message.
use single commas and don't commit mistake of double commas like me
The levels may vary: DEBUG, LOG, INFO, NOTICE, WARNING, and EXCEPTION. I can even see LOG in my pgsql's docker log :'-)
3

Also, the SELECT INTO you're using looks like PostgreSQL for copying rows into a table. See, for example, the SELECT INTO doc page which state: "SELECT INTO -- define a new table from the results of a query"

In pgplSQL I'm used to seeing it in this order: SELECT INTO ... so your first line should probably be:

        SELECT contact_id INTO deletedContactId FROM myContacts WHERE id=206351;

As described in Executing a Query with a Single-Row Result.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.