Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 7D4859FB28A for ; Fri, 19 Jan 2007 02:02:37 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-new, port 10024) with ESMTP id 77509-05 for ; Fri, 19 Jan 2007 02:02:35 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from mailbox.samurai.com (mailbox.samurai.com [205.207.28.82]) by postgresql.org (Postfix) with ESMTP id 8D7B29F9A8D for ; Fri, 19 Jan 2007 02:02:34 -0400 (AST) Received: from localhost (mailbox.samurai.com [205.207.28.82]) by mailbox.samurai.com (Postfix) with ESMTP id 8547C62237; Fri, 19 Jan 2007 01:02:33 -0500 (EST) X-Virus-Scanned: by amavisd-new at mailbox.samurai.com Received: from mailbox.samurai.com ([205.207.28.82]) by localhost (mailbox.samurai.com [205.207.28.82]) (amavisd-new, port 10024) with LMTP id REyxDO-XCKsk; Fri, 19 Jan 2007 01:02:31 -0500 (EST) Received: from [192.168.1.106] (d226-84-131.home.cgocable.net [24.226.84.131]) by mailbox.samurai.com (Postfix) with ESMTP id 49AD0621F8; Fri, 19 Jan 2007 01:02:31 -0500 (EST) Subject: Re: pg_get_domaindef From: Neil Conway To: FAST PostgreSQL Cc: pgsql-patches@postgresql.org In-Reply-To: <12753.10901169180733.fast.fujitsu.com.au@MHS> References: <12753.10901169180733.fast.fujitsu.com.au@MHS> Content-Type: text/plain Date: Fri, 19 Jan 2007 01:02:30 -0500 Message-Id: <1169186550.27197.10.camel@localhost.localdomain> Mime-Version: 1.0 X-Mailer: Evolution 2.8.1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200701/347 X-Sequence-Number: 155 On Sat, 2007-01-20 at 02:28 +1100, FAST PostgreSQL wrote: > Attached is a small patch that implements the pg_get_domaindef(oid) function. A few minor comments: - don't use C++-style comments - why does this code append a "-" to the output when SPI_processed != 1, rather than erroring out? + if (spirc != SPI_OK_SELECT) + elog(ERROR, "failed to get pg_type tuple for domain %u", domainOid); + if (SPI_processed != 1) + appendStringInfo(&buf, "-"); + else + { - you probably want to elog(ERROR) if typeTuple is invalid: + /* + * Get the base type of the domain + */ + typeTuple = SearchSysCache(TYPEOID, + ObjectIdGetDatum(basetypeOid), + 0, 0, 0); + + if (HeapTupleIsValid(typeTuple)) + { + typebasetype = pstrdup(NameStr(((Form_pg_type) GETSTRUCT(typeTuple))->typname)); + appendStringInfo(&buf, "%s ", quote_identifier(typebasetype)); + } + ReleaseSysCache(typeTuple); It's also debatable whether the function ought to be using a *combination* of the syscaches and manual system catalog queries, since these two views may be inconsistent. I guess this is a widespread problem, though. + if (typnotnull || constraint != NULL) + { + if ( ( (contype != NULL) && (strcmp(contype, "c") != 0) ) || typnotnull ) + { + appendStringInfo(&buf, "CONSTRAINT "); + } + if (typnotnull) + { + appendStringInfo(&buf, "NOT NULL "); + } + } + if (constraint != NULL) + { + appendStringInfo(&buf, quote_identifier(constraint)); + } This logic seems pretty awkward. Perhaps simpler would be a check for typnotnull (and then appending "CONSTRAINT NOT NULL"), and then handling the non-typnotnull branch separately. -Neil