doc-exports/docs/dws/dev/dws_06_0169.html
Lu, Huayi a24ca60074 DWS DEVELOPER 811 version
Reviewed-by: Hasko, Vladimir <vladimir.hasko@t-systems.com>
Co-authored-by: Lu, Huayi <luhuayi@huawei.com>
Co-committed-by: Lu, Huayi <luhuayi@huawei.com>
2023-01-19 13:37:49 +00:00

186 lines
28 KiB
HTML

<a name="EN-US_TOPIC_0000001098990908"></a><a name="EN-US_TOPIC_0000001098990908"></a>
<h1 class="topictitle1">CREATE ROW LEVEL SECURITY POLICY</h1>
<div id="body1560407392208"><div class="section" id="EN-US_TOPIC_0000001098990908__section196521854173211"><h4 class="sectiontitle">Function</h4><p id="EN-US_TOPIC_0000001098990908__p48721529133312"><strong id="EN-US_TOPIC_0000001098990908__b1028518341089">CREATE ROW LEVEL SECURITY POLICY</strong> creates a row-level access control policy for a table.</p>
<p id="EN-US_TOPIC_0000001098990908__p1479416593558">The policy takes effect only after row-level access control is enabled (by running <strong id="EN-US_TOPIC_0000001098990908__b992122891010">ALTER TABLE... ENABLE ROW LEVEL SECURITY</strong>).</p>
<p id="EN-US_TOPIC_0000001098990908__p1643314285913">Currently, row-level access control affects the read (<strong id="EN-US_TOPIC_0000001098990908__b2028817617125">SELECT</strong>, <strong id="EN-US_TOPIC_0000001098990908__b1089912951216">UPDATE</strong>, <strong id="EN-US_TOPIC_0000001098990908__b188693135129">DELETE</strong>) of data tables and does not affect the write (<strong id="EN-US_TOPIC_0000001098990908__b2741955161210">INSERT</strong> and <strong id="EN-US_TOPIC_0000001098990908__b737282101320">MERGE INTO</strong>) of data tables. The table owner or system administrators can create an expression in the <strong id="EN-US_TOPIC_0000001098990908__b93774397136">USING</strong> clause. When the client reads the data table, the database server combines the expressions that meet the condition and applies it to the execution plan in the statement rewriting phase of a query. For each tuple in a data table, if the expression returns <strong id="EN-US_TOPIC_0000001098990908__b1841663971617">TRUE</strong>, the tuple is visible to the current user; if the expression returns <strong id="EN-US_TOPIC_0000001098990908__b513195751618">FALSE</strong> or <strong id="EN-US_TOPIC_0000001098990908__b8839459151616">NULL</strong>, the tuple is invisible to the current user.</p>
<p id="EN-US_TOPIC_0000001098990908__p559544117276">A row-level access control policy name is specific to a table. A data table cannot have row-level access control policies with the same name. Different data tables can have the same row-level access control policy.</p>
<p id="EN-US_TOPIC_0000001098990908__p18488195263013">Row-level access control policies can be applied to specified operations (<strong id="EN-US_TOPIC_0000001098990908__b134521951111813">SELECT</strong>, <strong id="EN-US_TOPIC_0000001098990908__b31061854121815">UPDATE</strong>, <strong id="EN-US_TOPIC_0000001098990908__b17564165610186">DELETE</strong>, and <strong id="EN-US_TOPIC_0000001098990908__b51521259111820">ALL</strong>). <strong id="EN-US_TOPIC_0000001098990908__b182704413190">ALL</strong> indicates that <strong id="EN-US_TOPIC_0000001098990908__b64561410131916">SELECT</strong>, <strong id="EN-US_TOPIC_0000001098990908__b144212151918">UPDATE</strong>, and <strong id="EN-US_TOPIC_0000001098990908__b1256919144197">DELETE</strong> will be affected. For a new row-level access control policy, the default value <strong id="EN-US_TOPIC_0000001098990908__b1694915812202">ALL</strong> will be used if you do not specify the operations that will be affected.</p>
<p id="EN-US_TOPIC_0000001098990908__p162715143367">Row-level access control policies can be applied to a specified user (role) or to all users (<strong id="EN-US_TOPIC_0000001098990908__b12959123817213">PUBLIC</strong>). For a new row-level access control policy, the default value <strong id="EN-US_TOPIC_0000001098990908__b11177155592112">PUBLIC</strong> will be used if you do not specify the user that will be affected.</p>
</div>
<div class="section" id="EN-US_TOPIC_0000001098990908__section12765201893310"><h4 class="sectiontitle">Precautions</h4><ul id="EN-US_TOPIC_0000001098990908__ul0606822105013"><li id="EN-US_TOPIC_0000001098990908__li5717812205115">Row-level access control policies can be defined for row-store tables, row-store partitioned tables, column-store tables, column-store partitioned tables, replication tables, unlogged tables, and hash tables.</li><li id="EN-US_TOPIC_0000001098990908__li1817414546507">Row-level access control policies cannot be defined for HDFS tables, foreign tables, and temporary tables.</li><li id="EN-US_TOPIC_0000001098990908__l964f3d68d79343ec895f266accae7521">Row-level access control policies cannot be defined for views.</li><li id="EN-US_TOPIC_0000001098990908__li11400107183312">A maximum of 100 row-level access control policies cannot be defined for a table.</li><li id="EN-US_TOPIC_0000001098990908__li155511718334">System administrators are not affected by row-level access control policies and can view all data in a table.</li><li id="EN-US_TOPIC_0000001098990908__li107451922104418">Tables queried by using SQL statements, views, functions, and stored procedures are affected by row-level access control policies.</li><li id="EN-US_TOPIC_0000001098990908__li1812625922720">The type of a column that a row-level access control policy depends on cannot be changed. For example, the following modifications are not supported:<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001098990908__screen11920599328"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">ALTER</span><span class="w"> </span><span class="k">TABLE</span><span class="w"> </span><span class="k">public</span><span class="p">.</span><span class="n">all_data</span><span class="w"> </span><span class="k">ALTER</span><span class="w"> </span><span class="k">COLUMN</span><span class="w"> </span><span class="k">role</span><span class="w"> </span><span class="k">TYPE</span><span class="w"> </span><span class="nb">text</span><span class="p">;</span><span class="w"></span>
</pre></div></td></tr></table></div>
</div>
</li></ul>
</div>
<div class="section" id="EN-US_TOPIC_0000001098990908__section16798192723415"><h4 class="sectiontitle">Syntax</h4><div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001098990908__s406f02107ea34794962476b1aeb9cde9"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
<span class="normal">2</span>
<span class="normal">3</span>
<span class="normal">4</span>
<span class="normal">5</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">CREATE</span><span class="w"> </span><span class="p">[</span><span class="w"> </span><span class="k">ROW</span><span class="w"> </span><span class="k">LEVEL</span><span class="w"> </span><span class="k">SECURITY</span><span class="w"> </span><span class="p">]</span><span class="w"> </span><span class="n">POLICY</span><span class="w"> </span><span class="n">policy_name</span><span class="w"> </span><span class="k">ON</span><span class="w"> </span><span class="k">table_name</span><span class="w"></span>
<span class="w"> </span><span class="p">[</span><span class="w"> </span><span class="k">AS</span><span class="w"> </span><span class="err">{</span><span class="w"> </span><span class="n">PERMISSIVE</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">RESTRICTIVE</span><span class="w"> </span><span class="err">}</span><span class="w"> </span><span class="p">]</span><span class="w"></span>
<span class="w"> </span><span class="p">[</span><span class="w"> </span><span class="k">FOR</span><span class="w"> </span><span class="err">{</span><span class="w"> </span><span class="k">ALL</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="k">SELECT</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="k">UPDATE</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="k">DELETE</span><span class="w"> </span><span class="err">}</span><span class="w"> </span><span class="p">]</span><span class="w"></span>
<span class="w"> </span><span class="p">[</span><span class="w"> </span><span class="k">TO</span><span class="w"> </span><span class="err">{</span><span class="w"> </span><span class="n">role_name</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="k">PUBLIC</span><span class="w"> </span><span class="err">}</span><span class="w"> </span><span class="p">[,</span><span class="w"> </span><span class="p">...]</span><span class="w"> </span><span class="p">]</span><span class="w"></span>
<span class="w"> </span><span class="k">USING</span><span class="w"> </span><span class="p">(</span><span class="w"> </span><span class="n">using_expression</span><span class="w"> </span><span class="p">)</span><span class="w"></span>
</pre></div></td></tr></table></div>
</div>
</div>
<div class="section" id="EN-US_TOPIC_0000001098990908__section11851526346"><h4 class="sectiontitle">Parameter Description</h4><ul id="EN-US_TOPIC_0000001098990908__ul1797082105710"><li id="EN-US_TOPIC_0000001098990908__l10d04a708e44432c8552ce5ae19edc79"><em id="EN-US_TOPIC_0000001098990908__i163991140112717">policy_name</em><p id="EN-US_TOPIC_0000001098990908__aa7b5db6826fb4cb2b492870e9a57f6e9">Specifies the name of a row-level access control policy to be created. The names of row-level access control policies for a table must be unique.</p>
</li><li id="EN-US_TOPIC_0000001098990908__ld8389117085641808615b13f1a9db00e"><em id="EN-US_TOPIC_0000001098990908__i15915413162914">table_name</em><p id="EN-US_TOPIC_0000001098990908__a618a27d6c2d648e488b84233937ff15c">Specifies the name of a table to which a row-level access control policy is applied.</p>
</li><li id="EN-US_TOPIC_0000001098990908__li23671960577"><strong id="EN-US_TOPIC_0000001098990908__b191466534158">PERMISSIVE</strong><p id="EN-US_TOPIC_0000001098990908__p236736145720">Specifies that the row-level access control policy is to be created as a permissive policy. For a given query, all applicable permissive policies are combined using the OR operator. Row-level access control policies are permissive by default.</p>
</li><li id="EN-US_TOPIC_0000001098990908__li68404815573"><strong id="EN-US_TOPIC_0000001098990908__b9534131101610">RESTRICTIVE</strong><p id="EN-US_TOPIC_0000001098990908__p0396942165716">Specifies that the row-level access control policy is to be created as a restrictive policy. For a given query, all applicable restrictive policies are combined using the AND operator.</p>
<div class="notice" id="EN-US_TOPIC_0000001098990908__n95706c955b064a6bb1a9c9b4587869d1"><span class="noticetitle"><img src="public_sys-resources/notice_3.0-en-us.png"> </span><div class="noticebody"><p id="EN-US_TOPIC_0000001098990908__a4e2eaf842bd64d839214ff5de378ef6c">At least one permissive policy is required to grant access to data records. If only restrictive policies are used, no records will be accessible. When both permissive and restrictive policies are used, a record is accessible only when it passes at least one permissive policy and all restrictive policies.</p>
</div></div>
</li><li id="EN-US_TOPIC_0000001098990908__l7dc2a705b8484c5585b6fd66af58b22c"><em id="EN-US_TOPIC_0000001098990908__i17773164315292">command</em><p id="EN-US_TOPIC_0000001098990908__a5d44972e84d649f7b2836d6d25574319">Specifies the SQL operations affected by a row-level access control policy, including <strong id="EN-US_TOPIC_0000001098990908__b22511047153013">ALL</strong>, <strong id="EN-US_TOPIC_0000001098990908__b764419501309">SELECT</strong>, <strong id="EN-US_TOPIC_0000001098990908__b7833165417305">UPDATE</strong>, and <strong id="EN-US_TOPIC_0000001098990908__b917645943012">DELETE</strong>. If this parameter is not specified, the default value <strong id="EN-US_TOPIC_0000001098990908__b1599151683111">ALL</strong> will be used, covering <strong id="EN-US_TOPIC_0000001098990908__b199181943116">SELECT</strong>, <strong id="EN-US_TOPIC_0000001098990908__b191801522133110">UPDATE</strong>, and <strong id="EN-US_TOPIC_0000001098990908__b6768825203115">DELETE</strong>.</p>
<p id="EN-US_TOPIC_0000001098990908__p144561153115919">If <em id="EN-US_TOPIC_0000001098990908__i176845003117">command</em> is set to <strong id="EN-US_TOPIC_0000001098990908__b14958153143113">SELECT</strong>, only tuple data that meets the condition (the return value of <em id="EN-US_TOPIC_0000001098990908__i2335513103420">using_expression</em> is <strong id="EN-US_TOPIC_0000001098990908__b189614153346">TRUE</strong>) can be queried. The operations that are affected include <strong id="EN-US_TOPIC_0000001098990908__b9933833123410">SELECT</strong>, <strong id="EN-US_TOPIC_0000001098990908__b257016451347">UPDATE.... RETURNING</strong>, and <strong id="EN-US_TOPIC_0000001098990908__b56514111355">DELETE... RETURNING</strong>.</p>
<p id="EN-US_TOPIC_0000001098990908__p742517159271">If <em id="EN-US_TOPIC_0000001098990908__i0323191663515">command</em> is set to <strong id="EN-US_TOPIC_0000001098990908__b3326316203518">UPDATE</strong>, only tuple data that meets the condition (the return value of <em id="EN-US_TOPIC_0000001098990908__i03281616183512">using_expression</em> is <strong id="EN-US_TOPIC_0000001098990908__b63299169358">TRUE</strong>) can be updated. The operations that are affected include <strong id="EN-US_TOPIC_0000001098990908__b106661659103513">UPDATE</strong>, <strong id="EN-US_TOPIC_0000001098990908__b84401036368">UPDATE ... RETURNING</strong>, and <strong id="EN-US_TOPIC_0000001098990908__b15297121113616">SELECT ... FOR UPDATE/SHARE</strong>.</p>
<p id="EN-US_TOPIC_0000001098990908__p576538133519">If <em id="EN-US_TOPIC_0000001098990908__i778917167364">command</em> is set to <strong id="EN-US_TOPIC_0000001098990908__b117928163365">DELETE</strong>, only tuple data that meets the condition (the return value of <em id="EN-US_TOPIC_0000001098990908__i19793111618369">using_expression</em> is <strong id="EN-US_TOPIC_0000001098990908__b11795191615365">TRUE</strong>) can be deleted. The operations that are affected include <strong id="EN-US_TOPIC_0000001098990908__b09989465368">DELETE</strong> and<strong id="EN-US_TOPIC_0000001098990908__b203481255153618"> DELETE ... RETURNING</strong>.</p>
<p id="EN-US_TOPIC_0000001098990908__p1876162412392">The following table describes the relationship between row-level access control policies and SQL statements.</p>
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="EN-US_TOPIC_0000001098990908__table198047342176" frame="border" border="1" rules="all"><caption><b>Table 1 </b>Relationship between row-level security policies and SQL statements</caption><thead align="left"><tr id="EN-US_TOPIC_0000001098990908__row14804134141720"><th align="left" class="cellrowborder" valign="top" width="25%" id="mcps1.3.4.2.5.7.2.5.1.1"><p id="EN-US_TOPIC_0000001098990908__p15480134519170">Command</p>
</th>
<th align="left" class="cellrowborder" valign="top" width="25%" id="mcps1.3.4.2.5.7.2.5.1.2"><p id="EN-US_TOPIC_0000001098990908__p68052034131718">SELECT/ALL Policy</p>
</th>
<th align="left" class="cellrowborder" valign="top" width="25%" id="mcps1.3.4.2.5.7.2.5.1.3"><p id="EN-US_TOPIC_0000001098990908__p1780563415176">UPDATE/ALL Policy</p>
</th>
<th align="left" class="cellrowborder" valign="top" width="25%" id="mcps1.3.4.2.5.7.2.5.1.4"><p id="EN-US_TOPIC_0000001098990908__p999710011189">DELETE/ALL Policy</p>
</th>
</tr>
</thead>
<tbody><tr id="EN-US_TOPIC_0000001098990908__row8805153420177"><td class="cellrowborder" valign="top" width="25%" headers="mcps1.3.4.2.5.7.2.5.1.1 "><p id="EN-US_TOPIC_0000001098990908__p198427121816"><strong id="EN-US_TOPIC_0000001098990908__b39847712186">SELECT</strong></p>
</td>
<td class="cellrowborder" valign="top" width="25%" headers="mcps1.3.4.2.5.7.2.5.1.2 "><p id="EN-US_TOPIC_0000001098990908__p11691414151820">Existing row</p>
</td>
<td class="cellrowborder" valign="top" width="25%" headers="mcps1.3.4.2.5.7.2.5.1.3 "><p id="EN-US_TOPIC_0000001098990908__p2069141491810">No</p>
</td>
<td class="cellrowborder" valign="top" width="25%" headers="mcps1.3.4.2.5.7.2.5.1.4 "><p id="EN-US_TOPIC_0000001098990908__p96911141181">No</p>
</td>
</tr>
<tr id="EN-US_TOPIC_0000001098990908__row88060345170"><td class="cellrowborder" valign="top" width="25%" headers="mcps1.3.4.2.5.7.2.5.1.1 "><p id="EN-US_TOPIC_0000001098990908__p119842715181"><strong id="EN-US_TOPIC_0000001098990908__b179841175185">SELECT FOR UPDATE/SHARE</strong></p>
</td>
<td class="cellrowborder" valign="top" width="25%" headers="mcps1.3.4.2.5.7.2.5.1.2 "><p id="EN-US_TOPIC_0000001098990908__p18691161415188">Existing row</p>
</td>
<td class="cellrowborder" valign="top" width="25%" headers="mcps1.3.4.2.5.7.2.5.1.3 "><p id="EN-US_TOPIC_0000001098990908__p96921114181819">Existing row</p>
</td>
<td class="cellrowborder" valign="top" width="25%" headers="mcps1.3.4.2.5.7.2.5.1.4 "><p id="EN-US_TOPIC_0000001098990908__p9692814151819">No</p>
</td>
</tr>
<tr id="EN-US_TOPIC_0000001098990908__row28066347171"><td class="cellrowborder" valign="top" width="25%" headers="mcps1.3.4.2.5.7.2.5.1.1 "><p id="EN-US_TOPIC_0000001098990908__p99841079188"><strong id="EN-US_TOPIC_0000001098990908__b119841971185">UPDATE</strong></p>
</td>
<td class="cellrowborder" valign="top" width="25%" headers="mcps1.3.4.2.5.7.2.5.1.2 "><p id="EN-US_TOPIC_0000001098990908__p1769241415186">No</p>
</td>
<td class="cellrowborder" valign="top" width="25%" headers="mcps1.3.4.2.5.7.2.5.1.3 "><p id="EN-US_TOPIC_0000001098990908__p46921714201817">Existing row</p>
</td>
<td class="cellrowborder" valign="top" width="25%" headers="mcps1.3.4.2.5.7.2.5.1.4 "><p id="EN-US_TOPIC_0000001098990908__p126921514111820">No</p>
</td>
</tr>
<tr id="EN-US_TOPIC_0000001098990908__row7807434141711"><td class="cellrowborder" valign="top" width="25%" headers="mcps1.3.4.2.5.7.2.5.1.1 "><p id="EN-US_TOPIC_0000001098990908__p098513761813"><strong id="EN-US_TOPIC_0000001098990908__b189853718182">UPDATE RETURNING</strong></p>
</td>
<td class="cellrowborder" valign="top" width="25%" headers="mcps1.3.4.2.5.7.2.5.1.2 "><p id="EN-US_TOPIC_0000001098990908__p8693191411810">Existing row</p>
</td>
<td class="cellrowborder" valign="top" width="25%" headers="mcps1.3.4.2.5.7.2.5.1.3 "><p id="EN-US_TOPIC_0000001098990908__p11693114171815">Existing row</p>
</td>
<td class="cellrowborder" valign="top" width="25%" headers="mcps1.3.4.2.5.7.2.5.1.4 "><p id="EN-US_TOPIC_0000001098990908__p1869315142184">No</p>
</td>
</tr>
<tr id="EN-US_TOPIC_0000001098990908__row980723420178"><td class="cellrowborder" valign="top" width="25%" headers="mcps1.3.4.2.5.7.2.5.1.1 "><p id="EN-US_TOPIC_0000001098990908__p59855771815"><strong id="EN-US_TOPIC_0000001098990908__b1298517171813">DELETE</strong></p>
</td>
<td class="cellrowborder" valign="top" width="25%" headers="mcps1.3.4.2.5.7.2.5.1.2 "><p id="EN-US_TOPIC_0000001098990908__p869361491810">No</p>
</td>
<td class="cellrowborder" valign="top" width="25%" headers="mcps1.3.4.2.5.7.2.5.1.3 "><p id="EN-US_TOPIC_0000001098990908__p1369451421818">No</p>
</td>
<td class="cellrowborder" valign="top" width="25%" headers="mcps1.3.4.2.5.7.2.5.1.4 "><p id="EN-US_TOPIC_0000001098990908__p1269413146181">Existing row</p>
</td>
</tr>
<tr id="EN-US_TOPIC_0000001098990908__row5809133491710"><td class="cellrowborder" valign="top" width="25%" headers="mcps1.3.4.2.5.7.2.5.1.1 "><p id="EN-US_TOPIC_0000001098990908__p1698557101810"><strong id="EN-US_TOPIC_0000001098990908__b119851970188">DELETE RETURNING</strong></p>
</td>
<td class="cellrowborder" valign="top" width="25%" headers="mcps1.3.4.2.5.7.2.5.1.2 "><p id="EN-US_TOPIC_0000001098990908__p2694171421811">Existing row</p>
</td>
<td class="cellrowborder" valign="top" width="25%" headers="mcps1.3.4.2.5.7.2.5.1.3 "><p id="EN-US_TOPIC_0000001098990908__p1269401481815">No</p>
</td>
<td class="cellrowborder" valign="top" width="25%" headers="mcps1.3.4.2.5.7.2.5.1.4 "><p id="EN-US_TOPIC_0000001098990908__p19694121417189">Existing row</p>
</td>
</tr>
</tbody>
</table>
</div>
</li><li id="EN-US_TOPIC_0000001098990908__li191762417"><em id="EN-US_TOPIC_0000001098990908__i18274216124012">role_name</em><p id="EN-US_TOPIC_0000001098990908__p12809171364012">Specifies database users affected by a row-level access control policy.</p>
<p id="EN-US_TOPIC_0000001098990908__p791567418">If this parameter is not specified, the default value <strong id="EN-US_TOPIC_0000001098990908__b1142175934017">PUBLIC</strong> will be used, indicating that all database users will be affected. You can specify multiple affected database users.</p>
<div class="notice" id="EN-US_TOPIC_0000001098990908__n2040f1a1a91d43919bfb3baec9444bd1"><span class="noticetitle"><img src="public_sys-resources/notice_3.0-en-us.png"> </span><div class="noticebody"><p id="EN-US_TOPIC_0000001098990908__a08982048baf14db4bba5f6b6f7687a09">System administrators are not affected by row access control.</p>
</div></div>
</li></ul>
</div>
<ul id="EN-US_TOPIC_0000001098990908__ul128779149498"><li id="EN-US_TOPIC_0000001098990908__li10877161416497"><em id="EN-US_TOPIC_0000001098990908__i8437114212411">using_expression</em><p id="EN-US_TOPIC_0000001098990908__p1111512144213">Specifies an expression defined for a row-level access control policy (return type: boolean).</p>
<p id="EN-US_TOPIC_0000001098990908__p270517316512">The expression cannot contain aggregate functions and window functions. In the statement rewriting phase of a query, if row-level access control for a data table is enabled, the expressions that meet the specified conditions will be added to the plan tree. The expression is calculated for each tuple in the data table. For <strong id="EN-US_TOPIC_0000001098990908__b19345172710468">SELECT</strong>, <strong id="EN-US_TOPIC_0000001098990908__b9550231194611">UPDATE</strong>, and <strong id="EN-US_TOPIC_0000001098990908__b1348744114463">DELETE</strong>, row data is visible to the current user only when the return value of the expression is <strong id="EN-US_TOPIC_0000001098990908__b96951252154616">TRUE</strong>. If the expression returns <strong id="EN-US_TOPIC_0000001098990908__b542740154711">FALSE</strong>, the tuple is invisible to the current user. In this case, the user cannot view the tuple through the <strong id="EN-US_TOPIC_0000001098990908__b1892412634710">SELECT</strong> statement, update the tuple through the <strong id="EN-US_TOPIC_0000001098990908__b1019303217477">UPDATE</strong> statement, or delete the tuple through the <strong id="EN-US_TOPIC_0000001098990908__b1031293644719">DELETE</strong> statement.</p>
</li></ul>
<div class="section" id="EN-US_TOPIC_0000001098990908__section17979101023515"><h4 class="sectiontitle">Example 1: Create a row-level access control policy that the current user can only view its own data.</h4><ol id="EN-US_TOPIC_0000001098990908__ol2680194314412"><li id="EN-US_TOPIC_0000001098990908__li468064394114">Create users <strong id="EN-US_TOPIC_0000001098990908__b870106144620">alice</strong> and <strong id="EN-US_TOPIC_0000001098990908__b4701661460">bob</strong>.<pre class="screen" id="EN-US_TOPIC_0000001098990908__screen4435111816815">CREATE ROLE alice PASSWORD '<em id="EN-US_TOPIC_0000001098990908__i11527155911457">{password1}</em>'
CREATE ROLE bob PASSWORD '<em id="EN-US_TOPIC_0000001098990908__i364823319496">{password2}</em>';</pre>
</li><li id="EN-US_TOPIC_0000001098990908__li835195494116">Create the data table <strong id="EN-US_TOPIC_0000001098990908__b9276104444611">public.all_data</strong>.<pre class="screen" id="EN-US_TOPIC_0000001098990908__screen16204193713817">CREATE TABLE public.all_data(id int, role varchar(100), data varchar(100));</pre>
</li><li id="EN-US_TOPIC_0000001098990908__li15753105984111">Insert data into the table.<pre class="screen" id="EN-US_TOPIC_0000001098990908__screen1469120711912">INSERT INTO all_data VALUES(1, 'alice', 'alice data');
INSERT INTO all_data VALUES(2, 'bob', 'bob data');
INSERT INTO all_data VALUES(3, 'peter', 'peter data');</pre>
</li><li id="EN-US_TOPIC_0000001098990908__li75921162422">Grant the read permission for the <strong id="EN-US_TOPIC_0000001098990908__b734245619460">all_data</strong> table to users <strong id="EN-US_TOPIC_0000001098990908__b1234975613465">alice</strong> and <strong id="EN-US_TOPIC_0000001098990908__b1534915604620">bob</strong>.<pre class="screen" id="EN-US_TOPIC_0000001098990908__screen7817242295">GRANT SELECT ON all_data TO alice, bob;</pre>
</li><li id="EN-US_TOPIC_0000001098990908__li6493181317426">Enable row-level access control.<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001098990908__screen354714495289"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">ALTER</span><span class="w"> </span><span class="k">TABLE</span><span class="w"> </span><span class="n">all_data</span><span class="w"> </span><span class="n">ENABLE</span><span class="w"> </span><span class="k">ROW</span><span class="w"> </span><span class="k">LEVEL</span><span class="w"> </span><span class="k">SECURITY</span><span class="p">;</span><span class="w"></span>
</pre></div></td></tr></table></div>
</div>
</li><li id="EN-US_TOPIC_0000001098990908__li590620185422">Create a row-level access control policy to specify that the current user can view only its own data.<pre class="screen" id="EN-US_TOPIC_0000001098990908__screen97291034152817">CREATE ROW LEVEL SECURITY POLICY all_data_rls ON all_data USING(role = CURRENT_USER);</pre>
</li><li id="EN-US_TOPIC_0000001098990908__li8535102413423">View information about the <strong id="EN-US_TOPIC_0000001098990908__b1249747144715">all_data</strong> table.<pre class="screen" id="EN-US_TOPIC_0000001098990908__screen1967711273102">\d+ all_data
Table "public.all_data"
Column | Type | Modifiers | Storage | Stats target | Description
--------+------------------------+-----------+----------+--------------+-------------
id | integer | | plain | |
role | character varying(100) | | extended | |
data | character varying(100) | | extended | |
Row Level Security Policies:
POLICY "all_data_rls"
USING (((role)::name = "current_user"()))
Has OIDs: no
Distribute By: HASH(id)
Location Nodes: ALL DATANODES
Options: orientation=row, compression=no, enable_rowsecurity=true</pre>
</li><li id="EN-US_TOPIC_0000001098990908__li14122142124218">Run <strong id="EN-US_TOPIC_0000001098990908__b145341813164719">SELECT</strong>.<pre class="screen" id="EN-US_TOPIC_0000001098990908__screen580710562511">SELECT * FROM all_data;
id | role | data
----+-------+------------
1 | alice | alice data
2 | bob | bob data
3 | peter | peter data
(3 rows)
EXPLAIN(COSTS OFF) SELECT * FROM all_data;
QUERY PLAN
----------------------------
Streaming (type: GATHER)
Node/s: All datanodes
-&gt; Seq Scan on all_data
(3 rows)</pre>
</li><li id="EN-US_TOPIC_0000001098990908__li10899347134216">Switch to the <strong id="EN-US_TOPIC_0000001098990908__b221018193475">alice</strong> user.<pre class="screen" id="EN-US_TOPIC_0000001098990908__screen7817103562420">set role alice password '<em id="EN-US_TOPIC_0000001098990908__i16502175810492">{password1}</em>';</pre>
</li><li id="EN-US_TOPIC_0000001098990908__li1455755315420">Perform the SELECT operation.<pre class="screen" id="EN-US_TOPIC_0000001098990908__screen233012011246">SELECT * FROM all_data;
id | role | data
----+-------+------------
1 | alice | alice data
(1 row)
EXPLAIN(COSTS OFF) SELECT * FROM all_data;
QUERY PLAN
----------------------------------------------------------------
Streaming (type: GATHER)
Node/s: All datanodes
-&gt; Seq Scan on all_data
Filter: ((role)::name = 'alice'::name)
Notice: This query is influenced by row level security feature
(5 rows)</pre>
</li></ol>
</div>
<div class="section" id="EN-US_TOPIC_0000001098990908__section1426016489355"><h4 class="sectiontitle">Helpful Links</h4><p id="EN-US_TOPIC_0000001098990908__p9325125517354"><a href="dws_06_0200.html">DROP ROW LEVEL SECURITY POLICY</a></p>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="dws_06_0118.html">DDL Syntax</a></div>
</div>
</div>