Search code examples
postgresqlgoogle-cloud-platformgoogle-cloud-sql

How to remove the LANGUAGE c functions in a PostgreSQL dump for import in CloudSQL?


I want to import a Postgres dump into CloudSQL. I actually use the uuid-ossp extension and some functions in Language c are exported into my dump.

However... functions in LANGUAGE c are not allowed on cloud SQL https://cloud.google.com/sql/docs/postgres/extensions#language and I need to remove them from the dump. (Note that those functions can be re-enabled later by activating the extention uuid-ossp in the cloudSQL database)

So... I need a trick to remove those functions from my dump file.

Extract from dump containing these functions :

--
-- TOC entry 542 (class 1255 OID 16529)
-- Name: uuid_generate_v1(); Type: FUNCTION; Schema: public; Owner: -
--

CREATE FUNCTION uuid_generate_v1() RETURNS uuid
    LANGUAGE c STRICT
    AS '$libdir/uuid-ossp', 'uuid_generate_v1';


--
-- TOC entry 543 (class 1255 OID 16530)
-- Name: uuid_generate_v1mc(); Type: FUNCTION; Schema: public; Owner: -
--

CREATE FUNCTION uuid_generate_v1mc() RETURNS uuid
    LANGUAGE c STRICT
    AS '$libdir/uuid-ossp', 'uuid_generate_v1mc';


--
-- TOC entry 544 (class 1255 OID 16531)
-- Name: uuid_generate_v3(uuid, text); Type: FUNCTION; Schema: public; Owner: -
--

CREATE FUNCTION uuid_generate_v3(namespace uuid, name text) RETURNS uuid
    LANGUAGE c IMMUTABLE STRICT
    AS '$libdir/uuid-ossp', 'uuid_generate_v3';


--
-- TOC entry 545 (class 1255 OID 16532)
-- Name: uuid_generate_v4(); Type: FUNCTION; Schema: public; Owner: -
--

CREATE FUNCTION uuid_generate_v4() RETURNS uuid
    LANGUAGE c STRICT
    AS '$libdir/uuid-ossp', 'uuid_generate_v4';


--
-- TOC entry 546 (class 1255 OID 16533)
-- Name: uuid_generate_v5(uuid, text); Type: FUNCTION; Schema: public; Owner: -
--

CREATE FUNCTION uuid_generate_v5(namespace uuid, name text) RETURNS uuid
    LANGUAGE c IMMUTABLE STRICT
    AS '$libdir/uuid-ossp', 'uuid_generate_v5';


--
-- TOC entry 547 (class 1255 OID 16534)
-- Name: uuid_nil(); Type: FUNCTION; Schema: public; Owner: -
--

CREATE FUNCTION uuid_nil() RETURNS uuid
    LANGUAGE c IMMUTABLE STRICT
    AS '$libdir/uuid-ossp', 'uuid_nil';


--
-- TOC entry 548 (class 1255 OID 16535)
-- Name: uuid_ns_dns(); Type: FUNCTION; Schema: public; Owner: -
--

CREATE FUNCTION uuid_ns_dns() RETURNS uuid
    LANGUAGE c IMMUTABLE STRICT
    AS '$libdir/uuid-ossp', 'uuid_ns_dns';


--
-- TOC entry 549 (class 1255 OID 16536)
-- Name: uuid_ns_oid(); Type: FUNCTION; Schema: public; Owner: -
--

CREATE FUNCTION uuid_ns_oid() RETURNS uuid
    LANGUAGE c IMMUTABLE STRICT
    AS '$libdir/uuid-ossp', 'uuid_ns_oid';


--
-- TOC entry 550 (class 1255 OID 16537)
-- Name: uuid_ns_url(); Type: FUNCTION; Schema: public; Owner: -
--

CREATE FUNCTION uuid_ns_url() RETURNS uuid
    LANGUAGE c IMMUTABLE STRICT
    AS '$libdir/uuid-ossp', 'uuid_ns_url';


--
-- TOC entry 512 (class 1255 OID 16538)
-- Name: uuid_ns_x500(); Type: FUNCTION; Schema: public; Owner: -
--

CREATE FUNCTION uuid_ns_x500() RETURNS uuid
    LANGUAGE c IMMUTABLE STRICT
    AS '$libdir/uuid-ossp', 'uuid_ns_x500';

Solution

  • Solution 1 : Clean database and re-export the dump

    (Edited according @Laurenz Albe comment)

    1. Perform a DROP FUNCTION on all the LANGUAGE c functions
    2. Dump the database
    3. Add the CREATE EXTENSION uuid-ossp at the begening of the dump

    Solution 2 : Parse and clean the dump

    Not the best, but seems to work.

    It's based on the fact that, by luck, the wanted functions to remove are on 3 lines:

    # Extract all the language c functions (1 line before and 1 line after the "LANGUAGE c" line
    grep -B 1 -A 1 "LANGUAGE c" schema.sql  > language-c-functions.sql
    
    # Make the diff between the files, and keep line that are not in both files
    diff schema.sql language-c-functions.sql | grep \^\< | sed 's/^<\ //' > cleaned.sql