postgresql/src/backend/commands/alter.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

1091 lines
30 KiB
C
Raw Normal View History

2003-06-27 16:45:32 +02:00
/*-------------------------------------------------------------------------
*
* alter.c
* Drivers for generic alter commands
*
* Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
2003-06-27 16:45:32 +02:00
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
2010-09-20 22:08:53 +02:00
* src/backend/commands/alter.c
2003-06-27 16:45:32 +02:00
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "access/htup_details.h"
#include "access/relation.h"
#include "access/sysattr.h"
#include "access/table.h"
#include "catalog/dependency.h"
#include "catalog/indexing.h"
2003-06-27 16:45:32 +02:00
#include "catalog/namespace.h"
#include "catalog/objectaccess.h"
#include "catalog/pg_collation.h"
#include "catalog/pg_conversion.h"
Add new predefined role pg_create_subscription. This role can be granted to non-superusers to allow them to issue CREATE SUBSCRIPTION. The non-superuser must additionally have CREATE permissions on the database in which the subscription is to be created. Most forms of ALTER SUBSCRIPTION, including ALTER SUBSCRIPTION .. SKIP, now require only that the role performing the operation own the subscription, or inherit the privileges of the owner. However, to use ALTER SUBSCRIPTION ... RENAME or ALTER SUBSCRIPTION ... OWNER TO, you also need CREATE permission on the database. This is similar to what we do for schemas. To change the owner of a schema, you must also have permission to SET ROLE to the new owner, similar to what we do for other object types. Non-superusers are required to specify a password for authentication and the remote side must use the password, similar to what is required for postgres_fdw and dblink. A superuser who wants a non-superuser to own a subscription that does not rely on password authentication may set the new password_required=false property on that subscription. A non-superuser may not set password_required=false and may not modify a subscription that already has password_required=false. This new password_required subscription property works much like the eponymous postgres_fdw property. In both cases, the actual semantics are that a password is not required if either (1) the property is set to false or (2) the relevant user is the superuser. Patch by me, reviewed by Andres Freund, Jeff Davis, Mark Dilger, and Stephen Frost (but some of those people did not fully endorse all of the decisions that the patch makes). Discussion: http://postgr.es/m/CA+TgmoaDH=0Xj7OBiQnsHTKcF2c4L+=gzPBUKSJLh8zed2_+Dg@mail.gmail.com
2023-03-30 17:37:19 +02:00
#include "catalog/pg_database_d.h"
#include "catalog/pg_event_trigger.h"
#include "catalog/pg_foreign_data_wrapper.h"
#include "catalog/pg_foreign_server.h"
#include "catalog/pg_language.h"
#include "catalog/pg_largeobject.h"
#include "catalog/pg_largeobject_metadata.h"
#include "catalog/pg_namespace.h"
#include "catalog/pg_opclass.h"
#include "catalog/pg_opfamily.h"
#include "catalog/pg_proc.h"
Implement multivariate n-distinct coefficients Add support for explicitly declared statistic objects (CREATE STATISTICS), allowing collection of statistics on more complex combinations that individual table columns. Companion commands DROP STATISTICS and ALTER STATISTICS ... OWNER TO / SET SCHEMA / RENAME are added too. All this DDL has been designed so that more statistic types can be added later on, such as multivariate most-common-values and multivariate histograms between columns of a single table, leaving room for permitting columns on multiple tables, too, as well as expressions. This commit only adds support for collection of n-distinct coefficient on user-specified sets of columns in a single table. This is useful to estimate number of distinct groups in GROUP BY and DISTINCT clauses; estimation errors there can cause over-allocation of memory in hashed aggregates, for instance, so it's a worthwhile problem to solve. A new special pseudo-type pg_ndistinct is used. (num-distinct estimation was deemed sufficiently useful by itself that this is worthwhile even if no further statistic types are added immediately; so much so that another version of essentially the same functionality was submitted by Kyotaro Horiguchi: https://postgr.es/m/20150828.173334.114731693.horiguchi.kyotaro@lab.ntt.co.jp though this commit does not use that code.) Author: Tomas Vondra. Some code rework by Álvaro. Reviewed-by: Dean Rasheed, David Rowley, Kyotaro Horiguchi, Jeff Janes, Ideriha Takeshi Discussion: https://postgr.es/m/543AFA15.4080608@fuzzy.cz https://postgr.es/m/20170320190220.ixlaueanxegqd5gr@alvherre.pgsql
2017-03-24 18:06:10 +01:00
#include "catalog/pg_statistic_ext.h"
#include "catalog/pg_subscription.h"
#include "catalog/pg_ts_config.h"
#include "catalog/pg_ts_dict.h"
#include "catalog/pg_ts_parser.h"
#include "catalog/pg_ts_template.h"
2003-06-27 16:45:32 +02:00
#include "commands/alter.h"
#include "commands/collationcmds.h"
2003-06-27 16:45:32 +02:00
#include "commands/conversioncmds.h"
#include "commands/dbcommands.h"
#include "commands/defrem.h"
#include "commands/event_trigger.h"
#include "commands/extension.h"
Row-Level Security Policies (RLS) Building on the updatable security-barrier views work, add the ability to define policies on tables to limit the set of rows which are returned from a query and which are allowed to be added to a table. Expressions defined by the policy for filtering are added to the security barrier quals of the query, while expressions defined to check records being added to a table are added to the with-check options of the query. New top-level commands are CREATE/ALTER/DROP POLICY and are controlled by the table owner. Row Security is able to be enabled and disabled by the owner on a per-table basis using ALTER TABLE .. ENABLE/DISABLE ROW SECURITY. Per discussion, ROW SECURITY is disabled on tables by default and must be enabled for policies on the table to be used. If no policies exist on a table with ROW SECURITY enabled, a default-deny policy is used and no records will be visible. By default, row security is applied at all times except for the table owner and the superuser. A new GUC, row_security, is added which can be set to ON, OFF, or FORCE. When set to FORCE, row security will be applied even for the table owner and superusers. When set to OFF, row security will be disabled when allowed and an error will be thrown if the user does not have rights to bypass row security. Per discussion, pg_dump sets row_security = OFF by default to ensure that exports and backups will have all data in the table or will error if there are insufficient privileges to bypass row security. A new option has been added to pg_dump, --enable-row-security, to ask pg_dump to export with row security enabled. A new role capability, BYPASSRLS, which can only be set by the superuser, is added to allow other users to be able to bypass row security using row_security = OFF. Many thanks to the various individuals who have helped with the design, particularly Robert Haas for his feedback. Authors include Craig Ringer, KaiGai Kohei, Adam Brightwell, Dean Rasheed, with additional changes and rework by me. Reviewers have included all of the above, Greg Smith, Jeff McCormick, and Robert Haas.
2014-09-19 17:18:35 +02:00
#include "commands/policy.h"
2003-06-27 16:45:32 +02:00
#include "commands/proclang.h"
#include "commands/publicationcmds.h"
2003-06-27 16:45:32 +02:00
#include "commands/schemacmds.h"
#include "commands/subscriptioncmds.h"
2003-06-27 16:45:32 +02:00
#include "commands/tablecmds.h"
#include "commands/tablespace.h"
2003-06-27 16:45:32 +02:00
#include "commands/trigger.h"
#include "commands/typecmds.h"
2003-06-27 16:45:32 +02:00
#include "commands/user.h"
#include "miscadmin.h"
#include "parser/parse_func.h"
#include "replication/logicalworker.h"
#include "rewrite/rewriteDefine.h"
#include "tcop/utility.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
2003-06-27 16:45:32 +02:00
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/syscache.h"
2003-06-27 16:45:32 +02:00
static Oid AlterObjectNamespace_internal(Relation rel, Oid objid, Oid nspOid);
/*
* Raise an error to the effect that an object of the given name is already
* present in the given namespace.
*/
static void
report_name_conflict(Oid classId, const char *name)
2003-06-27 16:45:32 +02:00
{
char *msgfmt;
switch (classId)
2003-06-27 16:45:32 +02:00
{
case EventTriggerRelationId:
msgfmt = gettext_noop("event trigger \"%s\" already exists");
break;
case ForeignDataWrapperRelationId:
msgfmt = gettext_noop("foreign-data wrapper \"%s\" already exists");
break;
case ForeignServerRelationId:
msgfmt = gettext_noop("server \"%s\" already exists");
break;
case LanguageRelationId:
msgfmt = gettext_noop("language \"%s\" already exists");
break;
case PublicationRelationId:
msgfmt = gettext_noop("publication \"%s\" already exists");
break;
case SubscriptionRelationId:
msgfmt = gettext_noop("subscription \"%s\" already exists");
break;
default:
elog(ERROR, "unsupported object class: %u", classId);
break;
}
2003-06-27 16:45:32 +02:00
ereport(ERROR,
(errcode(ERRCODE_DUPLICATE_OBJECT),
errmsg(msgfmt, name)));
}
static void
report_namespace_conflict(Oid classId, const char *name, Oid nspOid)
{
char *msgfmt;
Assert(OidIsValid(nspOid));
2003-06-27 16:45:32 +02:00
switch (classId)
{
case ConversionRelationId:
Assert(OidIsValid(nspOid));
msgfmt = gettext_noop("conversion \"%s\" already exists in schema \"%s\"");
break;
Implement multivariate n-distinct coefficients Add support for explicitly declared statistic objects (CREATE STATISTICS), allowing collection of statistics on more complex combinations that individual table columns. Companion commands DROP STATISTICS and ALTER STATISTICS ... OWNER TO / SET SCHEMA / RENAME are added too. All this DDL has been designed so that more statistic types can be added later on, such as multivariate most-common-values and multivariate histograms between columns of a single table, leaving room for permitting columns on multiple tables, too, as well as expressions. This commit only adds support for collection of n-distinct coefficient on user-specified sets of columns in a single table. This is useful to estimate number of distinct groups in GROUP BY and DISTINCT clauses; estimation errors there can cause over-allocation of memory in hashed aggregates, for instance, so it's a worthwhile problem to solve. A new special pseudo-type pg_ndistinct is used. (num-distinct estimation was deemed sufficiently useful by itself that this is worthwhile even if no further statistic types are added immediately; so much so that another version of essentially the same functionality was submitted by Kyotaro Horiguchi: https://postgr.es/m/20150828.173334.114731693.horiguchi.kyotaro@lab.ntt.co.jp though this commit does not use that code.) Author: Tomas Vondra. Some code rework by Álvaro. Reviewed-by: Dean Rasheed, David Rowley, Kyotaro Horiguchi, Jeff Janes, Ideriha Takeshi Discussion: https://postgr.es/m/543AFA15.4080608@fuzzy.cz https://postgr.es/m/20170320190220.ixlaueanxegqd5gr@alvherre.pgsql
2017-03-24 18:06:10 +01:00
case StatisticExtRelationId:
Assert(OidIsValid(nspOid));
msgfmt = gettext_noop("statistics object \"%s\" already exists in schema \"%s\"");
Implement multivariate n-distinct coefficients Add support for explicitly declared statistic objects (CREATE STATISTICS), allowing collection of statistics on more complex combinations that individual table columns. Companion commands DROP STATISTICS and ALTER STATISTICS ... OWNER TO / SET SCHEMA / RENAME are added too. All this DDL has been designed so that more statistic types can be added later on, such as multivariate most-common-values and multivariate histograms between columns of a single table, leaving room for permitting columns on multiple tables, too, as well as expressions. This commit only adds support for collection of n-distinct coefficient on user-specified sets of columns in a single table. This is useful to estimate number of distinct groups in GROUP BY and DISTINCT clauses; estimation errors there can cause over-allocation of memory in hashed aggregates, for instance, so it's a worthwhile problem to solve. A new special pseudo-type pg_ndistinct is used. (num-distinct estimation was deemed sufficiently useful by itself that this is worthwhile even if no further statistic types are added immediately; so much so that another version of essentially the same functionality was submitted by Kyotaro Horiguchi: https://postgr.es/m/20150828.173334.114731693.horiguchi.kyotaro@lab.ntt.co.jp though this commit does not use that code.) Author: Tomas Vondra. Some code rework by Álvaro. Reviewed-by: Dean Rasheed, David Rowley, Kyotaro Horiguchi, Jeff Janes, Ideriha Takeshi Discussion: https://postgr.es/m/543AFA15.4080608@fuzzy.cz https://postgr.es/m/20170320190220.ixlaueanxegqd5gr@alvherre.pgsql
2017-03-24 18:06:10 +01:00
break;
case TSParserRelationId:
Assert(OidIsValid(nspOid));
msgfmt = gettext_noop("text search parser \"%s\" already exists in schema \"%s\"");
break;
case TSDictionaryRelationId:
Assert(OidIsValid(nspOid));
msgfmt = gettext_noop("text search dictionary \"%s\" already exists in schema \"%s\"");
break;
case TSTemplateRelationId:
Assert(OidIsValid(nspOid));
msgfmt = gettext_noop("text search template \"%s\" already exists in schema \"%s\"");
break;
case TSConfigRelationId:
Assert(OidIsValid(nspOid));
msgfmt = gettext_noop("text search configuration \"%s\" already exists in schema \"%s\"");
break;
default:
elog(ERROR, "unsupported object class: %u", classId);
break;
}
2003-06-27 16:45:32 +02:00
ereport(ERROR,
(errcode(ERRCODE_DUPLICATE_OBJECT),
errmsg(msgfmt, name, get_namespace_name(nspOid))));
}
/*
* AlterObjectRename_internal
*
* Generic function to rename the given object, for simple cases (won't
* work for tables, nor other cases where we need to do more than change
* the name column of a single catalog entry).
*
* rel: catalog relation containing object (RowExclusiveLock'd by caller)
* objectId: OID of object to be renamed
* new_name: CString representation of new name
*/
static void
AlterObjectRename_internal(Relation rel, Oid objectId, const char *new_name)
{
Oid classId = RelationGetRelid(rel);
int oidCacheId = get_object_catcache_oid(classId);
int nameCacheId = get_object_catcache_name(classId);
AttrNumber Anum_name = get_object_attnum_name(classId);
AttrNumber Anum_namespace = get_object_attnum_namespace(classId);
AttrNumber Anum_owner = get_object_attnum_owner(classId);
HeapTuple oldtup;
HeapTuple newtup;
Datum datum;
bool isnull;
Oid namespaceId;
Oid ownerId;
char *old_name;
AclResult aclresult;
Datum *values;
bool *nulls;
bool *replaces;
NameData nameattrdata;
oldtup = SearchSysCache1(oidCacheId, ObjectIdGetDatum(objectId));
if (!HeapTupleIsValid(oldtup))
elog(ERROR, "cache lookup failed for object %u of catalog \"%s\"",
objectId, RelationGetRelationName(rel));
datum = heap_getattr(oldtup, Anum_name,
RelationGetDescr(rel), &isnull);
Assert(!isnull);
old_name = NameStr(*(DatumGetName(datum)));
2003-06-27 16:45:32 +02:00
/* Get OID of namespace */
if (Anum_namespace > 0)
{
datum = heap_getattr(oldtup, Anum_namespace,
RelationGetDescr(rel), &isnull);
Assert(!isnull);
namespaceId = DatumGetObjectId(datum);
}
else
namespaceId = InvalidOid;
2003-06-27 16:45:32 +02:00
/* Permission checks ... superusers can always do it */
if (!superuser())
{
/* Fail if object does not have an explicit owner */
if (Anum_owner <= 0)
ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("must be superuser to rename %s",
getObjectDescriptionOids(classId, objectId))));
2003-06-27 16:45:32 +02:00
/* Otherwise, must be owner of the existing object */
datum = heap_getattr(oldtup, Anum_owner,
RelationGetDescr(rel), &isnull);
Assert(!isnull);
ownerId = DatumGetObjectId(datum);
if (!has_privs_of_role(GetUserId(), DatumGetObjectId(ownerId)))
aclcheck_error(ACLCHECK_NOT_OWNER, get_object_type(classId, objectId),
old_name);
/* User must have CREATE privilege on the namespace */
if (OidIsValid(namespaceId))
{
aclresult = object_aclcheck(NamespaceRelationId, namespaceId, GetUserId(),
ACL_CREATE);
if (aclresult != ACLCHECK_OK)
aclcheck_error(aclresult, OBJECT_SCHEMA,
get_namespace_name(namespaceId));
}
Add new predefined role pg_create_subscription. This role can be granted to non-superusers to allow them to issue CREATE SUBSCRIPTION. The non-superuser must additionally have CREATE permissions on the database in which the subscription is to be created. Most forms of ALTER SUBSCRIPTION, including ALTER SUBSCRIPTION .. SKIP, now require only that the role performing the operation own the subscription, or inherit the privileges of the owner. However, to use ALTER SUBSCRIPTION ... RENAME or ALTER SUBSCRIPTION ... OWNER TO, you also need CREATE permission on the database. This is similar to what we do for schemas. To change the owner of a schema, you must also have permission to SET ROLE to the new owner, similar to what we do for other object types. Non-superusers are required to specify a password for authentication and the remote side must use the password, similar to what is required for postgres_fdw and dblink. A superuser who wants a non-superuser to own a subscription that does not rely on password authentication may set the new password_required=false property on that subscription. A non-superuser may not set password_required=false and may not modify a subscription that already has password_required=false. This new password_required subscription property works much like the eponymous postgres_fdw property. In both cases, the actual semantics are that a password is not required if either (1) the property is set to false or (2) the relevant user is the superuser. Patch by me, reviewed by Andres Freund, Jeff Davis, Mark Dilger, and Stephen Frost (but some of those people did not fully endorse all of the decisions that the patch makes). Discussion: http://postgr.es/m/CA+TgmoaDH=0Xj7OBiQnsHTKcF2c4L+=gzPBUKSJLh8zed2_+Dg@mail.gmail.com
2023-03-30 17:37:19 +02:00
if (classId == SubscriptionRelationId)
{
Form_pg_subscription form;
/* must have CREATE privilege on database */
aclresult = object_aclcheck(DatabaseRelationId, MyDatabaseId,
GetUserId(), ACL_CREATE);
if (aclresult != ACLCHECK_OK)
aclcheck_error(aclresult, OBJECT_DATABASE,
get_database_name(MyDatabaseId));
/*
* Don't allow non-superuser modification of a subscription with
* password_required=false.
*/
form = (Form_pg_subscription) GETSTRUCT(oldtup);
if (!form->subpasswordrequired && !superuser())
ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("password_required=false is superuser-only"),
errhint("Subscriptions with the password_required option set to false may only be created or modified by the superuser.")));
}
}
/*
* Check for duplicate name (more friendly than unique-index failure).
* Since this is just a friendliness check, we can just skip it in cases
* where there isn't suitable support.
*/
if (classId == ProcedureRelationId)
{
Form_pg_proc proc = (Form_pg_proc) GETSTRUCT(oldtup);
IsThereFunctionInNamespace(new_name, proc->pronargs,
&proc->proargtypes, proc->pronamespace);
}
else if (classId == CollationRelationId)
{
Form_pg_collation coll = (Form_pg_collation) GETSTRUCT(oldtup);
IsThereCollationInNamespace(new_name, coll->collnamespace);
}
else if (classId == OperatorClassRelationId)
{
Form_pg_opclass opc = (Form_pg_opclass) GETSTRUCT(oldtup);
IsThereOpClassInNamespace(new_name, opc->opcmethod,
opc->opcnamespace);
}
else if (classId == OperatorFamilyRelationId)
{
Form_pg_opfamily opf = (Form_pg_opfamily) GETSTRUCT(oldtup);
IsThereOpFamilyInNamespace(new_name, opf->opfmethod,
opf->opfnamespace);
}
else if (classId == SubscriptionRelationId)
{
if (SearchSysCacheExists2(SUBSCRIPTIONNAME, MyDatabaseId,
CStringGetDatum(new_name)))
report_name_conflict(classId, new_name);
Add an enforcement mechanism for global object names in regression tests. In commit 18555b132 we tentatively established a rule that regression tests should use names containing "regression" for databases, and names starting with "regress_" for all other globally-visible object names, so as to circumscribe the side-effects that "make installcheck" could have on an existing installation. This commit adds a simple enforcement mechanism for that rule: if the code is compiled with ENFORCE_REGRESSION_TEST_NAME_RESTRICTIONS defined, it will emit a warning (not an error) whenever a database, role, tablespace, subscription, or replication origin name is created that doesn't obey the rule. Running one or more buildfarm members with that symbol defined should be enough to catch new violations, at least in the regular regression tests. Most TAP tests wouldn't notice such warnings, but that's actually fine because TAP tests don't execute against an existing server anyway. Since it's already the case that running src/test/modules/ tests in installcheck mode is deprecated, we can use that as a home for tests that seem unsafe to run against an existing server, such as tests that might have side-effects on existing roles. Document that (though this commit doesn't in itself make it any less safe than before). Update regress.sgml to define these restrictions more clearly, and to clean up assorted lack-of-up-to-date-ness in its descriptions of the available regression tests. Discussion: https://postgr.es/m/16638.1468620817@sss.pgh.pa.us
2019-06-29 17:34:00 +02:00
/* Also enforce regression testing naming rules, if enabled */
#ifdef ENFORCE_REGRESSION_TEST_NAME_RESTRICTIONS
if (strncmp(new_name, "regress_", 8) != 0)
elog(WARNING, "subscriptions created by regression test cases should have names starting with \"regress_\"");
#endif
/* Wake up related replication workers to handle this change quickly */
LogicalRepWorkersWakeupAtCommit(objectId);
}
else if (nameCacheId >= 0)
{
if (OidIsValid(namespaceId))
{
if (SearchSysCacheExists2(nameCacheId,
CStringGetDatum(new_name),
ObjectIdGetDatum(namespaceId)))
report_namespace_conflict(classId, new_name, namespaceId);
}
else
{
if (SearchSysCacheExists1(nameCacheId,
CStringGetDatum(new_name)))
report_name_conflict(classId, new_name);
}
}
/* Build modified tuple */
values = palloc0(RelationGetNumberOfAttributes(rel) * sizeof(Datum));
nulls = palloc0(RelationGetNumberOfAttributes(rel) * sizeof(bool));
replaces = palloc0(RelationGetNumberOfAttributes(rel) * sizeof(bool));
namestrcpy(&nameattrdata, new_name);
values[Anum_name - 1] = NameGetDatum(&nameattrdata);
replaces[Anum_name - 1] = true;
newtup = heap_modify_tuple(oldtup, RelationGetDescr(rel),
values, nulls, replaces);
/* Perform actual update */
CatalogTupleUpdate(rel, &oldtup->t_self, newtup);
InvokeObjectPostAlterHook(classId, objectId, 0);
/* Release memory */
pfree(values);
pfree(nulls);
pfree(replaces);
heap_freetuple(newtup);
ReleaseSysCache(oldtup);
}
/*
* Executes an ALTER OBJECT / RENAME TO statement. Based on the object
* type, the function appropriate to that type is executed.
*
* Return value is the address of the renamed object.
*/
ObjectAddress
ExecRenameStmt(RenameStmt *stmt)
{
switch (stmt->renameType)
{
case OBJECT_TABCONSTRAINT:
case OBJECT_DOMCONSTRAINT:
return RenameConstraint(stmt);
case OBJECT_DATABASE:
return RenameDatabase(stmt->subname, stmt->newname);
case OBJECT_ROLE:
return RenameRole(stmt->subname, stmt->newname);
2003-06-27 16:45:32 +02:00
case OBJECT_SCHEMA:
return RenameSchema(stmt->subname, stmt->newname);
2003-06-27 16:45:32 +02:00
case OBJECT_TABLESPACE:
return RenameTableSpace(stmt->subname, stmt->newname);
2003-06-27 16:45:32 +02:00
case OBJECT_TABLE:
case OBJECT_SEQUENCE:
case OBJECT_VIEW:
case OBJECT_MATVIEW:
2004-08-22 02:08:28 +02:00
case OBJECT_INDEX:
case OBJECT_FOREIGN_TABLE:
return RenameRelation(stmt);
2003-06-27 16:45:32 +02:00
case OBJECT_COLUMN:
case OBJECT_ATTRIBUTE:
return renameatt(stmt);
case OBJECT_RULE:
return RenameRewriteRule(stmt->relation, stmt->subname,
stmt->newname);
2003-06-27 16:45:32 +02:00
case OBJECT_TRIGGER:
return renametrig(stmt);
2003-06-27 16:45:32 +02:00
Row-Level Security Policies (RLS) Building on the updatable security-barrier views work, add the ability to define policies on tables to limit the set of rows which are returned from a query and which are allowed to be added to a table. Expressions defined by the policy for filtering are added to the security barrier quals of the query, while expressions defined to check records being added to a table are added to the with-check options of the query. New top-level commands are CREATE/ALTER/DROP POLICY and are controlled by the table owner. Row Security is able to be enabled and disabled by the owner on a per-table basis using ALTER TABLE .. ENABLE/DISABLE ROW SECURITY. Per discussion, ROW SECURITY is disabled on tables by default and must be enabled for policies on the table to be used. If no policies exist on a table with ROW SECURITY enabled, a default-deny policy is used and no records will be visible. By default, row security is applied at all times except for the table owner and the superuser. A new GUC, row_security, is added which can be set to ON, OFF, or FORCE. When set to FORCE, row security will be applied even for the table owner and superusers. When set to OFF, row security will be disabled when allowed and an error will be thrown if the user does not have rights to bypass row security. Per discussion, pg_dump sets row_security = OFF by default to ensure that exports and backups will have all data in the table or will error if there are insufficient privileges to bypass row security. A new option has been added to pg_dump, --enable-row-security, to ask pg_dump to export with row security enabled. A new role capability, BYPASSRLS, which can only be set by the superuser, is added to allow other users to be able to bypass row security using row_security = OFF. Many thanks to the various individuals who have helped with the design, particularly Robert Haas for his feedback. Authors include Craig Ringer, KaiGai Kohei, Adam Brightwell, Dean Rasheed, with additional changes and rework by me. Reviewers have included all of the above, Greg Smith, Jeff McCormick, and Robert Haas.
2014-09-19 17:18:35 +02:00
case OBJECT_POLICY:
return rename_policy(stmt);
case OBJECT_DOMAIN:
case OBJECT_TYPE:
return RenameType(stmt);
case OBJECT_AGGREGATE:
case OBJECT_COLLATION:
case OBJECT_CONVERSION:
case OBJECT_EVENT_TRIGGER:
case OBJECT_FDW:
case OBJECT_FOREIGN_SERVER:
case OBJECT_FUNCTION:
case OBJECT_OPCLASS:
case OBJECT_OPFAMILY:
case OBJECT_LANGUAGE:
case OBJECT_PROCEDURE:
case OBJECT_ROUTINE:
Implement multivariate n-distinct coefficients Add support for explicitly declared statistic objects (CREATE STATISTICS), allowing collection of statistics on more complex combinations that individual table columns. Companion commands DROP STATISTICS and ALTER STATISTICS ... OWNER TO / SET SCHEMA / RENAME are added too. All this DDL has been designed so that more statistic types can be added later on, such as multivariate most-common-values and multivariate histograms between columns of a single table, leaving room for permitting columns on multiple tables, too, as well as expressions. This commit only adds support for collection of n-distinct coefficient on user-specified sets of columns in a single table. This is useful to estimate number of distinct groups in GROUP BY and DISTINCT clauses; estimation errors there can cause over-allocation of memory in hashed aggregates, for instance, so it's a worthwhile problem to solve. A new special pseudo-type pg_ndistinct is used. (num-distinct estimation was deemed sufficiently useful by itself that this is worthwhile even if no further statistic types are added immediately; so much so that another version of essentially the same functionality was submitted by Kyotaro Horiguchi: https://postgr.es/m/20150828.173334.114731693.horiguchi.kyotaro@lab.ntt.co.jp though this commit does not use that code.) Author: Tomas Vondra. Some code rework by Álvaro. Reviewed-by: Dean Rasheed, David Rowley, Kyotaro Horiguchi, Jeff Janes, Ideriha Takeshi Discussion: https://postgr.es/m/543AFA15.4080608@fuzzy.cz https://postgr.es/m/20170320190220.ixlaueanxegqd5gr@alvherre.pgsql
2017-03-24 18:06:10 +01:00
case OBJECT_STATISTIC_EXT:
case OBJECT_TSCONFIGURATION:
case OBJECT_TSDICTIONARY:
case OBJECT_TSPARSER:
case OBJECT_TSTEMPLATE:
case OBJECT_PUBLICATION:
case OBJECT_SUBSCRIPTION:
{
ObjectAddress address;
Relation catalog;
Relation relation;
address = get_object_address(stmt->renameType,
stmt->object,
&relation,
AccessExclusiveLock, false);
Assert(relation == NULL);
catalog = table_open(address.classId, RowExclusiveLock);
AlterObjectRename_internal(catalog,
address.objectId,
stmt->newname);
table_close(catalog, RowExclusiveLock);
return address;
}
2003-06-27 16:45:32 +02:00
default:
elog(ERROR, "unrecognized rename stmt type: %d",
(int) stmt->renameType);
return InvalidObjectAddress; /* keep compiler happy */
2003-06-27 16:45:32 +02:00
}
}
/*
* Executes an ALTER OBJECT / [NO] DEPENDS ON EXTENSION statement.
*
* Return value is the address of the altered object. refAddress is an output
* argument which, if not null, receives the address of the object that the
* altered object now depends on.
*/
ObjectAddress
ExecAlterObjectDependsStmt(AlterObjectDependsStmt *stmt, ObjectAddress *refAddress)
{
ObjectAddress address;
ObjectAddress refAddr;
Relation rel;
address =
get_object_address_rv(stmt->objectType, stmt->relation, (List *) stmt->object,
&rel, AccessExclusiveLock, false);
/*
* Verify that the user is entitled to run the command.
*
* We don't check any privileges on the extension, because that's not
* needed. The object owner is stipulating, by running this command, that
* the extension owner can drop the object whenever they feel like it,
* which is not considered a problem.
*/
check_object_ownership(GetUserId(),
stmt->objectType, address, stmt->object, rel);
/*
* If a relation was involved, it would have been opened and locked. We
* don't need the relation here, but we'll retain the lock until commit.
*/
if (rel)
table_close(rel, NoLock);
refAddr = get_object_address(OBJECT_EXTENSION, (Node *) stmt->extname,
&rel, AccessExclusiveLock, false);
Assert(rel == NULL);
if (refAddress)
*refAddress = refAddr;
if (stmt->remove)
{
deleteDependencyRecordsForSpecific(address.classId, address.objectId,
DEPENDENCY_AUTO_EXTENSION,
refAddr.classId, refAddr.objectId);
}
else
{
List *currexts;
/* Avoid duplicates */
currexts = getAutoExtensionsOfObject(address.classId,
address.objectId);
if (!list_member_oid(currexts, refAddr.objectId))
recordDependencyOn(&address, &refAddr, DEPENDENCY_AUTO_EXTENSION);
}
return address;
}
/*
* Executes an ALTER OBJECT / SET SCHEMA statement. Based on the object
* type, the function appropriate to that type is executed.
*
* Return value is that of the altered object.
*
* oldSchemaAddr is an output argument which, if not NULL, is set to the object
* address of the original schema.
*/
ObjectAddress
ExecAlterObjectSchemaStmt(AlterObjectSchemaStmt *stmt,
ObjectAddress *oldSchemaAddr)
{
ObjectAddress address;
Oid oldNspOid;
switch (stmt->objectType)
{
case OBJECT_EXTENSION:
address = AlterExtensionNamespace(strVal(stmt->object), stmt->newschema,
oldSchemaAddr ? &oldNspOid : NULL);
break;
case OBJECT_FOREIGN_TABLE:
case OBJECT_SEQUENCE:
case OBJECT_TABLE:
case OBJECT_VIEW:
case OBJECT_MATVIEW:
address = AlterTableNamespace(stmt,
oldSchemaAddr ? &oldNspOid : NULL);
break;
2005-10-15 04:49:52 +02:00
case OBJECT_DOMAIN:
case OBJECT_TYPE:
address = AlterTypeNamespace(castNode(List, stmt->object), stmt->newschema,
stmt->objectType,
oldSchemaAddr ? &oldNspOid : NULL);
break;
/* generic code path */
case OBJECT_AGGREGATE:
case OBJECT_COLLATION:
case OBJECT_CONVERSION:
case OBJECT_FUNCTION:
case OBJECT_OPERATOR:
case OBJECT_OPCLASS:
case OBJECT_OPFAMILY:
case OBJECT_PROCEDURE:
case OBJECT_ROUTINE:
Implement multivariate n-distinct coefficients Add support for explicitly declared statistic objects (CREATE STATISTICS), allowing collection of statistics on more complex combinations that individual table columns. Companion commands DROP STATISTICS and ALTER STATISTICS ... OWNER TO / SET SCHEMA / RENAME are added too. All this DDL has been designed so that more statistic types can be added later on, such as multivariate most-common-values and multivariate histograms between columns of a single table, leaving room for permitting columns on multiple tables, too, as well as expressions. This commit only adds support for collection of n-distinct coefficient on user-specified sets of columns in a single table. This is useful to estimate number of distinct groups in GROUP BY and DISTINCT clauses; estimation errors there can cause over-allocation of memory in hashed aggregates, for instance, so it's a worthwhile problem to solve. A new special pseudo-type pg_ndistinct is used. (num-distinct estimation was deemed sufficiently useful by itself that this is worthwhile even if no further statistic types are added immediately; so much so that another version of essentially the same functionality was submitted by Kyotaro Horiguchi: https://postgr.es/m/20150828.173334.114731693.horiguchi.kyotaro@lab.ntt.co.jp though this commit does not use that code.) Author: Tomas Vondra. Some code rework by Álvaro. Reviewed-by: Dean Rasheed, David Rowley, Kyotaro Horiguchi, Jeff Janes, Ideriha Takeshi Discussion: https://postgr.es/m/543AFA15.4080608@fuzzy.cz https://postgr.es/m/20170320190220.ixlaueanxegqd5gr@alvherre.pgsql
2017-03-24 18:06:10 +01:00
case OBJECT_STATISTIC_EXT:
case OBJECT_TSCONFIGURATION:
case OBJECT_TSDICTIONARY:
case OBJECT_TSPARSER:
case OBJECT_TSTEMPLATE:
{
Relation catalog;
Relation relation;
Oid classId;
Oid nspOid;
address = get_object_address(stmt->objectType,
stmt->object,
&relation,
AccessExclusiveLock,
false);
Assert(relation == NULL);
classId = address.classId;
catalog = table_open(classId, RowExclusiveLock);
nspOid = LookupCreationNamespace(stmt->newschema);
oldNspOid = AlterObjectNamespace_internal(catalog, address.objectId,
nspOid);
table_close(catalog, RowExclusiveLock);
}
break;
2005-10-15 04:49:52 +02:00
default:
elog(ERROR, "unrecognized AlterObjectSchemaStmt type: %d",
(int) stmt->objectType);
return InvalidObjectAddress; /* keep compiler happy */
}
if (oldSchemaAddr)
ObjectAddressSet(*oldSchemaAddr, NamespaceRelationId, oldNspOid);
return address;
}
/*
* Change an object's namespace given its classOid and object Oid.
*
* Objects that don't have a namespace should be ignored.
*
* This function is currently used only by ALTER EXTENSION SET SCHEMA,
* so it only needs to cover object types that can be members of an
* extension, and it doesn't have to deal with certain special cases
* such as not wanting to process array types --- those should never
* be direct members of an extension anyway. Nonetheless, we insist
* on listing all OCLASS types in the switch.
*
* Returns the OID of the object's previous namespace, or InvalidOid if
* object doesn't have a schema.
*/
Oid
AlterObjectNamespace_oid(Oid classId, Oid objid, Oid nspOid,
ObjectAddresses *objsMoved)
{
Oid oldNspOid = InvalidOid;
ObjectAddress dep;
dep.classId = classId;
dep.objectId = objid;
dep.objectSubId = 0;
switch (getObjectClass(&dep))
{
case OCLASS_CLASS:
{
Relation rel;
rel = relation_open(objid, AccessExclusiveLock);
oldNspOid = RelationGetNamespace(rel);
AlterTableNamespaceInternal(rel, oldNspOid, nspOid, objsMoved);
relation_close(rel, NoLock);
break;
}
case OCLASS_TYPE:
oldNspOid = AlterTypeNamespace_oid(objid, nspOid, objsMoved);
break;
case OCLASS_PROC:
case OCLASS_COLLATION:
case OCLASS_CONVERSION:
case OCLASS_OPERATOR:
case OCLASS_OPCLASS:
case OCLASS_OPFAMILY:
case OCLASS_STATISTIC_EXT:
case OCLASS_TSPARSER:
case OCLASS_TSDICT:
case OCLASS_TSTEMPLATE:
case OCLASS_TSCONFIG:
{
Relation catalog;
catalog = table_open(classId, RowExclusiveLock);
oldNspOid = AlterObjectNamespace_internal(catalog, objid,
nspOid);
table_close(catalog, RowExclusiveLock);
}
break;
case OCLASS_CAST:
case OCLASS_CONSTRAINT:
case OCLASS_DEFAULT:
case OCLASS_LANGUAGE:
case OCLASS_LARGEOBJECT:
case OCLASS_AM:
case OCLASS_AMOP:
case OCLASS_AMPROC:
case OCLASS_REWRITE:
case OCLASS_TRIGGER:
case OCLASS_SCHEMA:
case OCLASS_ROLE:
case OCLASS_ROLE_MEMBERSHIP:
case OCLASS_DATABASE:
case OCLASS_TBLSPACE:
case OCLASS_FDW:
case OCLASS_FOREIGN_SERVER:
case OCLASS_USER_MAPPING:
case OCLASS_DEFACL:
case OCLASS_EXTENSION:
case OCLASS_EVENT_TRIGGER:
case OCLASS_PARAMETER_ACL:
case OCLASS_POLICY:
case OCLASS_PUBLICATION:
case OCLASS_PUBLICATION_NAMESPACE:
case OCLASS_PUBLICATION_REL:
case OCLASS_SUBSCRIPTION:
case OCLASS_TRANSFORM:
/* ignore object types that don't have schema-qualified names */
break;
/*
* There's intentionally no default: case here; we want the
* compiler to warn if a new OCLASS hasn't been handled above.
*/
}
return oldNspOid;
}
/*
* Generic function to change the namespace of a given object, for simple
* cases (won't work for tables, nor other cases where we need to do more
* than change the namespace column of a single catalog entry).
*
* rel: catalog relation containing object (RowExclusiveLock'd by caller)
* objid: OID of object to change the namespace of
* nspOid: OID of new namespace
*
* Returns the OID of the object's previous namespace.
*/
static Oid
AlterObjectNamespace_internal(Relation rel, Oid objid, Oid nspOid)
{
Oid classId = RelationGetRelid(rel);
int oidCacheId = get_object_catcache_oid(classId);
int nameCacheId = get_object_catcache_name(classId);
AttrNumber Anum_name = get_object_attnum_name(classId);
AttrNumber Anum_namespace = get_object_attnum_namespace(classId);
AttrNumber Anum_owner = get_object_attnum_owner(classId);
Oid oldNspOid;
Datum name,
namespace;
bool isnull;
HeapTuple tup,
newtup;
Datum *values;
bool *nulls;
bool *replaces;
tup = SearchSysCacheCopy1(oidCacheId, ObjectIdGetDatum(objid));
if (!HeapTupleIsValid(tup)) /* should not happen */
elog(ERROR, "cache lookup failed for object %u of catalog \"%s\"",
objid, RelationGetRelationName(rel));
name = heap_getattr(tup, Anum_name, RelationGetDescr(rel), &isnull);
Assert(!isnull);
namespace = heap_getattr(tup, Anum_namespace, RelationGetDescr(rel),
&isnull);
Assert(!isnull);
oldNspOid = DatumGetObjectId(namespace);
/*
* If the object is already in the correct namespace, we don't need to do
* anything except fire the object access hook.
*/
if (oldNspOid == nspOid)
{
InvokeObjectPostAlterHook(classId, objid, 0);
return oldNspOid;
}
/* Check basic namespace related issues */
CheckSetNamespace(oldNspOid, nspOid);
/* Permission checks ... superusers can always do it */
if (!superuser())
{
Datum owner;
Oid ownerId;
AclResult aclresult;
/* Fail if object does not have an explicit owner */
if (Anum_owner <= 0)
ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("must be superuser to set schema of %s",
getObjectDescriptionOids(classId, objid))));
/* Otherwise, must be owner of the existing object */
owner = heap_getattr(tup, Anum_owner, RelationGetDescr(rel), &isnull);
Assert(!isnull);
ownerId = DatumGetObjectId(owner);
if (!has_privs_of_role(GetUserId(), ownerId))
aclcheck_error(ACLCHECK_NOT_OWNER, get_object_type(classId, objid),
NameStr(*(DatumGetName(name))));
/* User must have CREATE privilege on new namespace */
aclresult = object_aclcheck(NamespaceRelationId, nspOid, GetUserId(), ACL_CREATE);
if (aclresult != ACLCHECK_OK)
aclcheck_error(aclresult, OBJECT_SCHEMA,
get_namespace_name(nspOid));
}
/*
* Check for duplicate name (more friendly than unique-index failure).
* Since this is just a friendliness check, we can just skip it in cases
* where there isn't suitable support.
*/
if (classId == ProcedureRelationId)
{
Form_pg_proc proc = (Form_pg_proc) GETSTRUCT(tup);
IsThereFunctionInNamespace(NameStr(proc->proname), proc->pronargs,
&proc->proargtypes, nspOid);
}
else if (classId == CollationRelationId)
{
Form_pg_collation coll = (Form_pg_collation) GETSTRUCT(tup);
IsThereCollationInNamespace(NameStr(coll->collname), nspOid);
}
else if (classId == OperatorClassRelationId)
{
Form_pg_opclass opc = (Form_pg_opclass) GETSTRUCT(tup);
IsThereOpClassInNamespace(NameStr(opc->opcname),
opc->opcmethod, nspOid);
}
else if (classId == OperatorFamilyRelationId)
{
Form_pg_opfamily opf = (Form_pg_opfamily) GETSTRUCT(tup);
IsThereOpFamilyInNamespace(NameStr(opf->opfname),
opf->opfmethod, nspOid);
}
else if (nameCacheId >= 0 &&
SearchSysCacheExists2(nameCacheId, name,
ObjectIdGetDatum(nspOid)))
report_namespace_conflict(classId,
NameStr(*(DatumGetName(name))),
nspOid);
/* Build modified tuple */
values = palloc0(RelationGetNumberOfAttributes(rel) * sizeof(Datum));
nulls = palloc0(RelationGetNumberOfAttributes(rel) * sizeof(bool));
replaces = palloc0(RelationGetNumberOfAttributes(rel) * sizeof(bool));
values[Anum_namespace - 1] = ObjectIdGetDatum(nspOid);
replaces[Anum_namespace - 1] = true;
newtup = heap_modify_tuple(tup, RelationGetDescr(rel),
values, nulls, replaces);
/* Perform actual update */
CatalogTupleUpdate(rel, &tup->t_self, newtup);
/* Release memory */
pfree(values);
pfree(nulls);
pfree(replaces);
/* update dependencies to point to the new schema */
changeDependencyFor(classId, objid,
NamespaceRelationId, oldNspOid, nspOid);
InvokeObjectPostAlterHook(classId, objid, 0);
return oldNspOid;
}
/*
* Executes an ALTER OBJECT / OWNER TO statement. Based on the object
* type, the function appropriate to that type is executed.
*/
ObjectAddress
ExecAlterOwnerStmt(AlterOwnerStmt *stmt)
{
Oid newowner = get_rolespec_oid(stmt->newowner, false);
switch (stmt->objectType)
{
case OBJECT_DATABASE:
return AlterDatabaseOwner(strVal(stmt->object), newowner);
case OBJECT_SCHEMA:
return AlterSchemaOwner(strVal(stmt->object), newowner);
case OBJECT_TYPE:
case OBJECT_DOMAIN: /* same as TYPE */
return AlterTypeOwner(castNode(List, stmt->object), newowner, stmt->objectType);
break;
case OBJECT_FDW:
return AlterForeignDataWrapperOwner(strVal(stmt->object),
newowner);
case OBJECT_FOREIGN_SERVER:
return AlterForeignServerOwner(strVal(stmt->object),
newowner);
case OBJECT_EVENT_TRIGGER:
return AlterEventTriggerOwner(strVal(stmt->object),
newowner);
case OBJECT_PUBLICATION:
return AlterPublicationOwner(strVal(stmt->object),
newowner);
case OBJECT_SUBSCRIPTION:
return AlterSubscriptionOwner(strVal(stmt->object),
newowner);
/* Generic cases */
case OBJECT_AGGREGATE:
case OBJECT_COLLATION:
case OBJECT_CONVERSION:
case OBJECT_FUNCTION:
case OBJECT_LANGUAGE:
case OBJECT_LARGEOBJECT:
case OBJECT_OPERATOR:
case OBJECT_OPCLASS:
case OBJECT_OPFAMILY:
case OBJECT_PROCEDURE:
case OBJECT_ROUTINE:
Implement multivariate n-distinct coefficients Add support for explicitly declared statistic objects (CREATE STATISTICS), allowing collection of statistics on more complex combinations that individual table columns. Companion commands DROP STATISTICS and ALTER STATISTICS ... OWNER TO / SET SCHEMA / RENAME are added too. All this DDL has been designed so that more statistic types can be added later on, such as multivariate most-common-values and multivariate histograms between columns of a single table, leaving room for permitting columns on multiple tables, too, as well as expressions. This commit only adds support for collection of n-distinct coefficient on user-specified sets of columns in a single table. This is useful to estimate number of distinct groups in GROUP BY and DISTINCT clauses; estimation errors there can cause over-allocation of memory in hashed aggregates, for instance, so it's a worthwhile problem to solve. A new special pseudo-type pg_ndistinct is used. (num-distinct estimation was deemed sufficiently useful by itself that this is worthwhile even if no further statistic types are added immediately; so much so that another version of essentially the same functionality was submitted by Kyotaro Horiguchi: https://postgr.es/m/20150828.173334.114731693.horiguchi.kyotaro@lab.ntt.co.jp though this commit does not use that code.) Author: Tomas Vondra. Some code rework by Álvaro. Reviewed-by: Dean Rasheed, David Rowley, Kyotaro Horiguchi, Jeff Janes, Ideriha Takeshi Discussion: https://postgr.es/m/543AFA15.4080608@fuzzy.cz https://postgr.es/m/20170320190220.ixlaueanxegqd5gr@alvherre.pgsql
2017-03-24 18:06:10 +01:00
case OBJECT_STATISTIC_EXT:
case OBJECT_TABLESPACE:
case OBJECT_TSDICTIONARY:
case OBJECT_TSCONFIGURATION:
{
Relation catalog;
Relation relation;
Oid classId;
ObjectAddress address;
address = get_object_address(stmt->objectType,
stmt->object,
&relation,
AccessExclusiveLock,
false);
Assert(relation == NULL);
classId = address.classId;
/*
* XXX - get_object_address returns Oid of pg_largeobject
* catalog for OBJECT_LARGEOBJECT because of historical
* reasons. Fix up it here.
*/
if (classId == LargeObjectRelationId)
classId = LargeObjectMetadataRelationId;
catalog = table_open(classId, RowExclusiveLock);
AlterObjectOwner_internal(catalog, address.objectId, newowner);
table_close(catalog, RowExclusiveLock);
return address;
}
break;
default:
elog(ERROR, "unrecognized AlterOwnerStmt type: %d",
(int) stmt->objectType);
return InvalidObjectAddress; /* keep compiler happy */
}
}
/*
* Generic function to change the ownership of a given object, for simple
* cases (won't work for tables, nor other cases where we need to do more than
* change the ownership column of a single catalog entry).
*
* rel: catalog relation containing object (RowExclusiveLock'd by caller)
* objectId: OID of object to change the ownership of
* new_ownerId: OID of new object owner
*/
void
AlterObjectOwner_internal(Relation rel, Oid objectId, Oid new_ownerId)
{
Oid classId = RelationGetRelid(rel);
Remove WITH OIDS support, change oid catalog column visibility. Previously tables declared WITH OIDS, including a significant fraction of the catalog tables, stored the oid column not as a normal column, but as part of the tuple header. This special column was not shown by default, which was somewhat odd, as it's often (consider e.g. pg_class.oid) one of the more important parts of a row. Neither pg_dump nor COPY included the contents of the oid column by default. The fact that the oid column was not an ordinary column necessitated a significant amount of special case code to support oid columns. That already was painful for the existing, but upcoming work aiming to make table storage pluggable, would have required expanding and duplicating that "specialness" significantly. WITH OIDS has been deprecated since 2005 (commit ff02d0a05280e0). Remove it. Removing includes: - CREATE TABLE and ALTER TABLE syntax for declaring the table to be WITH OIDS has been removed (WITH (oids[ = true]) will error out) - pg_dump does not support dumping tables declared WITH OIDS and will issue a warning when dumping one (and ignore the oid column). - restoring an pg_dump archive with pg_restore will warn when restoring a table with oid contents (and ignore the oid column) - COPY will refuse to load binary dump that includes oids. - pg_upgrade will error out when encountering tables declared WITH OIDS, they have to be altered to remove the oid column first. - Functionality to access the oid of the last inserted row (like plpgsql's RESULT_OID, spi's SPI_lastoid, ...) has been removed. The syntax for declaring a table WITHOUT OIDS (or WITH (oids = false) for CREATE TABLE) is still supported. While that requires a bit of support code, it seems unnecessary to break applications / dumps that do not use oids, and are explicit about not using them. The biggest user of WITH OID columns was postgres' catalog. This commit changes all 'magic' oid columns to be columns that are normally declared and stored. To reduce unnecessary query breakage all the newly added columns are still named 'oid', even if a table's column naming scheme would indicate 'reloid' or such. This obviously requires adapting a lot code, mostly replacing oid access via HeapTupleGetOid() with access to the underlying Form_pg_*->oid column. The bootstrap process now assigns oids for all oid columns in genbki.pl that do not have an explicit value (starting at the largest oid previously used), only oids assigned later by oids will be above FirstBootstrapObjectId. As the oid column now is a normal column the special bootstrap syntax for oids has been removed. Oids are not automatically assigned during insertion anymore, all backend code explicitly assigns oids with GetNewOidWithIndex(). For the rare case that insertions into the catalog via SQL are called for the new pg_nextoid() function can be used (which only works on catalog tables). The fact that oid columns on system tables are now normal columns means that they will be included in the set of columns expanded by * (i.e. SELECT * FROM pg_class will now include the table's oid, previously it did not). It'd not technically be hard to hide oid column by default, but that'd mean confusing behavior would either have to be carried forward forever, or it'd cause breakage down the line. While it's not unlikely that further adjustments are needed, the scope/invasiveness of the patch makes it worthwhile to get merge this now. It's painful to maintain externally, too complicated to commit after the code code freeze, and a dependency of a number of other patches. Catversion bump, for obvious reasons. Author: Andres Freund, with contributions by John Naylor Discussion: https://postgr.es/m/20180930034810.ywp2c7awz7opzcfr@alap3.anarazel.de
2018-11-21 00:36:57 +01:00
AttrNumber Anum_oid = get_object_attnum_oid(classId);
AttrNumber Anum_owner = get_object_attnum_owner(classId);
AttrNumber Anum_namespace = get_object_attnum_namespace(classId);
AttrNumber Anum_acl = get_object_attnum_acl(classId);
AttrNumber Anum_name = get_object_attnum_name(classId);
HeapTuple oldtup;
Datum datum;
bool isnull;
Oid old_ownerId;
Oid namespaceId = InvalidOid;
Remove WITH OIDS support, change oid catalog column visibility. Previously tables declared WITH OIDS, including a significant fraction of the catalog tables, stored the oid column not as a normal column, but as part of the tuple header. This special column was not shown by default, which was somewhat odd, as it's often (consider e.g. pg_class.oid) one of the more important parts of a row. Neither pg_dump nor COPY included the contents of the oid column by default. The fact that the oid column was not an ordinary column necessitated a significant amount of special case code to support oid columns. That already was painful for the existing, but upcoming work aiming to make table storage pluggable, would have required expanding and duplicating that "specialness" significantly. WITH OIDS has been deprecated since 2005 (commit ff02d0a05280e0). Remove it. Removing includes: - CREATE TABLE and ALTER TABLE syntax for declaring the table to be WITH OIDS has been removed (WITH (oids[ = true]) will error out) - pg_dump does not support dumping tables declared WITH OIDS and will issue a warning when dumping one (and ignore the oid column). - restoring an pg_dump archive with pg_restore will warn when restoring a table with oid contents (and ignore the oid column) - COPY will refuse to load binary dump that includes oids. - pg_upgrade will error out when encountering tables declared WITH OIDS, they have to be altered to remove the oid column first. - Functionality to access the oid of the last inserted row (like plpgsql's RESULT_OID, spi's SPI_lastoid, ...) has been removed. The syntax for declaring a table WITHOUT OIDS (or WITH (oids = false) for CREATE TABLE) is still supported. While that requires a bit of support code, it seems unnecessary to break applications / dumps that do not use oids, and are explicit about not using them. The biggest user of WITH OID columns was postgres' catalog. This commit changes all 'magic' oid columns to be columns that are normally declared and stored. To reduce unnecessary query breakage all the newly added columns are still named 'oid', even if a table's column naming scheme would indicate 'reloid' or such. This obviously requires adapting a lot code, mostly replacing oid access via HeapTupleGetOid() with access to the underlying Form_pg_*->oid column. The bootstrap process now assigns oids for all oid columns in genbki.pl that do not have an explicit value (starting at the largest oid previously used), only oids assigned later by oids will be above FirstBootstrapObjectId. As the oid column now is a normal column the special bootstrap syntax for oids has been removed. Oids are not automatically assigned during insertion anymore, all backend code explicitly assigns oids with GetNewOidWithIndex(). For the rare case that insertions into the catalog via SQL are called for the new pg_nextoid() function can be used (which only works on catalog tables). The fact that oid columns on system tables are now normal columns means that they will be included in the set of columns expanded by * (i.e. SELECT * FROM pg_class will now include the table's oid, previously it did not). It'd not technically be hard to hide oid column by default, but that'd mean confusing behavior would either have to be carried forward forever, or it'd cause breakage down the line. While it's not unlikely that further adjustments are needed, the scope/invasiveness of the patch makes it worthwhile to get merge this now. It's painful to maintain externally, too complicated to commit after the code code freeze, and a dependency of a number of other patches. Catversion bump, for obvious reasons. Author: Andres Freund, with contributions by John Naylor Discussion: https://postgr.es/m/20180930034810.ywp2c7awz7opzcfr@alap3.anarazel.de
2018-11-21 00:36:57 +01:00
oldtup = get_catalog_object_by_oid(rel, Anum_oid, objectId);
if (oldtup == NULL)
elog(ERROR, "cache lookup failed for object %u of catalog \"%s\"",
objectId, RelationGetRelationName(rel));
datum = heap_getattr(oldtup, Anum_owner,
RelationGetDescr(rel), &isnull);
Assert(!isnull);
old_ownerId = DatumGetObjectId(datum);
if (Anum_namespace != InvalidAttrNumber)
{
datum = heap_getattr(oldtup, Anum_namespace,
RelationGetDescr(rel), &isnull);
Assert(!isnull);
namespaceId = DatumGetObjectId(datum);
}
if (old_ownerId != new_ownerId)
{
AttrNumber nattrs;
HeapTuple newtup;
Datum *values;
bool *nulls;
bool *replaces;
/* Superusers can bypass permission checks */
if (!superuser())
{
/* must be owner */
if (!has_privs_of_role(GetUserId(), old_ownerId))
{
char *objname;
char namebuf[NAMEDATALEN];
if (Anum_name != InvalidAttrNumber)
{
datum = heap_getattr(oldtup, Anum_name,
RelationGetDescr(rel), &isnull);
Assert(!isnull);
objname = NameStr(*DatumGetName(datum));
}
else
{
Remove WITH OIDS support, change oid catalog column visibility. Previously tables declared WITH OIDS, including a significant fraction of the catalog tables, stored the oid column not as a normal column, but as part of the tuple header. This special column was not shown by default, which was somewhat odd, as it's often (consider e.g. pg_class.oid) one of the more important parts of a row. Neither pg_dump nor COPY included the contents of the oid column by default. The fact that the oid column was not an ordinary column necessitated a significant amount of special case code to support oid columns. That already was painful for the existing, but upcoming work aiming to make table storage pluggable, would have required expanding and duplicating that "specialness" significantly. WITH OIDS has been deprecated since 2005 (commit ff02d0a05280e0). Remove it. Removing includes: - CREATE TABLE and ALTER TABLE syntax for declaring the table to be WITH OIDS has been removed (WITH (oids[ = true]) will error out) - pg_dump does not support dumping tables declared WITH OIDS and will issue a warning when dumping one (and ignore the oid column). - restoring an pg_dump archive with pg_restore will warn when restoring a table with oid contents (and ignore the oid column) - COPY will refuse to load binary dump that includes oids. - pg_upgrade will error out when encountering tables declared WITH OIDS, they have to be altered to remove the oid column first. - Functionality to access the oid of the last inserted row (like plpgsql's RESULT_OID, spi's SPI_lastoid, ...) has been removed. The syntax for declaring a table WITHOUT OIDS (or WITH (oids = false) for CREATE TABLE) is still supported. While that requires a bit of support code, it seems unnecessary to break applications / dumps that do not use oids, and are explicit about not using them. The biggest user of WITH OID columns was postgres' catalog. This commit changes all 'magic' oid columns to be columns that are normally declared and stored. To reduce unnecessary query breakage all the newly added columns are still named 'oid', even if a table's column naming scheme would indicate 'reloid' or such. This obviously requires adapting a lot code, mostly replacing oid access via HeapTupleGetOid() with access to the underlying Form_pg_*->oid column. The bootstrap process now assigns oids for all oid columns in genbki.pl that do not have an explicit value (starting at the largest oid previously used), only oids assigned later by oids will be above FirstBootstrapObjectId. As the oid column now is a normal column the special bootstrap syntax for oids has been removed. Oids are not automatically assigned during insertion anymore, all backend code explicitly assigns oids with GetNewOidWithIndex(). For the rare case that insertions into the catalog via SQL are called for the new pg_nextoid() function can be used (which only works on catalog tables). The fact that oid columns on system tables are now normal columns means that they will be included in the set of columns expanded by * (i.e. SELECT * FROM pg_class will now include the table's oid, previously it did not). It'd not technically be hard to hide oid column by default, but that'd mean confusing behavior would either have to be carried forward forever, or it'd cause breakage down the line. While it's not unlikely that further adjustments are needed, the scope/invasiveness of the patch makes it worthwhile to get merge this now. It's painful to maintain externally, too complicated to commit after the code code freeze, and a dependency of a number of other patches. Catversion bump, for obvious reasons. Author: Andres Freund, with contributions by John Naylor Discussion: https://postgr.es/m/20180930034810.ywp2c7awz7opzcfr@alap3.anarazel.de
2018-11-21 00:36:57 +01:00
snprintf(namebuf, sizeof(namebuf), "%u", objectId);
objname = namebuf;
}
aclcheck_error(ACLCHECK_NOT_OWNER, get_object_type(classId, objectId),
objname);
}
/* Must be able to become new owner */
check_can_set_role(GetUserId(), new_ownerId);
/* New owner must have CREATE privilege on namespace */
if (OidIsValid(namespaceId))
{
AclResult aclresult;
aclresult = object_aclcheck(NamespaceRelationId, namespaceId, new_ownerId,
ACL_CREATE);
if (aclresult != ACLCHECK_OK)
aclcheck_error(aclresult, OBJECT_SCHEMA,
get_namespace_name(namespaceId));
}
}
/* Build a modified tuple */
nattrs = RelationGetNumberOfAttributes(rel);
values = palloc0(nattrs * sizeof(Datum));
nulls = palloc0(nattrs * sizeof(bool));
replaces = palloc0(nattrs * sizeof(bool));
values[Anum_owner - 1] = ObjectIdGetDatum(new_ownerId);
replaces[Anum_owner - 1] = true;
/*
* Determine the modified ACL for the new owner. This is only
* necessary when the ACL is non-null.
*/
if (Anum_acl != InvalidAttrNumber)
{
datum = heap_getattr(oldtup,
Anum_acl, RelationGetDescr(rel), &isnull);
if (!isnull)
{
Acl *newAcl;
newAcl = aclnewowner(DatumGetAclP(datum),
old_ownerId, new_ownerId);
values[Anum_acl - 1] = PointerGetDatum(newAcl);
replaces[Anum_acl - 1] = true;
}
}
newtup = heap_modify_tuple(oldtup, RelationGetDescr(rel),
values, nulls, replaces);
/* Perform actual update */
CatalogTupleUpdate(rel, &newtup->t_self, newtup);
/* Update owner dependency reference */
if (classId == LargeObjectMetadataRelationId)
classId = LargeObjectRelationId;
Remove WITH OIDS support, change oid catalog column visibility. Previously tables declared WITH OIDS, including a significant fraction of the catalog tables, stored the oid column not as a normal column, but as part of the tuple header. This special column was not shown by default, which was somewhat odd, as it's often (consider e.g. pg_class.oid) one of the more important parts of a row. Neither pg_dump nor COPY included the contents of the oid column by default. The fact that the oid column was not an ordinary column necessitated a significant amount of special case code to support oid columns. That already was painful for the existing, but upcoming work aiming to make table storage pluggable, would have required expanding and duplicating that "specialness" significantly. WITH OIDS has been deprecated since 2005 (commit ff02d0a05280e0). Remove it. Removing includes: - CREATE TABLE and ALTER TABLE syntax for declaring the table to be WITH OIDS has been removed (WITH (oids[ = true]) will error out) - pg_dump does not support dumping tables declared WITH OIDS and will issue a warning when dumping one (and ignore the oid column). - restoring an pg_dump archive with pg_restore will warn when restoring a table with oid contents (and ignore the oid column) - COPY will refuse to load binary dump that includes oids. - pg_upgrade will error out when encountering tables declared WITH OIDS, they have to be altered to remove the oid column first. - Functionality to access the oid of the last inserted row (like plpgsql's RESULT_OID, spi's SPI_lastoid, ...) has been removed. The syntax for declaring a table WITHOUT OIDS (or WITH (oids = false) for CREATE TABLE) is still supported. While that requires a bit of support code, it seems unnecessary to break applications / dumps that do not use oids, and are explicit about not using them. The biggest user of WITH OID columns was postgres' catalog. This commit changes all 'magic' oid columns to be columns that are normally declared and stored. To reduce unnecessary query breakage all the newly added columns are still named 'oid', even if a table's column naming scheme would indicate 'reloid' or such. This obviously requires adapting a lot code, mostly replacing oid access via HeapTupleGetOid() with access to the underlying Form_pg_*->oid column. The bootstrap process now assigns oids for all oid columns in genbki.pl that do not have an explicit value (starting at the largest oid previously used), only oids assigned later by oids will be above FirstBootstrapObjectId. As the oid column now is a normal column the special bootstrap syntax for oids has been removed. Oids are not automatically assigned during insertion anymore, all backend code explicitly assigns oids with GetNewOidWithIndex(). For the rare case that insertions into the catalog via SQL are called for the new pg_nextoid() function can be used (which only works on catalog tables). The fact that oid columns on system tables are now normal columns means that they will be included in the set of columns expanded by * (i.e. SELECT * FROM pg_class will now include the table's oid, previously it did not). It'd not technically be hard to hide oid column by default, but that'd mean confusing behavior would either have to be carried forward forever, or it'd cause breakage down the line. While it's not unlikely that further adjustments are needed, the scope/invasiveness of the patch makes it worthwhile to get merge this now. It's painful to maintain externally, too complicated to commit after the code code freeze, and a dependency of a number of other patches. Catversion bump, for obvious reasons. Author: Andres Freund, with contributions by John Naylor Discussion: https://postgr.es/m/20180930034810.ywp2c7awz7opzcfr@alap3.anarazel.de
2018-11-21 00:36:57 +01:00
changeDependencyOnOwner(classId, objectId, new_ownerId);
/* Release memory */
pfree(values);
pfree(nulls);
pfree(replaces);
}
InvokeObjectPostAlterHook(classId, objectId, 0);
}