Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id D6AA41337B80 for ; Thu, 4 Nov 2010 15:41:16 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 69498-01 for ; Thu, 4 Nov 2010 18:41:09 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.126.186]) by mail.postgresql.org (Postfix) with ESMTP id 33FD71337B8D for ; Thu, 4 Nov 2010 15:41:09 -0300 (ADT) Received: from [192.168.1.172] (p5DC80523.dip0.t-ipconnect.de [93.200.5.35]) by mrelayeu.kundenserver.de (node=mreu0) with ESMTP (Nemesis) id 0Lj7Ag-1OhTXu2UsI-00dBwf; Thu, 04 Nov 2010 19:41:00 +0100 Message-ID: <4CD2FE35.50702@altum.de> Date: Thu, 04 Nov 2010 19:40:53 +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: Tom Lane CC: Dimitri Fontaine , pgsql-bugs@postgresql.org Subject: Re: BUG #5740: contrib/spi/moddatetime.c doesn't work with timezones. References: <201011021514.oA2FE1FT054444@wwwmaster.postgresql.org> <4CD1D141.7070108@altum.de> <20091.1288842928@sss.pgh.pa.us> In-Reply-To: <20091.1288842928@sss.pgh.pa.us> X-Enigmail-Version: 1.1.2 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="------------enigF934CFD32ABC4F54E5D240A5" X-Provags-ID: V02:K0:IaKVl2tg9FzsCgcgQAYJZnRFvDPWK1jy9pXmilvPasf /2iSxwNwByLEvzN/heaMWcqBYZkJGeT8vCmqIIoZ0URVRjOHN+ bu+02jxoFn3dbhYzQ2VVI2OLyjU54zAbaS+RU4vOPo7S+/23bR jRPe1PxreydDFtwwQ4BbpqkogVTuEThepgD+jVM0TEHpL6k/XS sCWZUiGhUaMEaVfAv6WYA== 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/38 X-Sequence-Number: 28618 This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --------------enigF934CFD32ABC4F54E5D240A5 Content-Type: multipart/mixed; boundary="------------000007070008070500010604" This is a multi-part message in MIME format. --------------000007070008070500010604 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Am 04.11.2010 04:55, schrieb Tom Lane: > I don't actually see any point in having two functions at all. Since > the trigger is examining the column type internally, it could perfectly= > well do the right thing at runtime depending on column type. Got the point. Here's another patch, hope my limited C skills are sufficient... Works here, at least. BTW: Is there a way to achieve the same in pure PL/pgSQL or PL/perl? Bye... Dirk --------------000007070008070500010604 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-04 19:32:11.877235825 +0100 @@ -38,6 +38,7 @@ Relation rel; /* triggered relation */ HeapTuple rettuple =3D NULL; TupleDesc tupdesc; /* tuple description */ + Oid stampOid; =20 if (!CALLED_AS_TRIGGER(fcinfo)) /* internal error */ @@ -75,12 +76,6 @@ /* must be the field layout? */ tupdesc =3D rel->rd_att; =20 - /* Get the current datetime. */ - newdt =3D DirectFunctionCall3(timestamp_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. @@ -102,16 +97,29 @@ * modifying is really a timestamp field. Hay, error checking, what a * novel idea !-) */ - if (SPI_gettypeid(tupdesc, attnum) !=3D TIMESTAMPOID) - ereport(ERROR, - (errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION), - errmsg("attribute \"%s\" of \"%s\" must be type TIMESTAMP", - args[0], relname))); + stampOid =3D SPI_gettypeid(tupdesc, attnum); + if (stampOid =3D=3D TIMESTAMPOID) + /* Get the current datetime. */ + newdt =3D DirectFunctionCall3(timestamp_in, + CStringGetDatum("now"), + ObjectIdGetDatum(InvalidOid), + Int32GetDatum(-1)); + else if (stampOid =3D=3D TIMESTAMPTZOID) + /* Get the current datetime. */ + newdt =3D DirectFunctionCall3(timestamptz_in, + CStringGetDatum("now"), + ObjectIdGetDatum(InvalidOid), + Int32GetDatum(-1)); + else + ereport(ERROR, + (errcode(ERRCODE_TRIGGERED_ACTION_EXCEPTION), + errmsg("attribute \"%s\" of \"%s\" must be type TIMESTAMP", + args[0], relname))); =20 /* 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. */ --------------000007070008070500010604-- --------------enigF934CFD32ABC4F54E5D240A5 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/ iD8DBQFM0v428NVtnsLkZ7sRAkWwAJwK4xa0l+yqxRkE3SXoK/h+gUJslQCggDh6 F8LZSsLecrdQvmW1IGjcgvc= =e+LI -----END PGP SIGNATURE----- --------------enigF934CFD32ABC4F54E5D240A5--