Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement create_base_type API. #206

Merged
merged 8 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 172 additions & 0 deletions include/compatibility.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#define SET_USER_COMPAT_H

#include "catalog/pg_namespace.h"
#include "catalog/pg_proc.h"
#include "tcop/utility.h"

/*
Expand Down Expand Up @@ -171,6 +172,12 @@
#ifndef TYPALIGN_INT
#define TYPALIGN_INT 'i' /* int alignment (typically 4 bytes) */
#endif
#ifndef TYPSTORAGE_PLAIN
#define TYPSTORAGE_PLAIN 'p' /* type not prepared for toasting */
#endif
#ifndef TYPSTORAGE_EXTENDED
#define TYPSTORAGE_EXTENDED 'x' /* fully toastable */
#endif

/*
* PostgreSQL 13 changed the SPI interface to include a "numvals" attribute that
Expand Down Expand Up @@ -240,16 +247,181 @@

#endif

#if PG_VERSION_NUM >= 140000
#define TYPE_CREATE(isArrayType, newTypeOid, typeName, typeNamespace, relationOid, relationKind, ownerId, internalSize, typeType, typeCategory, typePreferred, typDelim, inputProcedure, outputProcedure, receiveProcedure, sendProcedure, typmodinProcedure, typmodoutProcedure, analyzeProcedure, elementType, isImplicitArray, arrayType, baseType, defaultTypeValue, defaultTypeBin, passedByValue, alignment, storage, typeMod, typNDims, typeNotNull, typeCollation) \
TypeCreate(newTypeOid, \
typeName, \
typeNamespace, \
relationOid, \
relationKind, \
ownerId, \
internalSize, \
typeType, \
typeCategory, \
typePreferred, \
typDelim, \
inputProcedure, \
outputProcedure, \
receiveProcedure, \
sendProcedure, \
typmodinProcedure, \
typmodoutProcedure, \
analyzeProcedure, \
isArrayType ? F_ARRAY_SUBSCRIPT_HANDLER : InvalidOid, /* subscript procedure */ \
elementType, \
isImplicitArray, \
arrayType, \
baseType, \
defaultTypeValue, \
defaultTypeBin, \
passedByValue, \
alignment, \
storage, \
typeMod, \
typNDims, \
typeNotNull, \
typeCollation)
#else
#define TYPE_CREATE(isArrayType, newTypeOid, typeName, typeNamespace, relationOid, relationKind, ownerId, internalSize, typeType, typeCategory, typePreferred, typDelim, inputProcedure, outputProcedure, receiveProcedure, sendProcedure, typmodinProcedure, typmodoutProcedure, analyzeProcedure, elementType, isImplicitArray, arrayType, baseType, defaultTypeValue, defaultTypeBin, passedByValue, alignment, storage, typeMod, typNDims, typeNotNull, typeCollation) \
TypeCreate(newTypeOid, \
typeName, \
typeNamespace, \
relationOid, \
relationKind, \
ownerId, \
internalSize, \
typeType, \
typeCategory, \
typePreferred, \
typDelim, \
inputProcedure, \
outputProcedure, \
receiveProcedure, \
sendProcedure, \
typmodinProcedure, \
typmodoutProcedure, \
analyzeProcedure, \
elementType, \
isImplicitArray, \
arrayType, \
baseType, \
defaultTypeValue, \
defaultTypeBin, \
passedByValue, \
alignment, \
storage, \
typeMod, \
typNDims, \
typeNotNull, \
typeCollation)
#endif

#if PG_VERSION_NUM >= 140000
#define PROCEDURE_CREATE(procedureName, procNamespace, replace, returnsSet, returnType, proowner, languageObjectId, languageValidator, prosrc, probin, prokind, security_definer, isLeakProof, isStrict, volatility, parallel, parameterTypes, allParameterTypes, parameterModes, parameterNames, parameterDefaults, trftypes, proconfig, procost, prorows) \
ProcedureCreate(procedureName, \
procNamespace, \
replace, \
returnsSet, \
returnType, \
proowner, \
languageObjectId, \
languageValidator, \
prosrc, \
probin, \
NULL, /* prosqlbody */ \
prokind, \
security_definer, \
isLeakProof, \
isStrict, \
volatility, \
parallel, \
parameterTypes, \
allParameterTypes, \
parameterModes, \
parameterNames, \
parameterDefaults, \
trftypes, \
proconfig, \
InvalidOid, /* prosupport */ \
procost, \
prorows)
#elif PG_VERSION_NUM >= 120000
#define PROCEDURE_CREATE(procedureName, procNamespace, replace, returnsSet, returnType, proowner, languageObjectId, languageValidator, prosrc, probin, prokind, security_definer, isLeakProof, isStrict, volatility, parallel, parameterTypes, allParameterTypes, parameterModes, parameterNames, parameterDefaults, trftypes, proconfig, procost, prorows) \
ProcedureCreate(procedureName, \
procNamespace, \
replace, \
returnsSet, \
returnType, \
proowner, \
languageObjectId, \
languageValidator, \
prosrc, \
probin, \
prokind, \
security_definer, \
isLeakProof, \
isStrict, \
volatility, \
parallel, \
parameterTypes, \
allParameterTypes, \
parameterModes, \
parameterNames, \
parameterDefaults, \
trftypes, \
proconfig, \
InvalidOid, /* prosupport */ \
procost, \
prorows)
#else
#define PROCEDURE_CREATE(procedureName, procNamespace, replace, returnsSet, returnType, proowner, languageObjectId, languageValidator, prosrc, probin, prokind, security_definer, isLeakProof, isStrict, volatility, parallel, parameterTypes, allParameterTypes, parameterModes, parameterNames, parameterDefaults, trftypes, proconfig, procost, prorows) \
ProcedureCreate(procedureName, \
procNamespace, \
replace, \
returnsSet, \
returnType, \
proowner, \
languageObjectId, \
languageValidator, \
prosrc, \
probin, \
prokind, \
security_definer, \
isLeakProof, \
isStrict, \
volatility, \
parallel, \
parameterTypes, \
allParameterTypes, \
parameterModes, \
parameterNames, \
parameterDefaults, \
trftypes, \
proconfig, \
procost, \
prorows)
#endif

#if PG_VERSION_NUM >= 120000
#define GET_TYPE_OID(cacheId, key1, key2) GetSysCacheOid2(cacheId, Anum_pg_type_oid, key1, key2);
#else
#define GET_TYPE_OID(cacheId, key1, key2) GetSysCacheOid2(cacheId, key1, key2);
#endif

#if (PG_VERSION_NUM < 160000)
#define PG_DATABASE_ACLCHECK(DatabaseId, UserId, Operation) pg_database_aclcheck(DatabaseId, UserId, Operation)
#define PG_EXTENSION_OWNERCHECK(ExtensionOid, UserId) pg_extension_ownercheck(ExtensionOid, UserId)
#define PG_NAMESPACE_ACLCHECK(NamespaceOid, UserId, Operation) pg_namespace_aclcheck(NamespaceOid, UserId, Operation)
#define PG_PROC_OWNERCHECK(ProcOid, UserId) pg_proc_ownercheck(ProcOid, UserId)
#define PG_TYPE_OWNERCHECK(TypeOid, UserId) pg_type_ownercheck(TypeOid, UserId)
#define STRING_TO_QUALIFIED_NAME_LIST(string) stringToQualifiedNameList(string)
#define CHECK_CAN_SET_ROLE(member, role) check_is_member_of_role(member, role)
#else
#define PG_DATABASE_ACLCHECK(DatabaseId, UserId, Operation) object_aclcheck(DatabaseRelationId, DatabaseId, UserId, Operation);
#define PG_EXTENSION_OWNERCHECK(ExtensionOid, UserId) object_ownercheck(ExtensionRelationId, ExtensionOid, UserId)
#define PG_NAMESPACE_ACLCHECK(NamespaceOid, UserId, Operation) object_aclcheck(NamespaceRelationId, NamespaceOid, UserId, Operation)
#define PG_PROC_OWNERCHECK(ProcOid, UserId) object_ownercheck(ProcedureRelationId, ProcOid, UserId)
#define PG_TYPE_OWNERCHECK(TypeOid, UserId) object_ownercheck(TypeRelationId, TypeOid, UserId)
#define STRING_TO_QUALIFIED_NAME_LIST(string) stringToQualifiedNameList(string, NULL)
#define CHECK_CAN_SET_ROLE(member, role) check_can_set_role(member, role)
#endif
Expand Down
9 changes: 9 additions & 0 deletions include/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@
#define TLE_EXT_CONTROL_SUFFIX ".control"
#define TLE_EXT_SQL_SUFFIX ".sql"

#define TLE_BASE_TYPE_IN "pg_tle_base_type_in"
#define TLE_BASE_TYPE_OUT "pg_tle_base_type_out"

/*
* TLE_BASE_TYPE_SIZE_LIMIT is the maximum allowed size of pg_tle type.
*
*/
#define TLE_BASE_TYPE_SIZE_LIMIT PG_INT16_MAX - VARHDRSZ

/*
* Sets the limit on how many entries can be in a requires.
* This is an arbitrary limit and could be changed or dropped in the future.
Expand Down
72 changes: 72 additions & 0 deletions pg_tle--1.0.4--1.0.5.sql
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,42 @@ STRICT
AS 'MODULE_PATHNAME', 'pg_tle_create_shell_type_if_not_exists'
LANGUAGE C;

CREATE FUNCTION pgtle.create_base_type
(
typenamespace regnamespace,
typename name,
infunc regprocedure,
outfunc regprocedure,
internallength int4
)
RETURNS void
SET search_path TO 'pgtle'
STRICT
AS 'MODULE_PATHNAME', 'pg_tle_create_base_type'
LANGUAGE C;

CREATE FUNCTION pgtle.create_base_type_if_not_exists
(
typenamespace regnamespace,
typename name,
infunc regprocedure,
outfunc regprocedure,
internallength int4
)
RETURNS boolean
SET search_path TO 'pgtle'
AS $_pgtleie_$
BEGIN
PERFORM pgtle.create_base_type(typenamespace, typename, infunc, outfunc, internallength);
RETURN TRUE;
EXCEPTION
-- only catch the duplicate_object exception, let all other exceptions pass through.
WHEN duplicate_object THEN
RETURN FALSE;
END;
$_pgtleie_$
LANGUAGE plpgsql STRICT;

REVOKE EXECUTE ON FUNCTION pgtle.create_shell_type
(
typenamespace regnamespace,
Expand All @@ -51,6 +87,24 @@ REVOKE EXECUTE ON FUNCTION pgtle.create_shell_type_if_not_exists
typename name
) FROM PUBLIC;

REVOKE EXECUTE ON FUNCTION pgtle.create_base_type
(
typenamespace regnamespace,
typename name,
infunc regprocedure,
outfunc regprocedure,
internallength int4
) FROM PUBLIC;

REVOKE EXECUTE ON FUNCTION pgtle.create_base_type_if_not_exists
(
typenamespace regnamespace,
typename name,
infunc regprocedure,
outfunc regprocedure,
internallength int4
) FROM PUBLIC;

GRANT EXECUTE ON FUNCTION pgtle.create_shell_type
(
typenamespace regnamespace,
Expand All @@ -62,3 +116,21 @@ GRANT EXECUTE ON FUNCTION pgtle.create_shell_type_if_not_exists
typenamespace regnamespace,
typename name
) TO pgtle_admin;

GRANT EXECUTE ON FUNCTION pgtle.create_base_type
(
typenamespace regnamespace,
typename name,
infunc regprocedure,
outfunc regprocedure,
internallength int4
) TO pgtle_admin;

GRANT EXECUTE ON FUNCTION pgtle.create_base_type_if_not_exists
(
typenamespace regnamespace,
typename name,
infunc regprocedure,
outfunc regprocedure,
internallength int4
) TO pgtle_admin;
Loading