Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id EC92CB5DBDE for ; Wed, 12 Oct 2011 14:36:37 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 19613-09 for ; Wed, 12 Oct 2011 17:36:31 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id 5458EB5DBC2 for ; Wed, 12 Oct 2011 14:36:31 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id p9CHaRdG019679; Wed, 12 Oct 2011 13:36:27 -0400 (EDT) To: Magnus Hagander cc: Heikki Linnakangas , Andrew Dunstan , Robert Haas , Stefan Kaltenbrunner , PostgreSQL Subject: Re: [BUGS] *.sql contrib files contain unresolvable MODULE_PATHNAME In-reply-to: References: <20111012075314.GA25470@piware.de> <4E954895.6040701@kaltenbrunner.cc> <4E95523E.1010608@enterprisedb.com> <12966.1318428759@sss.pgh.pa.us> <4E95A4BE.8050501@dunslane.net> <4E95AD97.8040800@enterprisedb.com> <13817.1318432821@sss.pgh.pa.us> <4E95B131.4010505@enterprisedb.com> <14185.1318434032@sss.pgh.pa.us> Comments: In-reply-to Magnus Hagander message dated "Wed, 12 Oct 2011 18:39:28 +0200" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <19672.1318440966.0@sss.pgh.pa.us> Date: Wed, 12 Oct 2011 13:36:27 -0400 Message-ID: <19678.1318440987@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.404 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RP_MATCHES_RCVD=-0.504 X-Spam-Level: X-Archive-Number: 201110/737 X-Sequence-Number: 196543 ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <19672.1318440966.1@sss.pgh.pa.us> Magnus Hagander writes: > On Wed, Oct 12, 2011 at 17:40, Tom Lane wrote: >> The only thing the \echo approach will cost us is a few more lines of C >> code in execute_extension_script(), and I think it's more than worth >> that given the evident scope of the problem. > +1. PFA, a sample patch for this --- I've only bothered to change one script file here, but will of course do the rest if there are no further objections. The technique actually works even better than I expected, because of the seemingly nowhere documented fact that \quit in a script file doesn't terminate psql, only processing of the script. So what I get is regression=# \i ~/postgres/share/extension/cube--1.0.sql Use "CREATE EXTENSION cube" to load this file. regression=# which is about as good as one could hope for. (Looks like a patch to the psql docs is upcoming, too.) regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-patch; charset="us-ascii" Content-ID: <19672.1318440966.2@sss.pgh.pa.us> Content-Description: echo-in-extension-files.patch Content-Transfer-Encoding: quoted-printable diff --git a/contrib/cube/cube--1.0.sql b/contrib/cube/cube--1.0.sql index ee9febe005315bf13e93a9ef216a7411fc13a445..0d259c0969d9df4a00f160bf1f= c00407273e2b1d 100644 *** a/contrib/cube/cube--1.0.sql --- b/contrib/cube/cube--1.0.sql *************** *** 1,5 **** --- 1,7 ---- /* contrib/cube/cube--1.0.sql */ = + \echo Use "CREATE EXTENSION cube" to load this file. \quit + = -- Create the user-defined type for N-dimensional boxes = CREATE FUNCTION cube_in(cstring) diff --git a/src/backend/commands/extension.c b/src/backend/commands/exten= sion.c index 3af15dd38bb7044ec6f733787cad6897b204ccd3..ba1e2c45cd97c89265912d338a= 98f52bf48ea4b0 100644 *** a/src/backend/commands/extension.c --- b/src/backend/commands/extension.c *************** *** 33,38 **** --- 33,39 ---- #include "catalog/indexing.h" #include "catalog/namespace.h" #include "catalog/objectaccess.h" + #include "catalog/pg_collation.h" #include "catalog/pg_depend.h" #include "catalog/pg_extension.h" #include "catalog/pg_namespace.h" *************** execute_extension_script(Oid extensionOi *** 855,880 **** CurrentExtensionObject =3D extensionOid; PG_TRY(); { ! char *sql =3D read_extension_script_file(control, filename); = /* * If it's not relocatable, substitute the target schema name for * occcurrences of @extschema@. * ! * For a relocatable extension, we just run the script as-is. There ! * cannot be any need for @extschema@, else it wouldn't be ! * relocatable. */ if (!control->relocatable) { const char *qSchemaName =3D quote_identifier(schemaName); = ! sql =3D text_to_cstring( ! DatumGetTextPP( ! DirectFunctionCall3(replace_text, ! CStringGetTextDatum(sql), ! CStringGetTextDatum("@extschema@"), ! CStringGetTextDatum(qSchemaName)))); } = /* --- 856,894 ---- CurrentExtensionObject =3D extensionOid; PG_TRY(); { ! char *c_sql =3D read_extension_script_file(control, filename); ! Datum t_sql; ! = ! /* We use various functions that want to operate on text datums */ ! t_sql =3D CStringGetTextDatum(c_sql); ! = ! /* ! * Reduce any lines beginning with "\echo" to empty. This allows ! * scripts to contain messages telling people not to run them via ! * psql, which has been found to be necessary due to old habits. ! */ ! t_sql =3D DirectFunctionCall4Coll(textregexreplace, ! C_COLLATION_OID, ! t_sql, ! CStringGetTextDatum("^\\\\echo.*$"), ! CStringGetTextDatum(""), ! CStringGetTextDatum("ng")); = /* * If it's not relocatable, substitute the target schema name for * occcurrences of @extschema@. * ! * For a relocatable extension, we needn't do this. There cannot be ! * any need for @extschema@, else it wouldn't be relocatable. */ if (!control->relocatable) { const char *qSchemaName =3D quote_identifier(schemaName); = ! t_sql =3D DirectFunctionCall3(replace_text, ! t_sql, ! CStringGetTextDatum("@extschema@"), ! CStringGetTextDatum(qSchemaName)); } = /* *************** execute_extension_script(Oid extensionOi *** 883,897 **** */ if (control->module_pathname) { ! sql =3D text_to_cstring( ! DatumGetTextPP( ! DirectFunctionCall3(replace_text, ! CStringGetTextDatum(sql), ! CStringGetTextDatum("MODULE_PATHNAME"), ! CStringGetTextDatum(control->module_pathname)))); } = ! execute_sql_string(sql, filename); } PG_CATCH(); { --- 897,912 ---- */ if (control->module_pathname) { ! t_sql =3D DirectFunctionCall3(replace_text, ! t_sql, ! CStringGetTextDatum("MODULE_PATHNAME"), ! CStringGetTextDatum(control->module_pathname)); } = ! /* And now back to C string */ ! c_sql =3D text_to_cstring(DatumGetTextPP(t_sql)); ! = ! execute_sql_string(c_sql, filename); } PG_CATCH(); { ------- =_aaaaaaaaaa0--