473,598 Members | 3,409 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 9054
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
1787
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 written in python, I get a "server closed the connection unexpectedly" exception after executing a few sql commands. I print each query before I execute it (debugging) so I tried executing the
0
18450
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 use a hidden form to transmit information back to the server so it will know which is the next photo to load. All of this works as intended in modern versions of Internet Explorer, Netscape and Mozilla. With Opera, however, users receive a...
4
6717
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
4658
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 Windows 2000 professional, 1024 or 256 or 512 MB of RAMand 40Gb of disk. Postmaster shows version 7.4.1 Problem:
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 processing the request. The connection to the server was lost. Attempting reset: Failed." I've read through the posts but found no answer to the problem.
6
2554
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 them alive for later use. How do I convince the connection pool to drop the connections without closing my program. Regards, John
0
2107
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 of my other clients and I am unable to reproduce the problem. I have 4 web services and 2 of the web services seems to be working fine, they are basically authentication and displaying some information. The error happens when they try to...
1
5960
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 SqlConnection = New SqlConnection(connectionString)
4
4558
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:// www.dotnetspider.com/resources/2596-e-underlying-connection-was-closed-A-connection.aspx), it's a bug of .NET 2.0, and the author suggests that we use KeepAlive=false until Microsoft fixes it. It seems like almost 5 years have passes since the...
0
7991
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7902
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8395
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8398
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8265
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5850
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5438
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3898
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1504
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.