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

Commit197ced5

Browse files
committed
Read info for DEFAULT from pg_attrdef.
1 parente3accff commit197ced5

File tree

1 file changed

+154
-41
lines changed

1 file changed

+154
-41
lines changed

‎src/backend/utils/cache/relcache.c

Lines changed: 154 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/utils/cache/relcache.c,v 1.16 1997/08/20 14:54:07 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/utils/cache/relcache.c,v 1.17 1997/08/21 01:36:09 vadim Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -81,6 +81,7 @@
8181
#include"catalog/pg_variable.h"
8282
#include"catalog/pg_log.h"
8383
#include"catalog/pg_time.h"
84+
#include"catalog/pg_attrdef.h"
8485
#include"catalog/indexing.h"
8586
#include"catalog/index.h"
8687
#include"fmgr.h"
@@ -90,22 +91,6 @@ static void RelationFlushRelation(Relation *relationPtr,
9091
staticRelationRelationNameCacheGetRelation(char*relationName);
9192
staticvoidinit_irels(void);
9293
staticvoidwrite_irels(void);
93-
/* non-export function prototypes */
94-
staticvoidformrdesc(char*relationName,u_intnatts,
95-
FormData_pg_attributeatt[]);
96-
staticHeapTupleScanPgRelation(RelationBuildDescInfobuildinfo);
97-
staticHeapTuplescan_pg_rel_seq(RelationBuildDescInfobuildinfo);
98-
staticHeapTuplescan_pg_rel_ind(RelationBuildDescInfobuildinfo);
99-
staticRelationAllocateRelationDesc(u_intnatts,Form_pg_classrelp);
100-
staticvoidRelationBuildTupleDesc(RelationBuildDescInfobuildinfo,
101-
Relationrelation,AttributeTupleFormattp,u_intnatts);
102-
staticvoidbuild_tupdesc_seq(RelationBuildDescInfobuildinfo,
103-
Relationrelation,AttributeTupleFormattp,u_intnatts);
104-
staticvoidbuild_tupdesc_ind(RelationBuildDescInfobuildinfo,
105-
Relationrelation,AttributeTupleFormattp,u_intnatts);
106-
staticRelationRelationBuildDesc(RelationBuildDescInfobuildinfo);
107-
staticvoidIndexedAccessMethodInitialize(Relationrelation);
108-
10994

11095
/* ----------------
11196
*defines
@@ -254,6 +239,28 @@ typedef struct relnamecacheent {
254239
} \
255240
}
256241

242+
/* non-export function prototypes */
243+
staticvoidformrdesc(char*relationName,u_intnatts,
244+
FormData_pg_attributeatt[]);
245+
246+
#if0/* See comments at line 1304 */
247+
staticvoidRelationFlushIndexes(Relation*r,OidaccessMethodId);
248+
#endif
249+
250+
staticHeapTupleScanPgRelation(RelationBuildDescInfobuildinfo);
251+
staticHeapTuplescan_pg_rel_seq(RelationBuildDescInfobuildinfo);
252+
staticHeapTuplescan_pg_rel_ind(RelationBuildDescInfobuildinfo);
253+
staticRelationAllocateRelationDesc(u_intnatts,Form_pg_classrelp);
254+
staticvoidRelationBuildTupleDesc(RelationBuildDescInfobuildinfo,
255+
Relationrelation,u_intnatts);
256+
staticvoidbuild_tupdesc_seq(RelationBuildDescInfobuildinfo,
257+
Relationrelation,u_intnatts);
258+
staticvoidbuild_tupdesc_ind(RelationBuildDescInfobuildinfo,
259+
Relationrelation,u_intnatts);
260+
staticRelationRelationBuildDesc(RelationBuildDescInfobuildinfo);
261+
staticvoidIndexedAccessMethodInitialize(Relationrelation);
262+
staticvoidAttrDefaultFetch (Relationrelation);
263+
257264
/*
258265
* newlyCreatedRelns -
259266
* relations created during this transaction. We need to keep track of
@@ -268,7 +275,7 @@ static List *newlyCreatedRelns = NULL;
268275
*/
269276

270277

271-
#ifdefNOT_USED/* XXX This doesn't seem to be used anywhere */
278+
#ifNOT_USED/* XXX This doesn't seem to be used anywhere */
272279
/* --------------------------------
273280
*BuildDescInfoError returns a string appropriate to
274281
*the buildinfo passed to it
@@ -481,7 +488,6 @@ AllocateRelationDesc(u_int natts, Form_pg_class relp)
481488
staticvoid
482489
RelationBuildTupleDesc(RelationBuildDescInfobuildinfo,
483490
Relationrelation,
484-
AttributeTupleFormattp,
485491
u_intnatts)
486492
{
487493
/*
@@ -491,20 +497,26 @@ RelationBuildTupleDesc(RelationBuildDescInfo buildinfo,
491497
*/
492498

493499
if (IsBootstrapProcessingMode())
494-
build_tupdesc_seq(buildinfo,relation,attp,natts);
500+
build_tupdesc_seq(buildinfo,relation,natts);
495501
else
496-
build_tupdesc_ind(buildinfo,relation,attp,natts);
502+
{
503+
relation->rd_att->constr= (AttrConstr*)palloc(sizeof(structattrConstr));
504+
relation->rd_att->constr->num_check=0;
505+
relation->rd_att->constr->num_defval=0;
506+
relation->rd_att->constr->has_not_null= false;
507+
build_tupdesc_ind(buildinfo,relation,natts);
508+
}
497509
}
498510

499511
staticvoid
500512
build_tupdesc_seq(RelationBuildDescInfobuildinfo,
501513
Relationrelation,
502-
AttributeTupleFormattp,
503514
u_intnatts)
504515
{
505516
HeapTuplepg_attribute_tuple;
506517
Relationpg_attribute_desc;
507518
HeapScanDescpg_attribute_scan;
519+
AttributeTupleFormattp;
508520
ScanKeyDatakey;
509521
intneed;
510522

@@ -530,9 +542,6 @@ build_tupdesc_seq(RelationBuildDescInfo buildinfo,
530542
* ----------------
531543
*/
532544
need=natts;
533-
if (!relation->rd_att->constr)
534-
relation->rd_att->constr= (AttrConstr*)palloc(sizeof(structattrConstr));
535-
relation->rd_att->constr->has_not_null= false;
536545

537546
pg_attribute_tuple=heap_getnext(pg_attribute_scan,0, (Buffer*)NULL);
538547
while (HeapTupleIsValid(pg_attribute_tuple)&&need>0) {
@@ -545,11 +554,6 @@ build_tupdesc_seq(RelationBuildDescInfo buildinfo,
545554
memmove((char*) (relation->rd_att->attrs[attp->attnum-1]),
546555
(char*)attp,
547556
ATTRIBUTE_TUPLE_SIZE);
548-
549-
/* Update if this attribute have a constraint */
550-
if (attp->attnotnull)
551-
relation->rd_att->constr->has_not_null= true;
552-
553557
need--;
554558
}
555559
pg_attribute_tuple=heap_getnext(pg_attribute_scan,
@@ -571,26 +575,24 @@ build_tupdesc_seq(RelationBuildDescInfo buildinfo,
571575
staticvoid
572576
build_tupdesc_ind(RelationBuildDescInfobuildinfo,
573577
Relationrelation,
574-
AttributeTupleFormattp,
575578
u_intnatts)
576579
{
577580
Relationattrel;
578581
HeapTupleatttup;
582+
AttributeTupleFormattp;
583+
AttrDefault*attrdef=NULL;
584+
intndef=0;
579585
inti;
580586

581-
if (!relation->rd_att->constr)
582-
relation->rd_att->constr= (AttrConstr*)palloc(sizeof(structattrConstr));
583-
relation->rd_att->constr->has_not_null= false;
584-
585587
attrel=heap_openr(AttributeRelationName);
586588

587589
for (i=1;i <=relation->rd_rel->relnatts;i++) {
588590

589591
atttup= (HeapTuple)AttributeNumIndexScan(attrel,relation->rd_id,i);
590592

591593
if (!HeapTupleIsValid(atttup))
592-
elog(WARN,"cannot find attribute %d of relation %.16s",i,
593-
&(relation->rd_rel->relname.data[0]));
594+
elog(WARN,"cannot find attribute %d of relation %.*s",i,
595+
NAMEDATALEN,&(relation->rd_rel->relname.data[0]));
594596
attp= (AttributeTupleForm)GETSTRUCT(atttup);
595597

596598
relation->rd_att->attrs[i-1]=
@@ -602,10 +604,33 @@ build_tupdesc_ind(RelationBuildDescInfo buildinfo,
602604

603605
/* Update if this attribute have a constraint */
604606
if (attp->attnotnull)
605-
relation->rd_att->constr->has_not_null= true;
607+
relation->rd_att->constr->has_not_null= true;
608+
609+
if (attp->atthasdef)
610+
{
611+
if (attrdef==NULL )
612+
attrdef= (AttrDefault*)palloc (relation->rd_rel->relnatts*
613+
sizeof (AttrDefault));
614+
attrdef[ndef].adnum=i;
615+
attrdef[ndef].adbin=NULL;
616+
attrdef[ndef].adsrc=NULL;
617+
ndef++;
618+
}
606619
}
607620

608621
heap_close(attrel);
622+
623+
if (ndef>0 )
624+
{
625+
if (ndef>relation->rd_rel->relnatts )
626+
relation->rd_att->constr->defval= (AttrDefault*)
627+
repalloc (attrdef,ndef*sizeof (AttrDefault));
628+
else
629+
relation->rd_att->constr->defval=attrdef;
630+
relation->rd_att->constr->num_defval=ndef;
631+
AttrDefaultFetch (relation);
632+
}
633+
609634
}
610635

611636
/* --------------------------------
@@ -759,7 +784,6 @@ RelationBuildDesc(RelationBuildDescInfo buildinfo)
759784
Oidrelid;
760785
Oidrelam;
761786
Form_pg_classrelp;
762-
AttributeTupleFormattp=NULL;
763787

764788
MemoryContextoldcxt;
765789

@@ -834,7 +858,7 @@ RelationBuildDesc(RelationBuildDescInfo buildinfo)
834858
* already allocated for it by AllocateRelationDesc.
835859
* ----------------
836860
*/
837-
RelationBuildTupleDesc(buildinfo,relation,attp,natts);
861+
RelationBuildTupleDesc(buildinfo,relation,natts);
838862

839863
/* ----------------
840864
* initialize rules that affect this relation
@@ -1346,7 +1370,7 @@ RelationIdInvalidateRelationCacheByRelationId(Oid relationId)
13461370
}
13471371
}
13481372

1349-
#ifdefNOT_USED/* See comments at line 1304 */
1373+
#ifNOT_USED/* See comments at line 1304 */
13501374
/* --------------------------------
13511375
*RelationIdInvalidateRelationCacheByAccessMethodId
13521376
*
@@ -1576,6 +1600,95 @@ RelationInitialize(void)
15761600
MemoryContextSwitchTo(oldcxt);
15771601
}
15781602

1603+
staticvoid
1604+
AttrDefaultFetch (Relationrelation)
1605+
{
1606+
AttrDefault*attrdef=relation->rd_att->constr->defval;
1607+
intndef=relation->rd_att->constr->num_defval;
1608+
Relationadrel;
1609+
Relationirel;
1610+
ScanKeyDataskey;
1611+
HeapTupletuple;
1612+
Form_pg_attrdefadform;
1613+
IndexScanDescsd;
1614+
RetrieveIndexResultindexRes;
1615+
Bufferbuffer;
1616+
ItemPointeriptr;
1617+
structvarlena*val;
1618+
boolisnull;
1619+
intfound;
1620+
inti;
1621+
1622+
ScanKeyEntryInitialize(&skey,
1623+
(bits16)0x0,
1624+
(AttrNumber)1,
1625+
(RegProcedure)ObjectIdEqualRegProcedure,
1626+
ObjectIdGetDatum(relation->rd_id));
1627+
1628+
adrel=heap_openr(AttrDefaultRelationName);
1629+
irel=index_openr(AttrDefaultIndex);
1630+
sd=index_beginscan(irel, false,1,&skey);
1631+
tuple= (HeapTuple)NULL;
1632+
1633+
for (found=0; ; )
1634+
{
1635+
indexRes=index_getnext(sd,ForwardScanDirection);
1636+
if (!indexRes)
1637+
break;
1638+
1639+
iptr=&indexRes->heap_iptr;
1640+
tuple=heap_fetch(adrel,NowTimeQual,iptr,&buffer);
1641+
pfree(indexRes);
1642+
if (!HeapTupleIsValid(tuple))
1643+
continue;
1644+
adform= (Form_pg_attrdef)GETSTRUCT(tuple);
1645+
for (i=1;i <=ndef;i++)
1646+
{
1647+
if (adform->adnum!=attrdef[i].adnum )
1648+
continue;
1649+
if (attrdef[i].adsrc!=NULL )
1650+
elog (WARN,"AttrDefaultFetch: second record found for attr %.*s in rel %.*s",
1651+
NAMEDATALEN,relation->rd_att->attrs[adform->adnum-1]->attname.data,
1652+
NAMEDATALEN,relation->rd_rel->relname.data);
1653+
1654+
val= (structvarlena*)fastgetattr (tuple,
1655+
Anum_pg_attrdef_adbin,
1656+
adrel->rd_att,&isnull);
1657+
if (isnull )
1658+
elog (WARN,"AttrDefaultFetch: adbin IS NULL for attr %.*s in rel %.*s",
1659+
NAMEDATALEN,relation->rd_att->attrs[adform->adnum-1]->attname.data,
1660+
NAMEDATALEN,relation->rd_rel->relname.data);
1661+
attrdef[i].adbin=textout (val);
1662+
val= (structvarlena*)fastgetattr (tuple,
1663+
Anum_pg_attrdef_adsrc,
1664+
adrel->rd_att,&isnull);
1665+
if (isnull )
1666+
elog (WARN,"AttrDefaultFetch: adsrc IS NULL for attr %.*s in rel %.*s",
1667+
NAMEDATALEN,relation->rd_att->attrs[adform->adnum-1]->attname.data,
1668+
NAMEDATALEN,relation->rd_rel->relname.data);
1669+
attrdef[i].adsrc=textout (val);
1670+
found++;
1671+
}
1672+
1673+
if (i>ndef )
1674+
elog (WARN,"AttrDefaultFetch: unexpected record found for attr %d in rel %.*s",
1675+
adform->adnum,
1676+
NAMEDATALEN,relation->rd_rel->relname.data);
1677+
ReleaseBuffer(buffer);
1678+
}
1679+
1680+
if (found<ndef )
1681+
elog (WARN,"AttrDefaultFetch: %d record not found for rel %.*s",
1682+
ndef-found,
1683+
NAMEDATALEN,relation->rd_rel->relname.data);
1684+
1685+
index_endscan (sd);
1686+
pfree (sd);
1687+
index_close (irel);
1688+
heap_close (adrel);
1689+
1690+
}
1691+
15791692
/*
15801693
* init_irels(), write_irels() -- handle special-case initialization of
15811694
* index relation descriptors.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp