Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 33F8F1337B77 for ; Wed, 3 Nov 2010 18:29:52 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 20207-07 for ; Wed, 3 Nov 2010 21:29:44 +0000 (UTC) X-Greylist: delayed 00:12:46.165669 by SQLgrey-1.7.6 Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.126.171]) by mail.postgresql.org (Postfix) with ESMTP id 83AE21337B56 for ; Wed, 3 Nov 2010 18:29:44 -0300 (ADT) Received: from [192.168.1.172] (p5DC80507.dip0.t-ipconnect.de [93.200.5.7]) by mrelayeu.kundenserver.de (node=mreu2) with ESMTP (Nemesis) id 0MPc3R-1P93k01aAT-004eyP; Wed, 03 Nov 2010 22:16:57 +0100 Message-ID: <4CD1D141.7070108@altum.de> Date: Wed, 03 Nov 2010 22:16:49 +0100 From: Dirk Heinrichs Organization: Privat User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.12) Gecko/20101027 Thunderbird/3.1.6 MIME-Version: 1.0 To: Dimitri Fontaine CC: pgsql-bugs@postgresql.org Subject: Re: BUG #5740: contrib/spi/moddatetime.c doesn't work with timezones. References: <201011021514.oA2FE1FT054444@wwwmaster.postgresql.org> In-Reply-To: X-Enigmail-Version: 1.1.2 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="------------enigDC12F062468D0C3BFCCF5CF9" X-Provags-ID: V02:K0:zsemE/Zlf8QZy3uuFqE9Bzq/eKmXAkRk6x1ZWWL9C6M hswyQIIeYFAFh8e6MuYGrI9ZILH6UupbCJ2fCZeXwDQ1WJq8nT TUHSzeJZlk4QfQmpl4WtoA5uOzzD5ChMwWIRTOtPQe9lLM3Pv7 PtbT56fLFABfTKx/hh7UwaFIUTvmSGWPv//jwQgsd0aibCNRcV PoAJAfTlxgvi4+RfQKiqg== X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001 X-Spam-Level: X-Archive-Number: 201011/27 X-Sequence-Number: 28607 This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --------------enigDC12F062468D0C3BFCCF5CF9 Content-Type: multipart/mixed; boundary="------------080803020508060208050408" This is a multi-part message in MIME format. --------------080803020508060208050408 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Am 02.11.2010 23:09, schrieb Dimitri Fontaine: > So I guess that you need to modify very little code to get the trigger > to work for both types. Please find the patch attached. It's against 8.4.5. Bye... Dirk --------------080803020508060208050408 Content-Type: text/x-diff; name="moddatetime.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="moddatetime.patch" diff -u spi.old/moddatetime.c spi/moddatetime.c --- spi.old/moddatetime.c 2010-10-01 15:35:31.000000000 +0200 +++ spi/moddatetime.c 2010-11-03 20:08:38.030772830 +0100 @@ -22,6 +22,7 @@ PG_MODULE_MAGIC; =20 extern Datum moddatetime(PG_FUNCTION_ARGS); +extern Datum moddatetimetz(PG_FUNCTION_ARGS); =20 PG_FUNCTION_INFO_V1(moddatetime); =20 @@ -111,7 +112,7 @@ /* 1 is the number of items in the arrays attnum and newdt. attnum is the positional number of the field to be updated. newdt is the new datetime stamp. - NOTE that attnum and newdt are not arrays, but then a 1 ellement array + NOTE that attnum and newdt are not arrays, but then a 1 element array is not an array any more then they are. Thus, they can be considered a= one element array. */ @@ -123,6 +124,111 @@ relname, SPI_result); =20 /* Clean up */ + pfree(relname); + + return PointerGetDatum(rettuple); +} + +PG_FUNCTION_INFO_V1(moddatetimetz); + +Datum +moddatetimetz(PG_FUNCTION_ARGS) +{ + TriggerData *trigdata =3D (TriggerData *) fcinfo->context; + Trigger *trigger; /* to get trigger name */ + int nargs; /* # of arguments */ + int attnum; /* positional number of field to change */ + Datum newdt; /* The current datetime. */ + char **args; /* arguments */ + char *relname; /* triggered relation name */ + Relation rel; /* triggered relation */ + HeapTuple rettuple =3D NULL; + TupleDesc tupdesc; /* tuple description */ + + if (!CALLED_AS_TRIGGER(fcinfo)) + /* internal error */ + elog(ERROR, "moddatetimetz: not fired by trigger manager"); + + if (TRIGGER_FIRED_FOR_STATEMENT(trigdata->tg_event)) + /* internal error */ + elog(ERROR, "moddatetimetz: cannot process STATEMENT events"); + + if (TRIGGER_FIRED_AFTER(trigdata->tg_event)) + /* internal error */ + elog(ERROR, "moddatetimetz: must be fired before event"); + + if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event)) + /* internal error */ + elog(ERROR, "moddatetimetz: must be fired before event"); + else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event)) + rettuple =3D trigdata->tg_newtuple; + else + /* internal error */ + elog(ERROR, "moddatetimetz: cannot process DELETE events"); + + rel =3D trigdata->tg_relation; + relname =3D SPI_getrelname(rel); + + trigger =3D trigdata->tg_trigger; + + nargs =3D trigger->tgnargs; + + if (nargs !=3D 1) + /* internal error */ + elog(ERROR, "moddatetimetz (%s): A single argument was expected", reln= ame); + + args =3D trigger->tgargs; + /* must be the field layout? */ + tupdesc =3D rel->rd_att; + + /* Get the current datetime. */ + newdt =3D DirectFunctionCall3(timestamptz_in, + CStringGetDatum("now"), + ObjectIdGetDatum(InvalidOid), + Int32GetDatum(-1)); + + /* + * This gets the position in the tuple of the field we want. args[0] be= ing + * the name of the field to update, as passed in from the trigger. + */ + attnum =3D SPI_fnumber(tupdesc, args[0]); + + /* + * This is were we check to see if the field we are supposed to update + * even exits. The above function must return -1 if name not found? + */ + if (attnum < 0) + ereport(ERROR, + (errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION), + errmsg("\"%s\" has no attribute \"%s\"", + relname, args[0]))); + + /* + * OK, this is where we make sure the timestamp field that we are + * modifying is really a timestamptz field. Hay, error checking, what a= + * novel idea !-) + */ + if (SPI_gettypeid(tupdesc, attnum) !=3D TIMESTAMPTZOID) + ereport(ERROR, + (errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION), + errmsg("attribute \"%s\" of \"%s\" must be type TIMESTAMP WITH TIME= ZONE", + args[0], relname))); + +/* 1 is the number of items in the arrays attnum and newdt. + attnum is the positional number of the field to be updated. + newdt is the new datetime stamp. + NOTE that attnum and newdt are not arrays, but then a 1 element array + is not an array any more then they are. Thus, they can be considered a= + one element array. +*/ + rettuple =3D SPI_modifytuple(rel, rettuple, 1, &attnum, &newdt, NULL); + + if (rettuple =3D=3D NULL) + /* internal error */ + elog(ERROR, "moddatetimetz (%s): %d returned by SPI_modifytuple", + relname, SPI_result); + +/* Clean up */ pfree(relname); =20 return PointerGetDatum(rettuple); diff -u spi.old/moddatetime.sql.in spi/moddatetime.sql.in --- spi.old/moddatetime.sql.in 2010-10-01 15:35:31.000000000 +0200 +++ spi/moddatetime.sql.in 2010-11-03 19:41:51.806037512 +0100 @@ -7,3 +7,8 @@ RETURNS trigger AS 'MODULE_PATHNAME' LANGUAGE C; + +CREATE OR REPLACE FUNCTION moddatetimetz() +RETURNS trigger +AS 'MODULE_PATHNAME' +LANGUAGE C; --------------080803020508060208050408-- --------------enigDC12F062468D0C3BFCCF5CF9 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.10 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iD8DBQFM0dFI8NVtnsLkZ7sRAhSFAJ9skqbgalw6ap4ClnVWvuHDlL8KawCcCQyu /SSDS72PnsJg8SO4jEIkSqg= =tvI2 -----END PGP SIGNATURE----- --------------enigDC12F062468D0C3BFCCF5CF9--