postmaster core dumps with SPI_repalloc

postmaster core dumps with SPI_repalloc

Post by Andrey Y. Mosienk » Wed, 15 May 2002 19:20:06



Hello All!

I noticed when I create memory pool with SPI_palloc(less than 100) and then
try to reallocate it with SPI_repalloc postmaster core dumps.

But when first SPI_palloc(100 or bigger) everything works fine!

What can be wrong?

Here is trigger example:

CREATE SEQUENCE base_id_seq;

CREATE TABLE "base" (
    "id" int4 DEFAULT nextval('base_id_seq'::text) NOT NULL,
    "field" text NOT NULL,
    "field_m" text NOT NULL,
    "field_n" text NOT NULL,
    "changed" timestamp NOT NULL DEFAULT NOW(),
    PRIMARY KEY ("id"), UNIQUE ("id")
);

INSERT INTO "base" (field, field_m, field_n) VALUES ('test', 'test', 'test');

CREATE FUNCTION archive() RETURNS OPAQUE AS
'/home/feo/WORK/backup/archive.so' LANGUAGE 'C';

CREATE TRIGGER "archive" AFTER UPDATE
    ON base FOR EACH ROW
    EXECUTE PROCEDURE archive();

archive.c:

#include "executor/spi.h"       /* this is what you need to work with SPI */
#include "commands/trigger.h"   /* -"- and triggers */

extern Datum archive(PG_FUNCTION_ARGS);

PG_FUNCTION_INFO_V1(archive);

Datum
archive(PG_FUNCTION_ARGS)
{
    //TriggerData *trigdata = (TriggerData *) fcinfo->context;

    int             ret, i;
    char*           sql;
    char*           fields_names;
    char*           values;

    // Ok ... this is a trigger function ...
    // were we called from a trigger??
    //
    if (!CALLED_AS_TRIGGER(fcinfo))
        elog(ERROR, "archive: not fired by trigger manager");

    // Connect to the SPI manager
    //
    if( (ret = SPI_connect()) == SPI_OK_CONNECT ) {
        fields_names = SPI_palloc(100);
        values = SPI_palloc(100);
        sql = SPI_palloc(40);

        for (i = 0; i < 10; i++) {
            SPI_repalloc(fields_names, sizeof(fields_names) + 100);
            SPI_repalloc(values, sizeof(values) + 100);
        };
        SPI_pfree(fields_names);
        SPI_pfree(values);
        SPI_pfree(sql);

        SPI_finish();
    };
    // Or if everything failes (no SPI connection)
    // we'll just return ... nothing!
    return PointerGetDatum(NULL);

Quote:}

PgSQL log:
DEBUG:  query: update base set field_m = 'test4', changed = NOW() where id = '1';
DEBUG:  server process (pid 5909) was terminated by signal 11

--
with respection Andrey Feofilactovich.

ICQ: 28073807

---------------------------(end of broadcast)---------------------------

 
 
 

postmaster core dumps with SPI_repalloc

Post by Tom La » Thu, 16 May 2002 00:41:59



Quote:>     char*           fields_names;
>     ...
>             SPI_repalloc(fields_names, sizeof(fields_names) + 100);

Not sure what you were expecting this to do ... but you do realize
that sizeof(fields_names) is a constant here?  (Probably 4, but you
did not mention your platform.)

                        regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster