Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/BABEL_5_X_DEV__PG_17_X' into j…
Browse files Browse the repository at this point in the history
…ira-babel-4307

Merging upstream into current
  • Loading branch information
“manisha-deshpande” committed Jan 30, 2025
2 parents a84719b + 84b4e98 commit 1981587
Show file tree
Hide file tree
Showing 22 changed files with 245 additions and 64 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
sudo apt install -y libipc-run-perl
- name: build-postgres
run: |
./configure --with-icu --enable-cassert --enable-tap-tests
./configure --with-icu --enable-cassert --enable-tap-tests --without-readline
make world-bin -j8 COPT='-Werror -Wno-error=maybe-uninitialized'
- name: run-tests
run: |
Expand Down
29 changes: 28 additions & 1 deletion BabelfishDump.spec
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

Name: BabelfishDump
Summary: Postgresql dump utilities modified for Babelfish
Version: 16.4
Version: 17.2
Release: 1%{?dist}%{?_trivial}%{?_buildid}
License: PostgreSQL
Url: https://github.com/babelfish-for-postgresql/postgresql_modified_for_babelfish
Expand Down Expand Up @@ -151,6 +151,33 @@ LD_LIBRARY_PATH=%{_builddir}/%{name}/src/interfaces/libpq $RPM_BUILD_ROOT/usr/bi
%{_bindir}/bbf_dumpall

%changelog
* Wed Dec 11 2024 Sharu Goel <[email protected]> - 17.2-1
- Add support to dump linked roles associated with members of db_owner role

* Wed Dec 11 2024 ANJU BHARTI <[email protected]> - 17.2-1
- Handle dump logic for babelfish db_ddladmin fixed database role

* Wed Dec 11 2024 Harsh Lunagariya <[email protected]> - 17.2-1
- Handle dump logic for babelfish db_securityadmin fixed database role

* Wed Dec 11 2024 ANJU BHARTI <[email protected]> - 17.2-1
- Handle dump logic for babelfish db_creator fixed server role

* Wed Dec 11 2024 Shalini Lohia <[email protected]> - 17.2-1
- Handle dump logic for babelfish db_datareader/db_datawriter fixed database roles

* Wed Dec 11 2024 Tanzeel Khan <[email protected]> - 17.2-1
- Handle dump logic for babelfish db_accessadmin fixed database role

* Wed Dec 11 2024 ANJU BHARTI <[email protected]> - 17.2-1
- Handle dump logic for babelfish securityadmin fixed server role

* Tue Nov 19 2024 Rishabh Tanwar <[email protected]> - 17.2-1
- Enable babelfishpg_tsql.dump_restore GUC while restoring roles

* Thu Oct 3 2024 Tanzeel Khan <[email protected]> - 17.2-1
- Dump physical database aclprivs for fixed database roles

* Mon Aug 5 2024 Masahiko Sawada <[email protected]> - 16.4-1
- [CVE-2024-7348] Restrict accesses to non-system views and foreign tables during pg_dump.

Expand Down
32 changes: 4 additions & 28 deletions src/backend/catalog/aclchk.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ static void recordExtensionInitPrivWorker(Oid objoid, Oid classoid, int objsubid

tsql_has_linked_srv_permissions_hook_type tsql_has_linked_srv_permissions_hook = NULL;
bbf_execute_grantstmt_as_dbsecadmin_hook_type bbf_execute_grantstmt_as_dbsecadmin_hook = NULL;

pltsql_allow_storing_init_privs_hook_type pltsql_allow_storing_init_privs_hook = NULL;
/*
* If is_grant is true, adds the given privileges for the list of
* grantees to the existing old_acl. If is_grant is false, the
Expand Down Expand Up @@ -4705,6 +4705,9 @@ recordExtensionInitPriv(Oid objoid, Oid classoid, int objsubid, Acl *new_acl)
if (!creating_extension && !binary_upgrade_record_init_privs)
return;

if (pltsql_allow_storing_init_privs_hook && !((*pltsql_allow_storing_init_privs_hook)(objoid, classoid, objsubid)))
return;

recordExtensionInitPrivWorker(objoid, classoid, objsubid, new_acl);
}

Expand Down Expand Up @@ -5039,32 +5042,6 @@ RemoveRoleFromInitPriv(Oid roleid, Oid classid, Oid objid, int32 objsubid)
* Generate new ACL. Grantor of rights is always the same as the owner.
*/
if (old_acl != NULL)
{
Oid sysadminOid;
Acl *temp_acl;
const char *babelfish_db_name;

/*
* For babelfish database, grantor will be sysadmin instead of object owner.
*/
if (bbf_get_sysadmin_oid_hook &&
classid == DatabaseRelationId &&
(babelfish_db_name = GetConfigOption("babelfishpg_tsql.database_name", true, false)) &&
objid == get_database_oid(babelfish_db_name, true) &&
is_member_of_role(GetUserId(), sysadminOid = (*bbf_get_sysadmin_oid_hook)()))
{

temp_acl = merge_acl_with_grant(old_acl,
false, /* is_grant */
false, /* grant_option */
DROP_RESTRICT,
list_make1_oid(roleid),
ACLITEM_ALL_PRIV_BITS,
sysadminOid,
ownerId);
old_acl = temp_acl;
}

new_acl = merge_acl_with_grant(old_acl,
false, /* is_grant */
false, /* grant_option */
Expand All @@ -5073,7 +5050,6 @@ RemoveRoleFromInitPriv(Oid roleid, Oid classid, Oid objid, int32 objsubid)
ACLITEM_ALL_PRIV_BITS,
ownerId,
ownerId);
}
else
new_acl = NULL; /* this case shouldn't happen, probably */

Expand Down
7 changes: 7 additions & 0 deletions src/backend/catalog/namespace.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ char *SYS_NAMESPACE_NAME = "sys";

relname_lookup_hook_type relname_lookup_hook = NULL;
match_pltsql_func_call_hook_type match_pltsql_func_call_hook = NULL;
remove_db_name_in_schema_hook_type remove_db_name_in_schema_hook = NULL;


/* Local functions */
Expand Down Expand Up @@ -3633,10 +3634,16 @@ get_namespace_oid(const char *nspname, bool missing_ok)
oid = GetSysCacheOid1(NAMESPACENAME, Anum_pg_namespace_oid,
CStringGetDatum(nspname));
if (!OidIsValid(oid) && !missing_ok)
{
if (sql_dialect == SQL_DIALECT_TSQL && remove_db_name_in_schema_hook)
{
nspname = (*remove_db_name_in_schema_hook)(nspname, "sch");
}
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_SCHEMA),
errmsg("schema \"%s\" does not exist", nspname)));

}
return oid;
}

Expand Down
2 changes: 0 additions & 2 deletions src/backend/commands/copyfromparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -1090,8 +1090,6 @@ NextCopyFrom(CopyFromState cstate, ExprContext *econtext,
if (fill_missing_values_in_copyfrom_hook)
fill_missing_values_in_copyfrom_hook(cstate->rel, values, nulls);

pfree(cstate->defaults);

return true;
}

Expand Down
4 changes: 2 additions & 2 deletions src/backend/executor/nodeModifyTable.c
Original file line number Diff line number Diff line change
Expand Up @@ -3954,8 +3954,8 @@ ExecModifyTable(PlanState *pstate)
bool tuplock;
/* for INSERT ... EXECUTE */
bool tsql_insert_exec = node->callStmt != NULL;
Tuplestorestate *tss;
TupleDesc tupdesc;
Tuplestorestate *tss = NULL;
TupleDesc tupdesc = NULL;
DestReceiver *dest = NULL;

CHECK_FOR_INTERRUPTS();
Expand Down
30 changes: 30 additions & 0 deletions src/backend/parser/parse_coerce.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

find_coercion_pathway_hook_type find_coercion_pathway_hook = NULL;
determine_datatype_precedence_hook_type determine_datatype_precedence_hook = NULL;
is_tsql_base_datatype_hook_type is_tsql_base_datatype_hook = NULL;
coerce_string_literal_hook_type coerce_string_literal_hook = NULL;
validate_implicit_conversion_from_string_literal_hook_type validate_implicit_conversion_from_string_literal_hook = NULL;
select_common_type_hook_type select_common_type_hook = NULL;
Expand Down Expand Up @@ -1504,9 +1505,38 @@ select_common_type(ParseState *pstate, List *exprs, const char *context,
pcategory = ncategory;
pispreferred = nispreferred;
}
} else if (sql_dialect == SQL_DIALECT_TSQL && ntype == ptype)
{
/*
* For the columns which have the same base type, we choose the
* expression with higher precedence type in T-SQL.
* For example, smallmoney UNION money, the base type of
* them are both fixeddecimal. But we shouldn't use smallmoney as
* the result type, it could loss precision.
* Here we don't need to update other variables since they are the
* same.
*/
if (is_tsql_base_datatype_hook &&
(*is_tsql_base_datatype_hook)(exprType(nexpr)) &&
determine_datatype_precedence_hook &&
determine_datatype_precedence_hook(exprType(nexpr),
exprType(pexpr)))
pexpr = nexpr;
}
}

/*
* If the preferred type is not the result type of corresponding
* expression, it means the result type is a domain type in Postgres. Some
* base data types in T-SQL are implemented as domain types in Babelfish.
* From SQL Server's perspective, we should try to retain those types as
* result types.
*/
if (sql_dialect == SQL_DIALECT_TSQL && ptype != exprType(pexpr) &&
is_tsql_base_datatype_hook &&
(*is_tsql_base_datatype_hook)(exprType(pexpr)))
ptype = exprType(pexpr);

/*
* If all the inputs were UNKNOWN type --- ie, unknown-type literals ---
* then resolve as type TEXT. This situation comes up with constructs
Expand Down
4 changes: 4 additions & 0 deletions src/backend/parser/parse_func.c
Original file line number Diff line number Diff line change
Expand Up @@ -2063,6 +2063,10 @@ funcname_signature_string(const char *funcname, int nargs,

initStringInfo(&argbuf);

if (sql_dialect == SQL_DIALECT_TSQL && remove_db_name_in_schema_hook) {
funcname = (*remove_db_name_in_schema_hook)(funcname, "func");
}

appendStringInfo(&argbuf, "%s(", funcname);

numposargs = nargs - list_length(argnames);
Expand Down
9 changes: 9 additions & 0 deletions src/backend/parser/parse_relation.c
Original file line number Diff line number Diff line change
Expand Up @@ -1453,10 +1453,19 @@ parserOpenTable(ParseState *pstate, const RangeVar *relation, int lockmode)
if (rel == NULL)
{
if (relation->schemaname)
{
if (sql_dialect == SQL_DIALECT_TSQL && remove_db_name_in_schema_hook){
const char *schema_name = (*remove_db_name_in_schema_hook)(relation->schemaname, "sch");
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_TABLE),
errmsg("relation \"%s.%s\" does not exist",
schema_name, relation->relname)));
} else
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_TABLE),
errmsg("relation \"%s.%s\" does not exist",
relation->schemaname, relation->relname)));
}
else
{
/*
Expand Down
8 changes: 7 additions & 1 deletion src/backend/postmaster/postmaster.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@ ProtocolExtensionConfig default_protocol_config = {
libpq_end_command,
NULL, NULL, NULL, NULL, /* use libpq defaults for printtup*() */
NULL,
libpq_report_param_status
libpq_report_param_status,
libpq_direct_ssl_handshake
};

/* still more option variables */
Expand Down Expand Up @@ -1511,6 +1512,11 @@ libpq_end_command(QueryCompletion *qc, CommandDest dest)
EndCommand(qc, dest, false);
}

int
libpq_direct_ssl_handshake(struct Port *port)
{
return ProcessSSLStartup(port);
}

/*
* on_proc_exit callback to close server's listen sockets
Expand Down
11 changes: 5 additions & 6 deletions src/backend/tcop/backend_startup.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
bool Trace_connection_negotiation = false;
static void BackendInitialize(ClientSocket *client_sock, CAC_state cac, ProtocolExtensionConfig *protocol_config);
int ProcessStartupPacket(Port *port, bool ssl_done, bool gss_done);
static int ProcessSSLStartup(Port *port);
int ProcessSSLStartup(Port *port);
static void SendNegotiateProtocolVersion(List *unrecognized_protocol_options);
static void process_startup_packet_die(SIGNAL_ARGS);
static void StartupPacketTimeoutHandler(void);
Expand Down Expand Up @@ -253,15 +253,14 @@ BackendInitialize(ClientSocket *client_sock, CAC_state cac, ProtocolExtensionCon
RegisterTimeout(STARTUP_PACKET_TIMEOUT, StartupPacketTimeoutHandler);
enable_timeout_after(STARTUP_PACKET_TIMEOUT, AuthenticationTimeout * 1000);

/* Handle direct SSL handshake for non-TDS connections */
if (!port->is_tds_conn)
status = ProcessSSLStartup(port);
/* Handle protocol-specific direct SSL handshake */
status = port->protocol_config->fn_direct_ssl_handshake(port);

/*
* Receive the startup packet (which might turn out to be a cancel request
* packet).
*/
if (port->is_tds_conn || status == STATUS_OK)
if (status == STATUS_OK)
status = (port->protocol_config->fn_start)(port);

/*
Expand Down Expand Up @@ -360,7 +359,7 @@ BackendInitialize(ClientSocket *client_sock, CAC_state cac, ProtocolExtensionCon
* This happens before the startup packet so we are careful not to actually
* read any bytes from the stream if it's not a direct SSL connection.
*/
static int
int
ProcessSSLStartup(Port *port)
{
int firstbyte;
Expand Down
7 changes: 7 additions & 0 deletions src/backend/utils/adt/acl.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ bbf_get_sysadmin_oid_hook_type bbf_get_sysadmin_oid_hook = NULL;
get_bbf_admin_oid_hook_type get_bbf_admin_oid_hook = NULL;
pltsql_get_object_owner_hook_type pltsql_get_object_owner_hook = NULL;
is_bbf_db_ddladmin_operation_hook_type is_bbf_db_ddladmin_operation_hook = NULL;
bbf_check_member_has_direct_priv_to_grant_role_hook_type bbf_check_member_has_direct_priv_to_grant_role_hook = NULL;


/*
Expand Down Expand Up @@ -5278,6 +5279,12 @@ is_admin_of_role(Oid member, Oid role)
if (member == role)
return false;

if ((bbf_check_member_has_direct_priv_to_grant_role_hook)
&& (*bbf_check_member_has_direct_priv_to_grant_role_hook)(member, role))
{
return true;
}

(void) roles_is_member_of(member, ROLERECURSE_MEMBERS, role, &admin_role);
return OidIsValid(admin_role);
}
Expand Down
2 changes: 1 addition & 1 deletion src/backend/utils/adt/jsonpath_exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ tsql_openjson_with_columnize(Jsonb *jb, char *col_info)
if (token)
{
token = strtok(NULL, ")");
if (token)
if (token && atoi(token) > 0)
col_size = atoi(token);
}
}
Expand Down
Loading

0 comments on commit 1981587

Please sign in to comment.