Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5510931

Browse files
committed
Add more ALTER <object> .. SET SCHEMA commands.
This adds support for changing the schema of a conversion, operator, operator class, operator family, text search configuration, text search dictionary, text search parser, or text search template. Dimitri Fontaine, with assorted corrections and other kibitzing.
1 parent 1d9a0ab commit 5510931

21 files changed

+618
-11
lines changed

doc/src/sgml/ref/alter_conversion.sgml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ PostgreSQL documentation
2323
<synopsis>
2424
ALTER CONVERSION <replaceable>name</replaceable> RENAME TO <replaceable>new_name</replaceable>
2525
ALTER CONVERSION <replaceable>name</replaceable> OWNER TO <replaceable>new_owner</replaceable>
26+
ALTER CONVERSION <replaceable>name</replaceable> SET SCHEMA <replaceable>new_schema</replaceable>
2627
</synopsis>
2728
</refsynopsisdiv>
2829

@@ -75,6 +76,15 @@ ALTER CONVERSION <replaceable>name</replaceable> OWNER TO <replaceable>new_owner
7576
</para>
7677
</listitem>
7778
</varlistentry>
79+
80+
<varlistentry>
81+
<term><replaceable class="parameter">new_schema</replaceable></term>
82+
<listitem>
83+
<para>
84+
The new schema for the conversion.
85+
</para>
86+
</listitem>
87+
</varlistentry>
7888
</variablelist>
7989
</refsect1>
8090

doc/src/sgml/ref/alter_opclass.sgml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ PostgreSQL documentation
2323
<synopsis>
2424
ALTER OPERATOR CLASS <replaceable>name</replaceable> USING <replaceable class="parameter">index_method</replaceable> RENAME TO <replaceable>new_name</replaceable>
2525
ALTER OPERATOR CLASS <replaceable>name</replaceable> USING <replaceable class="parameter">index_method</replaceable> OWNER TO <replaceable>new_owner</replaceable>
26+
ALTER OPERATOR CLASS <replaceable>name</replaceable> USING <replaceable class="parameter">index_method</replaceable> SET SCHEMA <replaceable>new_schema</replaceable>
2627
</synopsis>
2728
</refsynopsisdiv>
2829

@@ -85,6 +86,15 @@ ALTER OPERATOR CLASS <replaceable>name</replaceable> USING <replaceable class="p
8586
</para>
8687
</listitem>
8788
</varlistentry>
89+
90+
<varlistentry>
91+
<term><replaceable class="parameter">new_schema</replaceable></term>
92+
<listitem>
93+
<para>
94+
The new schema for the operator class.
95+
</para>
96+
</listitem>
97+
</varlistentry>
8898
</variablelist>
8999
</refsect1>
90100

doc/src/sgml/ref/alter_operator.sgml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ PostgreSQL documentation
2222
<refsynopsisdiv>
2323
<synopsis>
2424
ALTER OPERATOR <replaceable>name</replaceable> ( { <replaceable>left_type</replaceable> | NONE } , { <replaceable>right_type</replaceable> | NONE } ) OWNER TO <replaceable>new_owner</replaceable>
25+
ALTER OPERATOR <replaceable>name</replaceable> ( { <replaceable>left_type</replaceable> | NONE } , { <replaceable>right_type</replaceable> | NONE } ) SET SCHEMA <replaceable>new_schema</replaceable>
2526
</synopsis>
2627
</refsynopsisdiv>
2728

@@ -85,6 +86,15 @@ ALTER OPERATOR <replaceable>name</replaceable> ( { <replaceable>left_type</repla
8586
</para>
8687
</listitem>
8788
</varlistentry>
89+
90+
<varlistentry>
91+
<term><replaceable class="parameter">new_schema</replaceable></term>
92+
<listitem>
93+
<para>
94+
The new schema for the operator.
95+
</para>
96+
</listitem>
97+
</varlistentry>
8898
</variablelist>
8999
</refsect1>
90100

doc/src/sgml/ref/alter_opfamily.sgml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ ALTER OPERATOR FAMILY <replaceable>name</replaceable> USING <replaceable class="
3131
} [, ... ]
3232
ALTER OPERATOR FAMILY <replaceable>name</replaceable> USING <replaceable class="parameter">index_method</replaceable> RENAME TO <replaceable>new_name</replaceable>
3333
ALTER OPERATOR FAMILY <replaceable>name</replaceable> USING <replaceable class="parameter">index_method</replaceable> OWNER TO <replaceable>new_owner</replaceable>
34+
ALTER OPERATOR FAMILY <replaceable>name</replaceable> USING <replaceable class="parameter">index_method</replaceable> SET SCHEMA <replaceable>new_schema</replaceable>
3435
</synopsis>
3536
</refsynopsisdiv>
3637

@@ -216,6 +217,15 @@ ALTER OPERATOR FAMILY <replaceable>name</replaceable> USING <replaceable class="
216217
</para>
217218
</listitem>
218219
</varlistentry>
220+
221+
<varlistentry>
222+
<term><replaceable class="parameter">new_schema</replaceable></term>
223+
<listitem>
224+
<para>
225+
The new schema for the operator family.
226+
</para>
227+
</listitem>
228+
</varlistentry>
219229
</variablelist>
220230

221231
<para>

doc/src/sgml/ref/alter_tsconfig.sgml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ ALTER TEXT SEARCH CONFIGURATION <replaceable>name</replaceable>
3333
DROP MAPPING [ IF EXISTS ] FOR <replaceable class="parameter">token_type</replaceable> [, ... ]
3434
ALTER TEXT SEARCH CONFIGURATION <replaceable>name</replaceable> RENAME TO <replaceable>new_name</replaceable>
3535
ALTER TEXT SEARCH CONFIGURATION <replaceable>name</replaceable> OWNER TO <replaceable>new_owner</replaceable>
36+
ALTER TEXT SEARCH CONFIGURATION <replaceable>name</replaceable> SET SCHEMA <replaceable>new_schema</replaceable>
3637
</synopsis>
3738
</refsynopsisdiv>
3839

@@ -123,6 +124,15 @@ ALTER TEXT SEARCH CONFIGURATION <replaceable>name</replaceable> OWNER TO <replac
123124
</para>
124125
</listitem>
125126
</varlistentry>
127+
128+
<varlistentry>
129+
<term><replaceable class="parameter">new_schema</replaceable></term>
130+
<listitem>
131+
<para>
132+
The new schema for the text search configuration.
133+
</para>
134+
</listitem>
135+
</varlistentry>
126136
</variablelist>
127137

128138
<para>

doc/src/sgml/ref/alter_tsdictionary.sgml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ ALTER TEXT SEARCH DICTIONARY <replaceable>name</replaceable> (
2626
)
2727
ALTER TEXT SEARCH DICTIONARY <replaceable>name</replaceable> RENAME TO <replaceable>new_name</replaceable>
2828
ALTER TEXT SEARCH DICTIONARY <replaceable>name</replaceable> OWNER TO <replaceable>new_owner</replaceable>
29+
ALTER TEXT SEARCH DICTIONARY <replaceable>name</replaceable> SET SCHEMA <replaceable>new_schema</replaceable>
2930
</synopsis>
3031
</refsynopsisdiv>
3132

@@ -96,6 +97,15 @@ ALTER TEXT SEARCH DICTIONARY <replaceable>name</replaceable> OWNER TO <replaceab
9697
</para>
9798
</listitem>
9899
</varlistentry>
100+
101+
<varlistentry>
102+
<term><replaceable class="parameter">new_schema</replaceable></term>
103+
<listitem>
104+
<para>
105+
The new schema for the text search dictionary.
106+
</para>
107+
</listitem>
108+
</varlistentry>
99109
</variablelist>
100110

101111
<para>

doc/src/sgml/ref/alter_tsparser.sgml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ PostgreSQL documentation
2222
<refsynopsisdiv>
2323
<synopsis>
2424
ALTER TEXT SEARCH PARSER <replaceable>name</replaceable> RENAME TO <replaceable>new_name</replaceable>
25+
ALTER TEXT SEARCH PARSER <replaceable>name</replaceable> SET SCHEMA <replaceable>new_schema</replaceable>
2526
</synopsis>
2627
</refsynopsisdiv>
2728

@@ -60,6 +61,15 @@ ALTER TEXT SEARCH PARSER <replaceable>name</replaceable> RENAME TO <replaceable>
6061
</para>
6162
</listitem>
6263
</varlistentry>
64+
65+
<varlistentry>
66+
<term><replaceable class="parameter">new_schema</replaceable></term>
67+
<listitem>
68+
<para>
69+
The new schema for the text search parser.
70+
</para>
71+
</listitem>
72+
</varlistentry>
6373
</variablelist>
6474
</refsect1>
6575

doc/src/sgml/ref/alter_tstemplate.sgml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ PostgreSQL documentation
2222
<refsynopsisdiv>
2323
<synopsis>
2424
ALTER TEXT SEARCH TEMPLATE <replaceable>name</replaceable> RENAME TO <replaceable>new_name</replaceable>
25+
ALTER TEXT SEARCH TEMPLATE <replaceable>name</replaceable> SET SCHEMA <replaceable>new_schema</replaceable>
2526
</synopsis>
2627
</refsynopsisdiv>
2728

@@ -60,6 +61,15 @@ ALTER TEXT SEARCH TEMPLATE <replaceable>name</replaceable> RENAME TO <replaceabl
6061
</para>
6162
</listitem>
6263
</varlistentry>
64+
65+
<varlistentry>
66+
<term><replaceable class="parameter">new_schema</replaceable></term>
67+
<listitem>
68+
<para>
69+
The new schema for the text search template.
70+
</para>
71+
</listitem>
72+
</varlistentry>
6373
</variablelist>
6474
</refsect1>
6575

src/backend/commands/alter.c

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@
1414
*/
1515
#include "postgres.h"
1616

17+
#include "catalog/dependency.h"
18+
#include "catalog/indexing.h"
1719
#include "catalog/namespace.h"
1820
#include "catalog/pg_largeobject.h"
21+
#include "catalog/pg_namespace.h"
1922
#include "commands/alter.h"
2023
#include "commands/conversioncmds.h"
2124
#include "commands/dbcommands.h"
@@ -33,6 +36,7 @@
3336
#include "utils/acl.h"
3437
#include "utils/builtins.h"
3538
#include "utils/lsyscache.h"
39+
#include "utils/syscache.h"
3640

3741

3842
/*
@@ -178,11 +182,27 @@ ExecAlterObjectSchemaStmt(AlterObjectSchemaStmt *stmt)
178182
stmt->newschema);
179183
break;
180184

185+
case OBJECT_CONVERSION:
186+
AlterConversionNamespace(stmt->object, stmt->newschema);
187+
break;
188+
181189
case OBJECT_FUNCTION:
182190
AlterFunctionNamespace(stmt->object, stmt->objarg, false,
183191
stmt->newschema);
184192
break;
185193

194+
case OBJECT_OPERATOR:
195+
AlterOperatorNamespace(stmt->object, stmt->objarg, stmt->newschema);
196+
break;
197+
198+
case OBJECT_OPCLASS:
199+
AlterOpClassNamespace(stmt->object, stmt->objarg, stmt->newschema);
200+
break;
201+
202+
case OBJECT_OPFAMILY:
203+
AlterOpFamilyNamespace(stmt->object, stmt->objarg, stmt->newschema);
204+
break;
205+
186206
case OBJECT_SEQUENCE:
187207
case OBJECT_TABLE:
188208
case OBJECT_VIEW:
@@ -191,6 +211,22 @@ ExecAlterObjectSchemaStmt(AlterObjectSchemaStmt *stmt)
191211
stmt->objectType, AccessExclusiveLock);
192212
break;
193213

214+
case OBJECT_TSPARSER:
215+
AlterTSParserNamespace(stmt->object, stmt->newschema);
216+
break;
217+
218+
case OBJECT_TSDICTIONARY:
219+
AlterTSDictionaryNamespace(stmt->object, stmt->newschema);
220+
break;
221+
222+
case OBJECT_TSTEMPLATE:
223+
AlterTSTemplateNamespace(stmt->object, stmt->newschema);
224+
break;
225+
226+
case OBJECT_TSCONFIGURATION:
227+
AlterTSConfigurationNamespace(stmt->object, stmt->newschema);
228+
break;
229+
194230
case OBJECT_TYPE:
195231
case OBJECT_DOMAIN:
196232
AlterTypeNamespace(stmt->object, stmt->newschema);
@@ -202,6 +238,104 @@ ExecAlterObjectSchemaStmt(AlterObjectSchemaStmt *stmt)
202238
}
203239
}
204240

241+
/*
242+
* Generic function to change the namespace of a given object, for simple
243+
* cases (won't work for tables or functions, objects which have more than 2
244+
* key-attributes to use when searching for their syscache entries --- we
245+
* don't want nor need to get this generic here).
246+
*
247+
* The AlterFooNamespace() calls just above will call a function whose job
248+
* is to lookup the arguments for the generic function here.
249+
*
250+
* Relation must already by open, it's the responsibility of the caller to
251+
* close it.
252+
*/
253+
void
254+
AlterObjectNamespace(Relation rel, int cacheId,
255+
Oid classId, Oid objid, Oid nspOid,
256+
int Anum_name, int Anum_namespace, int Anum_owner,
257+
AclObjectKind acl_kind,
258+
bool superuser_only)
259+
{
260+
Oid oldNspOid;
261+
Datum name, namespace;
262+
bool isnull;
263+
HeapTuple tup, newtup = NULL;
264+
Datum *values;
265+
bool *nulls;
266+
bool *replaces;
267+
268+
tup = SearchSysCacheCopy1(cacheId, ObjectIdGetDatum(objid));
269+
if (!HeapTupleIsValid(tup)) /* should not happen */
270+
elog(ERROR, "cache lookup failed for object %u: %s",
271+
objid, getObjectDescriptionOids(classId, objid));
272+
273+
name = heap_getattr(tup, Anum_name, rel->rd_att, &isnull);
274+
namespace = heap_getattr(tup, Anum_namespace, rel->rd_att, &isnull);
275+
oldNspOid = DatumGetObjectId(namespace);
276+
277+
/* Check basic namespace related issues */
278+
CheckSetNamespace(oldNspOid, nspOid, classId, objid);
279+
280+
/* check for duplicate name (more friendly than unique-index failure) */
281+
if (SearchSysCacheExists2(cacheId, name, ObjectIdGetDatum(nspOid)))
282+
ereport(ERROR,
283+
(errcode(ERRCODE_DUPLICATE_OBJECT),
284+
errmsg("%s already exists in schema \"%s\"",
285+
getObjectDescriptionOids(classId, objid),
286+
get_namespace_name(nspOid))));
287+
288+
/* Superusers can always do it */
289+
if (!superuser())
290+
{
291+
Datum owner;
292+
Oid ownerId;
293+
AclResult aclresult;
294+
295+
if (superuser_only)
296+
ereport(ERROR,
297+
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
298+
(errmsg("must be superuser to SET SCHEMA of %s",
299+
getObjectDescriptionOids(classId, objid)))));
300+
301+
/* Otherwise, must be owner of the existing object */
302+
owner = heap_getattr(tup, Anum_owner, rel->rd_att, &isnull);
303+
ownerId = DatumGetObjectId(owner);
304+
305+
if (!has_privs_of_role(GetUserId(), ownerId))
306+
aclcheck_error(ACLCHECK_NOT_OWNER, acl_kind,
307+
NameStr(*(DatumGetName(name))));
308+
309+
/* owner must have CREATE privilege on namespace */
310+
aclresult = pg_namespace_aclcheck(oldNspOid, GetUserId(), ACL_CREATE);
311+
if (aclresult != ACLCHECK_OK)
312+
aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
313+
get_namespace_name(oldNspOid));
314+
}
315+
316+
/* Prepare to update tuple */
317+
values = palloc0(rel->rd_att->natts * sizeof(Datum));
318+
nulls = palloc0(rel->rd_att->natts * sizeof(bool));
319+
replaces = palloc0(rel->rd_att->natts * sizeof(bool));
320+
values[Anum_namespace - 1] = nspOid;
321+
replaces[Anum_namespace - 1] = true;
322+
newtup = heap_modify_tuple(tup, rel->rd_att, values, nulls, replaces);
323+
324+
/* Perform actual update */
325+
simple_heap_update(rel, &tup->t_self, newtup);
326+
CatalogUpdateIndexes(rel, newtup);
327+
328+
/* Release memory */
329+
pfree(values);
330+
pfree(nulls);
331+
pfree(replaces);
332+
333+
/* update dependencies to point to the new schema */
334+
changeDependencyFor(classId, objid,
335+
NamespaceRelationId, oldNspOid, nspOid);
336+
}
337+
338+
205339
/*
206340
* Executes an ALTER OBJECT / OWNER TO statement. Based on the object
207341
* type, the function appropriate to that type is executed.

0 commit comments

Comments
 (0)