Thread: Using host variables -- segmentation fault

Using host variables -- segmentation fault

From
Yadnyesh Joshi
Date:
Hi,

I am trying to use C variables in embedded sql statements.
But I am getting a segmentation fault.
Also, out put at server is :
LOG:  unexpected EOF on client connection
When I try to insert values without using C variables, they're added to database without any problem.
Problem occurs when I try to insert values from C variables.

Here is the simple program I have written -

/*first.c*/
/*Assume create table simple (s int);*/
#include<stdio.h>
int main()
{
        EXEC SQL BEGIN DECLARE SECTION;
        char *target="mydb@localhost";
        int i;
        char msg[10];
        EXEC SQL END DECLARE SECTION;

        //strcpy(msg,"\'AAAA\'");

        EXEC SQL CONNECT TO :target;

        EXEC SQL INSERT INTO simple values (6);
        EXEC SQL COMMIT;
        EXEC SQL INSERT INTO simple values (:i);
        EXEC SQL COMMIT;

        //EXEC SQL INSERT INTO fromprg values (:i,:msg);
        //EXEC SQL COMMIT;

        EXEC SQL DISCONNECT;
}

Then as usual, I executed two commands -

ecpg first.pgc
gcc first.c -o first -lecpg

I get segmentation fault.
LOG:  unexpected EOF on client connection

The first EXEC SQL INSERT INTO simple values (6); executes fine. The value is added in the database as I am doing a COMMIT.

However, segmentation fault occurs at EXEC SQL INSERT INTO simple values (:i);
What can be the problem?

Thank you,
Yadnyesh.


Find out what India is talking about on - Yahoo! Answers India
Send FREE SMS to your friend's mobile from Yahoo! Messenger Version 8. Get it NOW

Re: Using host variables -- segmentation fault

From
Michael Fuhr
Date:
On Mon, Oct 16, 2006 at 05:16:06AM -0700, Yadnyesh Joshi wrote:
> The first EXEC SQL INSERT INTO simple values (6); executes fine. The
> value is added in the database as I am doing a COMMIT.
>
> However, segmentation fault occurs at EXEC SQL INSERT INTO simple values (:i);
> What can be the problem?

The program you posted doesn't initialize i but I'd expect that to
result in a garbage value being inserted instead of a segmentation
fault.  Do you still get a segmentation fault if you initialize i?
What about if you comment out the line that inserts i?

Your program doesn't return a value from main().  I haven't worked
on any platforms where that would cause a segmentation fault but it
should be fixed in any case.

What platform are you on?  What version of PostgreSQL?  Did you get
a core dump?  If so, what does a stack trace show?  What do you see
if you add "ECPGdebug(1, stderr);" at the beginning of the program?

--
Michael Fuhr

Re: Using host variables -- segmentation fault

From
Yadnyesh Joshi
Date:


----- Original Message ----
From: Michael Fuhr <mike@fuhr.org>
To: Yadnyesh Joshi <yadnyesh_joshi@yahoo.com>
Cc: pgsql-novice@postgresql.org
Sent: Monday, 16 October, 2006 7:11:24 PM
Subject: Re: [NOVICE] Using host variables -- segmentation fault

On Mon, Oct 16, 2006 at 05:16:06AM -0700, Yadnyesh Joshi wrote:
> The first EXEC SQL INSERT INTO simple values (6); executes fine. The
> value is added in the database as I am doing a COMMIT.
>
> However, segmentation fault occurs at EXEC SQL INSERT INTO simple values (:i);
> What can be the problem?

The program you posted doesn't initialize i but I'd expect that to
result in a garbage value being inserted instead of a segmentation
fault.  Do you still get a segmentation fault if you initialize i?
What about if you comment out the line that inserts i?

Initializing i doesn't make any difference. I still get a segmentation fault.
If I comment the line that inserts i, program executes fine. It runs to the comepletion.

Your program doesn't return a value from main().  I haven't worked
on any platforms where that would cause a segmentation fault but it
should be fixed in any case.

Return value is not a problem. If I add return 1; at the end, the problem persists.

What platform are you on?  What version of PostgreSQL?
PostgreSQL version - 8.1.5
I am working on Fedora Core 4 - 64 bit version.
Did you get a core dump?  If so, what does a stack trace show?
I didn't get core dump.

What do you see if you add "ECPGdebug(1, stderr);" at the beginning of the program?
[12328]: ECPGdebug: set to 1
[12328]: ECPGconnect: opening database mydb on localhost port <DEFAULT>
[12328]: got TSD connection
[12328]: ECPGexecute line 16: QUERY: insert into simple values( 6 ) on connection mydb
[12328]: ECPGexecute line 16 Ok: INSERT 0 1
[12328]: got TSD connection
[12328]: ECPGtrans line 17 action = commit connection = mydb
Before inserting i
[12328]: got TSD connection
Segmentation fault

I added a printf statement just before inserting into i.
For your refernce, here is first.pgc file again..

#include<stdio.h>
int main()
{
        EXEC SQL BEGIN DECLARE SECTION;
        char *target="mydb@localhost";
        int i;
        char msg[10];
        EXEC SQL END DECLARE SECTION;

        ECPGdebug(1, stderr);
        //strcpy(msg,"\'AAAA\'");
        i=9;

        EXEC SQL CONNECT TO :target;

        EXEC SQL INSERT INTO simple values (6);
        EXEC SQL COMMIT;
        printf("Before inserting i\n");
        EXEC SQL INSERT INTO simple values (:i);
        EXEC SQL COMMIT;

        //EXEC SQL INSERT INTO fromprg values (:i,:msg);
        //EXEC SQL COMMIT;

        EXEC SQL DISCONNECT;
        return 1;
}


Thanks,
Yadnyesh.




--
Michael Fuhr


Send free SMS to your Friends on Mobile from your Yahoo! Messenger. Download Now! http://messenger.yahoo.com/download.php

Re: Using host variables -- segmentation fault

From
Yadnyesh Joshi
Date:
Hi,
I found out what was the problem.
I had not set LD_LIBRARY_PATH environment variable.
But I didn't see anything in documentation which says that if I don't set the environment variable, I won't be able to use host variables in C.
Morevoer, the usual insert statement (without using host variables) was working fine; so it created more trouble..
Regards,
Yadnyesh.


----- Original Message ----
From: Yadnyesh Joshi <yadnyesh_joshi@yahoo.com>
To: pgsql-novice@postgresql.org
Sent: Monday, 16 October, 2006 9:15:59 PM
Subject: Re: [NOVICE] Using host variables -- segmentation fault



----- Original Message ----
From: Michael Fuhr <mike@fuhr.org>
To: Yadnyesh Joshi <yadnyesh_joshi@yahoo.com>
Cc: pgsql-novice@postgresql.org
Sent: Monday, 16 October, 2006 7:11:24 PM
Subject: Re: [NOVICE] Using host variables -- segmentation fault

On Mon, Oct 16, 2006 at 05:16:06AM -0700, Yadnyesh Joshi wrote:
> The first EXEC SQL INSERT INTO simple values (6); executes fine. The
> value is added in the database as I am doing a COMMIT.
>
> However, segmentation fault occurs at EXEC SQL INSERT INTO simple values (:i);
> What can be the problem?

The program you posted doesn't initialize i but I'd expect that to
result in a garbage value being inserted instead of a segmentation
fault.  Do you still get a segmentation fault if you initialize i?
What about if you comment out the line that inserts i?

Initializing i doesn't make any difference. I still get a segmentation fault.
If I comment the line that inserts i, program executes fine. It runs to the comepletion.

Your program doesn't return a value from main().  I haven't worked
on any platforms where that would cause a segmentation fault but it
should be fixed in any case.

Return value is not a problem. If I add return 1; at the end, the problem persists.

What platform are you on?  What version of PostgreSQL?
PostgreSQL version - 8.1.5
I am working on Fedora Core 4 - 64 bit version.
Did you get a core dump?  If so, what does a stack trace show?
I didn't get core dump.

What do you see if you add "ECPGdebug(1, stderr);" at the beginning of the program?
[12328]: ECPGdebug: set to 1
[12328]: ECPGconnect: opening database mydb on localhost port <DEFAULT>
[12328]: got TSD connection
[12328]: ECPGexecute line 16: QUERY: insert into simple values( 6 ) on connection mydb
[12328]: ECPGexecute line 16 Ok: INSERT 0 1
[12328]: got TSD connection
[12328]: ECPGtrans line 17 action = commit connection = mydb
Before inserting i
[12328]: got TSD connection
Segmentation fault

I added a printf statement just before inserting into i.
For your refernce, here is first.pgc file again..

#include<stdio.h>
int main()
{
        EXEC SQL BEGIN DECLARE SECTION;
        char *target="mydb@localhost";
        int i;
        char msg[10];
        EXEC SQL END DECLARE SECTION;

        ECPGdebug(1, stderr);
        //strcpy(msg,"\'AAAA\'");
        i=9;

        EXEC SQL CONNECT TO :target;

        EXEC SQL INSERT INTO simple values (6);
        EXEC SQL COMMIT;
        printf("Before inserting i\n");
        EXEC SQL INSERT INTO simple values (:i);
        EXEC SQL COMMIT;

        //EXEC SQL INSERT INTO fromprg values (:i,:msg);
        //EXEC SQL COMMIT;

        EXEC SQL DISCONNECT;
        return 1;
}


Thanks,
Yadnyesh.




--
Michael Fuhr


Send free SMS to your Friends on Mobile from your Yahoo! Messenger. Download Now! http://messenger.yahoo.com/download.php


Send free SMS to your Friends on Mobile from your Yahoo! Messenger. Download Now! http://messenger.yahoo.com/download.php

Re: Using host variables -- segmentation fault

From
Michael Fuhr
Date:
On Mon, Oct 16, 2006 at 10:02:12AM -0700, Yadnyesh Joshi wrote:
> I found out what was the problem.
> I had not set LD_LIBRARY_PATH environment variable.
> But I didn't see anything in documentation which says that if I don't set
> the environment variable, I won't be able to use host variables in C.

That's because setting LD_LIBRARY_PATH has nothing to do with using
host variables, at least not directly.  Apparently the library the
runtime linker was finding was incompatible with your object code
but the problem only manifested itself under certain conditions.
What version of ecpg did you use (ecpg --version)?  What version
of libecpg were you linking against?  What does "ldd the_program"
show both with and without LD_LIBRARY_PATH set?

--
Michael Fuhr