Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mfom3-00018j-TW for pgsql-general@arkaria.postgresql.org; Wed, 27 Oct 2021 19:39:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mfom2-0008Hw-SI for pgsql-general@arkaria.postgresql.org; Wed, 27 Oct 2021 19:39:46 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mfom2-0008Hm-G7 for pgsql-general@lists.postgresql.org; Wed, 27 Oct 2021 19:39:46 +0000 Received: from mail-pl1-x62e.google.com ([2607:f8b0:4864:20::62e]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mfom0-0000dc-5H for pgsql-general@lists.postgresql.org; Wed, 27 Oct 2021 19:39:45 +0000 Received: by mail-pl1-x62e.google.com with SMTP id n18so2777588plc.2 for ; Wed, 27 Oct 2021 12:39:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:content-transfer-encoding:mime-version:subject:message-id:date :to; bh=NOnNf8fxHPYHq1fCCmyyIOsUzFJ+JY8EjbIcUbEPGOc=; b=XusBdSCWlCs0mWZHFJwGa+DaDVYQFTLof+rQi1+Wp267dZt6+vfuxNwUZf3mjbOgkE x4PHkPo4PZfcJojEeuxyll67RePJaVul35Mk4oowl4u4x3rOeBaRZeHGrkwRDcQ4k72v SuAX1Yp/7aVizmBZ1iCt7dZrVTTisC81lbze+J9OWyKnghJtkvsurQpnhQvRufV7AoF5 1QBrkdzM/ui53msIZWaVZSYzFH57N2oXJ/2w8D64TicVBIlQP2Pa5V2ZrD3NT6lL2kOu jlS6Br5pUikPnFWCDXE/JY6JvqadTh6RiCNkuV4vvWCiwlEbmYXsfejsbKHrZFseTILk Cf7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:content-transfer-encoding:mime-version :subject:message-id:date:to; bh=NOnNf8fxHPYHq1fCCmyyIOsUzFJ+JY8EjbIcUbEPGOc=; b=is/kqlpb0kacMuLs5o0wmEP1XrecZAWzEIU0dGkfND3/SjDwcanch4CkZG+boLHRsN T19uxu7QdCtSNczenUtCMkrBgRRertPfVATD25QeHJ5H1Kdg6g0eXTMHNfpep7C/kfcb uxKAFpTlXL4jY27S0/gdAZz74PU6JLLuNUJLrNZ6y5YyG6wsvGgZ1RDb2SePG4zJ6J77 jncLEdb8Debk8ofBoN8gi6v9olyCpTzW+ArFZfcipwJd7F5QTZLDvQwOJgE7ehMpyUUM HqehK7CLrMHaRxzvYWA+tr1jiXiS/aatX2ngK36Zbmcra+oA4RmIH2hfwy8l70qE8JwN X61Q== X-Gm-Message-State: AOAM5322uxNBydY+JfPRylFq/eXSVM7RvBcn+8mXhlTNtdWeRXTZmEow 0KamVYoLP71TUK2mT75cUwUsdgP8mmo= X-Google-Smtp-Source: ABdhPJzO6xz5GD8cKnqr5jmqFGd28PtHyfTw2TScyinYWYq/VAy7J/whC/bZsr5hhPiG+xh1hcn6+Q== X-Received: by 2002:a17:90b:3647:: with SMTP id nh7mr7931659pjb.45.1635363582564; Wed, 27 Oct 2021 12:39:42 -0700 (PDT) Received: from smtpclient.apple (node-1w7jr9pis2gio3aejyv2ivtii.ipv6.telus.net. [2001:569:5119:b800:1974:efff:47eb:b12a]) by smtp.gmail.com with ESMTPSA id j16sm810889pfj.16.2021.10.27.12.39.41 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 27 Oct 2021 12:39:42 -0700 (PDT) From: Louise Grandjonc Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.120.0.1.13\)) Subject: Segmentation fault in volatile c function Message-Id: <5FEFE970-6A6B-454E-A3C7-FFD0FAAFDABD@gmail.com> Date: Wed, 27 Oct 2021 12:39:41 -0700 To: pgsql-general@lists.postgresql.org X-Mailer: Apple Mail (2.3654.120.0.1.13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, I'm creating my first ever extension. The function that I'm trying to = write takes the schema and name of a table, and adds it in a table with = all the tables the extension follows. In that table I want the schema, name and oid of the table. I created a C function for that. Here is the code ``` Datum add_mygreat_table(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(add_mygreat_table); Datum add_mygreat_table(PG_FUNCTION_ARGS) { char *tableSchema, *tableName; Oid tableNamespace, relationId; tableSchema =3D PG_GETARG_CSTRING(0); tableName =3D PG_GETARG_CSTRING(1); tableNamespace =3D get_namespace_oid(tableSchema, false); relationId =3D get_relname_relid(tableName, tableNamespace); if (relationId =3D=3D NULL) { ereport(ERROR, (errmsg("could not create remote table: " "relation does not exist"))); } Datum values[3]; bool isNulls[3]; memset(isNulls, false, sizeof(isNulls)); values[0] =3D CStringGetTextDatum(tableSchema); values[1] =3D CStringGetTextDatum(tableName); values[2] =3D ObjectIdGetDatum(relationId); Relation greatTable =3D table_open(MyGreatTablesRelationId(), = RowExclusiveLock); HeapTuple tuple =3D heap_form_tuple(RelationGetDescr(greatTable), = values, isNulls); /* * CatalogTupleInsert() is originally for PostgreSQL's catalog. = However, * it is used at here for convenience. */ CatalogTupleInsert(greatTable, tuple); table_close(greatTable, RowExclusiveLock); heap_freetuple(tuple); CommandCounterIncrement(); ereport(NOTICE, (errmsg("Table %s is now a great table", tableName))); PG_RETURN_BOOL(true); } ``` And in the sql I have ``` CREATE OR REPLACE FUNCTION mygreat.add_mygreat_table(cstring, cstring) RETURNS bool LANGUAGE C STRICT IMMUTABLE AS '$libdir/mygreat', $$add_mygreat_table$$; ``` Here is my problem, it works as expected when the table doesn't exist. ``` mygreat_test=3D# create extension hydra; CREATE EXTENSION mygreat_test=3D# select mygreat.add_mygreat_table('public', = 'sampledata3'); ERROR: could not create remote table: relation does not exist ``` However if the table exists it just completely crashes ``` hydra_test=3D# select hydra.add_remote_table('snowflake', 'public', = 'sampledata2'); NOTICE: Table sampledata2 is now a mygreat table server closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request. The connection to the server was lost. Attempting reset: Failed. ``` I figured that it was because of the `IMMUTABLE` as the function does = modify the database. And according to documentation ``` IMMUTABLE indicates that the function cannot modify the database and = always returns the same result when given the same argument values; t ``` So I tried creating it as a `VOLATILE` function. ``` CREATE OR REPLACE FUNCTION hydra.add_remote_table(cstring, cstring) RETURNS bool LANGUAGE C STRICT VOLATILE AS '$libdir/mygreat', $$add_mygreat_table$$; CREATE FUNCTION ``` But now it just always crashes, and what used to return the proper error = message when ran against a table that doesn't exist now returns ``` mygreat_test=3D# select mygreat.add_mygreat_table('public', = 'sampledata3'); server closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request. The connection to the server was lost. Attempting reset: Failed. ``` In the logs, for the latest, I get ``` 2021-10-27 12:28:02.712 PDT [44115] LOG: statement: select = mygreat.add_mygreat_table('public', 'sampledata3'); 2021-10-27 12:28:02.718 PDT [43936] LOG: server process (PID 44115) was = terminated by signal 11: Segmentation fault: 11 2021-10-27 12:28:02.718 PDT [43936] DETAIL: Failed process was running: = select mygreat.add_mygreat_table('public', 'sampledata3'); 2021-10-27 12:28:02.719 PDT [43936] LOG: terminating any other active = server processes ``` What am I doing wrong? Can anyone help me with this? Thank you!