INFO output despite "SET client_min_messages TO WARNING" just before - postgresql

postgresql-9.0.15 on CentOS 6.5. I have a plperlu function that outputs an INFO message. I want to suppress it during testing (using psql, which also behaves as below), but I can't even seem to do it from a pgAdminIII (1.18.1 for win2003) query window:
SET client_min_messages TO WARNING;
select my_info_outputting_function('lalala')
I run that and look in the "messages" tab, and there's my INFO message.
(This may appear similar to How to suppress INFO messages when running psql scripts , but I don't want to disable INFO messages for my whole session, just part of it and then set the minimum back to NOTICE.)
What am I doing wrong with the above code snippet? Does client_min_messages not apply to pl/perlu functions?
UPDATE: upon further investigation, it seems to happen even with plpgsql functions, not just plperlu functions:
create or replace function my_info_outputting_function() returns void as $$
begin
raise INFO 'this should not appear...';
return;
end;
$$ language plpgsql;
SET client_min_messages TO WARNING;
select my_info_outputting_function();
I run the above snippet in a pgAdminIII query window and "this should not appear" appears in the messages tab. Quoi?
Update 2: I also tried log_min_messages just in case. Same behaviour.

I asked on the postgresql-general mailing list and received an informative answer: what distinguishes INFO from NOTICE is that INFO does not have a level: it's intended to always go through, no matter what client_min_messages or anything else is set to, from functions that you would call specifically for INFO output. So in my case, the appropriate thing is to output only NOTICE from my function.

Related

Can't see raise messages in DBeaver or PgAdmin

When i execute the function below (in the picture), i can't find the raise message anywhere even in execution logs: is there anyway to make it appear.
script:
SELECT helloworld('myname');
CREATE OR REPLACE FUNCTION helloWorld(name text) RETURNS void AS $helloWorld$
DECLARE
BEGIN
RAISE LOG 'Hello, %', name;
END;
$helloWorld$ LANGUAGE plpgsql;
Messages with the level LOG typicall don't get sent to the client.
Either use RAISE NOTICE or set client_min_messages to log.
also dont do a basic mistake like me that to see the raise notice messages inside function, you first have to call that function by select public.helloworld()

TRY..CATCH Error_Line()....line

I'm looking at this example provided by MS as I'm trying to learn Try...Catch. I understand the syntax and Output (for the most part) but I have one question:
The Output will show the Error_Line as '4'. This is fine but if I remove the line break between GO and BEGIN TRY it'll show the Error_Line as '3'. I just want to understand the logic here.
What I imagine is happening is that SQL Server is counting the lines by beginning the batch immediately after GO, even if that line is blank but I do not know this for certain. Can anyone clarify? If that theory is correct, wouldn't that make finding errors difficult if scripts are written with line breaks like this?
-- Verify that the stored procedure does not already exist.
IF OBJECT_ID ( 'usp_GetErrorInfo', 'P' ) IS NOT NULL
DROP PROCEDURE usp_GetErrorInfo;
GO
-- Create procedure to retrieve error information.
CREATE PROCEDURE usp_GetErrorInfo
AS
SELECT
ERROR_NUMBER() AS ErrorNumber
,ERROR_SEVERITY() AS ErrorSeverity
,ERROR_STATE() AS ErrorState
,ERROR_PROCEDURE() AS ErrorProcedure
,ERROR_LINE() AS ErrorLine
,ERROR_MESSAGE() AS ErrorMessage;
GO
--Line 1
BEGIN TRY --Line 2
-- Generate divide-by-zero error. --Line 3
SELECT 1/0; --Line 4
END TRY
BEGIN CATCH
-- Execute error retrieval routine.
EXECUTE usp_GetErrorInfo;
END CATCH;
You can't really rely on ERROR_LINE(), especially when the error is thrown in internal stored procedure or there is dynamic T-SQL statement which is executed.
But do you really need the exact error line?
in real production code, the fix for the line causing the error may not be so obvious as in your example;
it will be better to debug the stored procedure or the function with the corresponding input parameter in order to reproduce the error
In this way it will be easier to fix an issue. In order to debug a SQL routine:
just script it
remove the drop and create stuff
add declare in front of the input parameters and initialized them with the values causing the error
Basically, instead of the exact error line (which can be easily fine having the correct input parameters and executing the routine) you may found useful two things:
which routing is causing the error (for example, you can add additional parameter to user usp_GetErrorInfo SP which is yielding the SP name as well
the input parameters which are causing the error (this can be done using separated table for logging the errors in the CATCH clause - you simple insert the input parameters in the table and information about the error)
Having this information, it will be easy to reproduce and then fix an issue (in many cases).

How to get Postgresql procedure warning messages?

While running a stored procedure, the procedure can raise warning messages.
Is there any way to get these messages using Postgresql driver(https://github.com/lib/pq) in Golang ?
For lib/pq driver, the answer is "still no".
See open issue and sources for details.
Meanwhile, another driver, https://github.com/jackc/pgx has support for messages in current (v4) version, see https://godoc.org/github.com/jackc/pgconn#Notice and earlier (see https://github.com/jackc/pgx/blob/v3/conn.go#L54)
Update: According to PR #932 which has been merged at Jan-2020, lib/pq supports NoticeHandler now. See example.
The answer appears to be no.
In my tests the Postgres server did not appear to send the warning with the results. Even if it did, returning an error along with the sql.Result would be confusing at best and would require lib/pq modifications. Raising an error in the function did return an error, but (obviously) no result.
If this is a critical requirement (and your function can support it) you might consider using a notification channel. Bear in mind that this would tie your code to Postgres.
--
Here is the function I used:
CREATE OR REPLACE function fugo()
RETURNS bool as $$
BEGIN
RAISE WARNING 'My function notice.' USING errcode = '01000';
return TRUE;
END;$$
language 'plpgsql';

pgp_sym_encrypt/pgp_sym_decrypt error handling

I had been using MySQL as database and had planned to move to postgresql. I had used aes_encrypt and aes_decrypt functions in MySQL extensively throughout my application. So whenever the encryption/decrytion fails, MySQL automatically returns 'null'.
I am unsure how to handle the same in postgresql. Tried using the pgp_sym_encrypt/pgp_sym_decrypt functions. If the encryption key is wrong, it throws error "Wrong key/corrupt data". I tried searching for some functions that could capture this error and return 'null' as in MySQL so that I need not modify my code. I had been searching but could not find one.
Has anybody used any error handling mechanism for individual queries? I had found that error handling can be done for procedures. But, I had to completely rewrite the entire application for that.
If you could share some details, it would be of great help. Thanks.
If you wish to avoid modifying your code and have the functions return NULL on error, you can do this by wrapping them in a PL/PgSQL function that uses a BEGIN ... EXCEPTION block to trap the error.
To do this, first I get the SQLSTATE for the error:
regress=# \set VERBOSITY verbose
regress=# SELECT pgp_sym_decrypt('fred','key');
ERROR: 39000: Wrong key or corrupt data
LOCATION: decrypt_internal, pgp-pgsql.c:607
I could use this directly in the error handler, but I prefer to use a symbolic name, so I look up the error name associated with 39000 in Appendix A - Error codes, finding that it's the generic function call error external_routine_invocation_exception. Not as specific as we would've liked, but it'll do.
Now a wrapper function is required. Something like this must be defined, with one function for each overloaded signature of pgp_sym_decrypt that you wish to support. For the (bytea,text) form that returns text, for example:
CREATE OR REPLACE FUNCTION pgp_sym_decrypt_null_on_err(data bytea, psw text) RETURNS text AS $$
BEGIN
RETURN pgp_sym_decrypt(data, psw);
EXCEPTION
WHEN external_routine_invocation_exception THEN
RAISE DEBUG USING
MESSAGE = format('Decryption failed: SQLSTATE %s, Msg: %s',
SQLSTATE,SQLERRM),
HINT = 'pgp_sym_encrypt(...) failed; check your key',
ERRCODE = 'external_routine_invocation_exception';
RETURN NULL;
END;
$$ LANGUAGE plpgsql;
I've chosen to preseve the original error in a DEBUG level message. Here's a comparison of the original and wrapper, with full message verbosity and debug level output.
Enable debug output to show the RAISE. Note that it also shows the *original query text of the pgp_decrypt_sym call, including parameters.
regress=# SET client_min_messages = DEBUG;
New wrapped function still reports the error if detailed logging is enabled, but returns NULL:
regress=# SELECT pgp_sym_decrypt_null_on_err('redsdfsfdsfd','bobsdf');
LOG: 00000: statement: SELECT pgp_sym_decrypt_null_on_err('redsdfsfdsfd','bobsdf');
LOCATION: exec_simple_query, postgres.c:860
DEBUG: 39000: Decryption failed: SQLSTATE 39000, Msg: Wrong key or corrupt data
HINT: pgp_sym_encrypt(...) failed; check your key
LOCATION: exec_stmt_raise, pl_exec.c:2806
pgp_sym_decrypt_null_on_err
-----------------------------
(1 row)
compared to the original, which fails:
regress=# SELECT pgp_sym_decrypt('redsdfsfdsfd','bobsdf');
LOG: 00000: statement: SELECT pgp_sym_decrypt('redsdfsfdsfd','bobsdf');
LOCATION: exec_simple_query, postgres.c:860
ERROR: 39000: Wrong key or corrupt data
LOCATION: decrypt_internal, pgp-pgsql.c:607
Note that both forms show the parameters the function was called with when it failed. The parameters won't be shown if you've used bind parameters ("prepared statements"), but you should still consider your logs to be security critical if you're using in-database encryption.
Personally, I think it's better to do crypto in the app, so the DB never has access to the keys.

Easiest way to silence "word is too long to be indexed" notices in PostgreSQL

I have some SQL statements that cause this to happen:
NOTICE: word is too long to be indexed
DETAIL: Words longer than 2047 characters are ignored.
What's the easiest way to not have these notices be generated in the first place? (It's a long story why I'd want to do it that way.)
An example of such a statement is this:
update rev set html = regexp_replace(html,
'***=<a href="' || old.url || '">',
'<a href="' || new.url || '">',
'gi')
where id in (
select id
from rev
where to_tsvector('tags_only', html) ##
plainto_tsquery('tags_only','<a href="' || old.url || '">')
)
It's not the A tags with long urls or anything causing the problem. It's probably embedded CDATA-style graphics. I don't care that they're not indexed, whatever they are. I just want these notices to not occur.
If you don't mind suppressing all notices just change PostgreSQL error reporting level. client_min_messages defines lowest level of error/warning/notice messages sent to client, log_min_messages does the same for messages saved in log. Possible values are: DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1, INFO, NOTICE, WARNING, ERROR, LOG, FATAL, PANIC
edit:
Disable for this query only: SET LOCAL client_min_messages TO WARNING;
Disable for this session only: SET SESSION client_min_messages TO WARNING;
Disable for this user: ALTER ROLE username SET client_min_messages TO WARNING;