Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 197ced5

Browse files
committed
Read info for DEFAULT from pg_attrdef.
1 parent e3accff commit 197ced5

File tree

1 file changed

+154
-41
lines changed

1 file changed

+154
-41
lines changed

src/backend/utils/cache/relcache.c

+154-41
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
static Relation RelationNameCacheGetRelation(char *relationName);
9192
static void init_irels(void);
9293
static void write_irels(void);
93-
/* non-export function prototypes */
94-
static void formrdesc(char *relationName, u_int natts,
95-
FormData_pg_attribute att[]);
96-
static HeapTuple ScanPgRelation(RelationBuildDescInfo buildinfo);
97-
static HeapTuple scan_pg_rel_seq(RelationBuildDescInfo buildinfo);
98-
static HeapTuple scan_pg_rel_ind(RelationBuildDescInfo buildinfo);
99-
static Relation AllocateRelationDesc(u_int natts, Form_pg_class relp);
100-
static void RelationBuildTupleDesc(RelationBuildDescInfo buildinfo,
101-
Relation relation, AttributeTupleForm attp, u_int natts);
102-
static void build_tupdesc_seq(RelationBuildDescInfo buildinfo,
103-
Relation relation, AttributeTupleForm attp, u_int natts);
104-
static void build_tupdesc_ind(RelationBuildDescInfo buildinfo,
105-
Relation relation, AttributeTupleForm attp, u_int natts);
106-
static Relation RelationBuildDesc(RelationBuildDescInfo buildinfo);
107-
static void IndexedAccessMethodInitialize(Relation relation);
108-
10994

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

242+
/* non-export function prototypes */
243+
static void formrdesc(char *relationName, u_int natts,
244+
FormData_pg_attribute att[]);
245+
246+
#if 0 /* See comments at line 1304 */
247+
static void RelationFlushIndexes(Relation *r, Oid accessMethodId);
248+
#endif
249+
250+
static HeapTuple ScanPgRelation(RelationBuildDescInfo buildinfo);
251+
static HeapTuple scan_pg_rel_seq(RelationBuildDescInfo buildinfo);
252+
static HeapTuple scan_pg_rel_ind(RelationBuildDescInfo buildinfo);
253+
static Relation AllocateRelationDesc(u_int natts, Form_pg_class relp);
254+
static void RelationBuildTupleDesc(RelationBuildDescInfo buildinfo,
255+
Relation relation, u_int natts);
256+
static void build_tupdesc_seq(RelationBuildDescInfo buildinfo,
257+
Relation relation, u_int natts);
258+
static void build_tupdesc_ind(RelationBuildDescInfo buildinfo,
259+
Relation relation, u_int natts);
260+
static Relation RelationBuildDesc(RelationBuildDescInfo buildinfo);
261+
static void IndexedAccessMethodInitialize(Relation relation);
262+
static void AttrDefaultFetch (Relation relation);
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-
#ifdef NOT_USED /* XXX This doesn't seem to be used anywhere */
278+
#if NOT_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
static void
482489
RelationBuildTupleDesc(RelationBuildDescInfo buildinfo,
483490
Relation relation,
484-
AttributeTupleForm attp,
485491
u_int natts)
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(struct attrConstr));
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
static void
500512
build_tupdesc_seq(RelationBuildDescInfo buildinfo,
501513
Relation relation,
502-
AttributeTupleForm attp,
503514
u_int natts)
504515
{
505516
HeapTuple pg_attribute_tuple;
506517
Relation pg_attribute_desc;
507518
HeapScanDesc pg_attribute_scan;
519+
AttributeTupleForm attp;
508520
ScanKeyData key;
509521
int need;
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(struct attrConstr));
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
static void
572576
build_tupdesc_ind(RelationBuildDescInfo buildinfo,
573577
Relation relation,
574-
AttributeTupleForm attp,
575578
u_int natts)
576579
{
577580
Relation attrel;
578581
HeapTuple atttup;
582+
AttributeTupleForm attp;
583+
AttrDefault *attrdef = NULL;
584+
int ndef = 0;
579585
int i;
580586

581-
if (!relation->rd_att->constr)
582-
relation->rd_att->constr = (AttrConstr *) palloc(sizeof(struct attrConstr));
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
Oid relid;
760785
Oid relam;
761786
Form_pg_class relp;
762-
AttributeTupleForm attp = NULL;
763787

764788
MemoryContext oldcxt;
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-
#ifdef NOT_USED /* See comments at line 1304 */
1373+
#if NOT_USED /* See comments at line 1304 */
13501374
/* --------------------------------
13511375
* RelationIdInvalidateRelationCacheByAccessMethodId
13521376
*
@@ -1576,6 +1600,95 @@ RelationInitialize(void)
15761600
MemoryContextSwitchTo(oldcxt);
15771601
}
15781602

1603+
static void
1604+
AttrDefaultFetch (Relation relation)
1605+
{
1606+
AttrDefault *attrdef = relation->rd_att->constr->defval;
1607+
int ndef = relation->rd_att->constr->num_defval;
1608+
Relation adrel;
1609+
Relation irel;
1610+
ScanKeyData skey;
1611+
HeapTuple tuple;
1612+
Form_pg_attrdef adform;
1613+
IndexScanDesc sd;
1614+
RetrieveIndexResult indexRes;
1615+
Buffer buffer;
1616+
ItemPointer iptr;
1617+
struct varlena *val;
1618+
bool isnull;
1619+
int found;
1620+
int i;
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 = (struct varlena*) 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 = (struct varlena*) 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)