Reference: SQL =============== 本部分给出了Oushu Database现在支持的SQL语法。 ABORT ------- Aborts the current transaction. Synopsis .. code-block:: html :linenos: ABORT [ WORK | TRANSACTION ] Description ABORT rolls back the current transaction and causes all the updates made by the transaction to be discarded. This command is identical in behavior to the standard SQL command ROLLBACK, and is present only for historical reasons. Parameters WORK TRANSACTION Optional key words. They have no effect. Notes Use COMMIT to successfully terminate a transaction. Issuing ABORT when not inside a transaction does no harm, but it will provoke a warning message. Compatibility This command is a OushuDB extension present for historical reasons. ROLLBACK is the equivalent standard SQL command. ALTER AGGREGATE ---------------- Changes the definition of an aggregate function. Synopsis .. code-block:: html :linenos: ALTER AGGREGATE ( [ , ... ] ) RENAME TO ALTER AGGREGATE ( [ , ... ] ) OWNER TO ALTER AGGREGATE ( [ , ... ] ) SET SCHEMA Description ALTER AGGREGATE changes the definition of an aggregate function. You must own the aggregate function to use ALTER AGGREGATE. To change the schema of an aggregate function, you must also have CREATE privilege on the new schema. To alter the owner, you must also be a direct or indirect member of the new owning role, and that role must have CREATE privilege on the aggregate function’s schema. (These restrictions enforce that altering the owner does not do anything you could not do by dropping and recreating the aggregate function. However, a superuser can alter ownership of any aggregate function anyway.) Parameters .. code-block:: html :linenos: The name (optionally schema-qualified) of an existing aggregate function. .. code-block:: html :linenos: An input data type on which the aggregate function operates. To reference a zero-argument aggregate function, write * in place of the list of input data types. .. code-block:: html :linenos: The new name of the aggregate function. .. code-block:: html :linenos: The new owner of the aggregate function. .. code-block:: html :linenos: The new schema for the aggregate function. Examples To rename the aggregate function myavg for type integer to my_average: .. code-block:: html :linenos: ALTER AGGREGATE myavg(integer) RENAME TO my_average; To change the owner of the aggregate function myavg for type integer to joe: .. code-block:: html :linenos: ALTER AGGREGATE myavg(integer) OWNER TO joe; To move the aggregate function myavg for type integer into schema myschema: .. code-block:: html :linenos: ALTER AGGREGATE myavg(integer) SET SCHEMA myschema; Compatibility There is no ALTER AGGREGATE statement in the SQL standard. ALTER DATABASE --------------- Changes the attributes of a database. Synopsis .. code-block:: html :linenos: ALTER DATABASE SET { TO | = } { | DEFAULT } ALTER DATABASE RESET Description ALTER DATABASE changes the attributes of a OushuDB database. SET and RESET changes the session default for a configuration parameter for a OushuDB database. Whenever a new session is subsequently started in that database, the specified value becomes the session default value. The database-specific default overrides whatever setting is present in the server configuration file (hawq-site.xml). Only the database owner or a superuser can change the session defaults for a database. Certain parameters cannot be set this way, or can only be set by a superuser. Parameters .. code-block:: html :linenos: The name of the database whose attributes are to be altered. Note: OushuDB reserves the database “hcatalog” for system use. You cannot connect to or alter the system “hcatalog” database. .. code-block:: html :linenos: Set this database’s session default for the specified configuration parameter to the given value. If value is DEFAULT or if RESET is used, the database-specific setting is removed, so the system-wide default setting will be inherited in new sessions. Use RESET ALL to clear all database-specific settings. See About Server Configuration Parameters for information about user-settable configuration parameters. Notes It is also possible to set a configuration parameter session default for a specific role (user) rather than to a database. Role-specific settings override database-specific ones if there is a conflict. Examples To set the default schema search path for the mydatabase database: .. code-block:: html :linenos: ALTER DATABASE mydatabase SET search_path TO myschema, public, pg_catalog; Compatibility The ALTER DATABASE statement is a OushuDB extension. ALTER EXTERNAL TABLE -------------------- Changes the definition of an external table. Synopsis .. code-block:: html :linenos: ALTER EXTERNAL TABLE name RENAME [COLUMN] column TO new_column ALTER EXTERNAL TABLE name RENAME TO new_name ALTER EXTERNAL TABLE name SET SCHEMA new_schema ALTER EXTERNAL TABLE name action [, ... ] where action is one of: ADD [COLUMN] column_name type DROP [COLUMN] column ALTER [COLUMN] column TYPE type [USING expression] OWNER TO new_owner Description ALTER EXTERNAL TABLE changes the definition of an existing external table. These are the supported ALTER EXTERNAL TABLE actions:ADD COLUMN, DROP COLUMN,ALTER COLUMN column TYPE and OWNER TO new_owner. You must own the external table to use ALTER EXTERNAL TABLE or ALTER TABLE. To change the schema of an external table, you must also have CREATE privilege on the new schema. To alter the owner, you must also be a direct or indirect member of the new owning role, and that role must have CREATE privilege on the external table's schema. A superuser has these privileges automatically. Changes to the external table definition with either ALTER EXTERNAL TABLE or ALTER TABLE do not affect the external data. The ALTER EXTERNAL TABLE and ALTER TABLE commands cannot modify the type external table (read, write, web), the table FORMAT information, or the location of the external data. To modify this information, you must drop and recreate the external table definition. Parameters .. code-block:: html :linenos: The name (possibly schema-qualified) of an existing external table definition to alter. .. code-block:: html :linenos: Name of a new column. .. code-block:: html :linenos: Name of an existing column. .. code-block:: html :linenos: Data type of the new column, or new data type for an existing column. .. code-block:: html :linenos: The role name of the new owner of the external table. Examples Add a new column to an external table definition: .. code-block:: html :linenos: ALTER EXTERNAL TABLE orcexternal ADD COLUMN manager text; Change the data type of an external table: .. code-block:: html :linenos: ALTER EXTERNAL TABLE orc_ext ALTER COLUMN p TYPE text; Drop a column of an external table: .. code-block:: html :linenos: ALTER EXTERNAL TABLE orcexternal DROP COLUMN q; Change the owner of an external table: .. code-block:: html :linenos: ALTER EXTERNAL TABLE orcexternal OWNER TO test_role; Compatibility ALTER EXTERNAL TABLE is a OushuDB extension. There is no ALTER EXTERNAL TABLE statement in the SQL standard or regular PostgreSQL. ALTER FUNCTION ---------------- Changes the definition of a function. Synopsis .. code-block:: html :linenos: ALTER FUNCTION ( [ [] [] [, ...] ] ) [, ... ] [RESTRICT] ALTER FUNCTION ( [ [] [] [, ...] ] ) RENAME TO ALTER FUNCTION ( [ [] [] [, ...] ] ) OWNER TO ALTER FUNCTION ( [ [] [] [, ...] ] ) SET SCHEMA where is one of: { CALLED ON NULL INPUT | RETURNS NULL ON NULL INPUT | STRICT } { IMMUTABLE | STABLE | VOLATILE } { [EXTERNAL] SECURITY INVOKER | [EXTERNAL] SECURITY DEFINER } Description ALTER FUNCTION changes the definition of a function. You must own the function to use ALTER FUNCTION. To change a function’s schema, you must also have CREATE privilege on the new schema. To alter the owner, you must also be a direct or indirect member of the new owning role, and that role must have CREATE privilege on the function’s schema. (These restrictions enforce that altering the owner does not do anything you could not do by dropping and recreating the function. However, a superuser can alter ownership of any function anyway.) Parameters .. code-block:: html :linenos: The name (optionally schema-qualified) of an existing function. .. code-block:: html :linenos: The mode of an argument: either IN, OUT, or INOUT. If omitted, the default is IN. Note that ALTER FUNCTION does not actually pay any attention to OUT arguments, since only the input arguments are needed to determine the function’s identity. So it is sufficient to list the IN and INOUT arguments. .. code-block:: html :linenos: The name of an argument. Note that ALTER FUNCTION does not actually pay any attention to argument names, since only the argument data types are needed to determine the function’s identity. .. code-block:: html :linenos: The data type(s) of the function’s arguments (optionally schema-qualified), if any. .. code-block:: html :linenos: The new name of the function. .. code-block:: html :linenos: The new owner of the function. Note that if the function is marked SECURITY DEFINER, it will subsequently execute as the new owner. .. code-block:: html :linenos: The new schema for the function. .. code-block:: html :linenos: CALLED ON NULL INPUT RETURNS NULL ON NULL INPUT STRICT CALLED ON NULL INPUT changes the function so that it will be invoked when some or all of its arguments are null. RETURNS NULL ON NULL INPUT or STRICT changes the function so that it is not invoked if any of its arguments are null; instead, a null result is assumed automatically. See CREATE FUNCTION for more information. .. code-block:: html :linenos: IMMUTABLE STABLE VOLATILE Change the volatility of the function to the specified setting. See CREATE FUNCTION for details. .. code-block:: html :linenos: [ EXTERNAL ] SECURITY INVOKER [ EXTERNAL ] SECURITY DEFINER Change whether the function is a security definer or not. The key word EXTERNAL is ignored for SQL conformance. See CREATE FUNCTION for more information about this capability. .. code-block:: html :linenos: RESTRICT Ignored for conformance with the SQL standard. Notes OushuDB has limitations on the use of functions defined as STABLE or VOLATILE. See CREATE FUNCTION for more information Examples To rename the function sqrt for type integer to square_root: .. code-block:: html :linenos: ALTER FUNCTION sqrt(integer) RENAME TO square_root; To change the owner of the function sqrt for type integer to joe: .. code-block:: html :linenos: ALTER FUNCTION sqrt(integer) OWNER TO joe; To change the schema of the function sqrt for type integer to math: .. code-block:: html :linenos: ALTER FUNCTION sqrt(integer) SET SCHEMA math; Compatibility This statement is partially compatible with the ALTER FUNCTION statement in the SQL standard. The standard allows more properties of a function to be modified, but does not provide the ability to rename a function, make a function a security definer, or change the owner, schema, or volatility of a function. The standard also requires the RESTRICT key word, which is optional in OushuDB. ALTER GROUP ----------------- ALTER GROUP -- change role name or membership Synopsis .. code-block:: html :linenos: ALTER GROUP groupname ADD USER username [, ... ] ALTER GROUP groupname DROP USER username [, ... ] ALTER GROUP groupname RENAME TO newname Description ALTER GROUP changes the attributes of a user group. This is an obsolete command, though still accepted for backwards compatibility, because groups (and users too) have been superseded by the more general concept of roles. The first two variants add users to a group or remove them from a group. (Any role can play the part of either a "user" or a "group" for this purpose.) These variants are effectively equivalent to granting or revoking membership in the role named as the "group"; so the preferred way to do this is to use GRANT or REVOKE. The third variant changes the name of the group. This is exactly equivalent to renaming the role with ALTER ROLE. Parameters .. code-block:: html :linenos: The name of the group (role) to modify. .. code-block:: html :linenos: Users (roles) that are to be added to or removed from the group. The users must already exist; ALTER GROUP does not create or drop users. .. code-block:: html :linenos: The new name of the group. Examples Add users to a group: .. code-block:: html :linenos: ALTER GROUP staff ADD USER karl, john; Remove a user from a group: .. code-block:: html :linenos: ALTER GROUP workers DROP USER beth; Compatibility There is no ALTER GROUP statement in the SQL standard. ALTER OPERATOR ---------------- Changes the definition of an operator. Synopsis .. code-block:: html :linenos: ALTER OPERATOR ( { | NONE} , { | NONE} ) OWNER TO Description ALTER OPERATOR changes the definition of an operator. The only currently available functionality is to change the owner of the operator. You must own the operator to use ALTER OPERATOR. To alter the owner, you must also be a direct or indirect member of the new owning role, and that role must have CREATE privilege on the operator’s schema. (These restrictions enforce that altering the owner does not do anything you could not do by dropping and recreating the operator. However, a superuser can alter ownership of any operator anyway.) Parameters .. code-block:: html :linenos: The name (optionally schema-qualified) of an existing operator. .. code-block:: html :linenos: The data type of the operator’s left operand; write NONE if the operator has no left operand. .. code-block:: html :linenos: The data type of the operator’s right operand; write NONE if the operator has no right operand. .. code-block:: html :linenos: The new owner of the operator. Examples Change the owner of a custom operator a @@ b for type text: .. code-block:: html :linenos: ALTER OPERATOR @@ (text, text) OWNER TO joe; Compatibility There is no ALTER OPERATOR statement in the SQL standard. ALTER RESOURCE QUEUE --------------------- Modify an existing resource queue. Synopsis .. code-block:: html :linenos: ALTER RESOURCE QUEUE WITH (= [, ... ]) where is: [MEMORY_LIMIT_CLUSTER=] [CORE_LIMIT_CLUSTER=] [ACTIVE_STATEMENTS=] [ALLOCATION_POLICY='even'] [VSEG_RESOURCE_QUOTA='mem:'] [RESOURCE_OVERCOMMIT_FACTOR=] [NVSEG_UPPER_LIMIT=] [NVSEG_LOWER_LIMIT=] [NVSEG_UPPER_LIMIT_PERSEG=] [NVSEG_LOWER_LIMIT_PERSEG=] ::= {128mb|256mb|512mb|1024mb|2048mb|4096mb| 8192mb|16384mb|1gb|2gb|4gb|8gb|16gb} ::= % Description Changes attributes for an existing resource queue in OushuDB. You cannot change the parent of an existing resource queue, and you cannot change a resource queue while it is active. Only a superuser can modify a resource queue. Resource queues with an ACTIVE_STATEMENTS threshold set a maximum limit on the number parallel active query statements that can be executed by roles assigned to the leaf queue. It controls the number of active queries that are allowed to run at the same time. The value for ACTIVE_STATEMENTS should be an integer greater than 0. If not specified, the default value is 20. When modifying the resource queue, use the MEMORY_LIMIT_CLUSTER and CORE_LIMIT_CLUSTER to tune the allowed resource usage of the resource queue. MEMORY_LIMIT_CLUSTER and CORE_LIMIT_CLUSTER must be equal for the same resource queue. In addition the sum of the percentages of MEMORY_LIMIT_CLUSTER (and CORE_LIMIT_CLUSTER) for resource queues that share the same parent cannot exceed 100%. To modify the role associated with the resource queue, use the ALTER ROLE or CREATE ROLE command. You can only assign roles to the leaf-level resource queues (resource queues that do not have any children.) The default memory allotment can be overridden on a per-query basis by using enforce_nvseg and enforce_memory_pervseg in vc property configuration parameters. Parameters .. code-block:: html :linenos: Required. The name of the resource queue you wish to modify. .. code-block:: html :linenos: MEMORY_LIMIT_CLUSTER= Required. Defines how much memory a resource queue can consume from its parent resource queue and consequently dispatch to the execution of parallel statements. The valid values are 1% to 100%. The value of MEMORY_LIMIT_CLUSTER must be identical to the value of CORE_LIMIT_CLUSTER. The sum of values for MEMORY_LIMIT_CLUSTER of this queue plus other queues that share the same parent cannot exceed 100%. The OushuDB resource manager periodically validates this restriction. Note: If you want to increase the percentage, you may need to decrease the percentage of any resource queue(s) that share the same parent resource queue first. The total cannot exceed 100%. .. code-block:: html :linenos: CORE_LIMIT_CLUSTER= Required. The percentage of consumable CPU (virtual core) resources that the resource queue can take from its parent resource queue. The valid values are 1% to 100%. The value of CORE_LIMIT_CLUSTER must be identical to the value of MEMORY_LIMIT_CLUSTER. The sum of values for CORE_LIMIT_CLUSTER of this queue and queues that share the same parent cannot exceed 100%. Note: If you want to increase the percentage, you may need to decrease the percentage of any resource queue(s) that share the same parent resource queue first. The total cannot exceed 100%. .. code-block:: html :linenos: ACTIVE_STATEMENTS= Optional. Defines the limit of the number of parallel active statements in one leaf queue. The maximum number of connections cannot exceed this limit. If this limit is reached, the OushuDB resource manager queues more query allocation requests. Note that a single session can have several concurrent statement executions that occupy multiple connection resources. The value for ACTIVE_STATEMENTS should be an integer greater than 0. The default value is 20. .. code-block:: html :linenos: ALLOCATION_POLICY= Optional. Defines the resource allocation policy for parallel statement execution. The default value is even. Note: This release only supports an even allocation policy. Even if you do not specify this attribute, the resource queue still applies an even allocation policy. Future releases will support alternative allocation policies. Setting the allocation policy to even means resources are always evenly dispatched based on current concurrency. When multiple query resource allocation requests are queued, the resource queue tries to evenly dispatch resources to queued requests until one of the following conditions are encountered: * There are no more allocated resources in this queue to dispatch, or * The ACTIVE_STATEMENTS limit has been reached For each query resource allocation request, the OushuDB resource mananger determines the minimum and maximum size of a virtual segment based on multiple factors including query cost, user configuration, table properties, and so on. For example, a hash distributed table requires fixed size of virtual segments. With an even allocation policy, the OushuDB resource manager uses the minimum virtual segment size requirement and evenly dispatches resources to each query resource allocation request in the resource queue. .. code-block:: html :linenos: VSEG_RESOURCE_QUOTA=‘mem:{128mb | 256mb | 512mb | 1024mb | 2048mb | 4096mb | 8192mb | 16384mb | 1gb | 2gb | 4gb | 8gb | 16gb}’ Optional. This quota defines how resources are split across multiple virtual segments. For example, when the OushuDB resource manager determines that 256GB memory and 128 vcores should be allocated to the current resource queue, there are multiple solutions on how to divide the resources across virtual segments. For example, you could use a) 2GB/1 vcore * 128 virtual segments or b) 1GB/0.5 vcore * 256 virtual segments. Therefore, you can use this attribute to make the OushuDB resource manager calculate the number of virtual segments based on how to divide the memory. For example, if VSEG_RESOURCE_QUOTA=’mem:512mb’, then the resource queue will use 512MB/0.25 vcore * 512 virtual segments. The default value is ’mem:256mb’. Note: To avoid resource fragmentation, make sure that the segment resource capacity configured for OushuDB (in OushuDB Standalone mode: hawq_rm_memory_limit_perseg; in YARN mode: yarn.nodemanager.resource.memory-mb must be a multiple of the resource quotas for all virtual segments and CPU to memory ratio must be a multiple of the amount configured for yarn.scheduler.minimum-allocation-mb .. code-block:: html :linenos: RESOURCE_OVERCOMMIT_FACTOR= Optional. This factor defines how much a resource can be overcommitted. The default value is 2.0. For example, if RESOURCE_OVERCOMMIT_FACTOR is set to 3.0 and MEMORY_LIMIT_CLUSTER is set to 30%, then the maximum possible resource allocation in this queue is 90% (30% x 3.0). If the resulting maximum is bigger than 100%, then 100% is adopted. The minimum value that this attribute can be set to is 1.0 .. code-block:: html :linenos: NVSEG_UPPER_LIMIT= / NVSEG_UPPER_LIMIT_PERSEG= Optional. These limits restrict the range of number of virtual segments allocated in this resource queue for executing one query statement. NVSEG_UPPER_LIMIT defines an upper limit of virtual segments for one statement execution regardless of actual cluster size, while NVSEG_UPPER_LIMIT_PERSEG defines the same limit by using the average number of virtual segments in one physical segment. Therefore, the limit defined by NVSEG_UPPER_LIMIT_PERSEG varies dynamically according to the changing size of the OushuDB cluster. For example, if you set NVSEG_UPPER_LIMIT=10 all query resource requests are strictly allocated no more than 10 virtual segments. If you set NVSEG_UPPER_LIMIT_PERSEG=2 and assume that currently there are 5 available OushuDB segments in the cluster, query resource requests are allocated 10 virtual segments at the most. NVSEG_UPPER_LIMIT cannot be set to a lower value than NVSEG_LOWER_LIMIT if both limits are enabled. In addition, the upper limit cannot be set to a value larger than the value set in global configuration parameter hawq_rm_nvseg_perquery_limit and hawq_rm_nvseg_perquery_perseg_limit. By default, both limits are set to -1, which means the limits are disabled. NVSEG_UPPER_LIMIT has higher priority than NVSEG_UPPER_LIMIT_PERSEG. If both limits are set, then NVSEG_UPPER_LIMIT_PERSEG is ignored. If you have enabled resource quotas for the query statement, then these limits are ignored. Note: If the actual lower limit of the number of virtual segments becomes greater than the upper limit, then the lower limit is automatically reduced to be equal to the upper limit. This situation is possible when user sets both NVSEG_UPPER_LIMITand NVSEG_LOWER_LIMIT_PERSEG. After expanding the cluster, the dynamic lower limit may become greater than the value set for the fixed upper limit. NVSEG_LOWER_LIMIT= / NVSEG_LOWER_LIMIT_PERSEG= Optional. These limits specify the minimum number of virtual segments allocated for one statement execution in order to guarantee query performance. NVSEG_LOWER_LIMIT defines the lower limit of virtual segments for one statement execution regardless the actual cluster size, while NVSEG_LOWER_LIMIT_PERSEG defines the same limit by the average virtual segment number in one segment. Therefore, the limit defined by NVSEG_LOWER_LIMIT_PERSEG varies dynamically along with the size of OushuDB cluster. NVSEG_UPPER_LIMIT_PERSEG cannot be less than NVSEG_LOWER_LIMIT_PERSEG if both limits are set enabled. For example, if you set NVSEG_LOWER_LIMIT=10, and one statement execution potentially needs no fewer than 10 virtual segments, then this request has at least 10 virtual segments allocated. If you set NVSEG_UPPER_LIMIT_PERSEG=2, assuming there are currently 5 available OushuDB segments in the cluster, and one statement execution potentially needs no fewer than 10 virtual segments, then the query resource request will be allocated at least 10 virtual segments. If one statement execution needs at most 4 virtual segments, the resource manager will allocate at most 4 virtual segments instead of 10 since this resource request does not need more than 9 virtual segments. By default, both limits are set to -1, which means the limits are disabled. NVSEG_LOWER_LIMIT has higher priority than NVSEG_LOWER_LIMIT_PERSEG. If both limits are set, then NVSEG_LOWER_LIMIT_PERSEG is ignored. If you have enabled resource quotas for the query statement, then these limits are ignored. Note: If the actual lower limit of the number of virtual segments becomes greater than the upper limit, then the lower limit is automatically reduced to be equal to the upper limit. This situation is possible when user sets both NVSEG_UPPER_LIMITand NVSEG_LOWER_LIMIT_PERSEG. After expanding the cluster, the dynamic lower limit may become greater than the value set for the fixed upper limit. Examples Change the memory and core limit of a resource queue: .. code-block:: html :linenos: ALTER RESOURCE QUEUE vc_default.test_queue_1 WITH (MEMORY_LIMIT_CLUSTER=40%, CORE_LIMIT_CLUSTER=40%); Change the active statements maximum for the resource queue: .. code-block:: html :linenos: ALTER RESOURCE QUEUE vc_default.test_queue_1 WITH (ACTIVE_STATEMENTS=50); Compatibility ALTER RESOURCE QUEUE is a OushuDB extension. There is no provision for resource queues or workload management in the SQL standard. ALTER ROLE ------------- Changes a database role (user or group). Synopsis .. code-block:: html :linenos: ALTER ROLE RENAME TO ALTER ROLE RESOURCE QUEUE { | NONE} ALTER ROLE [ [WITH]