191

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

407

You can raise a notice in Postgres as follows:

RAISE NOTICE 'Value: %', deletedContactId;

Read here for more details.

4
  • 25
    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. Commented Jul 6, 2016 at 14:19
  • 14
    You can also raise notice 'Value: % %', deletedContactId, E'\n'; Which also gives you newline after this message. Commented Mar 5, 2019 at 15:22
  • 2
    use single commas and don't commit mistake of double commas like me Commented Jul 13, 2021 at 19:30
  • The levels may vary: DEBUG, LOG, INFO, NOTICE, WARNING, and EXCEPTION. I can even see LOG in my pgsql's docker log :'-) Commented Jul 29, 2021 at 20:29
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
  • 1
    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.
    – Community Bot
    Commented Jan 29, 2022 at 8:35

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