Unix Technical Forum

fetch_search_path() and elog.c

This is a discussion on fetch_search_path() and elog.c within the pgsql Hackers forums, part of the PostgreSQL category; --> Hi, I need to have in the log_line_prefix the search_path where the query has run. So last week I've ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Hackers

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-11-2008, 04:49 AM
Ferruccio Zamuner
 
Posts: n/a
Default fetch_search_path() and elog.c

Hi,

I need to have in the log_line_prefix the search_path where the query
has run.
So last week I've started to read elog.c and I was thinking about a
small patch there using a new "%S" option.

First I've introduced a small code:
case 'S':
{
List *search_path = fetch_search_path(false);
if (search_path != NIL) {
ListCell *l;
foreach(l, search_path)
{
char *nspname;

nspname = get_namespace_name(lfirst_oid(l));
if (nspname) /* watch out for
deleted namespace */
{
appendStringInfo(buf, "%s ", nspname);
pfree(nspname);
}
}
list_free(search_path);
}
break;
}

but in this way postgres was starting with a core dump writing on log.

Then I've add some code, and now it starts but it kills the postgres
process as soon as a query has sent to log:

$ diff -u elog.c elog_new.c
--- elog.c Sat Mar 12 02:55:15 2005
+++ elog_new.c Sun Jul 10 10:16:35 2005
@@ -67,7 +67,8 @@
#include "tcop/tcopprot.h"
#include "utils/memutils.h"
#include "utils/guc.h"
-
+#include "catalog/namespace.h"
+#include "catalog/pg_type.h"

/* Global variables */
ErrorContextCallback *error_context_stack = NULL;
@@ -1444,6 +1445,32 @@
case '%':
appendStringInfoChar(buf, '%');
break;
+ /* */
+ case 'S':
+ /* estrae il search_path */
+ if (MyProcPort && (MyProcPort->commandTag !=
NULL)) {
+ char *cmd=MyProcPort->commandTag;
+ if ((strcasecmp(cmd,"SELECT")== 0) ||
(strcasecmp(cmd,"INSERT")== 0) || (strcasecmp(cmd,"UPDATE")== 0) ||
(strcasecmp(cmd,"DELETE")== 0)) {
+
+ }
+ }
+ break;
+List *search_path = fetch_search_path(false);
+ if (search_path != NIL) {
+ ListCell *l;
+ foreach(l, search_path)
+ {
+ char *nspname;
+
+ nspname =
get_namespace_name(lfirst_oid(l));
+ if (nspname) /*
watch out for deleted namespace */
+ {
+ appendStringInfo(buf, "%s ",
nspname);
+ pfree(nspname);
+ }
+ }
+ list_free(search_path);
+ }
default:
/* format error - ignore it */
break;

And here there is the client output:

bash-2.05a$ psql prova -U pgsql
Welcome to psql 8.0.3, the PostgreSQL interactive terminal.

Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit

prova=# select * from prova;
server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
The connection to the server was lost. Attempting reset: Failed.
!>

Where can I start to understand which checks I've missed and how to gain
the output I need?


Thank you in advance, \ferz
---
NonSoLoSoft - http://www.nonsolosoft.com/

---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-11-2008, 04:49 AM
Tom Lane
 
Posts: n/a
Default Re: fetch_search_path() and elog.c

Ferruccio Zamuner <nonsolosoft@diff.org> writes:
> I need to have in the log_line_prefix the search_path where the query
> has run.
> So last week I've started to read elog.c and I was thinking about a
> small patch there using a new "%S" option.


There's no chance of that code working when not inside a transaction;
which means that in most of the scenarios where you really want a log
entry to be made, it will fail.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-11-2008, 04:49 AM
Ferruccio Zamuner
 
Posts: n/a
Default Re: fetch_search_path() and elog.c

Tom Lane wrote:

>Ferruccio Zamuner <nonsolosoft@diff.org> writes:
>
>
>>I need to have in the log_line_prefix the search_path where the query
>>has run.
>>So last week I've started to read elog.c and I was thinking about a
>>small patch there using a new "%S" option.
>>
>>

>
>There's no chance of that code working when not inside a transaction;
>which means that in most of the scenarios where you really want a log
>entry to be made, it will fail.
>
> regards, tom lane
>
>

Now I've found a simpler solution:

case 'S':
/* estrae il search_path */
if (namespace_search_path != NULL)
appendStringInfo(buf, "%s ", namespace_search_path);
break;

It seems to work.

But I would like to understand why previous code was wrong.

---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-11-2008, 04:49 AM
Alvaro Herrera
 
Posts: n/a
Default Re: fetch_search_path() and elog.c

On Mon, Jul 11, 2005 at 06:22:26PM +0200, Ferruccio Zamuner wrote:

> Now I've found a simpler solution:
>
> case 'S':
> /* estrae il search_path */
> if (namespace_search_path != NULL)
> appendStringInfo(buf, "%s ", namespace_search_path);
> break;
>
> It seems to work.
>
> But I would like to understand why previous code was wrong.


I think it was because you were doing catalog lookups (the
get_namespace_name() function, etc), and these don't work outside a
transaction.

--
Alvaro Herrera (<alvherre[a]alvh.no-ip.org>)
"¿Cómo puedes confiar en algo que pagas y que no ves,
y no confiar en algo que te dan y te lo muestran?" (Germán Poo)

---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 04:05 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com