@@ -3761,6 +3761,7 @@ void
37613761getPolicies(Archive *fout, TableInfo tblinfo[], int numTables)
37623762{
37633763PQExpBuffer query;
3764+ PQExpBuffer tbloids;
37643765PGresult *res;
37653766PolicyInfo *polinfo;
37663767inti_oid;
@@ -3776,15 +3777,17 @@ getPolicies(Archive *fout, TableInfo tblinfo[], int numTables)
37763777j,
37773778ntups;
37783779
3780+ /* No policies before 9.5 */
37793781if (fout->remoteVersion < 90500)
37803782return;
37813783
37823784query = createPQExpBuffer();
3785+ tbloids = createPQExpBuffer();
37833786
37843787/*
3785- * First, check which tables have RLS enabled. We represent RLS being
3786- * enabled on a table by creating a PolicyInfo object with null polname.
3788+ * Identify tables of interest, and check which ones have RLS enabled.
37873789 */
3790+ appendPQExpBufferChar(tbloids, '{');
37883791for (i = 0; i < numTables; i++)
37893792{
37903793TableInfo *tbinfo = &tblinfo[i];
@@ -3793,9 +3796,23 @@ getPolicies(Archive *fout, TableInfo tblinfo[], int numTables)
37933796if (!(tbinfo->dobj.dump & DUMP_COMPONENT_POLICY))
37943797continue;
37953798
3799+ /* It can't have RLS or policies if it's not a table */
3800+ if (tbinfo->relkind != RELKIND_RELATION &&
3801+ tbinfo->relkind != RELKIND_PARTITIONED_TABLE)
3802+ continue;
3803+
3804+ /* Add it to the list of table OIDs to be probed below */
3805+ if (tbloids->len > 1)/* do we have more than the '{'? */
3806+ appendPQExpBufferChar(tbloids, ',');
3807+ appendPQExpBuffer(tbloids, "%u", tbinfo->dobj.catId.oid);
3808+
3809+ /* Is RLS enabled? (That's separate from whether it has policies) */
37963810if (tbinfo->rowsec)
37973811{
37983812/*
3813+ * We represent RLS being enabled on a table by creating a
3814+ * PolicyInfo object with null polname.
3815+ *
37993816 * Note: use tableoid 0 so that this object won't be mistaken for
38003817 * something that pg_depend entries apply to.
38013818 */
@@ -3815,15 +3832,18 @@ getPolicies(Archive *fout, TableInfo tblinfo[], int numTables)
38153832polinfo->polwithcheck = NULL;
38163833}
38173834}
3835+ appendPQExpBufferChar(tbloids, '}');
38183836
38193837/*
3820- * Now, read all RLS policies, and create PolicyInfo objects for all those
3821- * that are of interest.
3838+ * Now, read all RLS policies belonging to the tables of interest, and
3839+ * create PolicyInfo objects for them. (Note that we must filter the
3840+ * results server-side not locally, because we dare not apply pg_get_expr
3841+ * to tables we don't have lock on.)
38223842 */
38233843pg_log_info("reading row-level security policies");
38243844
38253845printfPQExpBuffer(query,
3826- "SELECT oid, tableoid, pol.polrelid, pol.polname, pol.polcmd, ");
3846+ "SELECTpol. oid,pol. tableoid, pol.polrelid, pol.polname, pol.polcmd, ");
38273847if (fout->remoteVersion >= 100000)
38283848appendPQExpBuffer(query, "pol.polpermissive, ");
38293849else
@@ -3833,7 +3853,9 @@ getPolicies(Archive *fout, TableInfo tblinfo[], int numTables)
38333853 " pg_catalog.array_to_string(ARRAY(SELECT pg_catalog.quote_ident(rolname) from pg_catalog.pg_roles WHERE oid = ANY(pol.polroles)), ', ') END AS polroles, "
38343854 "pg_catalog.pg_get_expr(pol.polqual, pol.polrelid) AS polqual, "
38353855 "pg_catalog.pg_get_expr(pol.polwithcheck, pol.polrelid) AS polwithcheck "
3836- "FROM pg_catalog.pg_policy pol");
3856+ "FROM unnest('%s'::pg_catalog.oid[]) AS src(tbloid)\n"
3857+ "JOIN pg_catalog.pg_policy pol ON (src.tbloid = pol.polrelid)",
3858+ tbloids->data);
38373859
38383860res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
38393861
@@ -3857,13 +3879,6 @@ getPolicies(Archive *fout, TableInfo tblinfo[], int numTables)
38573879Oidpolrelid = atooid(PQgetvalue(res, j, i_polrelid));
38583880TableInfo *tbinfo = findTableByOid(polrelid);
38593881
3860- /*
3861- * Ignore row security on tables not to be dumped. (This will
3862- * result in some harmless wasted slots in polinfo[].)
3863- */
3864- if (!(tbinfo->dobj.dump & DUMP_COMPONENT_POLICY))
3865- continue;
3866-
38673882polinfo[j].dobj.objType = DO_POLICY;
38683883polinfo[j].dobj.catId.tableoid =
38693884atooid(PQgetvalue(res, j, i_tableoid));
@@ -3898,6 +3913,7 @@ getPolicies(Archive *fout, TableInfo tblinfo[], int numTables)
38983913PQclear(res);
38993914
39003915destroyPQExpBuffer(query);
3916+ destroyPQExpBuffer(tbloids);
39013917}
39023918
39033919/*