Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit928c4de

Browse files
committed
Fix dependencies for extended statistics objects.
A stats object ought to have a dependency on each individual columnit reads, not the entire table. Doing this honestly lets us get ridof the hard-wired logic in RemoveStatisticsExt, which seems to havebeen misguidedly modeled on RemoveStatistics; and it will be far easierto extend to multiple tables later.Also, add overlooked dependency on owner, and make the dependency onschema be NORMAL like every other such dependency.There remains some unfinished work here, which is to allow statisticsobjects to be extension members. That takes more effort than justadding the dependency call, though, so I left it out for now.initdb forced because this changes the set of pg_depend records thatshould exist for a statistics object.Discussion:https://postgr.es/m/22676.1494557205@sss.pgh.pa.us
1 parentbc08520 commit928c4de

File tree

6 files changed

+48
-100
lines changed

6 files changed

+48
-100
lines changed

‎src/backend/catalog/heap.c

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
#include"catalog/pg_opclass.h"
5353
#include"catalog/pg_partitioned_table.h"
5454
#include"catalog/pg_statistic.h"
55-
#include"catalog/pg_statistic_ext.h"
5655
#include"catalog/pg_subscription_rel.h"
5756
#include"catalog/pg_tablespace.h"
5857
#include"catalog/pg_type.h"
@@ -1615,10 +1614,7 @@ RemoveAttributeById(Oid relid, AttrNumber attnum)
16151614
heap_close(attr_rel,RowExclusiveLock);
16161615

16171616
if (attnum>0)
1618-
{
16191617
RemoveStatistics(relid,attnum);
1620-
RemoveStatisticsExt(relid,attnum);
1621-
}
16221618

16231619
relation_close(rel,NoLock);
16241620
}
@@ -1873,7 +1869,6 @@ heap_drop_with_catalog(Oid relid)
18731869
* delete statistics
18741870
*/
18751871
RemoveStatistics(relid,0);
1876-
RemoveStatisticsExt(relid,0);
18771872

18781873
/*
18791874
* delete attribute tuples
@@ -2785,75 +2780,6 @@ RemoveStatistics(Oid relid, AttrNumber attnum)
27852780
}
27862781

27872782

2788-
/*
2789-
* RemoveStatisticsExt --- remove entries in pg_statistic_ext for a relation
2790-
*
2791-
* If attnum is zero, remove all entries for rel; else remove only the
2792-
* one(s) involving that column.
2793-
*/
2794-
void
2795-
RemoveStatisticsExt(Oidrelid,AttrNumberattnum)
2796-
{
2797-
Relationpgstatisticext;
2798-
SysScanDescscan;
2799-
ScanKeyDatakey;
2800-
HeapTupletuple;
2801-
2802-
/*
2803-
* Scan pg_statistic_ext to delete relevant tuples
2804-
*/
2805-
pgstatisticext=heap_open(StatisticExtRelationId,RowExclusiveLock);
2806-
2807-
ScanKeyInit(&key,
2808-
Anum_pg_statistic_ext_stxrelid,
2809-
BTEqualStrategyNumber,F_OIDEQ,
2810-
ObjectIdGetDatum(relid));
2811-
2812-
scan=systable_beginscan(pgstatisticext,
2813-
StatisticExtRelidIndexId,
2814-
true,NULL,1,&key);
2815-
2816-
while (HeapTupleIsValid(tuple=systable_getnext(scan)))
2817-
{
2818-
booldelete= false;
2819-
2820-
if (attnum==0)
2821-
delete= true;
2822-
elseif (attnum!=0)
2823-
{
2824-
Form_pg_statistic_extstaForm;
2825-
inti;
2826-
2827-
/*
2828-
* Decode the stxkeys array and delete any stats that involve the
2829-
* specified column.
2830-
*/
2831-
staForm= (Form_pg_statistic_ext)GETSTRUCT(tuple);
2832-
for (i=0;i<staForm->stxkeys.dim1;i++)
2833-
{
2834-
if (staForm->stxkeys.values[i]==attnum)
2835-
{
2836-
delete= true;
2837-
break;
2838-
}
2839-
}
2840-
}
2841-
2842-
if (delete)
2843-
{
2844-
CatalogTupleDelete(pgstatisticext,&tuple->t_self);
2845-
deleteDependencyRecordsFor(StatisticExtRelationId,
2846-
HeapTupleGetOid(tuple),
2847-
false);
2848-
}
2849-
}
2850-
2851-
systable_endscan(scan);
2852-
2853-
heap_close(pgstatisticext,RowExclusiveLock);
2854-
}
2855-
2856-
28572783
/*
28582784
* RelationTruncateIndexes - truncate all indexes associated
28592785
* with the heap relation to zero tuples.

‎src/backend/commands/statscmds.c

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ CreateStatistics(CreateStatsStmt *stmt)
5050
{
5151
int16attnums[STATS_MAX_DIMENSIONS];
5252
intnumcols=0;
53-
ObjectAddressaddress=InvalidObjectAddress;
5453
char*namestr;
5554
NameDatastxname;
5655
Oidstatoid;
5756
OidnamespaceId;
57+
Oidstxowner=GetUserId();
5858
HeapTuplehtup;
5959
Datumvalues[Natts_pg_statistic_ext];
6060
boolnulls[Natts_pg_statistic_ext];
@@ -63,7 +63,7 @@ CreateStatistics(CreateStatsStmt *stmt)
6363
Relationrel=NULL;
6464
Oidrelid;
6565
ObjectAddressparentobject,
66-
childobject;
66+
myself;
6767
Datumtypes[2];/* one for each possible type of statistics */
6868
intntypes;
6969
ArrayType*stxkind;
@@ -140,7 +140,7 @@ CreateStatistics(CreateStatsStmt *stmt)
140140
RelationGetRelationName(rel))));
141141

142142
/* You must own the relation to create stats on it */
143-
if (!pg_class_ownercheck(RelationGetRelid(rel),GetUserId()))
143+
if (!pg_class_ownercheck(RelationGetRelid(rel),stxowner))
144144
aclcheck_error(ACLCHECK_NOT_OWNER,ACL_KIND_CLASS,
145145
RelationGetRelationName(rel));
146146
}
@@ -185,7 +185,7 @@ CreateStatistics(CreateStatsStmt *stmt)
185185
attForm= (Form_pg_attribute)GETSTRUCT(atttuple);
186186

187187
/* Disallow use of system attributes in extended stats */
188-
if (attForm->attnum<0)
188+
if (attForm->attnum <=0)
189189
ereport(ERROR,
190190
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
191191
errmsg("statistics creation on system columns is not supported")));
@@ -205,7 +205,7 @@ CreateStatistics(CreateStatsStmt *stmt)
205205
errmsg("cannot have more than %d columns in statistics",
206206
STATS_MAX_DIMENSIONS)));
207207

208-
attnums[numcols]=((Form_pg_attribute)GETSTRUCT(atttuple))->attnum;
208+
attnums[numcols]=attForm->attnum;
209209
numcols++;
210210
ReleaseSysCache(atttuple);
211211
}
@@ -231,10 +231,12 @@ CreateStatistics(CreateStatsStmt *stmt)
231231
* just check consecutive elements.
232232
*/
233233
for (i=1;i<numcols;i++)
234+
{
234235
if (attnums[i]==attnums[i-1])
235236
ereport(ERROR,
236237
(errcode(ERRCODE_DUPLICATE_COLUMN),
237238
errmsg("duplicate column name in statistics definition")));
239+
}
238240

239241
/* Form an int2vector representation of the sorted column list */
240242
stxkeys=buildint2vector(attnums,numcols);
@@ -288,7 +290,7 @@ CreateStatistics(CreateStatsStmt *stmt)
288290
values[Anum_pg_statistic_ext_stxrelid-1]=ObjectIdGetDatum(relid);
289291
values[Anum_pg_statistic_ext_stxname-1]=NameGetDatum(&stxname);
290292
values[Anum_pg_statistic_ext_stxnamespace-1]=ObjectIdGetDatum(namespaceId);
291-
values[Anum_pg_statistic_ext_stxowner-1]=ObjectIdGetDatum(GetUserId());
293+
values[Anum_pg_statistic_ext_stxowner-1]=ObjectIdGetDatum(stxowner);
292294
values[Anum_pg_statistic_ext_stxkeys-1]=PointerGetDatum(stxkeys);
293295
values[Anum_pg_statistic_ext_stxkind-1]=PointerGetDatum(stxkind);
294296

@@ -312,29 +314,35 @@ CreateStatistics(CreateStatsStmt *stmt)
312314
relation_close(rel,NoLock);
313315

314316
/*
315-
* Add a dependency on the table, so that stats get dropped on DROP TABLE.
316-
*
317-
* XXX don't we need dependencies on the specific columns, instead?
317+
* Add an AUTO dependency on each column used in the stats, so that the
318+
* stats object goes away if any or all of them get dropped.
318319
*/
319-
ObjectAddressSet(parentobject,RelationRelationId,relid);
320-
ObjectAddressSet(childobject,StatisticExtRelationId,statoid);
321-
recordDependencyOn(&childobject,&parentobject,DEPENDENCY_AUTO);
320+
ObjectAddressSet(myself,StatisticExtRelationId,statoid);
321+
322+
for (i=0;i<numcols;i++)
323+
{
324+
ObjectAddressSubSet(parentobject,RelationRelationId,relid,attnums[i]);
325+
recordDependencyOn(&myself,&parentobject,DEPENDENCY_AUTO);
326+
}
322327

323328
/*
324-
* Also add dependency on the schema. This is required to ensure that we
325-
* drop the statistics on DROP SCHEMA. This is not handled automatically
326-
* by DROP TABLE because the statistics might be in a different schema
327-
* from the table itself. (This definition is a bit bizarre for the
328-
* single-table case, but it will make more sense if/when we support
329-
* extended stats across multiple tables.)
329+
* Also add dependencies on namespace and owner. These are required
330+
* because the stats object might have a different namespace and/or owner
331+
* than the underlying table(s).
330332
*/
331333
ObjectAddressSet(parentobject,NamespaceRelationId,namespaceId);
332-
recordDependencyOn(&childobject,&parentobject,DEPENDENCY_AUTO);
334+
recordDependencyOn(&myself,&parentobject,DEPENDENCY_NORMAL);
333335

334-
/* Return stats object's address */
335-
ObjectAddressSet(address,StatisticExtRelationId,statoid);
336+
recordDependencyOnOwner(StatisticExtRelationId,statoid,stxowner);
336337

337-
returnaddress;
338+
/*
339+
* XXX probably there should be a recordDependencyOnCurrentExtension call
340+
* here too, but we'd have to add support for ALTER EXTENSION ADD/DROP
341+
* STATISTICS, which is more work than it seems worth.
342+
*/
343+
344+
/* Return stats object's address */
345+
returnmyself;
338346
}
339347

340348
/*

‎src/include/catalog/catversion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/*yyyymmddN */
56-
#defineCATALOG_VERSION_NO201705111
56+
#defineCATALOG_VERSION_NO201705121
5757

5858
#endif

‎src/include/catalog/heap.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ extern void RemoveAttrDefault(Oid relid, AttrNumber attnum,
119119
DropBehaviorbehavior,boolcomplain,boolinternal);
120120
externvoidRemoveAttrDefaultById(OidattrdefId);
121121
externvoidRemoveStatistics(Oidrelid,AttrNumberattnum);
122-
externvoidRemoveStatisticsExt(Oidrelid,AttrNumberattnum);
123122

124123
externForm_pg_attributeSystemAttributeDefinition(AttrNumberattno,
125124
boolrelhasoids);

‎src/test/regress/expected/stats_ext.out

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ DROP STATISTICS regress_schema_2.ab1_a_b_stats;
4747
-- Ensure statistics are dropped when columns are
4848
CREATE STATISTICS ab1_b_c_stats ON b, c FROM ab1;
4949
CREATE STATISTICS ab1_a_b_c_stats ON a, b, c FROM ab1;
50-
CREATE STATISTICSab1_a_b_stats ONa, b FROM ab1;
50+
CREATE STATISTICSab1_b_a_stats ONb, a FROM ab1;
5151
ALTER TABLE ab1 DROP COLUMN a;
5252
\d ab1
5353
Table "public.ab1"
@@ -58,7 +58,19 @@ ALTER TABLE ab1 DROP COLUMN a;
5858
Statistics:
5959
"public"."ab1_b_c_stats" (ndistinct, dependencies) ON b, c FROM ab1
6060

61+
-- Ensure statistics are dropped when table is
62+
SELECT stxname FROM pg_statistic_ext WHERE stxname LIKE 'ab1%';
63+
stxname
64+
---------------
65+
ab1_b_c_stats
66+
(1 row)
67+
6168
DROP TABLE ab1;
69+
SELECT stxname FROM pg_statistic_ext WHERE stxname LIKE 'ab1%';
70+
stxname
71+
---------
72+
(0 rows)
73+
6274
-- Ensure things work sanely with SET STATISTICS 0
6375
CREATE TABLE ab1 (a INTEGER, b INTEGER);
6476
ALTER TABLE ab1 ALTER a SET STATISTICS 0;

‎src/test/regress/sql/stats_ext.sql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,13 @@ DROP STATISTICS regress_schema_2.ab1_a_b_stats;
3434
-- Ensure statistics are dropped when columns are
3535
CREATE STATISTICS ab1_b_c_statsON b, cFROM ab1;
3636
CREATE STATISTICS ab1_a_b_c_statsON a, b, cFROM ab1;
37-
CREATE STATISTICSab1_a_b_statsONa, bFROM ab1;
37+
CREATE STATISTICSab1_b_a_statsONb, aFROM ab1;
3838
ALTERTABLE ab1 DROP COLUMN a;
3939
\d ab1
40+
-- Ensure statistics are dropped when table is
41+
SELECT stxnameFROM pg_statistic_extWHERE stxnameLIKE'ab1%';
4042
DROPTABLE ab1;
43+
SELECT stxnameFROM pg_statistic_extWHERE stxnameLIKE'ab1%';
4144

4245
-- Ensure things work sanely with SET STATISTICS 0
4346
CREATETABLEab1 (aINTEGER, bINTEGER);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp