473,503 Members | 1,681 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

server closed the connection unexpectedly

1 New Member
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 9013
michaelb
534 Recognized Expert Contributor
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
1778
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
18436
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
6706
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
4643
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
1733
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
2549
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
2094
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
5946
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
4539
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
7202
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
7084
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
7278
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7458
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.