From 4eb49db7ae634fab9af7437b2e7b6388dfd83bd3 Mon Sep 17 00:00:00 2001 From: Tom Lane <tgl@sss.pgh.pa.us> Date: Thu, 17 Feb 2011 15:03:30 -0500 Subject: [PATCH] Fix contrib/pg_trgm to have smoother updates from 9.0. Take care of some loose ends in the update-from-unpackaged script, and apply some ugly hacks to ensure that it produces the same catalog state as the fresh-install script. Per discussion, this seems like a safer plan than having two different catalog states that both call themselves "pg_trgm 1.0", even if it's not immediately clear that the subtle differences would ever matter. Also, fix the stub function gin_extract_trgm() so that it works instead of just bleating. Needed because this function will get called during a regular dump and reload, if there are any indexes using its opclass. The user won't have an opportunity to update the extension till later, so telling him to do so is unhelpful. --- contrib/pg_trgm/pg_trgm--1.0.sql | 23 +++++-- contrib/pg_trgm/pg_trgm--unpackaged--1.0.sql | 65 ++++++++++++++++++-- contrib/pg_trgm/trgm_gin.c | 10 +-- 3 files changed, 83 insertions(+), 15 deletions(-) diff --git a/contrib/pg_trgm/pg_trgm--1.0.sql b/contrib/pg_trgm/pg_trgm--1.0.sql index 79ea5090422..d9cdd23d85f 100644 --- a/contrib/pg_trgm/pg_trgm--1.0.sql +++ b/contrib/pg_trgm/pg_trgm--1.0.sql @@ -109,9 +109,6 @@ CREATE OPERATOR CLASS gist_trgm_ops FOR TYPE text USING gist AS OPERATOR 1 % (text, text), - OPERATOR 2 <-> (text, text) FOR ORDER BY pg_catalog.float_ops, - OPERATOR 3 pg_catalog.~~ (text, text), - OPERATOR 4 pg_catalog.~~* (text, text), FUNCTION 1 gtrgm_consistent (internal, text, int, oid, internal), FUNCTION 2 gtrgm_union (bytea, internal), FUNCTION 3 gtrgm_compress (internal), @@ -119,9 +116,19 @@ AS FUNCTION 5 gtrgm_penalty (internal, internal, internal), FUNCTION 6 gtrgm_picksplit (internal, internal), FUNCTION 7 gtrgm_same (gtrgm, gtrgm, internal), - FUNCTION 8 gtrgm_distance (internal, text, int, oid), STORAGE gtrgm; +-- Add operators and support functions that are new in 9.1. We do it like +-- this, leaving them "loose" in the operator family rather than bound into +-- the gist_trgm_ops opclass, because that's the only state that can be +-- reproduced during an upgrade from 9.0 (see pg_trgm--unpackaged--1.0.sql). + +ALTER OPERATOR FAMILY gist_trgm_ops USING gist ADD + OPERATOR 2 <-> (text, text) FOR ORDER BY pg_catalog.float_ops, + OPERATOR 3 pg_catalog.~~ (text, text), + OPERATOR 4 pg_catalog.~~* (text, text), + FUNCTION 8 (text, text) gtrgm_distance (internal, text, int, oid); + -- support functions for gin CREATE FUNCTION gin_extract_value_trgm(text, internal) RETURNS internal @@ -143,10 +150,14 @@ CREATE OPERATOR CLASS gin_trgm_ops FOR TYPE text USING gin AS OPERATOR 1 % (text, text), - OPERATOR 3 pg_catalog.~~ (text, text), - OPERATOR 4 pg_catalog.~~* (text, text), FUNCTION 1 btint4cmp (int4, int4), FUNCTION 2 gin_extract_value_trgm (text, internal), FUNCTION 3 gin_extract_query_trgm (text, internal, int2, internal, internal, internal, internal), FUNCTION 4 gin_trgm_consistent (internal, int2, text, int4, internal, internal, internal, internal), STORAGE int4; + +-- Add operators that are new in 9.1. + +ALTER OPERATOR FAMILY gin_trgm_ops USING gin ADD + OPERATOR 3 pg_catalog.~~ (text, text), + OPERATOR 4 pg_catalog.~~* (text, text); diff --git a/contrib/pg_trgm/pg_trgm--unpackaged--1.0.sql b/contrib/pg_trgm/pg_trgm--unpackaged--1.0.sql index ba070384d0f..0e1c63f0752 100644 --- a/contrib/pg_trgm/pg_trgm--unpackaged--1.0.sql +++ b/contrib/pg_trgm/pg_trgm--unpackaged--1.0.sql @@ -10,7 +10,6 @@ ALTER EXTENSION pg_trgm ADD type gtrgm; ALTER EXTENSION pg_trgm ADD function gtrgm_in(cstring); ALTER EXTENSION pg_trgm ADD function gtrgm_out(gtrgm); ALTER EXTENSION pg_trgm ADD function gtrgm_consistent(internal,text,integer,oid,internal); -ALTER EXTENSION pg_trgm ADD function gtrgm_distance(internal,text,integer,oid); ALTER EXTENSION pg_trgm ADD function gtrgm_compress(internal); ALTER EXTENSION pg_trgm ADD function gtrgm_decompress(internal); ALTER EXTENSION pg_trgm ADD function gtrgm_penalty(internal,internal,internal); @@ -19,13 +18,52 @@ ALTER EXTENSION pg_trgm ADD function gtrgm_union(bytea,internal); ALTER EXTENSION pg_trgm ADD function gtrgm_same(gtrgm,gtrgm,internal); ALTER EXTENSION pg_trgm ADD operator family gist_trgm_ops using gist; ALTER EXTENSION pg_trgm ADD operator class gist_trgm_ops using gist; -ALTER EXTENSION pg_trgm ADD function gin_extract_value_trgm(text,internal); -ALTER EXTENSION pg_trgm ADD function gin_extract_query_trgm(text,internal,smallint,internal,internal,internal,internal); -ALTER EXTENSION pg_trgm ADD function gin_trgm_consistent(internal,smallint,text,integer,internal,internal,internal,internal); ALTER EXTENSION pg_trgm ADD operator family gin_trgm_ops using gin; ALTER EXTENSION pg_trgm ADD operator class gin_trgm_ops using gin; --- these were not in 9.0: +-- These functions had different names/signatures in 9.0. We can't just +-- drop and recreate them because they are linked into the GIN opclass, +-- so we need some ugly hacks. + +-- First, absorb them into the extension under their old names. + +ALTER EXTENSION pg_trgm ADD function gin_extract_trgm(text, internal); +ALTER EXTENSION pg_trgm ADD function gin_extract_trgm(text, internal, int2, internal, internal); +ALTER EXTENSION pg_trgm ADD function gin_trgm_consistent(internal,smallint,text,integer,internal,internal); + +-- Fix the names, and then do CREATE OR REPLACE to adjust the function +-- bodies to be correct (ie, reference the correct C symbol). + +ALTER FUNCTION gin_extract_trgm(text, internal) + RENAME TO gin_extract_value_trgm; +CREATE OR REPLACE FUNCTION gin_extract_value_trgm(text, internal) +RETURNS internal +AS 'MODULE_PATHNAME' +LANGUAGE C IMMUTABLE STRICT; + +ALTER FUNCTION gin_extract_trgm(text, internal, int2, internal, internal) + RENAME TO gin_extract_query_trgm; +CREATE OR REPLACE FUNCTION gin_extract_query_trgm(text, internal, int2, internal, internal) +RETURNS internal +AS 'MODULE_PATHNAME' +LANGUAGE C IMMUTABLE STRICT; + +-- gin_trgm_consistent didn't change name. + +-- Last, fix the parameter lists by means of direct UPDATE on the pg_proc +-- entries. This is ugly as can be, but there's no other way to do it +-- while preserving the identities (OIDs) of the functions. + +UPDATE pg_catalog.pg_proc +SET pronargs = 7, proargtypes = '25 2281 21 2281 2281 2281 2281' +WHERE oid = 'gin_extract_query_trgm(text,internal,int2,internal,internal)'::pg_catalog.regprocedure; + +UPDATE pg_catalog.pg_proc +SET pronargs = 8, proargtypes = '2281 21 25 23 2281 2281 2281 2281' +WHERE oid = 'gin_trgm_consistent(internal,smallint,text,integer,internal,internal)'::pg_catalog.regprocedure; + + +-- These were not in 9.0: CREATE FUNCTION similarity_dist(text,text) RETURNS float4 @@ -38,3 +76,20 @@ CREATE OPERATOR <-> ( PROCEDURE = similarity_dist, COMMUTATOR = '<->' ); + +CREATE FUNCTION gtrgm_distance(internal,text,int,oid) +RETURNS float8 +AS 'MODULE_PATHNAME' +LANGUAGE C IMMUTABLE STRICT; + +-- Add new stuff to the operator classes. See comment in pg_trgm--1.0.sql. + +ALTER OPERATOR FAMILY gist_trgm_ops USING gist ADD + OPERATOR 2 <-> (text, text) FOR ORDER BY pg_catalog.float_ops, + OPERATOR 3 pg_catalog.~~ (text, text), + OPERATOR 4 pg_catalog.~~* (text, text), + FUNCTION 8 (text, text) gtrgm_distance (internal, text, int, oid); + +ALTER OPERATOR FAMILY gin_trgm_ops USING gin ADD + OPERATOR 3 pg_catalog.~~ (text, text), + OPERATOR 4 pg_catalog.~~* (text, text); diff --git a/contrib/pg_trgm/trgm_gin.c b/contrib/pg_trgm/trgm_gin.c index cdaa72ea611..aaca1f97377 100644 --- a/contrib/pg_trgm/trgm_gin.c +++ b/contrib/pg_trgm/trgm_gin.c @@ -29,14 +29,16 @@ Datum gin_trgm_consistent(PG_FUNCTION_ARGS); /* * This function can only be called if a pre-9.1 version of the GIN operator * class definition is present in the catalogs (probably as a consequence - * of upgrade-in-place). Complain. + * of upgrade-in-place). Cope. */ Datum gin_extract_trgm(PG_FUNCTION_ARGS) { - ereport(ERROR, - (errmsg("GIN operator class for pg_trgm is out of date"), - errhint("Please drop and re-create the pg_trgm catalog entries."))); + if (PG_NARGS() == 3) + return gin_extract_value_trgm(fcinfo); + if (PG_NARGS() == 7) + return gin_extract_query_trgm(fcinfo); + elog(ERROR, "unexpected number of arguments to gin_extract_trgm"); PG_RETURN_NULL(); } -- GitLab