473,385 Members | 1,587 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

server closed the connection unexpectedly

this is regarding C trigger function in postgresql 8.2

The function trigf reports the number of rows in the table ttest and skips the actual operation if the command attempts to insert a null value into the column x. (So the trigger acts as a not-null constraint but doesn't abort the transaction.)

First, the table definition:
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE ttest (
  2.     x integer
  3. );
  4.  
This is the source code of the trigger function:
Expand|Select|Wrap|Line Numbers
  1. #include "postgres.h"
  2. #include "executor/spi.h"       /* this is what you need to work with SPI */
  3. #include "commands/trigger.h"   /* ... and triggers */
  4.  
  5. extern Datum trigf(PG_FUNCTION_ARGS);
  6.  
  7. PG_FUNCTION_INFO_V1(trigf);
  8.  
  9. Datum
  10. trigf(PG_FUNCTION_ARGS)
  11. {
  12.     TriggerData *trigdata = (TriggerData *) fcinfo->context;
  13.     TupleDesc   tupdesc;
  14.     HeapTuple   rettuple;
  15.     char       *when;
  16.     bool        checknull = false;
  17.     bool        isnull;
  18.     int         ret, i;
  19.  
  20.     /* make sure it's called as a trigger at all */
  21.     if (!CALLED_AS_TRIGGER(fcinfo))
  22.         elog(ERROR, "trigf: not called by trigger manager");
  23.  
  24.     /* tuple to return to executor */
  25.     if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
  26.         rettuple = trigdata->tg_newtuple;
  27.     else
  28.         rettuple = trigdata->tg_trigtuple;
  29.  
  30.     /* check for null values */
  31.     if (!TRIGGER_FIRED_BY_DELETE(trigdata->tg_event)
  32.         && TRIGGER_FIRED_BEFORE(trigdata->tg_event))
  33.         checknull = true;
  34.  
  35.     if (TRIGGER_FIRED_BEFORE(trigdata->tg_event))
  36.         when = "before";
  37.     else
  38.         when = "after ";
  39.  
  40.     tupdesc = trigdata->tg_relation->rd_att;
  41.  
  42.     /* connect to SPI manager */
  43.     if ((ret = SPI_connect()) < 0)
  44.         elog(INFO, "trigf (fired %s): SPI_connect returned %d", when, ret);
  45.  
  46.     /* get number of rows in table */
  47.     ret = SPI_exec("SELECT count(*) FROM ttest", 0);
  48.  
  49.     if (ret < 0)
  50.         elog(NOTICE, "trigf (fired %s): SPI_exec returned %d", when, ret);
  51.  
  52.     /* count(*) returns int8, so be careful to convert */
  53.     i = DatumGetInt64(SPI_getbinval(SPI_tuptable->vals[0],
  54.                                     SPI_tuptable->tupdesc,
  55.                                     1,
  56.                                     &isnull));
  57.  
  58.     elog (INFO, "trigf (fired %s): there are %d rows in ttest", when, i);
  59.  
  60.     SPI_finish();
  61.  
  62.     if (checknull)
  63.     {
  64.         SPI_getbinval(rettuple, tupdesc, 1, &isnull);
  65.         if (isnull)
  66.             rettuple = NULL;
  67.     }
  68.  
  69.     return PointerGetDatum(rettuple);
  70. }
  71.  
After compiling the source code, declare the function and the triggers:
Expand|Select|Wrap|Line Numbers
  1. CREATE FUNCTION trigf() RETURNS trigger
  2.     AS 'filename'
  3.     LANGUAGE C;
  4.  
  5. CREATE TRIGGER tbefore BEFORE INSERT OR UPDATE OR DELETE ON ttest 
  6.     FOR EACH ROW EXECUTE PROCEDURE trigf();
  7.  
  8. CREATE TRIGGER tafter AFTER INSERT OR UPDATE OR DELETE ON ttest 
  9.     FOR EACH ROW EXECUTE PROCEDURE trigf();
  10.  
Now you can test the operation of the trigger:

=> INSERT INTO ttest VALUES (NULL)

THE PROBLEM STARTS WHILE EXECUTING THIS INSERT QUERY.
IT SHOWS:
server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.

THE ERROR LOG FILE SHOWS:

SERVER PROCESS(PID 195) TERMINATED BY SIGNAL11


Please clarify on what should be done.
May 4 '07 #1
1 8997
michaelb
534 Expert 512MB
Please read the Posting Guidelines at the top of this forum. Make sure you quote your code snippets appropriately using the CODE tags.

It might be helpful to give us the OS and the database version you are using.

One good technique when dealing with problems like this is to enable verbose logging. You can put something like this in postgresql.conf:

Expand|Select|Wrap|Line Numbers
  1. client_min_messages = notice
  2. log_min_messages = notice
  3. log_min_error_statement = notice
  4. log_statement = all
Look here for all available options.

After you edited the Postgres configuration file you need to either restart the database server or make it re-read the config. by running pg_ctl reload

The code you posted references some filename:
Expand|Select|Wrap|Line Numbers
  1. CREATE FUNCTION trigf() RETURNS trigger
  2.     AS 'filename'
  3.     LANGUAGE C;
  4.  
Is this indeed the name of the compiled C code?
Did you build it as a shared library?
May 5 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jeff | last post by:
I have the following packages installed on my system: libpq3: 7.3.2r1-5 python-pygresql: 7.3.2r1-5 postgresql: 7.2.1-2woody2 postgresql-client: 7.2.1-2woody2 In my application, which is...
0
by: BGS | last post by:
I have a web site (www.on-the-matrix.com) that displays photos in a "slide show" format on ASPX pages that run in an inline frame on an ASP page (or in a separate pop-up window). The ASPX pages...
4
by: rs | last post by:
how I the client tell the server that the socket is closed? or this there an even that informs the server that the clients socket is close? Oh, I am using vb.net 2003 Thanks
1
by: Janez Kostanjček | last post by:
Hello everyone, this is my first post to postgres mailing list. System that I use: I am using Postgres under last CygWin published on public mirrors at the moment. OS system runing is...
4
by: ruben | last post by:
Hi: I'm getting this error when accessing a table with certain WHERE condition: "server closed the connection unexpectedly This probably means the server terminated abnormally before or while...
6
by: John J. Hughes II | last post by:
I have an application that connects to an SQL server. I need to be able to disconnect all connection that I opened. I am closing and disposing the connection but the connection pool is keeping...
0
by: DBC User | last post by:
Hello, One of my client reports they are getting "System.Net.WebException: The underlying connection was closed: The connection was closed unexpectedly." error suddently from the last week. None...
1
by: mark.norgate | last post by:
Hi I have a problem with a repeater and a data connection. In my Page_Load, I have the following code: Dim connectionString As String = WebConfig.ConnectionString Dim connection As...
4
by: Sin Jeong-hun | last post by:
I don't get the message so it's hard to debug that, but some of my clients report that they get "The underlying connection was closed unexpectedly" exception. According to this site (http://...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.