Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bRlR0-0000SO-2T for pgsql-hackers@arkaria.postgresql.org; Mon, 25 Jul 2016 19:21:02 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1bRlQz-0005CD-I4 for pgsql-hackers@arkaria.postgresql.org; Mon, 25 Jul 2016 19:21:01 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bRlQy-0005Af-JH for pgsql-hackers@postgresql.org; Mon, 25 Jul 2016 19:21:00 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by magus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1bRlQv-00059C-4b for pgsql-hackers@postgresql.org; Mon, 25 Jul 2016 19:21:00 +0000 Received: from localhost ([127.0.0.1]:34973 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtp (Exim 4.84 (FreeBSD)) (envelope-from ) id 1bRlQt-000GBV-FD; Mon, 25 Jul 2016 19:20:55 +0000 From: Andrew Gierth To: Robert Haas Cc: Tom Lane , "pgsql-hackers\@postgresql.org" , Stephen Frost Subject: Re: No longer possible to query catalogs for index capabilities? In-Reply-To: (Robert Haas's message of "Mon, 25 Jul 2016 13:46:08 -0400") Message-ID: <877fc9o7h9.fsf@news-spur.riddles.org.uk> References: <87mvl5on7n.fsf@news-spur.riddles.org.uk> <23668.1469454897@sss.pgh.pa.us> <87invtolnc.fsf@news-spur.riddles.org.uk> <25197.1469457317@sss.pgh.pa.us> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (berkeley-unix) Date: Mon, 25 Jul 2016 20:20:55 +0100 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Pg-Spam-Score: -3.2 (---) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org --=-=-= Content-Type: text/plain Here is my proposed code (first cut; obviously it needs docs too). Opinions? -- Andrew (irc:RhodiumToad) --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=amcap.patch Content-Description: access methods capabilities diff --git a/src/backend/access/index/amapi.c b/src/backend/access/index/amapi.c index d347ebc..3e7e084 100644 --- a/src/backend/access/index/amapi.c +++ b/src/backend/access/index/amapi.c @@ -16,7 +16,9 @@ #include "access/amapi.h" #include "access/htup_details.h" #include "catalog/pg_am.h" +#include "catalog/pg_class.h" #include "catalog/pg_opclass.h" +#include "utils/elog.h" #include "utils/syscache.h" @@ -119,3 +121,95 @@ amvalidate(PG_FUNCTION_ARGS) PG_RETURN_BOOL(result); } + + +/* + * Test capabilities of an index or index AM. + */ +static Datum +indexam_capability(FunctionCallInfo fcinfo, + Oid amoid, char *nameptr, int namelen) +{ + IndexAmRoutine *routine = NULL; + bool ret; + + if (namelen < 1 || namelen > 14) + PG_RETURN_NULL(); + + routine = GetIndexAmRoutineByAmId(amoid); + + if (namelen == 10 && memcmp(nameptr, "amcanorder", 10) == 0) + ret = (routine->amcanorder) ? true : false; + else if (namelen == 14 && memcmp(nameptr, "amcanorderbyop", 14) == 0) + ret = (routine->amcanorderbyop) ? true : false; + else if (namelen == 13 && memcmp(nameptr, "amcanbackward", 13) == 0) + ret = (routine->amcanbackward) ? true : false; + else if (namelen == 11 && memcmp(nameptr, "amcanunique", 11) == 0) + ret = (routine->amcanunique) ? true : false; + else if (namelen == 13 && memcmp(nameptr, "amcanmulticol", 13) == 0) + ret = (routine->amcanmulticol) ? true : false; + else if (namelen == 13 && memcmp(nameptr, "amoptionalkey", 13) == 0) + ret = (routine->amoptionalkey) ? true : false; + else if (namelen == 13 && memcmp(nameptr, "amsearcharray", 13) == 0) + ret = (routine->amsearcharray) ? true : false; + else if (namelen == 13 && memcmp(nameptr, "amsearchnulls", 13) == 0) + ret = (routine->amsearchnulls) ? true : false; + else if (namelen == 9 && memcmp(nameptr, "amstorage", 9) == 0) + ret = (routine->amstorage) ? true : false; + else if (namelen == 13 && memcmp(nameptr, "amclusterable", 13) == 0) + ret = (routine->amclusterable) ? true : false; + else if (namelen == 11 && memcmp(nameptr, "ampredlocks", 11) == 0) + ret = (routine->ampredlocks) ? true : false; + else if (namelen == 11 && memcmp(nameptr, "amcanreturn", 11) == 0) + ret = (routine->amcanreturn) ? true : false; + else if (namelen == 10 && memcmp(nameptr, "amgettuple", 10) == 0) + ret = (routine->amgettuple) ? true : false; + else if (namelen == 11 && memcmp(nameptr, "amgetbitmap", 11) == 0) + ret = (routine->amgetbitmap) ? true : false; + else + PG_RETURN_NULL(); + + PG_RETURN_BOOL(ret); +} + +/* + * Test capability of an AM specified by the AM Oid. + */ +Datum +pg_indexam_has_capability(PG_FUNCTION_ARGS) +{ + Oid amoid = PG_GETARG_OID(0); + text *name = PG_GETARG_TEXT_PP(1); + char *nameptr = VARDATA_ANY(name); + int namelen = VARSIZE_ANY_EXHDR(name); + + return indexam_capability(fcinfo, amoid, nameptr, namelen); +} + +/* + * Test capability of the AM for an index specified by relation Oid. + */ +Datum +pg_index_has_capability(PG_FUNCTION_ARGS) +{ + Oid relid = PG_GETARG_OID(0); + text *name = PG_GETARG_TEXT_PP(1); + char *nameptr = VARDATA_ANY(name); + int namelen = VARSIZE_ANY_EXHDR(name); + Oid amoid; + HeapTuple tuple; + Form_pg_class rd_rel; + + tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid)); + if (!HeapTupleIsValid(tuple)) + elog(ERROR, "could not find tuple for relation %u", relid); + rd_rel = (Form_pg_class) GETSTRUCT(tuple); + if (rd_rel->relkind != RELKIND_INDEX) + ereport(ERROR, + (errcode(ERRCODE_WRONG_OBJECT_TYPE), + errmsg("relation %u is not an index", relid))); + amoid = rd_rel->relam; + ReleaseSysCache(tuple); + + return indexam_capability(fcinfo, amoid, nameptr, namelen); +} diff --git a/src/include/access/amapi.h b/src/include/access/amapi.h index 35f1061..0e2f9d7 100644 --- a/src/include/access/amapi.h +++ b/src/include/access/amapi.h @@ -171,4 +171,7 @@ extern IndexAmRoutine *GetIndexAmRoutineByAmId(Oid amoid); extern Datum amvalidate(PG_FUNCTION_ARGS); +extern Datum pg_indexam_has_capability(PG_FUNCTION_ARGS); +extern Datum pg_index_has_capability(PG_FUNCTION_ARGS); + #endif /* AMAPI_H */ diff --git a/src/include/catalog/pg_proc.h b/src/include/catalog/pg_proc.h index 5d233e3..4e696fa 100644 --- a/src/include/catalog/pg_proc.h +++ b/src/include/catalog/pg_proc.h @@ -564,6 +564,11 @@ DESCR("spgist index access method handler"); DATA(insert OID = 335 ( brinhandler PGNSP PGUID 12 1 0 0 0 f f f f t f v s 1 0 325 "2281" _null_ _null_ _null_ _null_ _null_ brinhandler _null_ _null_ _null_ )); DESCR("brin index access method handler"); +DATA(insert OID = 336 ( pg_indexam_has_capability PGNSP PGUID 12 1 0 0 0 f f f f t f s s 2 0 16 "26 25" _null_ _null_ _null_ _null_ _null_ pg_indexam_has_capability _null_ _null_ _null_ )); +DESCR("test capability of an index access method"); +DATA(insert OID = 337 ( pg_index_has_capability PGNSP PGUID 12 1 0 0 0 f f f f t f s s 2 0 16 "26 25" _null_ _null_ _null_ _null_ _null_ pg_index_has_capability _null_ _null_ _null_ )); +DESCR("test capability of an index"); + DATA(insert OID = 338 ( amvalidate PGNSP PGUID 12 1 0 0 0 f f f f t f v s 1 0 16 "26" _null_ _null_ _null_ _null_ _null_ amvalidate _null_ _null_ _null_ )); DESCR("validate an operator class"); DATA(insert OID = 3952 ( brin_summarize_new_values PGNSP PGUID 12 1 0 0 0 f f f f t f v s 1 0 23 "2205" _null_ _null_ _null_ _null_ _null_ brin_summarize_new_values _null_ _null_ _null_ )); --=-=-= Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers --=-=-=--