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 4 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
11 changes: 11 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 @@ -244,12 +251,16 @@
#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
3 changes: 3 additions & 0 deletions include/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
#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"

/*
* 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
64 changes: 64 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,34 @@ 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'
STRICT
AS 'MODULE_PATHNAME', 'pg_tle_create_base_type_if_not_exists'
LANGUAGE C;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason this couldn't be a PL/pgSQL function similar to some of the others (e.g. https://github.com/aws/pg_tle/blob/main/pg_tle--1.0.0.sql#L389). I don't think there's anything C-specific that we need in this call, and we can push all the hard work down to create_base_type. This should reduce our surface area for C-specific bugs.

Copy link
Contributor Author

@lyupan lyupan Jun 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the latest commit, only create_base_type_if_not_exists is now defined in C, create_base_type is defined in plpgsql and will raise an exception if create_base_type_if_not_exists returns false.

The reason is that there are multiple cases where we could return ERRCODE_DUPLICATE_OBJECT, for example, if the shell type doesn't exist when create_baset_type is called (same error as postgres create type would return).


REVOKE EXECUTE ON FUNCTION pgtle.create_shell_type
(
typenamespace regnamespace,
Expand All @@ -51,6 +79,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 +108,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;
64 changes: 64 additions & 0 deletions pg_tle--1.0.5.sql
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,34 @@ 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'
STRICT
AS 'MODULE_PATHNAME', 'pg_tle_create_base_type_if_not_exists'
LANGUAGE C;

REVOKE EXECUTE ON FUNCTION pgtle.create_shell_type
(
typenamespace regnamespace,
Expand All @@ -448,6 +476,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 @@ -460,6 +506,24 @@ GRANT EXECUTE ON FUNCTION pgtle.create_shell_type_if_not_exists
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;

CREATE TYPE pgtle.pg_tle_features as ENUM ('passcheck');
CREATE TYPE pgtle.password_types as ENUM ('PASSWORD_TYPE_PLAINTEXT', 'PASSWORD_TYPE_MD5', 'PASSWORD_TYPE_SCRAM_SHA_256');

Expand Down
Loading