fetch_search_path() and elog.c

Ferruccio Zamuner <nonsolosoft@diff.org>

From: Ferruccio Zamuner <nonsolosoft@diff.org>
To: pgsql-hackers@postgresql.org
Date: 2005-07-11T07:53:37Z
Lists: pgsql-hackers
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/