473,651 Members | 2,566 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 9061
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
1788
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
18451
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
6721
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
4661
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
2555
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
2109
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
5961
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
4562
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
8275
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,...
1
8465
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8579
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...
0
7297
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5612
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
4283
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2699
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 we have to send another system
1
1909
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1587
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.