johnlemon
johnlemon

Reputation: 21509

How to log dblink_exec results?

In PostgreSQL I use dblink_exec. Is there any way I can log each time dblink gets executed ?

Upvotes: 0

Views: 966

Answers (1)

Erwin Brandstetter
Erwin Brandstetter

Reputation: 658572

I don't know of a simple way like a config setting to log just some functions in PostgreSQL. (If anybody does, I am interested!)

You could hack the source of the function to raise a notice on every call and recompile.

Or, if your knowledge of C is rusty like mine, you could, for a very small cost to performance, wrap the function in a plpgsql function that raises a notice / warning before or after calling dblink_exec. Like this:

CREATE OR REPLACE FUNCTION mydblink_exec(text)
  RETURNS text AS
$$
RAISE NOTICE 'My text. Called with: "%"', $1;

RETURN dblink_exec($1);
$$
  LANGUAGE plpgsql VOLATILE STRICT;

You could move the original dblink_exec() functions to another name or schema to make that a drop-in replacement. Only do that if you must. See below.

Careful to cover all forms of dblink_exec() that are needed. I quote the manual:

dblink_exec(text connname, text sql [, bool fail_on_error]) returns text
dblink_exec(text connstr, text sql [, bool fail_on_error]) returns text
dblink_exec(text sql [, bool fail_on_error]) returns text

You would make use of function overloading for that.
Careful with upgrading to a new version of PostgreSQL or reinstalling the dblink contrib module, if you in fact move the original functions to a custom name / schema.

Upvotes: 1

Related Questions